master ohmu/fastfetch-openbsd / src / common / mallocHelper.h
ohmu  ·  2026-10-15
 1#pragma once
 2
 3#include <stdlib.h>
 4#include <assert.h>
 5
 6#if FF_HAVE_MALLOC_USABLE_SIZE || FF_HAVE_MSVC_MSIZE
 7    #if __has_include(<malloc.h>)
 8        #include <malloc.h>
 9    #else
10        #include <malloc_np.h> // For DragonFly BSD
11    #endif
12#elif FF_HAVE_MALLOC_SIZE
13    #include <malloc/malloc.h>
14#endif
15
16[[gnu::always_inline, gnu::nonnull(1)]]
17static inline void ffWrapFree(const void* pPtr) {
18    if (*(void**) pPtr) {
19        free(*(void**) pPtr);
20    }
21}
22
23#define FF_AUTO_FREE [[gnu::cleanup(ffWrapFree)]]
24
25// ptr MUST be a malloc'ed pointer
26[[gnu::nonnull(1), nodiscard]]
27static inline size_t ffMallocUsableSize(const void* ptr) {
28#if FF_HAVE_MALLOC_USABLE_SIZE
29    return malloc_usable_size((void*) ptr);
30#elif FF_HAVE_MALLOC_SIZE
31    return malloc_size((void*) ptr);
32#elif FF_HAVE_MSVC_MSIZE
33    return _msize((void*) ptr);
34#else
35    (void) ptr;
36    return 0; // Not supported
37#endif
38}