1238106Sdes/* 2238106Sdes * util/alloc.h - memory allocation service. 3238106Sdes * 4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved. 5238106Sdes * 6238106Sdes * This software is open source. 7238106Sdes * 8238106Sdes * Redistribution and use in source and binary forms, with or without 9238106Sdes * modification, are permitted provided that the following conditions 10238106Sdes * are met: 11238106Sdes * 12238106Sdes * Redistributions of source code must retain the above copyright notice, 13238106Sdes * this list of conditions and the following disclaimer. 14238106Sdes * 15238106Sdes * Redistributions in binary form must reproduce the above copyright notice, 16238106Sdes * this list of conditions and the following disclaimer in the documentation 17238106Sdes * and/or other materials provided with the distribution. 18238106Sdes * 19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may 20238106Sdes * be used to endorse or promote products derived from this software without 21238106Sdes * specific prior written permission. 22238106Sdes * 23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34238106Sdes */ 35238106Sdes 36238106Sdes/** 37238106Sdes * \file 38238106Sdes * 39238106Sdes * This file contains memory allocation functions. 40238106Sdes * 41238106Sdes * The reasons for this service are: 42238106Sdes * o Avoid locking costs of getting global lock to call malloc(). 43238106Sdes * o The packed rrset type needs to be kept on special freelists, 44238106Sdes * so that they are reused for other packet rrset allocations. 45238106Sdes * 46238106Sdes */ 47238106Sdes 48238106Sdes#ifndef UTIL_ALLOC_H 49238106Sdes#define UTIL_ALLOC_H 50238106Sdes 51238106Sdes#include "util/locks.h" 52238106Sdesstruct ub_packed_rrset_key; 53238106Sdesstruct regional; 54238106Sdes 55238106Sdes/** The special type, packed rrset. Not allowed to be used for other memory */ 56238106Sdestypedef struct ub_packed_rrset_key alloc_special_t; 57238106Sdes/** clean the special type. Pass pointer. */ 58238106Sdes#define alloc_special_clean(x) (x)->id = 0; 59238106Sdes/** access next pointer. (in available spot). Pass pointer. */ 60238106Sdes#define alloc_special_next(x) ((alloc_special_t*)((x)->entry.overflow_next)) 61238106Sdes/** set next pointer. (in available spot). Pass pointers. */ 62238106Sdes#define alloc_set_special_next(x, y) \ 63238106Sdes ((x)->entry.overflow_next) = (struct lruhash_entry*)(y); 64238106Sdes 65238106Sdes/** how many blocks to cache locally. */ 66238106Sdes#define ALLOC_SPECIAL_MAX 10 67238106Sdes 68238106Sdes/** 69238106Sdes * Structure that provides allocation. Use one per thread. 70238106Sdes * The one on top has a NULL super pointer. 71238106Sdes */ 72238106Sdesstruct alloc_cache { 73238106Sdes /** lock, only used for the super. */ 74238106Sdes lock_quick_t lock; 75238106Sdes /** global allocator above this one. NULL for none (malloc/free) */ 76238106Sdes struct alloc_cache* super; 77238106Sdes /** singly linked lists of special type. These are free for use. */ 78238106Sdes alloc_special_t* quar; 79238106Sdes /** number of items in quarantine. */ 80238106Sdes size_t num_quar; 81238106Sdes /** thread number for id creation */ 82238106Sdes int thread_num; 83238106Sdes /** next id number to pass out */ 84238106Sdes uint64_t next_id; 85238106Sdes /** last id number possible */ 86238106Sdes uint64_t last_id; 87238106Sdes /** what function to call to cleanup when last id is reached */ 88238106Sdes void (*cleanup)(void*); 89238106Sdes /** user arg for cleanup */ 90238106Sdes void* cleanup_arg; 91238106Sdes 92238106Sdes /** how many regional blocks to keep back max */ 93238106Sdes size_t max_reg_blocks; 94238106Sdes /** how many regional blocks are kept now */ 95238106Sdes size_t num_reg_blocks; 96238106Sdes /** linked list of regional blocks, using regional->next */ 97238106Sdes struct regional* reg_list; 98238106Sdes}; 99238106Sdes 100238106Sdes/** 101238106Sdes * Init alloc (zeroes the struct). 102238106Sdes * @param alloc: this parameter is allocated by the caller. 103238106Sdes * @param super: super to use (init that before with super_init). 104238106Sdes * Pass this argument NULL to init the toplevel alloc structure. 105238106Sdes * @param thread_num: thread number for id creation of special type. 106238106Sdes */ 107238106Sdesvoid alloc_init(struct alloc_cache* alloc, struct alloc_cache* super, 108238106Sdes int thread_num); 109238106Sdes 110238106Sdes/** 111238106Sdes * Free the alloc. Pushes all the cached items into the super structure. 112238106Sdes * Or deletes them if alloc->super is NULL. 113238106Sdes * Does not free the alloc struct itself (it was also allocated by caller). 114238106Sdes * @param alloc: is almost zeroed on exit (except some stats). 115238106Sdes */ 116238106Sdesvoid alloc_clear(struct alloc_cache* alloc); 117238106Sdes 118238106Sdes/** 119238106Sdes * Get a new special_t element. 120238106Sdes * @param alloc: where to alloc it. 121238106Sdes * @return: memory block. Will not return NULL (instead fatal_exit). 122238106Sdes * The block is zeroed. 123238106Sdes */ 124238106Sdesalloc_special_t* alloc_special_obtain(struct alloc_cache* alloc); 125238106Sdes 126238106Sdes/** 127238106Sdes * Return special_t back to pool. 128238106Sdes * The block is cleaned up (zeroed) which also invalidates the ID inside. 129238106Sdes * @param alloc: where to alloc it. 130238106Sdes * @param mem: block to free. 131238106Sdes */ 132238106Sdesvoid alloc_special_release(struct alloc_cache* alloc, alloc_special_t* mem); 133238106Sdes 134238106Sdes/** 135238106Sdes * Set ID number of special type to a fresh new ID number. 136238106Sdes * In case of ID number overflow, the rrset cache has to be cleared. 137238106Sdes * @param alloc: the alloc cache 138238106Sdes * @return: fresh id is returned. 139238106Sdes */ 140238106Sdesuint64_t alloc_get_id(struct alloc_cache* alloc); 141238106Sdes 142238106Sdes/** 143238106Sdes * Get memory size of alloc cache, alloc structure including special types. 144238106Sdes * @param alloc: on what alloc. 145238106Sdes * @return size in bytes. 146238106Sdes */ 147238106Sdessize_t alloc_get_mem(struct alloc_cache* alloc); 148238106Sdes 149238106Sdes/** 150238106Sdes * Print debug information (statistics). 151238106Sdes * @param alloc: on what alloc. 152238106Sdes */ 153238106Sdesvoid alloc_stats(struct alloc_cache* alloc); 154238106Sdes 155238106Sdes/** 156238106Sdes * Get a new regional for query states 157238106Sdes * @param alloc: where to alloc it. 158238106Sdes * @return regional for use or NULL on alloc failure. 159238106Sdes */ 160238106Sdesstruct regional* alloc_reg_obtain(struct alloc_cache* alloc); 161238106Sdes 162238106Sdes/** 163238106Sdes * Put regional for query states back into alloc cache. 164238106Sdes * @param alloc: where to alloc it. 165238106Sdes * @param r: regional to put back. 166238106Sdes */ 167238106Sdesvoid alloc_reg_release(struct alloc_cache* alloc, struct regional* r); 168238106Sdes 169238106Sdes/** 170238106Sdes * Set cleanup on ID overflow callback function. This should remove all 171238106Sdes * RRset ID references from the program. Clear the caches. 172238106Sdes * @param alloc: the alloc 173238106Sdes * @param cleanup: the callback function, called as cleanup(arg). 174238106Sdes * @param arg: user argument to callback function. 175238106Sdes */ 176238106Sdesvoid alloc_set_id_cleanup(struct alloc_cache* alloc, void (*cleanup)(void*), 177238106Sdes void* arg); 178238106Sdes 179238106Sdes#ifdef UNBOUND_ALLOC_LITE 180291767Sdes# include <sldns/ldns.h> 181291767Sdes# include <sldns/packet.h> 182249141Sdes# ifdef HAVE_OPENSSL_SSL_H 183249141Sdes# include <openssl/ssl.h> 184249141Sdes# endif 185238106Sdes# define malloc(s) unbound_stat_malloc_lite(s, __FILE__, __LINE__, __func__) 186238106Sdes# define calloc(n,s) unbound_stat_calloc_lite(n, s, __FILE__, __LINE__, __func__) 187238106Sdes# define free(p) unbound_stat_free_lite(p, __FILE__, __LINE__, __func__) 188238106Sdes# define realloc(p,s) unbound_stat_realloc_lite(p, s, __FILE__, __LINE__, __func__) 189238106Sdesvoid *unbound_stat_malloc_lite(size_t size, const char* file, int line, 190238106Sdes const char* func); 191238106Sdesvoid *unbound_stat_calloc_lite(size_t nmemb, size_t size, const char* file, 192238106Sdes int line, const char* func); 193238106Sdesvoid unbound_stat_free_lite(void *ptr, const char* file, int line, 194238106Sdes const char* func); 195238106Sdesvoid *unbound_stat_realloc_lite(void *ptr, size_t size, const char* file, 196238106Sdes int line, const char* func); 197238106Sdes# ifdef strdup 198238106Sdes# undef strdup 199238106Sdes# endif 200238106Sdes# define strdup(s) unbound_strdup_lite(s, __FILE__, __LINE__, __func__) 201238106Sdeschar* unbound_strdup_lite(const char* s, const char* file, int line, 202238106Sdes const char* func); 203238106Sdeschar* unbound_lite_wrapstr(char* s); 204269257Sdes# define sldns_rr2str(rr) unbound_lite_wrapstr(sldns_rr2str(rr)) 205269257Sdes# define sldns_rdf2str(rdf) unbound_lite_wrapstr(sldns_rdf2str(rdf)) 206269257Sdes# define sldns_rr_type2str(t) unbound_lite_wrapstr(sldns_rr_type2str(t)) 207269257Sdes# define sldns_rr_class2str(c) unbound_lite_wrapstr(sldns_rr_class2str(c)) 208269257Sdes# define sldns_rr_list2str(r) unbound_lite_wrapstr(sldns_rr_list2str(r)) 209269257Sdes# define sldns_pkt2str(p) unbound_lite_wrapstr(sldns_pkt2str(p)) 210269257Sdes# define sldns_pkt_rcode2str(r) unbound_lite_wrapstr(sldns_pkt_rcode2str(r)) 211269257Sdes# define sldns_pkt2wire(a, r, s) unbound_lite_pkt2wire(a, r, s) 212269257Sdessldns_status unbound_lite_pkt2wire(uint8_t **dest, const sldns_pkt *p, size_t *size); 213238106Sdes# define i2d_DSA_SIG(d, s) unbound_lite_i2d_DSA_SIG(d, s) 214238106Sdesint unbound_lite_i2d_DSA_SIG(DSA_SIG* dsasig, unsigned char** sig); 215238106Sdes#endif /* UNBOUND_ALLOC_LITE */ 216238106Sdes 217238106Sdes#endif /* UTIL_ALLOC_H */ 218