xref: /freebsd/contrib/unbound/util/alloc.h (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*
2  * util/alloc.h - memory allocation service.
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  *
39  * This file contains memory allocation functions.
40  *
41  * The reasons for this service are:
42  *	o Avoid locking costs of getting global lock to call malloc().
43  *	o The packed rrset type needs to be kept on special freelists,
44  *	  so that they are reused for other packet rrset allocations.
45  *
46  */
47 
48 #ifndef UTIL_ALLOC_H
49 #define UTIL_ALLOC_H
50 
51 #include "util/locks.h"
52 struct ub_packed_rrset_key;
53 struct regional;
54 
55 /** The special type, packed rrset. Not allowed to be used for other memory */
56 typedef struct ub_packed_rrset_key alloc_special_type;
57 /** clean the special type. Pass pointer. */
58 #define alloc_special_clean(x) (x)->id = 0;
59 /** access next pointer. (in available spot). Pass pointer. */
60 #define alloc_special_next(x) ((alloc_special_type*)((x)->entry.overflow_next))
61 /** set next pointer. (in available spot). Pass pointers. */
62 #define alloc_set_special_next(x, y) \
63 	((x)->entry.overflow_next) = (struct lruhash_entry*)(y);
64 
65 /** how many blocks to cache locally. */
66 #define ALLOC_SPECIAL_MAX 10
67 
68 /**
69  * Structure that provides allocation. Use one per thread.
70  * The one on top has a NULL super pointer.
71  */
72 struct alloc_cache {
73 	/** lock, only used for the super. */
74 	lock_quick_type lock;
75 	/** global allocator above this one. NULL for none (malloc/free) */
76 	struct alloc_cache* super;
77 	/** singly linked lists of special type. These are free for use. */
78 	alloc_special_type* quar;
79 	/** number of items in quarantine. */
80 	size_t num_quar;
81 	/** thread number for id creation */
82 	int thread_num;
83 	/** next id number to pass out */
84 	uint64_t next_id;
85 	/** last id number possible */
86 	uint64_t last_id;
87 	/** what function to call to cleanup when last id is reached */
88 	void (*cleanup)(void*);
89 	/** user arg for cleanup */
90 	void* cleanup_arg;
91 
92 	/** how many regional blocks to keep back max */
93 	size_t max_reg_blocks;
94 	/** how many regional blocks are kept now */
95 	size_t num_reg_blocks;
96 	/** linked list of regional blocks, using regional->next */
97 	struct regional* reg_list;
98 };
99 
100 /**
101  * Init alloc (zeroes the struct).
102  * @param alloc: this parameter is allocated by the caller.
103  * @param super: super to use (init that before with super_init).
104  *    Pass this argument NULL to init the toplevel alloc structure.
105  * @param thread_num: thread number for id creation of special type.
106  */
107 void alloc_init(struct alloc_cache* alloc, struct alloc_cache* super,
108 	int thread_num);
109 
110 /**
111  * Free the alloc. Pushes all the cached items into the super structure.
112  * Or deletes them if alloc->super is NULL.
113  * Does not free the alloc struct itself (it was also allocated by caller).
114  * @param alloc: is almost zeroed on exit (except some stats).
115  */
116 void alloc_clear(struct alloc_cache* alloc);
117 
118 /**
119  * Free the special alloced items.  The rrset and message caches must be
120  * empty, there must be no more references to rrset pointers into the
121  * rrset cache.
122  * @param alloc: the special allocs are freed.
123  */
124 void alloc_clear_special(struct alloc_cache* alloc);
125 
126 /**
127  * Get a new special_type element.
128  * @param alloc: where to alloc it.
129  * @return: memory block. Will not return NULL (instead fatal_exit).
130  *    The block is zeroed.
131  */
132 alloc_special_type* alloc_special_obtain(struct alloc_cache* alloc);
133 
134 /**
135  * Return special_type back to pool.
136  * The block is cleaned up (zeroed) which also invalidates the ID inside.
137  * @param alloc: where to alloc it.
138  * @param mem: block to free.
139  */
140 void alloc_special_release(struct alloc_cache* alloc, alloc_special_type* mem);
141 
142 /**
143  * Set ID number of special type to a fresh new ID number.
144  * In case of ID number overflow, the rrset cache has to be cleared.
145  * @param alloc: the alloc cache
146  * @return: fresh id is returned.
147  */
148 uint64_t alloc_get_id(struct alloc_cache* alloc);
149 
150 /**
151  * Get memory size of alloc cache, alloc structure including special types.
152  * @param alloc: on what alloc.
153  * @return size in bytes.
154  */
155 size_t alloc_get_mem(struct alloc_cache* alloc);
156 
157 /**
158  * Print debug information (statistics).
159  * @param alloc: on what alloc.
160  */
161 void alloc_stats(struct alloc_cache* alloc);
162 
163 /**
164  * Get a new regional for query states
165  * @param alloc: where to alloc it.
166  * @return regional for use or NULL on alloc failure.
167  */
168 struct regional* alloc_reg_obtain(struct alloc_cache* alloc);
169 
170 /**
171  * Put regional for query states back into alloc cache.
172  * @param alloc: where to alloc it.
173  * @param r: regional to put back.
174  */
175 void alloc_reg_release(struct alloc_cache* alloc, struct regional* r);
176 
177 /**
178  * Set cleanup on ID overflow callback function. This should remove all
179  * RRset ID references from the program. Clear the caches.
180  * @param alloc: the alloc
181  * @param cleanup: the callback function, called as cleanup(arg).
182  * @param arg: user argument to callback function.
183  */
184 void alloc_set_id_cleanup(struct alloc_cache* alloc, void (*cleanup)(void*),
185 	void* arg);
186 
187 #ifdef UNBOUND_ALLOC_LITE
188 #  include <sldns/ldns.h>
189 #  include <sldns/packet.h>
190 #  ifdef HAVE_OPENSSL_SSL_H
191 #    include <openssl/ssl.h>
192 #  endif
193 #  define malloc(s) unbound_stat_malloc_lite(s, __FILE__, __LINE__, __func__)
194 #  define calloc(n,s) unbound_stat_calloc_lite(n, s, __FILE__, __LINE__, __func__)
195 #  define free(p) unbound_stat_free_lite(p, __FILE__, __LINE__, __func__)
196 #  define realloc(p,s) unbound_stat_realloc_lite(p, s, __FILE__, __LINE__, __func__)
197 void *unbound_stat_malloc_lite(size_t size, const char* file, int line,
198 	const char* func);
199 void *unbound_stat_calloc_lite(size_t nmemb, size_t size, const char* file,
200 	int line, const char* func);
201 void unbound_stat_free_lite(void *ptr, const char* file, int line,
202 	const char* func);
203 void *unbound_stat_realloc_lite(void *ptr, size_t size, const char* file,
204 	int line, const char* func);
205 #  ifdef strdup
206 #    undef strdup
207 #  endif
208 #  define strdup(s) unbound_strdup_lite(s, __FILE__, __LINE__, __func__)
209 char* unbound_strdup_lite(const char* s, const char* file, int line,
210 	const char* func);
211 char* unbound_lite_wrapstr(char* s);
212 #  define sldns_rr2str(rr) unbound_lite_wrapstr(sldns_rr2str(rr))
213 #  define sldns_rdf2str(rdf) unbound_lite_wrapstr(sldns_rdf2str(rdf))
214 #  define sldns_rr_type2str(t) unbound_lite_wrapstr(sldns_rr_type2str(t))
215 #  define sldns_rr_class2str(c) unbound_lite_wrapstr(sldns_rr_class2str(c))
216 #  define sldns_rr_list2str(r) unbound_lite_wrapstr(sldns_rr_list2str(r))
217 #  define sldns_pkt2str(p) unbound_lite_wrapstr(sldns_pkt2str(p))
218 #  define sldns_pkt_rcode2str(r) unbound_lite_wrapstr(sldns_pkt_rcode2str(r))
219 #  define sldns_pkt2wire(a, r, s) unbound_lite_pkt2wire(a, r, s)
220 sldns_status unbound_lite_pkt2wire(uint8_t **dest, const sldns_pkt *p, size_t *size);
221 #  define i2d_DSA_SIG(d, s) unbound_lite_i2d_DSA_SIG(d, s)
222 int unbound_lite_i2d_DSA_SIG(DSA_SIG* dsasig, unsigned char** sig);
223 #endif /* UNBOUND_ALLOC_LITE */
224 
225 #endif /* UTIL_ALLOC_H */
226