Util (mallocHelper): implements ffMallocUsableSize and uses it

This commit is contained in:
Carter Li
2025-12-19 23:24:56 +08:00
committed by 李通洲
parent a9f43ff466
commit b64b93fc4a
5 changed files with 74 additions and 4 deletions
+17
View File
@@ -1415,6 +1415,23 @@ elseif(GNU)
target_compile_definitions(libfastfetch PUBLIC PATH_MAX=4096)
endif()
if(WIN32)
check_function_exists(_msize HAVE_MSVC_MSIZE)
if(HAVE_MSVC_MSIZE)
target_compile_definitions(libfastfetch PUBLIC FF_HAVE_MSVC_MSIZE=1)
endif()
else()
check_function_exists(malloc_usable_size HAVE_MALLOC_USABLE_SIZE)
if(HAVE_MALLOC_USABLE_SIZE)
target_compile_definitions(libfastfetch PUBLIC FF_HAVE_MALLOC_USABLE_SIZE=1)
else()
check_function_exists(malloc_size HAVE_MALLOC_SIZE)
if(HAVE_MALLOC_SIZE)
target_compile_definitions(libfastfetch PUBLIC FF_HAVE_MALLOC_SIZE=1)
endif()
endif()
endif()
if(FreeBSD OR OpenBSD OR NetBSD)
include(CheckStructHasMember)
set(CMAKE_REQUIRED_DEFINITIONS "-D_IFI_OQDROPS=1")
+20 -3
View File
@@ -1,4 +1,5 @@
#include "FFstrbuf.h"
#include "util/mallocHelper.h"
#include <ctype.h>
#include <inttypes.h>
@@ -21,11 +22,27 @@ void ffStrbufInitVF(FFstrbuf* strbuf, const char* format, va_list arguments)
{
assert(format != NULL);
int len = vasprintf(&strbuf->chars, format, arguments);
char* buffer = NULL;
int len = vasprintf(&buffer, format, arguments);
assert(len >= 0);
strbuf->allocated = (uint32_t)(len + 1);
strbuf->length = (uint32_t)len;
ffStrbufInitMoveNS(strbuf, (uint32_t)len, buffer);
}
// Takes ownership of `heapStr`. The caller must not free `heapStr` after calling this
// function; the memory will be managed and freed via the associated FFstrbuf.
void ffStrbufInitMoveNS(FFstrbuf* strbuf, uint32_t length, char* heapStr)
{
assert(heapStr != NULL);
strbuf->length = length;
size_t allocSize = ffMallocUsableSize(heapStr);
if (allocSize == 0)
allocSize = length + 1;
else if (allocSize > UINT32_MAX)
allocSize = UINT32_MAX;
strbuf->allocated = (uint32_t) allocSize;
strbuf->chars = heapStr;
}
void ffStrbufEnsureFree(FFstrbuf* strbuf, uint32_t free)
+6
View File
@@ -34,6 +34,7 @@ typedef struct FFstrbuf
static inline void ffStrbufInit(FFstrbuf* strbuf);
void ffStrbufInitA(FFstrbuf* strbuf, uint32_t allocate);
void ffStrbufInitVF(FFstrbuf* strbuf, const char* format, va_list arguments);
void ffStrbufInitMoveNS(FFstrbuf* strbuf, uint32_t length, char* heapStr);
void ffStrbufEnsureFree(FFstrbuf* strbuf, uint32_t free);
void ffStrbufEnsureFixedLengthFree(FFstrbuf* strbuf, uint32_t free);
@@ -212,6 +213,11 @@ FF_C_NODISCARD static inline FFstrbuf ffStrbufCreateF(const char* format, ...)
return strbuf;
}
static inline void ffStrbufInitMoveS(FFstrbuf* strbuf, char* heapStr)
{
ffStrbufInitMoveNS(strbuf, (uint32_t) strlen(heapStr), heapStr);
}
static inline void ffStrbufDestroy(FFstrbuf* strbuf)
{
extern char* CHAR_NULL_PTR;
+21
View File
@@ -3,6 +3,12 @@
#include <stdlib.h>
#include <assert.h>
#if FF_HAVE_MALLOC_USABLE_SIZE || FF_HAVE_MSVC_MSIZE
#include <malloc.h>
#elif FF_HAVE_MALLOC_SIZE
#include <malloc/malloc.h>
#endif
static inline void ffWrapFree(const void* pPtr)
{
assert(pPtr);
@@ -11,3 +17,18 @@ static inline void ffWrapFree(const void* pPtr)
}
#define FF_AUTO_FREE __attribute__((__cleanup__(ffWrapFree)))
// ptr MUST be a malloc'ed pointer
static inline size_t ffMallocUsableSize(const void* ptr)
{
assert(ptr);
#if FF_HAVE_MALLOC_USABLE_SIZE
return malloc_usable_size((void*) ptr);
#elif FF_HAVE_MALLOC_SIZE
return malloc_size((void*) ptr);
#elif FF_HAVE_MSVC_MSIZE
return _msize((void*) ptr);
#else
return 0; // Not supported
#endif
}
+10 -1
View File
@@ -229,9 +229,18 @@ int main(void)
ffStrbufDestroy(&strbuf);
//initMoveS
{
char* heapStr = strdup("1234567890");
ffStrbufInitMoveS(&strbuf, heapStr);
VERIFY(ffStrbufEqualS(&strbuf, "1234567890"));
VERIFY(strbuf.allocated >= 11);
ffStrbufDestroy(&strbuf);
}
//initF
ffStrbufInitF(&strbuf, "%s", "1234567890123456789012345678901");
VERIFY(strbuf.allocated == 32);
VERIFY(strbuf.allocated >= 32);
VERIFY(ffStrbufEqualS(&strbuf, "1234567890123456789012345678901"));
//containC