mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-29 16:26:39 +01:00
Util (mallocHelper): implements ffMallocUsableSize and uses it
This commit is contained in:
+20
-3
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user