From 22a8cfdcfe956cd51372b75c2991b44ad72ffda6 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Wed, 8 Jun 2022 17:14:11 +0200 Subject: [PATCH] Remove obsolete strbuf function --- src/common/io.c | 2 +- src/util/FFstrbuf.c | 39 ++++++++++----------------------------- src/util/FFstrbuf.h | 1 - 3 files changed, 11 insertions(+), 31 deletions(-) diff --git a/src/common/io.c b/src/common/io.c index 1f598cef2..51d99afff 100644 --- a/src/common/io.c +++ b/src/common/io.c @@ -61,7 +61,7 @@ void ffAppendFDContent(int fd, FFstrbuf* buffer) (uint32_t) readed == free ) { buffer->length += (uint32_t) readed; - ffStrbufEnsureCapacity(buffer, (buffer->allocated * 2) - 1); // -1 for null terminator + ffStrbufEnsureFree(buffer, buffer->allocated - 1); // Doubles capacity every round. -1 for the null byte. free = ffStrbufGetFree(buffer); } diff --git a/src/util/FFstrbuf.c b/src/util/FFstrbuf.c index 8be5c94eb..92d33be4f 100644 --- a/src/util/FFstrbuf.c +++ b/src/util/FFstrbuf.c @@ -29,31 +29,12 @@ void ffStrbufInitCopy(FFstrbuf* strbuf, const FFstrbuf* src) ffStrbufAppend(strbuf, src); } -static void setCapacity(FFstrbuf* strbuf, uint32_t capacity) +uint32_t ffStrbufGetFree(const FFstrbuf* strbuf) { if(strbuf->allocated == 0) - { - strbuf->chars = malloc(sizeof(*strbuf->chars) * capacity); - strbuf->chars[0] = '\0'; - } - else - strbuf->chars = realloc(strbuf->chars, sizeof(*strbuf->chars) * capacity); + return 0; - strbuf->allocated = capacity; -} - -void ffStrbufEnsureCapacity(FFstrbuf* strbuf, uint32_t capacity) -{ - if(strbuf->allocated > capacity || capacity == 0) - return; - - if(capacity == UINT32_MAX) - { - fputs("Warning: ffStrbufEnsureCapacity called with UINT32_MAX. Highest allowed value is UINT32_MAX - 1. Exiting.\n", stderr); - exit(812); - } - - setCapacity(strbuf, capacity + 1); // + 1 for the null byte + return strbuf->allocated - strbuf->length - 1; // - 1 for the null byte } void ffStrbufEnsureFree(FFstrbuf* strbuf, uint32_t free) @@ -68,15 +49,15 @@ void ffStrbufEnsureFree(FFstrbuf* strbuf, uint32_t free) while((strbuf->length + free + 1) > allocate) // + 1 for the null byte allocate *= 2; - setCapacity(strbuf, allocate); -} - -uint32_t ffStrbufGetFree(const FFstrbuf* strbuf) -{ if(strbuf->allocated == 0) - return 0; + { + strbuf->chars = malloc(sizeof(*strbuf->chars) * allocate); + strbuf->chars[0] = '\0'; + } + else + strbuf->chars = realloc(strbuf->chars, sizeof(*strbuf->chars) * allocate); - return strbuf->allocated - strbuf->length - 1; // - 1 for the null byte + strbuf->allocated = allocate; } void ffStrbufClear(FFstrbuf* strbuf) diff --git a/src/util/FFstrbuf.h b/src/util/FFstrbuf.h index 31d02cded..076c4e467 100644 --- a/src/util/FFstrbuf.h +++ b/src/util/FFstrbuf.h @@ -23,7 +23,6 @@ void ffStrbufInit(FFstrbuf* strbuf); void ffStrbufInitA(FFstrbuf* strbuf, uint32_t allocate); void ffStrbufInitCopy(FFstrbuf* strbuf, const FFstrbuf* src); -void ffStrbufEnsureCapacity(FFstrbuf* strbuf, uint32_t capacity); void ffStrbufEnsureFree(FFstrbuf* strbuf, uint32_t free); uint32_t ffStrbufGetFree(const FFstrbuf* strbuf);