damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

flatcc_alloc.h (3508B)


      1 #ifndef FLATCC_ALLOC_H
      2 #define FLATCC_ALLOC_H
      3 
      4 #ifdef __cplusplus
      5 extern "C" {
      6 #endif
      7 
      8 /*
      9  * These allocation abstractions are __only__ for runtime libraries.
     10  *
     11  * The flatcc compiler uses Posix allocation routines regardless
     12  * of how this file is configured.
     13  *
     14  * This header makes it possible to use systems where malloc is not
     15  * valid to use. In this case the portable library will not help
     16  * because it implements Posix / C11 abstractions.
     17  *
     18  * Systems like FreeRTOS do not work with Posix memory calls and here it
     19  * can be helpful to override runtime allocation primitives.
     20  *
     21  * In general, it is better to customize the allocator and emitter via
     22  * flatcc_builder_custom_init and to avoid using the default emitter
     23  * specific high level calls the copy out a buffer that must later be
     24  * deallocated. This provides full control of allocation withou the need
     25  * for this file.
     26  *
     27  *
     28  * IMPORTANT
     29  *
     30  * If you override malloc, free, etc., make sure your applications
     31  * use the same allocation methods. For example, samples/monster.c
     32  * and several test cases are no longer guaranteed to work out of the
     33  * box.
     34  *
     35  * The changes must only affect target runtime compilation including the
     36  * the runtime library libflatccrt.
     37  *
     38  * The host system flatcc compiler and the compiler library libflatcc
     39  * should NOT be compiled with non-Posix allocation since the compiler
     40  * has a dependency on the runtime library and the wrong free operation
     41  * might be callled. The safest way to avoid this problem this is to
     42  * compile flatcc with the CMake script and the runtime files with a
     43  * dedicated build system for the target system.
     44  */
     45 
     46 #include <stdlib.h>
     47 
     48 #ifndef FLATCC_ALLOC
     49 #define FLATCC_ALLOC(n) malloc(n)
     50 #endif
     51 
     52 #ifndef FLATCC_FREE
     53 #define FLATCC_FREE(p) free(p)
     54 #endif
     55 
     56 #ifndef FLATCC_REALLOC
     57 #define FLATCC_REALLOC(p, n) realloc(p, n)
     58 #endif
     59 
     60 #ifndef FLATCC_CALLOC
     61 #define FLATCC_CALLOC(nm, n) calloc(nm, n)
     62 #endif
     63 
     64 /*
     65  * Implements `aligned_alloc` and `aligned_free`.
     66  * Even with C11, this implements non-standard aligned_free needed for portable
     67  * aligned_alloc implementations.
     68  */
     69 #ifndef FLATCC_USE_GENERIC_ALIGNED_ALLOC
     70 
     71 #ifndef FLATCC_NO_PALIGNED_ALLOC
     72 #include "portable/paligned_alloc.h"
     73 #else
     74 #if !defined(__aligned_free_is_defined) || !__aligned_free_is_defined
     75 #define aligned_free free
     76 #endif
     77 #endif
     78 
     79 #else /* FLATCC_USE_GENERIC_ALIGNED_ALLOC */
     80 
     81 #ifndef FLATCC_ALIGNED_ALLOC
     82 static inline void *__flatcc_aligned_alloc(size_t alignment, size_t size)
     83 {
     84     char *raw;
     85     void *buf;
     86     size_t total_size = (size + alignment - 1 + sizeof(void *));
     87 
     88     if (alignment < sizeof(void *)) {
     89         alignment = sizeof(void *);
     90     }
     91     raw = (char *)(size_t)FLATCC_ALLOC(total_size);
     92     buf = raw + alignment - 1 + sizeof(void *);
     93     buf = (void *)(((size_t)buf) & ~(alignment - 1));
     94     ((void **)buf)[-1] = raw;
     95     return buf;
     96 }
     97 #define FLATCC_ALIGNED_ALLOC(alignment, size) __flatcc_aligned_alloc(alignment, size)
     98 #endif /* FLATCC_USE_GENERIC_ALIGNED_ALLOC */
     99 
    100 #ifndef FLATCC_ALIGNED_FREE
    101 static inline void __flatcc_aligned_free(void *p)
    102 {
    103     char *raw;
    104 
    105     if (!p) return;
    106     raw = ((void **)p)[-1];
    107 
    108     FLATCC_FREE(raw);
    109 }
    110 #define FLATCC_ALIGNED_FREE(p) __flatcc_aligned_free(p)
    111 #endif
    112 
    113 #endif /* FLATCC_USE_GENERIC_ALIGNED_ALLOC */
    114 
    115 #ifndef FLATCC_ALIGNED_ALLOC
    116 #define FLATCC_ALIGNED_ALLOC(a, n) aligned_alloc(a, n)
    117 #endif
    118 
    119 #ifndef FLATCC_ALIGNED_FREE
    120 #define FLATCC_ALIGNED_FREE(p) aligned_free(p)
    121 #endif
    122 
    123 #ifdef __cplusplus
    124 }
    125 #endif
    126 
    127 #endif /* FLATCC_ALLOC_H */