summaryrefslogtreecommitdiffstats
path: root/src/common/mallocHelper.h
blob: 5b24df47133c8ec8ff8878769a02b3c3c348a986 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once

#include <stdlib.h>
#include <assert.h>

#if FF_HAVE_MALLOC_USABLE_SIZE || FF_HAVE_MSVC_MSIZE
    #if __has_include(<malloc.h>)
        #include <malloc.h>
    #else
        #include <malloc_np.h> // For DragonFly BSD
    #endif
#elif FF_HAVE_MALLOC_SIZE
    #include <malloc/malloc.h>
#endif

static inline void ffWrapFree(const void* pPtr) {
    assert(pPtr);
    if (*(void**) pPtr) {
        free(*(void**) pPtr);
    }
}

#define FF_AUTO_FREE FF_A_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
    (void) ptr;
    return 0; // Not supported
#endif
}