diff options
author | Daniel Lange <DLange@git.local> | 2024-04-07 08:39:15 +0200 |
---|---|---|
committer | Daniel Lange <DLange@git.local> | 2024-04-07 08:39:15 +0200 |
commit | 599233ffdf1783011276145bd6933a1fd46fd932 (patch) | |
tree | df48e986d1c4ed8ed22a9e5219491967ee168572 | |
parent | 85c3c3a88a000481254e01740ad396143ac2bca1 (diff) | |
parent | a6306f090abcb1316a81444dc5514d236409349b (diff) |
Merge branch 'richstring-buffer-realloc' of BenBE/htop
-rw-r--r-- | RichString.c | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/RichString.c b/RichString.c index ea904817..556674b0 100644 --- a/RichString.c +++ b/RichString.c @@ -20,18 +20,26 @@ in the source distribution for its full text. #define charBytes(n) (sizeof(CharType) * (n)) static void RichString_extendLen(RichString* this, int len) { - if (this->chlen <= RICHSTRING_MAXLEN) { + if (this->chptr == this->chstr) { + // String is in internal buffer if (len > RICHSTRING_MAXLEN) { + // Copy from internal buffer to allocated string this->chptr = xMalloc(charBytes(len + 1)); memcpy(this->chptr, this->chstr, charBytes(this->chlen)); + } else { + // Still fits in internal buffer, do nothing + assert(this->chlen <= RICHSTRING_MAXLEN); } } else { - if (len <= RICHSTRING_MAXLEN) { + // String is managed externally + if (len > RICHSTRING_MAXLEN) { + // Just reallocate the buffer accordingly + this->chptr = xRealloc(this->chptr, charBytes(len + 1)); + } else { + // Move string into internal buffer and free resources memcpy(this->chstr, this->chptr, charBytes(len)); free(this->chptr); this->chptr = this->chstr; - } else { - this->chptr = xRealloc(this->chptr, charBytes(len + 1)); } } |