xref: /freebsd/contrib/unbound/util/regional.h (revision 1fb62fb074788ca4713551be09d6569966a3abee)
1 /*
2  * regional.h -- region based memory allocator.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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.
34  */
35 
36 /**
37  * \file
38  * Regional allocator. Allocates small portions of of larger chunks.
39  * Based on region-allocator from NSD, but rewritten to be light.
40  *
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.
49  */
50 
51 #ifndef UTIL_REGIONAL_H_
52 #define UTIL_REGIONAL_H_
53 
54 /**
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.
58  */
59 struct regional
60 {
61 	/**
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.
65 	 */
66 	char* next;
67 	/** first large object, cast to char** to obtain next ptr */
68 	char* large_list;
69 	/** total large size */
70 	size_t total_large;
71 	/** initial chunk size */
72 	size_t first_size;
73 	/** number of bytes available in the current chunk. */
74 	size_t available;
75 	/** current chunk data position. */
76 	char* data;
77 };
78 
79 /**
80  * Create a new regional.
81  * @return: newly allocated regional.
82  */
83 struct regional* regional_create(void);
84 
85 /**
86  * Create a new region, with custom settings.
87  * @param size: length of first block.
88  * @return: newly allocated regional.
89  */
90 struct regional* regional_create_custom(size_t size);
91 
92 /**
93  * Free all memory associated with regional. Only keeps the first block with
94  * the regional inside it.
95  * @param r: the region.
96  */
97 void regional_free_all(struct regional *r);
98 
99 /**
100  * Destroy regional.  All memory associated with regional is freed as if
101  * regional_free_all was called, as well as destroying the regional struct.
102  * @param r: to delete.
103  */
104 void regional_destroy(struct regional *r);
105 
106 /**
107  * Allocate size bytes of memory inside regional.  The memory is
108  * deallocated when region_free_all is called for this region.
109  * @param r: the region.
110  * @param size: number of bytes.
111  * @return: pointer to memory allocated.
112  */
113 void *regional_alloc(struct regional *r, size_t size);
114 
115 /**
116  * Allocate size bytes of memory inside regional and copy INIT into it.
117  * The memory is deallocated when region_free_all is called for this
118  * region.
119  * @param r: the region.
120  * @param init: to copy.
121  * @param size: number of bytes.
122  * @return: pointer to memory allocated.
123  */
124 void *regional_alloc_init(struct regional* r, const void *init, size_t size);
125 
126 /**
127  * Allocate size bytes of memory inside regional that are initialized to
128  * 0.  The memory is deallocated when region_free_all is called for
129  * this region.
130  * @param r: the region.
131  * @param size: number of bytes.
132  * @return: pointer to memory allocated.
133  */
134 void *regional_alloc_zero(struct regional *r, size_t size);
135 
136 /**
137  * Duplicate string and allocate the result in regional.
138  * @param r: the region.
139  * @param string: null terminated string.
140  * @return: pointer to memory allocated.
141  */
142 char *regional_strdup(struct regional *r, const char *string);
143 
144 /** Debug print regional statistics to log */
145 void regional_log_stats(struct regional *r);
146 
147 /** get total memory size in use by region */
148 size_t regional_get_mem(struct regional* r);
149 
150 #endif /* UTIL_REGIONAL_H_ */
151