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 __private_extern__ tre_mem_t
tre_mem_new_impl(int provided
,
32 void *provided_block
);
33 __private_extern__
void *tre_mem_alloc_impl(tre_mem_t mem
, int provided
,
35 int zero
, size_t size
);
37 /* Returns a new memory allocator or NULL if out of memory. */
38 #define tre_mem_new() tre_mem_new_impl(0, NULL)
40 /* Allocates a block of `size' bytes from `mem'. Returns a pointer to the
41 allocated block or NULL if an underlying malloc() failed. */
42 #define tre_mem_alloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 0, size)
44 /* Allocates a block of `size' bytes from `mem'. Returns a pointer to the
45 allocated block or NULL if an underlying malloc() failed. The memory
47 #define tre_mem_calloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 1, size)
50 /* alloca() versions. Like above, but memory is allocated with alloca()
51 instead of malloc(). */
53 #define tre_mem_newa() \
54 tre_mem_new_impl(1, alloca(sizeof(struct tre_mem_struct)))
56 #define tre_mem_alloca(mem, size) \
58 ? tre_mem_alloc_impl((mem), 1, NULL, 0, (size)) \
59 : tre_mem_alloc_impl((mem), 1, alloca(TRE_MEM_BLOCK_SIZE), 0, (size)))
60 #endif /* TRE_USE_ALLOCA */
63 /* Frees the memory allocator and all memory allocated with it. */
64 __private_extern__
void tre_mem_destroy(tre_mem_t mem
);
66 #endif /* TRE_MEM_H */