2 * regional.h -- region based memory allocator.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Regional allocator. Allocates small portions of of larger chunks.
39 * Based on region-allocator from NSD, but rewritten to be light.
41 * Different from (nsd) region-allocator.h
42 * o does not have recycle bin
43 * o does not collect stats; just enough to answer get_mem() in use.
44 * o does not keep cleanup list
45 * o does not have function pointers to setup
46 * o allocs the regional struct inside the first block.
47 * o can take a block to create regional from.
48 * o blocks and large allocations are kept on singly linked lists.
51 #ifndef UTIL_REGIONAL_H_
52 #define UTIL_REGIONAL_H_
55 * the regional* is the first block*.
56 * every block has a ptr to the next in first bytes.
57 * and so does the regional struct, which is the first block.
62 * next chunk. NULL if first chunk is the only chunk.
63 * first inside that chunk is the char* next pointer.
64 * When regional_free_all() has been called this value is NULL.
67 /** first large object, cast to char** to obtain next ptr */
69 /** total large size */
71 /** initial chunk size */
73 /** number of bytes available in the current chunk. */
75 /** current chunk data position. */
77 /** threshold for outside of chunk allocations */
78 size_t large_object_size;
79 /** padding for sizeof8 alignment of sizeof(struct regional)
80 * for 32bit systems */
85 * Create a new regional.
86 * @return: newly allocated regional.
88 struct regional* regional_create(void);
91 * Create a new region, with custom settings.
92 * @param size: length of first block.
93 * @return: newly allocated regional.
95 struct regional* regional_create_custom(size_t size);
98 * Create a new region, with custom settings, that will allocate everything
99 * outside the region chunk.
100 * @param size: length of first block.
101 * @return: newly allocated regional.
103 struct regional* regional_create_nochunk(size_t size);
106 * Free all memory associated with regional. Only keeps the first block with
107 * the regional inside it.
108 * @param r: the region.
110 void regional_free_all(struct regional *r);
113 * Destroy regional. All memory associated with regional is freed as if
114 * regional_free_all was called, as well as destroying the regional struct.
115 * @param r: to delete.
117 void regional_destroy(struct regional *r);
120 * Allocate size bytes of memory inside regional. The memory is
121 * deallocated when region_free_all is called for this region.
122 * @param r: the region.
123 * @param size: number of bytes.
124 * @return: pointer to memory allocated.
126 void *regional_alloc(struct regional *r, size_t size);
129 * Allocate size bytes of memory inside regional and copy INIT into it.
130 * The memory is deallocated when region_free_all is called for this
132 * @param r: the region.
133 * @param init: to copy.
134 * @param size: number of bytes.
135 * @return: pointer to memory allocated.
137 void *regional_alloc_init(struct regional* r, const void *init, size_t size);
140 * Allocate size bytes of memory inside regional that are initialized to
141 * 0. The memory is deallocated when region_free_all is called for
143 * @param r: the region.
144 * @param size: number of bytes.
145 * @return: pointer to memory allocated.
147 void *regional_alloc_zero(struct regional *r, size_t size);
150 * Duplicate string and allocate the result in regional.
151 * @param r: the region.
152 * @param string: null terminated string.
153 * @return: pointer to memory allocated.
155 char *regional_strdup(struct regional *r, const char *string);
157 /** Debug print regional statistics to log */
158 void regional_log_stats(struct regional *r);
160 /** get total memory size in use by region */
161 size_t regional_get_mem(struct regional* r);
163 #endif /* UTIL_REGIONAL_H_ */