gdb - Local mods (compile)
[dragonfly.git] / contrib / tre / lib / tre-mem.h
1 /*
2   tre-mem.h - TRE memory allocator interface
3
4   This software is released under a BSD-style license.
5   See the file LICENSE for details and copyright.
6
7 */
8
9 #ifndef TRE_MEM_H
10 #define TRE_MEM_H 1
11
12 #include <stdlib.h>
13
14 #define TRE_MEM_BLOCK_SIZE 1024
15
16 typedef struct tre_list {
17   void *data;
18   struct tre_list *next;
19 } tre_list_t;
20
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;
29
30
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);
34
35 /* Returns a new memory allocator or NULL if out of memory. */
36 #define tre_mem_new()  tre_mem_new_impl(0, NULL)
37
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)
41
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)
46
47 #ifdef TRE_USE_ALLOCA
48 /* alloca() versions.  Like above, but memory is allocated with alloca()
49    instead of malloc(). */
50
51 #define tre_mem_newa() \
52   tre_mem_new_impl(1, alloca(sizeof(struct tre_mem_struct)))
53
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 */
59
60
61 /* Frees the memory allocator and all memory allocated with it. */
62 void tre_mem_destroy(tre_mem_t mem);
63
64 #endif /* TRE_MEM_H */
65
66 /* EOF */