1933707f3Ssthen /* 2933707f3Ssthen * util/alloc.h - memory allocation service. 3933707f3Ssthen * 4933707f3Ssthen * Copyright (c) 2007, NLnet Labs. All rights reserved. 5933707f3Ssthen * 6933707f3Ssthen * This software is open source. 7933707f3Ssthen * 8933707f3Ssthen * Redistribution and use in source and binary forms, with or without 9933707f3Ssthen * modification, are permitted provided that the following conditions 10933707f3Ssthen * are met: 11933707f3Ssthen * 12933707f3Ssthen * Redistributions of source code must retain the above copyright notice, 13933707f3Ssthen * this list of conditions and the following disclaimer. 14933707f3Ssthen * 15933707f3Ssthen * Redistributions in binary form must reproduce the above copyright notice, 16933707f3Ssthen * this list of conditions and the following disclaimer in the documentation 17933707f3Ssthen * and/or other materials provided with the distribution. 18933707f3Ssthen * 19933707f3Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may 20933707f3Ssthen * be used to endorse or promote products derived from this software without 21933707f3Ssthen * specific prior written permission. 22933707f3Ssthen * 23933707f3Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 245d76a658Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 255d76a658Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 265d76a658Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 275d76a658Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 285d76a658Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 295d76a658Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 305d76a658Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 315d76a658Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 325d76a658Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 335d76a658Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34933707f3Ssthen */ 35933707f3Ssthen 36933707f3Ssthen /** 37933707f3Ssthen * \file 38933707f3Ssthen * 39933707f3Ssthen * This file contains memory allocation functions. 40933707f3Ssthen * 41933707f3Ssthen * The reasons for this service are: 42933707f3Ssthen * o Avoid locking costs of getting global lock to call malloc(). 43933707f3Ssthen * o The packed rrset type needs to be kept on special freelists, 44933707f3Ssthen * so that they are reused for other packet rrset allocations. 45933707f3Ssthen * 46933707f3Ssthen */ 47933707f3Ssthen 48933707f3Ssthen #ifndef UTIL_ALLOC_H 49933707f3Ssthen #define UTIL_ALLOC_H 50933707f3Ssthen 51933707f3Ssthen #include "util/locks.h" 52933707f3Ssthen struct ub_packed_rrset_key; 53933707f3Ssthen struct regional; 54933707f3Ssthen 55933707f3Ssthen /** The special type, packed rrset. Not allowed to be used for other memory */ 5677079be7Ssthen typedef struct ub_packed_rrset_key alloc_special_type; 57933707f3Ssthen /** clean the special type. Pass pointer. */ 58933707f3Ssthen #define alloc_special_clean(x) (x)->id = 0; 59933707f3Ssthen /** access next pointer. (in available spot). Pass pointer. */ 6077079be7Ssthen #define alloc_special_next(x) ((alloc_special_type*)((x)->entry.overflow_next)) 61933707f3Ssthen /** set next pointer. (in available spot). Pass pointers. */ 62933707f3Ssthen #define alloc_set_special_next(x, y) \ 63933707f3Ssthen ((x)->entry.overflow_next) = (struct lruhash_entry*)(y); 64933707f3Ssthen 65933707f3Ssthen /** how many blocks to cache locally. */ 66933707f3Ssthen #define ALLOC_SPECIAL_MAX 10 67933707f3Ssthen 68933707f3Ssthen /** 69933707f3Ssthen * Structure that provides allocation. Use one per thread. 70933707f3Ssthen * The one on top has a NULL super pointer. 71933707f3Ssthen */ 72933707f3Ssthen struct alloc_cache { 73933707f3Ssthen /** lock, only used for the super. */ 7477079be7Ssthen lock_quick_type lock; 75933707f3Ssthen /** global allocator above this one. NULL for none (malloc/free) */ 76933707f3Ssthen struct alloc_cache* super; 77933707f3Ssthen /** singly linked lists of special type. These are free for use. */ 7877079be7Ssthen alloc_special_type* quar; 79933707f3Ssthen /** number of items in quarantine. */ 80933707f3Ssthen size_t num_quar; 81933707f3Ssthen /** thread number for id creation */ 82933707f3Ssthen int thread_num; 83933707f3Ssthen /** next id number to pass out */ 84933707f3Ssthen uint64_t next_id; 85933707f3Ssthen /** last id number possible */ 86933707f3Ssthen uint64_t last_id; 87933707f3Ssthen /** what function to call to cleanup when last id is reached */ 88933707f3Ssthen void (*cleanup)(void*); 89933707f3Ssthen /** user arg for cleanup */ 90933707f3Ssthen void* cleanup_arg; 91933707f3Ssthen 92933707f3Ssthen /** how many regional blocks to keep back max */ 93933707f3Ssthen size_t max_reg_blocks; 94933707f3Ssthen /** how many regional blocks are kept now */ 95933707f3Ssthen size_t num_reg_blocks; 96933707f3Ssthen /** linked list of regional blocks, using regional->next */ 97933707f3Ssthen struct regional* reg_list; 98933707f3Ssthen }; 99933707f3Ssthen 100933707f3Ssthen /** 101933707f3Ssthen * Init alloc (zeroes the struct). 102933707f3Ssthen * @param alloc: this parameter is allocated by the caller. 103933707f3Ssthen * @param super: super to use (init that before with super_init). 104933707f3Ssthen * Pass this argument NULL to init the toplevel alloc structure. 105933707f3Ssthen * @param thread_num: thread number for id creation of special type. 106933707f3Ssthen */ 107933707f3Ssthen void alloc_init(struct alloc_cache* alloc, struct alloc_cache* super, 108933707f3Ssthen int thread_num); 109933707f3Ssthen 110933707f3Ssthen /** 111933707f3Ssthen * Free the alloc. Pushes all the cached items into the super structure. 112933707f3Ssthen * Or deletes them if alloc->super is NULL. 113933707f3Ssthen * Does not free the alloc struct itself (it was also allocated by caller). 114933707f3Ssthen * @param alloc: is almost zeroed on exit (except some stats). 115933707f3Ssthen */ 116933707f3Ssthen void alloc_clear(struct alloc_cache* alloc); 117933707f3Ssthen 118933707f3Ssthen /** 119*20237c55Ssthen * Free the special alloced items. The rrset and message caches must be 120*20237c55Ssthen * empty, there must be no more references to rrset pointers into the 121*20237c55Ssthen * rrset cache. 122*20237c55Ssthen * @param alloc: the special allocs are freed. 123*20237c55Ssthen */ 124*20237c55Ssthen void alloc_clear_special(struct alloc_cache* alloc); 125*20237c55Ssthen 126*20237c55Ssthen /** 12777079be7Ssthen * Get a new special_type element. 128933707f3Ssthen * @param alloc: where to alloc it. 129933707f3Ssthen * @return: memory block. Will not return NULL (instead fatal_exit). 130933707f3Ssthen * The block is zeroed. 131933707f3Ssthen */ 13277079be7Ssthen alloc_special_type* alloc_special_obtain(struct alloc_cache* alloc); 133933707f3Ssthen 134933707f3Ssthen /** 13577079be7Ssthen * Return special_type back to pool. 136933707f3Ssthen * The block is cleaned up (zeroed) which also invalidates the ID inside. 137933707f3Ssthen * @param alloc: where to alloc it. 138933707f3Ssthen * @param mem: block to free. 139933707f3Ssthen */ 14077079be7Ssthen void alloc_special_release(struct alloc_cache* alloc, alloc_special_type* mem); 141933707f3Ssthen 142933707f3Ssthen /** 143933707f3Ssthen * Set ID number of special type to a fresh new ID number. 144933707f3Ssthen * In case of ID number overflow, the rrset cache has to be cleared. 145933707f3Ssthen * @param alloc: the alloc cache 146933707f3Ssthen * @return: fresh id is returned. 147933707f3Ssthen */ 148933707f3Ssthen uint64_t alloc_get_id(struct alloc_cache* alloc); 149933707f3Ssthen 150933707f3Ssthen /** 151933707f3Ssthen * Get memory size of alloc cache, alloc structure including special types. 152933707f3Ssthen * @param alloc: on what alloc. 153933707f3Ssthen * @return size in bytes. 154933707f3Ssthen */ 155933707f3Ssthen size_t alloc_get_mem(struct alloc_cache* alloc); 156933707f3Ssthen 157933707f3Ssthen /** 158933707f3Ssthen * Print debug information (statistics). 159933707f3Ssthen * @param alloc: on what alloc. 160933707f3Ssthen */ 161933707f3Ssthen void alloc_stats(struct alloc_cache* alloc); 162933707f3Ssthen 163933707f3Ssthen /** 164933707f3Ssthen * Get a new regional for query states 165933707f3Ssthen * @param alloc: where to alloc it. 166933707f3Ssthen * @return regional for use or NULL on alloc failure. 167933707f3Ssthen */ 168933707f3Ssthen struct regional* alloc_reg_obtain(struct alloc_cache* alloc); 169933707f3Ssthen 170933707f3Ssthen /** 171933707f3Ssthen * Put regional for query states back into alloc cache. 172933707f3Ssthen * @param alloc: where to alloc it. 173933707f3Ssthen * @param r: regional to put back. 174933707f3Ssthen */ 175933707f3Ssthen void alloc_reg_release(struct alloc_cache* alloc, struct regional* r); 176933707f3Ssthen 177933707f3Ssthen /** 178933707f3Ssthen * Set cleanup on ID overflow callback function. This should remove all 179933707f3Ssthen * RRset ID references from the program. Clear the caches. 180933707f3Ssthen * @param alloc: the alloc 181933707f3Ssthen * @param cleanup: the callback function, called as cleanup(arg). 182933707f3Ssthen * @param arg: user argument to callback function. 183933707f3Ssthen */ 184933707f3Ssthen void alloc_set_id_cleanup(struct alloc_cache* alloc, void (*cleanup)(void*), 185933707f3Ssthen void* arg); 186933707f3Ssthen 187933707f3Ssthen #ifdef UNBOUND_ALLOC_LITE 188fdfb4ba6Ssthen # include <sldns/ldns.h> 189fdfb4ba6Ssthen # include <sldns/packet.h> 1903dcb24b8Ssthen # ifdef HAVE_OPENSSL_SSL_H 191933707f3Ssthen # include <openssl/ssl.h> 1923dcb24b8Ssthen # endif 193933707f3Ssthen # define malloc(s) unbound_stat_malloc_lite(s, __FILE__, __LINE__, __func__) 194933707f3Ssthen # define calloc(n,s) unbound_stat_calloc_lite(n, s, __FILE__, __LINE__, __func__) 195933707f3Ssthen # define free(p) unbound_stat_free_lite(p, __FILE__, __LINE__, __func__) 196933707f3Ssthen # define realloc(p,s) unbound_stat_realloc_lite(p, s, __FILE__, __LINE__, __func__) 197933707f3Ssthen void *unbound_stat_malloc_lite(size_t size, const char* file, int line, 198933707f3Ssthen const char* func); 199933707f3Ssthen void *unbound_stat_calloc_lite(size_t nmemb, size_t size, const char* file, 200933707f3Ssthen int line, const char* func); 201933707f3Ssthen void unbound_stat_free_lite(void *ptr, const char* file, int line, 202933707f3Ssthen const char* func); 203933707f3Ssthen void *unbound_stat_realloc_lite(void *ptr, size_t size, const char* file, 204933707f3Ssthen int line, const char* func); 205933707f3Ssthen # ifdef strdup 206933707f3Ssthen # undef strdup 207933707f3Ssthen # endif 208933707f3Ssthen # define strdup(s) unbound_strdup_lite(s, __FILE__, __LINE__, __func__) 209933707f3Ssthen char* unbound_strdup_lite(const char* s, const char* file, int line, 210933707f3Ssthen const char* func); 211933707f3Ssthen char* unbound_lite_wrapstr(char* s); 2125d76a658Ssthen # define sldns_rr2str(rr) unbound_lite_wrapstr(sldns_rr2str(rr)) 2135d76a658Ssthen # define sldns_rdf2str(rdf) unbound_lite_wrapstr(sldns_rdf2str(rdf)) 2145d76a658Ssthen # define sldns_rr_type2str(t) unbound_lite_wrapstr(sldns_rr_type2str(t)) 2155d76a658Ssthen # define sldns_rr_class2str(c) unbound_lite_wrapstr(sldns_rr_class2str(c)) 2165d76a658Ssthen # define sldns_rr_list2str(r) unbound_lite_wrapstr(sldns_rr_list2str(r)) 2175d76a658Ssthen # define sldns_pkt2str(p) unbound_lite_wrapstr(sldns_pkt2str(p)) 2185d76a658Ssthen # define sldns_pkt_rcode2str(r) unbound_lite_wrapstr(sldns_pkt_rcode2str(r)) 2195d76a658Ssthen # define sldns_pkt2wire(a, r, s) unbound_lite_pkt2wire(a, r, s) 2205d76a658Ssthen sldns_status unbound_lite_pkt2wire(uint8_t **dest, const sldns_pkt *p, size_t *size); 221933707f3Ssthen # define i2d_DSA_SIG(d, s) unbound_lite_i2d_DSA_SIG(d, s) 222933707f3Ssthen int unbound_lite_i2d_DSA_SIG(DSA_SIG* dsasig, unsigned char** sig); 223933707f3Ssthen #endif /* UNBOUND_ALLOC_LITE */ 224933707f3Ssthen 225933707f3Ssthen #endif /* UTIL_ALLOC_H */ 226