summaryrefslogtreecommitdiff
path: root/prism/internal/allocator.h
blob: 6c54010dbf50426f87ba81ff09021c7404f84000 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef PRISM_INTERNAL_ALLOCATOR_H
#define PRISM_INTERNAL_ALLOCATOR_H

/* If you build Prism with a custom allocator, configure it with
 * "-D PRISM_XALLOCATOR" to use your own allocator that defines xmalloc,
 * xrealloc, xcalloc, and xfree.
 *
 * For example, your `prism_xallocator.h` file could look like this:
 *
 * ```
 * #ifndef PRISM_XALLOCATOR_H
 * #define PRISM_XALLOCATOR_H
 * #define xmalloc          my_malloc
 * #define xrealloc         my_realloc
 * #define xcalloc          my_calloc
 * #define xfree            my_free
 * #define xrealloc_sized   my_realloc_sized // (optional)
 * #define xfree_sized      my_free_sized    // (optional)
 * #endif
 * ```
 */
#ifdef PRISM_XALLOCATOR
    #include "prism_xallocator.h"
#else
    #ifndef xmalloc
        /* The malloc function that should be used. This can be overridden with
         * the PRISM_XALLOCATOR define. */
        #define xmalloc malloc
    #endif

    #ifndef xrealloc
        /* The realloc function that should be used. This can be overridden with
         * the PRISM_XALLOCATOR define. */
        #define xrealloc realloc
    #endif

    #ifndef xcalloc
        /* The calloc function that should be used. This can be overridden with
         * the PRISM_XALLOCATOR define. */
        #define xcalloc calloc
    #endif

    #ifndef xfree
        /* The free function that should be used. This can be overridden with
         * the PRISM_XALLOCATOR define. */
        #define xfree free
    #endif
#endif

#ifndef xfree_sized
    /* The free_sized function that should be used. This can be overridden with
     * the PRISM_XALLOCATOR define. If not defined, defaults to calling xfree.
     */
    #define xfree_sized(p, s) xfree(((void)(s), (p)))
#endif

#ifndef xrealloc_sized
    /* The xrealloc_sized function that should be used. This can be overridden
     * with the PRISM_XALLOCATOR define. If not defined, defaults to calling
     * xrealloc. */
    #define xrealloc_sized(p, ns, os) xrealloc((p), ((void)(os), (ns)))
#endif

#ifdef PRISM_BUILD_DEBUG
    #include "prism/internal/allocator_debug.h"
#endif

#endif