sys/vfs/hammer2: Fix multiple "radii" -> "radix"
[dragonfly.git] / contrib / tre / lib / tre-mem.h
blob285940457f3897c024f5f4e49866db993797be21
1 /*
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.
7 */
9 #ifndef TRE_MEM_H
10 #define TRE_MEM_H 1
12 #include <stdlib.h>
14 #define TRE_MEM_BLOCK_SIZE 1024
16 typedef struct tre_list {
17 void *data;
18 struct tre_list *next;
19 } tre_list_t;
21 typedef struct tre_mem_struct {
22 tre_list_t *blocks;
23 tre_list_t *current;
24 char *ptr;
25 size_t n;
26 int failed;
27 void **provided;
28 } *tre_mem_t;
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
44 is set to zero. */
45 #define tre_mem_calloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 1, size)
47 #ifdef TRE_USE_ALLOCA
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) \
55 ((mem)->n >= (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 */
66 /* EOF */