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_t; 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_t*)((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_t 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_t* 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 * Get a new special_t element. 120 * @param alloc: where to alloc it. 121 * @return: memory block. Will not return NULL (instead fatal_exit). 122 * The block is zeroed. 123 */ 124 alloc_special_t* alloc_special_obtain(struct alloc_cache* alloc); 125 126 /** 127 * Return special_t back to pool. 128 * The block is cleaned up (zeroed) which also invalidates the ID inside. 129 * @param alloc: where to alloc it. 130 * @param mem: block to free. 131 */ 132 void alloc_special_release(struct alloc_cache* alloc, alloc_special_t* mem); 133 134 /** 135 * Set ID number of special type to a fresh new ID number. 136 * In case of ID number overflow, the rrset cache has to be cleared. 137 * @param alloc: the alloc cache 138 * @return: fresh id is returned. 139 */ 140 uint64_t alloc_get_id(struct alloc_cache* alloc); 141 142 /** 143 * Get memory size of alloc cache, alloc structure including special types. 144 * @param alloc: on what alloc. 145 * @return size in bytes. 146 */ 147 size_t alloc_get_mem(struct alloc_cache* alloc); 148 149 /** 150 * Print debug information (statistics). 151 * @param alloc: on what alloc. 152 */ 153 void alloc_stats(struct alloc_cache* alloc); 154 155 /** 156 * Get a new regional for query states 157 * @param alloc: where to alloc it. 158 * @return regional for use or NULL on alloc failure. 159 */ 160 struct regional* alloc_reg_obtain(struct alloc_cache* alloc); 161 162 /** 163 * Put regional for query states back into alloc cache. 164 * @param alloc: where to alloc it. 165 * @param r: regional to put back. 166 */ 167 void alloc_reg_release(struct alloc_cache* alloc, struct regional* r); 168 169 /** 170 * Set cleanup on ID overflow callback function. This should remove all 171 * RRset ID references from the program. Clear the caches. 172 * @param alloc: the alloc 173 * @param cleanup: the callback function, called as cleanup(arg). 174 * @param arg: user argument to callback function. 175 */ 176 void alloc_set_id_cleanup(struct alloc_cache* alloc, void (*cleanup)(void*), 177 void* arg); 178 179 #ifdef UNBOUND_ALLOC_LITE 180 # include <sldns/ldns.h> 181 # include <sldns/packet.h> 182 # ifdef HAVE_OPENSSL_SSL_H 183 # include <openssl/ssl.h> 184 # endif 185 # define malloc(s) unbound_stat_malloc_lite(s, __FILE__, __LINE__, __func__) 186 # define calloc(n,s) unbound_stat_calloc_lite(n, s, __FILE__, __LINE__, __func__) 187 # define free(p) unbound_stat_free_lite(p, __FILE__, __LINE__, __func__) 188 # define realloc(p,s) unbound_stat_realloc_lite(p, s, __FILE__, __LINE__, __func__) 189 void *unbound_stat_malloc_lite(size_t size, const char* file, int line, 190 const char* func); 191 void *unbound_stat_calloc_lite(size_t nmemb, size_t size, const char* file, 192 int line, const char* func); 193 void unbound_stat_free_lite(void *ptr, const char* file, int line, 194 const char* func); 195 void *unbound_stat_realloc_lite(void *ptr, size_t size, const char* file, 196 int line, const char* func); 197 # ifdef strdup 198 # undef strdup 199 # endif 200 # define strdup(s) unbound_strdup_lite(s, __FILE__, __LINE__, __func__) 201 char* unbound_strdup_lite(const char* s, const char* file, int line, 202 const char* func); 203 char* unbound_lite_wrapstr(char* s); 204 # define sldns_rr2str(rr) unbound_lite_wrapstr(sldns_rr2str(rr)) 205 # define sldns_rdf2str(rdf) unbound_lite_wrapstr(sldns_rdf2str(rdf)) 206 # define sldns_rr_type2str(t) unbound_lite_wrapstr(sldns_rr_type2str(t)) 207 # define sldns_rr_class2str(c) unbound_lite_wrapstr(sldns_rr_class2str(c)) 208 # define sldns_rr_list2str(r) unbound_lite_wrapstr(sldns_rr_list2str(r)) 209 # define sldns_pkt2str(p) unbound_lite_wrapstr(sldns_pkt2str(p)) 210 # define sldns_pkt_rcode2str(r) unbound_lite_wrapstr(sldns_pkt_rcode2str(r)) 211 # define sldns_pkt2wire(a, r, s) unbound_lite_pkt2wire(a, r, s) 212 sldns_status unbound_lite_pkt2wire(uint8_t **dest, const sldns_pkt *p, size_t *size); 213 # define i2d_DSA_SIG(d, s) unbound_lite_i2d_DSA_SIG(d, s) 214 int unbound_lite_i2d_DSA_SIG(DSA_SIG* dsasig, unsigned char** sig); 215 #endif /* UNBOUND_ALLOC_LITE */ 216 217 #endif /* UTIL_ALLOC_H */ 218