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