2 tre-mem.h - TRE memory allocator interface
4 This software is released under a BSD-style license.
5 See the file LICENSE for details and copyright.
14 #define TRE_MEM_BLOCK_SIZE 1024
16 typedef struct tre_list {
18 struct tre_list *next;
21 typedef struct tre_mem_struct {
31 tre_mem_t tre_mem_new_impl(int provided, void *provided_block);
32 void *tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
33 int zero, size_t size);
35 /* Returns a new memory allocator or NULL if out of memory. */
36 #define tre_mem_new() tre_mem_new_impl(0, NULL)
38 /* Allocates a block of `size' bytes from `mem'. Returns a pointer to the
39 allocated block or NULL if an underlying malloc() failed. */
40 #define tre_mem_alloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 0, size)
42 /* Allocates a block of `size' bytes from `mem'. Returns a pointer to the
43 allocated block or NULL if an underlying malloc() failed. The memory
45 #define tre_mem_calloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 1, size)
48 /* alloca() versions. Like above, but memory is allocated with alloca()
49 instead of malloc(). */
51 #define tre_mem_newa() \
52 tre_mem_new_impl(1, alloca(sizeof(struct tre_mem_struct)))
54 #define tre_mem_alloca(mem, size) \
56 ? tre_mem_alloc_impl((mem), 1, NULL, 0, (size)) \
57 : tre_mem_alloc_impl((mem), 1, alloca(TRE_MEM_BLOCK_SIZE), 0, (size)))
58 #endif /* TRE_USE_ALLOCA */
61 /* Frees the memory allocator and all memory allocated with it. */
62 void tre_mem_destroy(tre_mem_t mem);
64 #endif /* TRE_MEM_H */