xref: /netbsd-src/external/bsd/unbound/dist/util/alloc.c (revision 01049ae6d55a7fce6c6379cd1e0c997c04dc0061)
13b6c3722Schristos /*
23b6c3722Schristos  * util/alloc.c - memory allocation service.
33b6c3722Schristos  *
43b6c3722Schristos  * Copyright (c) 2007, NLnet Labs. All rights reserved.
53b6c3722Schristos  *
63b6c3722Schristos  * This software is open source.
73b6c3722Schristos  *
83b6c3722Schristos  * Redistribution and use in source and binary forms, with or without
93b6c3722Schristos  * modification, are permitted provided that the following conditions
103b6c3722Schristos  * are met:
113b6c3722Schristos  *
123b6c3722Schristos  * Redistributions of source code must retain the above copyright notice,
133b6c3722Schristos  * this list of conditions and the following disclaimer.
143b6c3722Schristos  *
153b6c3722Schristos  * Redistributions in binary form must reproduce the above copyright notice,
163b6c3722Schristos  * this list of conditions and the following disclaimer in the documentation
173b6c3722Schristos  * and/or other materials provided with the distribution.
183b6c3722Schristos  *
193b6c3722Schristos  * Neither the name of the NLNET LABS nor the names of its contributors may
203b6c3722Schristos  * be used to endorse or promote products derived from this software without
213b6c3722Schristos  * specific prior written permission.
223b6c3722Schristos  *
233b6c3722Schristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
243b6c3722Schristos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
253b6c3722Schristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
263b6c3722Schristos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
273b6c3722Schristos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
283b6c3722Schristos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
293b6c3722Schristos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
303b6c3722Schristos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
313b6c3722Schristos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
323b6c3722Schristos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
333b6c3722Schristos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
343b6c3722Schristos  */
353b6c3722Schristos 
363b6c3722Schristos /**
373b6c3722Schristos  * \file
383b6c3722Schristos  *
393b6c3722Schristos  * This file contains memory allocation functions.
403b6c3722Schristos  */
413b6c3722Schristos 
423b6c3722Schristos #include "config.h"
433b6c3722Schristos #include "util/alloc.h"
443b6c3722Schristos #include "util/regional.h"
453b6c3722Schristos #include "util/data/packed_rrset.h"
463b6c3722Schristos #include "util/fptr_wlist.h"
473b6c3722Schristos 
483b6c3722Schristos /** custom size of cached regional blocks */
493b6c3722Schristos #define ALLOC_REG_SIZE	16384
503b6c3722Schristos /** number of bits for ID part of uint64, rest for number of threads. */
513b6c3722Schristos #define THRNUM_SHIFT	48	/* for 65k threads, 2^48 rrsets per thr. */
523b6c3722Schristos 
533b6c3722Schristos /** setup new special type */
543b6c3722Schristos static void
alloc_setup_special(alloc_special_type * t)550cd9f4ecSchristos alloc_setup_special(alloc_special_type* t)
563b6c3722Schristos {
573b6c3722Schristos 	memset(t, 0, sizeof(*t));
583b6c3722Schristos 	lock_rw_init(&t->entry.lock);
593b6c3722Schristos 	t->entry.key = t;
603b6c3722Schristos }
613b6c3722Schristos 
623b6c3722Schristos /** prealloc some entries in the cache. To minimize contention.
633b6c3722Schristos  * Result is 1 lock per alloc_max newly created entries.
643b6c3722Schristos  * @param alloc: the structure to fill up.
653b6c3722Schristos  */
663b6c3722Schristos static void
prealloc_setup(struct alloc_cache * alloc)670cd9f4ecSchristos prealloc_setup(struct alloc_cache* alloc)
683b6c3722Schristos {
690cd9f4ecSchristos 	alloc_special_type* p;
703b6c3722Schristos 	int i;
713b6c3722Schristos 	for(i=0; i<ALLOC_SPECIAL_MAX; i++) {
720cd9f4ecSchristos 		if(!(p = (alloc_special_type*)malloc(
730cd9f4ecSchristos 			sizeof(alloc_special_type)))) {
743b6c3722Schristos 			log_err("prealloc: out of memory");
753b6c3722Schristos 			return;
763b6c3722Schristos 		}
773b6c3722Schristos 		alloc_setup_special(p);
783b6c3722Schristos 		alloc_set_special_next(p, alloc->quar);
793b6c3722Schristos 		alloc->quar = p;
803b6c3722Schristos 		alloc->num_quar++;
813b6c3722Schristos 	}
823b6c3722Schristos }
833b6c3722Schristos 
843b6c3722Schristos /** prealloc region blocks */
853b6c3722Schristos static void
prealloc_blocks(struct alloc_cache * alloc,size_t num)863b6c3722Schristos prealloc_blocks(struct alloc_cache* alloc, size_t num)
873b6c3722Schristos {
883b6c3722Schristos 	size_t i;
893b6c3722Schristos 	struct regional* r;
903b6c3722Schristos 	for(i=0; i<num; i++) {
913b6c3722Schristos 		r = regional_create_custom(ALLOC_REG_SIZE);
923b6c3722Schristos 		if(!r) {
933b6c3722Schristos 			log_err("prealloc blocks: out of memory");
943b6c3722Schristos 			return;
953b6c3722Schristos 		}
963b6c3722Schristos 		r->next = (char*)alloc->reg_list;
973b6c3722Schristos 		alloc->reg_list = r;
983b6c3722Schristos 		alloc->num_reg_blocks ++;
993b6c3722Schristos 	}
1003b6c3722Schristos }
1013b6c3722Schristos 
1023b6c3722Schristos void
alloc_init(struct alloc_cache * alloc,struct alloc_cache * super,int thread_num)1033b6c3722Schristos alloc_init(struct alloc_cache* alloc, struct alloc_cache* super,
1043b6c3722Schristos 	int thread_num)
1053b6c3722Schristos {
1063b6c3722Schristos 	memset(alloc, 0, sizeof(*alloc));
1073b6c3722Schristos 	alloc->super = super;
1083b6c3722Schristos 	alloc->thread_num = thread_num;
1093b6c3722Schristos 	alloc->next_id = (uint64_t)thread_num; 	/* in steps, so that type */
1103b6c3722Schristos 	alloc->next_id <<= THRNUM_SHIFT; 	/* of *_id is used. */
1113b6c3722Schristos 	alloc->last_id = 1; 			/* so no 64bit constants, */
1123b6c3722Schristos 	alloc->last_id <<= THRNUM_SHIFT; 	/* or implicit 'int' ops. */
1133b6c3722Schristos 	alloc->last_id -= 1; 			/* for compiler portability. */
1143b6c3722Schristos 	alloc->last_id |= alloc->next_id;
1153b6c3722Schristos 	alloc->next_id += 1;			/* because id=0 is special. */
1163b6c3722Schristos 	alloc->max_reg_blocks = 100;
1173b6c3722Schristos 	alloc->num_reg_blocks = 0;
1183b6c3722Schristos 	alloc->reg_list = NULL;
1193b6c3722Schristos 	alloc->cleanup = NULL;
1203b6c3722Schristos 	alloc->cleanup_arg = NULL;
1213b6c3722Schristos 	if(alloc->super)
1223b6c3722Schristos 		prealloc_blocks(alloc, alloc->max_reg_blocks);
1233b6c3722Schristos 	if(!alloc->super) {
1243b6c3722Schristos 		lock_quick_init(&alloc->lock);
1253b6c3722Schristos 		lock_protect(&alloc->lock, alloc, sizeof(*alloc));
1263b6c3722Schristos 	}
1273b6c3722Schristos }
1283b6c3722Schristos 
1297cd94d69Schristos /** free the special list */
1307cd94d69Schristos static void
alloc_clear_special_list(struct alloc_cache * alloc)1317cd94d69Schristos alloc_clear_special_list(struct alloc_cache* alloc)
1327cd94d69Schristos {
1337cd94d69Schristos 	alloc_special_type* p, *np;
1347cd94d69Schristos 	/* free */
1357cd94d69Schristos 	p = alloc->quar;
1367cd94d69Schristos 	while(p) {
1377cd94d69Schristos 		np = alloc_special_next(p);
1387cd94d69Schristos 		/* deinit special type */
1397cd94d69Schristos 		lock_rw_destroy(&p->entry.lock);
1407cd94d69Schristos 		free(p);
1417cd94d69Schristos 		p = np;
1427cd94d69Schristos 	}
1437cd94d69Schristos }
1447cd94d69Schristos 
1457cd94d69Schristos void
alloc_clear_special(struct alloc_cache * alloc)1467cd94d69Schristos alloc_clear_special(struct alloc_cache* alloc)
1477cd94d69Schristos {
1487cd94d69Schristos 	if(!alloc->super) {
1497cd94d69Schristos 		lock_quick_lock(&alloc->lock);
1507cd94d69Schristos 	}
1517cd94d69Schristos 	alloc_clear_special_list(alloc);
1527cd94d69Schristos 	alloc->quar = 0;
1537cd94d69Schristos 	alloc->num_quar = 0;
1547cd94d69Schristos 	if(!alloc->super) {
1557cd94d69Schristos 		lock_quick_unlock(&alloc->lock);
1567cd94d69Schristos 	}
1577cd94d69Schristos }
1587cd94d69Schristos 
1593b6c3722Schristos void
alloc_clear(struct alloc_cache * alloc)1603b6c3722Schristos alloc_clear(struct alloc_cache* alloc)
1613b6c3722Schristos {
1627cd94d69Schristos 	alloc_special_type* p;
1633b6c3722Schristos 	struct regional* r, *nr;
1643b6c3722Schristos 	if(!alloc)
1653b6c3722Schristos 		return;
1663b6c3722Schristos 	if(!alloc->super) {
1673b6c3722Schristos 		lock_quick_destroy(&alloc->lock);
1683b6c3722Schristos 	}
1693b6c3722Schristos 	if(alloc->super && alloc->quar) {
1703b6c3722Schristos 		/* push entire list into super */
1713b6c3722Schristos 		p = alloc->quar;
1723b6c3722Schristos 		while(alloc_special_next(p)) /* find last */
1733b6c3722Schristos 			p = alloc_special_next(p);
1743b6c3722Schristos 		lock_quick_lock(&alloc->super->lock);
1753b6c3722Schristos 		alloc_set_special_next(p, alloc->super->quar);
1763b6c3722Schristos 		alloc->super->quar = alloc->quar;
1773b6c3722Schristos 		alloc->super->num_quar += alloc->num_quar;
1783b6c3722Schristos 		lock_quick_unlock(&alloc->super->lock);
1793b6c3722Schristos 	} else {
1807cd94d69Schristos 		alloc_clear_special_list(alloc);
1813b6c3722Schristos 	}
1823b6c3722Schristos 	alloc->quar = 0;
1833b6c3722Schristos 	alloc->num_quar = 0;
1843b6c3722Schristos 	r = alloc->reg_list;
1853b6c3722Schristos 	while(r) {
1863b6c3722Schristos 		nr = (struct regional*)r->next;
1873b6c3722Schristos 		free(r);
1883b6c3722Schristos 		r = nr;
1893b6c3722Schristos 	}
1903b6c3722Schristos 	alloc->reg_list = NULL;
1913b6c3722Schristos 	alloc->num_reg_blocks = 0;
1923b6c3722Schristos }
1933b6c3722Schristos 
1943b6c3722Schristos uint64_t
alloc_get_id(struct alloc_cache * alloc)1953b6c3722Schristos alloc_get_id(struct alloc_cache* alloc)
1963b6c3722Schristos {
1973b6c3722Schristos 	uint64_t id = alloc->next_id++;
1983b6c3722Schristos 	if(id == alloc->last_id) {
1993b6c3722Schristos 		log_warn("rrset alloc: out of 64bit ids. Clearing cache.");
2003b6c3722Schristos 		fptr_ok(fptr_whitelist_alloc_cleanup(alloc->cleanup));
2013b6c3722Schristos 		(*alloc->cleanup)(alloc->cleanup_arg);
2023b6c3722Schristos 
2033b6c3722Schristos 		/* start back at first number */   	/* like in alloc_init*/
2043b6c3722Schristos 		alloc->next_id = (uint64_t)alloc->thread_num;
2053b6c3722Schristos 		alloc->next_id <<= THRNUM_SHIFT; 	/* in steps for comp. */
2063b6c3722Schristos 		alloc->next_id += 1;			/* portability. */
2073b6c3722Schristos 		/* and generate new and safe id */
2083b6c3722Schristos 		id = alloc->next_id++;
2093b6c3722Schristos 	}
2103b6c3722Schristos 	return id;
2113b6c3722Schristos }
2123b6c3722Schristos 
2130cd9f4ecSchristos alloc_special_type*
alloc_special_obtain(struct alloc_cache * alloc)2143b6c3722Schristos alloc_special_obtain(struct alloc_cache* alloc)
2153b6c3722Schristos {
2160cd9f4ecSchristos 	alloc_special_type* p;
2173b6c3722Schristos 	log_assert(alloc);
2183b6c3722Schristos 	/* see if in local cache */
2193b6c3722Schristos 	if(alloc->quar) {
2203b6c3722Schristos 		p = alloc->quar;
2213b6c3722Schristos 		alloc->quar = alloc_special_next(p);
2223b6c3722Schristos 		alloc->num_quar--;
2233b6c3722Schristos 		p->id = alloc_get_id(alloc);
2243b6c3722Schristos 		return p;
2253b6c3722Schristos 	}
2263b6c3722Schristos 	/* see if in global cache */
2273b6c3722Schristos 	if(alloc->super) {
2283b6c3722Schristos 		/* could maybe grab alloc_max/2 entries in one go,
2293b6c3722Schristos 		 * but really, isn't that just as fast as this code? */
2303b6c3722Schristos 		lock_quick_lock(&alloc->super->lock);
2313b6c3722Schristos 		if((p = alloc->super->quar)) {
2323b6c3722Schristos 			alloc->super->quar = alloc_special_next(p);
2333b6c3722Schristos 			alloc->super->num_quar--;
2343b6c3722Schristos 		}
2353b6c3722Schristos 		lock_quick_unlock(&alloc->super->lock);
2363b6c3722Schristos 		if(p) {
2373b6c3722Schristos 			p->id = alloc_get_id(alloc);
2383b6c3722Schristos 			return p;
2393b6c3722Schristos 		}
2403b6c3722Schristos 	}
2413b6c3722Schristos 	/* allocate new */
2420cd9f4ecSchristos 	prealloc_setup(alloc);
2430cd9f4ecSchristos 	if(!(p = (alloc_special_type*)malloc(sizeof(alloc_special_type)))) {
2443b6c3722Schristos 		log_err("alloc_special_obtain: out of memory");
2453b6c3722Schristos 		return NULL;
2463b6c3722Schristos 	}
2473b6c3722Schristos 	alloc_setup_special(p);
2483b6c3722Schristos 	p->id = alloc_get_id(alloc);
2493b6c3722Schristos 	return p;
2503b6c3722Schristos }
2513b6c3722Schristos 
2523b6c3722Schristos /** push mem and some more items to the super */
2533b6c3722Schristos static void
pushintosuper(struct alloc_cache * alloc,alloc_special_type * mem)2540cd9f4ecSchristos pushintosuper(struct alloc_cache* alloc, alloc_special_type* mem)
2553b6c3722Schristos {
2563b6c3722Schristos 	int i;
2570cd9f4ecSchristos 	alloc_special_type *p = alloc->quar;
2583b6c3722Schristos 	log_assert(p);
2593b6c3722Schristos 	log_assert(alloc && alloc->super &&
2603b6c3722Schristos 		alloc->num_quar >= ALLOC_SPECIAL_MAX);
2613b6c3722Schristos 	/* push ALLOC_SPECIAL_MAX/2 after mem */
2623b6c3722Schristos 	alloc_set_special_next(mem, alloc->quar);
2633b6c3722Schristos 	for(i=1; i<ALLOC_SPECIAL_MAX/2; i++) {
2643b6c3722Schristos 		p = alloc_special_next(p);
2653b6c3722Schristos 	}
2663b6c3722Schristos 	alloc->quar = alloc_special_next(p);
2673b6c3722Schristos 	alloc->num_quar -= ALLOC_SPECIAL_MAX/2;
2683b6c3722Schristos 
2693b6c3722Schristos 	/* dump mem+list into the super quar list */
2703b6c3722Schristos 	lock_quick_lock(&alloc->super->lock);
2713b6c3722Schristos 	alloc_set_special_next(p, alloc->super->quar);
2723b6c3722Schristos 	alloc->super->quar = mem;
2733b6c3722Schristos 	alloc->super->num_quar += ALLOC_SPECIAL_MAX/2 + 1;
2743b6c3722Schristos 	lock_quick_unlock(&alloc->super->lock);
2753b6c3722Schristos 	/* so 1 lock per mem+alloc/2 deletes */
2763b6c3722Schristos }
2773b6c3722Schristos 
2783b6c3722Schristos void
alloc_special_release(struct alloc_cache * alloc,alloc_special_type * mem)2790cd9f4ecSchristos alloc_special_release(struct alloc_cache* alloc, alloc_special_type* mem)
2803b6c3722Schristos {
2813b6c3722Schristos 	log_assert(alloc);
2823b6c3722Schristos 	if(!mem)
2833b6c3722Schristos 		return;
2843b6c3722Schristos 	if(!alloc->super) {
2853b6c3722Schristos 		lock_quick_lock(&alloc->lock); /* superalloc needs locking */
2863b6c3722Schristos 	}
2873b6c3722Schristos 
2883b6c3722Schristos 	alloc_special_clean(mem);
2893b6c3722Schristos 	if(alloc->super && alloc->num_quar >= ALLOC_SPECIAL_MAX) {
2903b6c3722Schristos 		/* push it to the super structure */
2913b6c3722Schristos 		pushintosuper(alloc, mem);
2923b6c3722Schristos 		return;
2933b6c3722Schristos 	}
2943b6c3722Schristos 
2953b6c3722Schristos 	alloc_set_special_next(mem, alloc->quar);
2963b6c3722Schristos 	alloc->quar = mem;
2973b6c3722Schristos 	alloc->num_quar++;
2983b6c3722Schristos 	if(!alloc->super) {
2993b6c3722Schristos 		lock_quick_unlock(&alloc->lock);
3003b6c3722Schristos 	}
3013b6c3722Schristos }
3023b6c3722Schristos 
3033b6c3722Schristos void
alloc_stats(struct alloc_cache * alloc)3043b6c3722Schristos alloc_stats(struct alloc_cache* alloc)
3053b6c3722Schristos {
3063b6c3722Schristos 	log_info("%salloc: %d in cache, %d blocks.", alloc->super?"":"sup",
3073b6c3722Schristos 		(int)alloc->num_quar, (int)alloc->num_reg_blocks);
3083b6c3722Schristos }
3093b6c3722Schristos 
alloc_get_mem(struct alloc_cache * alloc)3103b6c3722Schristos size_t alloc_get_mem(struct alloc_cache* alloc)
3113b6c3722Schristos {
3120cd9f4ecSchristos 	alloc_special_type* p;
3133b6c3722Schristos 	size_t s = sizeof(*alloc);
3143b6c3722Schristos 	if(!alloc->super) {
3153b6c3722Schristos 		lock_quick_lock(&alloc->lock); /* superalloc needs locking */
3163b6c3722Schristos 	}
3170cd9f4ecSchristos 	s += sizeof(alloc_special_type) * alloc->num_quar;
3183b6c3722Schristos 	for(p = alloc->quar; p; p = alloc_special_next(p)) {
3193b6c3722Schristos 		s += lock_get_mem(&p->entry.lock);
3203b6c3722Schristos 	}
3213b6c3722Schristos 	s += alloc->num_reg_blocks * ALLOC_REG_SIZE;
3223b6c3722Schristos 	if(!alloc->super) {
3233b6c3722Schristos 		lock_quick_unlock(&alloc->lock);
3243b6c3722Schristos 	}
3253b6c3722Schristos 	return s;
3263b6c3722Schristos }
3273b6c3722Schristos 
3283b6c3722Schristos struct regional*
alloc_reg_obtain(struct alloc_cache * alloc)3293b6c3722Schristos alloc_reg_obtain(struct alloc_cache* alloc)
3303b6c3722Schristos {
3313b6c3722Schristos 	if(alloc->num_reg_blocks > 0) {
3323b6c3722Schristos 		struct regional* r = alloc->reg_list;
3333b6c3722Schristos 		alloc->reg_list = (struct regional*)r->next;
3343b6c3722Schristos 		r->next = NULL;
3353b6c3722Schristos 		alloc->num_reg_blocks--;
3363b6c3722Schristos 		return r;
3373b6c3722Schristos 	}
3383b6c3722Schristos 	return regional_create_custom(ALLOC_REG_SIZE);
3393b6c3722Schristos }
3403b6c3722Schristos 
3413b6c3722Schristos void
alloc_reg_release(struct alloc_cache * alloc,struct regional * r)3423b6c3722Schristos alloc_reg_release(struct alloc_cache* alloc, struct regional* r)
3433b6c3722Schristos {
3443b6c3722Schristos 	if(alloc->num_reg_blocks >= alloc->max_reg_blocks) {
3453b6c3722Schristos 		regional_destroy(r);
3463b6c3722Schristos 		return;
3473b6c3722Schristos 	}
3483b6c3722Schristos 	if(!r) return;
3493b6c3722Schristos 	regional_free_all(r);
3503b6c3722Schristos 	log_assert(r->next == NULL);
3513b6c3722Schristos 	r->next = (char*)alloc->reg_list;
3523b6c3722Schristos 	alloc->reg_list = r;
3533b6c3722Schristos 	alloc->num_reg_blocks++;
3543b6c3722Schristos }
3553b6c3722Schristos 
3563b6c3722Schristos void
alloc_set_id_cleanup(struct alloc_cache * alloc,void (* cleanup)(void *),void * arg)3573b6c3722Schristos alloc_set_id_cleanup(struct alloc_cache* alloc, void (*cleanup)(void*),
3583b6c3722Schristos         void* arg)
3593b6c3722Schristos {
3603b6c3722Schristos 	alloc->cleanup = cleanup;
3613b6c3722Schristos 	alloc->cleanup_arg = arg;
3623b6c3722Schristos }
3633b6c3722Schristos 
3643b6c3722Schristos /** global debug value to keep track of total memory mallocs */
3653b6c3722Schristos size_t unbound_mem_alloc = 0;
3663b6c3722Schristos /** global debug value to keep track of total memory frees */
3673b6c3722Schristos size_t unbound_mem_freed = 0;
3683b6c3722Schristos #ifdef UNBOUND_ALLOC_STATS
3693b6c3722Schristos /** special value to know if the memory is being tracked */
3703b6c3722Schristos uint64_t mem_special = (uint64_t)0xfeed43327766abcdLL;
3713b6c3722Schristos #ifdef malloc
3723b6c3722Schristos #undef malloc
3733b6c3722Schristos #endif
3743b6c3722Schristos /** malloc with stats */
unbound_stat_malloc(size_t size)3753b6c3722Schristos void *unbound_stat_malloc(size_t size)
3763b6c3722Schristos {
3773b6c3722Schristos 	void* res;
3783b6c3722Schristos 	if(size == 0) size = 1;
379*01049ae6Schristos 	log_assert(size <= SIZE_MAX-16);
3803b6c3722Schristos 	res = malloc(size+16);
3813b6c3722Schristos 	if(!res) return NULL;
3823b6c3722Schristos 	unbound_mem_alloc += size;
3833b6c3722Schristos 	log_info("stat %p=malloc(%u)", res+16, (unsigned)size);
3843b6c3722Schristos 	memcpy(res, &size, sizeof(size));
3853b6c3722Schristos 	memcpy(res+8, &mem_special, sizeof(mem_special));
3863b6c3722Schristos 	return res+16;
3873b6c3722Schristos }
3883b6c3722Schristos #ifdef calloc
3893b6c3722Schristos #undef calloc
3903b6c3722Schristos #endif
3913b6c3722Schristos #ifndef INT_MAX
3923b6c3722Schristos #define INT_MAX (((int)-1)>>1)
3933b6c3722Schristos #endif
3943b6c3722Schristos /** calloc with stats */
unbound_stat_calloc(size_t nmemb,size_t size)3953b6c3722Schristos void *unbound_stat_calloc(size_t nmemb, size_t size)
3963b6c3722Schristos {
3973b6c3722Schristos 	size_t s;
3983b6c3722Schristos 	void* res;
3993b6c3722Schristos 	if(nmemb != 0 && INT_MAX/nmemb < size)
4003b6c3722Schristos 		return NULL; /* integer overflow check */
4013b6c3722Schristos 	s = (nmemb*size==0)?(size_t)1:nmemb*size;
402*01049ae6Schristos 	log_assert(s <= SIZE_MAX-16);
4033b6c3722Schristos 	res = calloc(1, s+16);
4043b6c3722Schristos 	if(!res) return NULL;
4053b6c3722Schristos 	log_info("stat %p=calloc(%u, %u)", res+16, (unsigned)nmemb, (unsigned)size);
4063b6c3722Schristos 	unbound_mem_alloc += s;
4073b6c3722Schristos 	memcpy(res, &s, sizeof(s));
4083b6c3722Schristos 	memcpy(res+8, &mem_special, sizeof(mem_special));
4093b6c3722Schristos 	return res+16;
4103b6c3722Schristos }
4113b6c3722Schristos #ifdef free
4123b6c3722Schristos #undef free
4133b6c3722Schristos #endif
4143b6c3722Schristos /** free with stats */
unbound_stat_free(void * ptr)4153b6c3722Schristos void unbound_stat_free(void *ptr)
4163b6c3722Schristos {
4173b6c3722Schristos 	size_t s;
4183b6c3722Schristos 	if(!ptr) return;
4193b6c3722Schristos 	if(memcmp(ptr-8, &mem_special, sizeof(mem_special)) != 0) {
4203b6c3722Schristos 		free(ptr);
4213b6c3722Schristos 		return;
4223b6c3722Schristos 	}
4233b6c3722Schristos 	ptr-=16;
4243b6c3722Schristos 	memcpy(&s, ptr, sizeof(s));
4253b6c3722Schristos 	log_info("stat free(%p) size %u", ptr+16, (unsigned)s);
4263b6c3722Schristos 	memset(ptr+8, 0, 8);
4273b6c3722Schristos 	unbound_mem_freed += s;
4283b6c3722Schristos 	free(ptr);
4293b6c3722Schristos }
4303b6c3722Schristos #ifdef realloc
4313b6c3722Schristos #undef realloc
4323b6c3722Schristos #endif
4333b6c3722Schristos /** realloc with stats */
unbound_stat_realloc(void * ptr,size_t size)4343b6c3722Schristos void *unbound_stat_realloc(void *ptr, size_t size)
4353b6c3722Schristos {
4363b6c3722Schristos 	size_t cursz;
4373b6c3722Schristos 	void* res;
4383b6c3722Schristos 	if(!ptr) return unbound_stat_malloc(size);
4393b6c3722Schristos 	if(memcmp(ptr-8, &mem_special, sizeof(mem_special)) != 0) {
4403b6c3722Schristos 		return realloc(ptr, size);
4413b6c3722Schristos 	}
4423b6c3722Schristos 	if(size==0) {
4433b6c3722Schristos 		unbound_stat_free(ptr);
4443b6c3722Schristos 		return NULL;
4453b6c3722Schristos 	}
4463b6c3722Schristos 	ptr -= 16;
4473b6c3722Schristos 	memcpy(&cursz, ptr, sizeof(cursz));
4483b6c3722Schristos 	if(cursz == size) {
4493b6c3722Schristos 		/* nothing changes */
4503b6c3722Schristos 		return ptr;
4513b6c3722Schristos 	}
452*01049ae6Schristos 	log_assert(size <= SIZE_MAX-16);
4533b6c3722Schristos 	res = malloc(size+16);
4543b6c3722Schristos 	if(!res) return NULL;
4553b6c3722Schristos 	unbound_mem_alloc += size;
4563b6c3722Schristos 	unbound_mem_freed += cursz;
4573b6c3722Schristos 	log_info("stat realloc(%p, %u) from %u", ptr+16, (unsigned)size, (unsigned)cursz);
4583b6c3722Schristos 	if(cursz > size) {
4593b6c3722Schristos 		memcpy(res+16, ptr+16, size);
4603b6c3722Schristos 	} else if(size > cursz) {
4613b6c3722Schristos 		memcpy(res+16, ptr+16, cursz);
4623b6c3722Schristos 	}
4633b6c3722Schristos 	memset(ptr+8, 0, 8);
4643b6c3722Schristos 	free(ptr);
4653b6c3722Schristos 	memcpy(res, &size, sizeof(size));
4663b6c3722Schristos 	memcpy(res+8, &mem_special, sizeof(mem_special));
4673b6c3722Schristos 	return res+16;
4683b6c3722Schristos }
4693b6c3722Schristos 
4703b6c3722Schristos /** log to file where alloc was done */
unbound_stat_malloc_log(size_t size,const char * file,int line,const char * func)4713b6c3722Schristos void *unbound_stat_malloc_log(size_t size, const char* file, int line,
4723b6c3722Schristos         const char* func)
4733b6c3722Schristos {
4743b6c3722Schristos 	log_info("%s:%d %s malloc(%u)", file, line, func, (unsigned)size);
4753b6c3722Schristos 	return unbound_stat_malloc(size);
4763b6c3722Schristos }
4773b6c3722Schristos 
4783b6c3722Schristos /** log to file where alloc was done */
unbound_stat_calloc_log(size_t nmemb,size_t size,const char * file,int line,const char * func)4793b6c3722Schristos void *unbound_stat_calloc_log(size_t nmemb, size_t size, const char* file,
4803b6c3722Schristos         int line, const char* func)
4813b6c3722Schristos {
4823b6c3722Schristos 	log_info("%s:%d %s calloc(%u, %u)", file, line, func,
4833b6c3722Schristos 		(unsigned) nmemb, (unsigned)size);
4843b6c3722Schristos 	return unbound_stat_calloc(nmemb, size);
4853b6c3722Schristos }
4863b6c3722Schristos 
4873b6c3722Schristos /** log to file where free was done */
unbound_stat_free_log(void * ptr,const char * file,int line,const char * func)4883b6c3722Schristos void unbound_stat_free_log(void *ptr, const char* file, int line,
4893b6c3722Schristos         const char* func)
4903b6c3722Schristos {
4913b6c3722Schristos 	if(ptr && memcmp(ptr-8, &mem_special, sizeof(mem_special)) == 0) {
4923b6c3722Schristos 		size_t s;
4933b6c3722Schristos 		memcpy(&s, ptr-16, sizeof(s));
4943b6c3722Schristos 		log_info("%s:%d %s free(%p) size %u",
4953b6c3722Schristos 			file, line, func, ptr, (unsigned)s);
4963b6c3722Schristos 	} else
4973b6c3722Schristos 		log_info("%s:%d %s unmatched free(%p)", file, line, func, ptr);
4983b6c3722Schristos 	unbound_stat_free(ptr);
4993b6c3722Schristos }
5003b6c3722Schristos 
5013b6c3722Schristos /** log to file where alloc was done */
unbound_stat_realloc_log(void * ptr,size_t size,const char * file,int line,const char * func)5023b6c3722Schristos void *unbound_stat_realloc_log(void *ptr, size_t size, const char* file,
5033b6c3722Schristos         int line, const char* func)
5043b6c3722Schristos {
5053b6c3722Schristos 	log_info("%s:%d %s realloc(%p, %u)", file, line, func,
5063b6c3722Schristos 		ptr, (unsigned)size);
5073b6c3722Schristos 	return unbound_stat_realloc(ptr, size);
5083b6c3722Schristos }
5093b6c3722Schristos 
5103b6c3722Schristos #endif /* UNBOUND_ALLOC_STATS */
5113b6c3722Schristos #ifdef UNBOUND_ALLOC_LITE
5123b6c3722Schristos #undef malloc
5133b6c3722Schristos #undef calloc
5143b6c3722Schristos #undef free
5153b6c3722Schristos #undef realloc
5163b6c3722Schristos /** length of prefix and suffix */
5173b6c3722Schristos static size_t lite_pad = 16;
5183b6c3722Schristos /** prefix value to check */
5193b6c3722Schristos static char* lite_pre = "checkfront123456";
5203b6c3722Schristos /** suffix value to check */
5213b6c3722Schristos static char* lite_post= "checkafter123456";
5223b6c3722Schristos 
unbound_stat_malloc_lite(size_t size,const char * file,int line,const char * func)5233b6c3722Schristos void *unbound_stat_malloc_lite(size_t size, const char* file, int line,
5243b6c3722Schristos         const char* func)
5253b6c3722Schristos {
5263b6c3722Schristos 	/*  [prefix .. len .. actual data .. suffix] */
527*01049ae6Schristos 	void* res;
528*01049ae6Schristos 	log_assert(size <= SIZE_MAX-(lite_pad*2+sizeof(size_t)));
529*01049ae6Schristos 	res = malloc(size+lite_pad*2+sizeof(size_t));
5303b6c3722Schristos 	if(!res) return NULL;
5313b6c3722Schristos 	memmove(res, lite_pre, lite_pad);
5323b6c3722Schristos 	memmove(res+lite_pad, &size, sizeof(size_t));
5333b6c3722Schristos 	memset(res+lite_pad+sizeof(size_t), 0x1a, size); /* init the memory */
5343b6c3722Schristos 	memmove(res+lite_pad+size+sizeof(size_t), lite_post, lite_pad);
5353b6c3722Schristos 	return res+lite_pad+sizeof(size_t);
5363b6c3722Schristos }
5373b6c3722Schristos 
unbound_stat_calloc_lite(size_t nmemb,size_t size,const char * file,int line,const char * func)5383b6c3722Schristos void *unbound_stat_calloc_lite(size_t nmemb, size_t size, const char* file,
5393b6c3722Schristos         int line, const char* func)
5403b6c3722Schristos {
5413b6c3722Schristos 	size_t req;
5423b6c3722Schristos 	void* res;
5433b6c3722Schristos 	if(nmemb != 0 && INT_MAX/nmemb < size)
5443b6c3722Schristos 		return NULL; /* integer overflow check */
5453b6c3722Schristos 	req = nmemb * size;
546*01049ae6Schristos 	log_assert(req <= SIZE_MAX-(lite_pad*2+sizeof(size_t)));
5473b6c3722Schristos 	res = malloc(req+lite_pad*2+sizeof(size_t));
5483b6c3722Schristos 	if(!res) return NULL;
5493b6c3722Schristos 	memmove(res, lite_pre, lite_pad);
5503b6c3722Schristos 	memmove(res+lite_pad, &req, sizeof(size_t));
5513b6c3722Schristos 	memset(res+lite_pad+sizeof(size_t), 0, req);
5523b6c3722Schristos 	memmove(res+lite_pad+req+sizeof(size_t), lite_post, lite_pad);
5533b6c3722Schristos 	return res+lite_pad+sizeof(size_t);
5543b6c3722Schristos }
5553b6c3722Schristos 
unbound_stat_free_lite(void * ptr,const char * file,int line,const char * func)5563b6c3722Schristos void unbound_stat_free_lite(void *ptr, const char* file, int line,
5573b6c3722Schristos         const char* func)
5583b6c3722Schristos {
5593b6c3722Schristos 	void* real;
5603b6c3722Schristos 	size_t orig = 0;
5613b6c3722Schristos 	if(!ptr) return;
5623b6c3722Schristos 	real = ptr-lite_pad-sizeof(size_t);
5633b6c3722Schristos 	if(memcmp(real, lite_pre, lite_pad) != 0) {
5643b6c3722Schristos 		log_err("free(): prefix failed %s:%d %s", file, line, func);
5653b6c3722Schristos 		log_hex("prefix here", real, lite_pad);
5663b6c3722Schristos 		log_hex("  should be", lite_pre, lite_pad);
5673b6c3722Schristos 		fatal_exit("alloc assertion failed");
5683b6c3722Schristos 	}
5693b6c3722Schristos 	memmove(&orig, real+lite_pad, sizeof(size_t));
5703b6c3722Schristos 	if(memcmp(real+lite_pad+orig+sizeof(size_t), lite_post, lite_pad)!=0){
5713b6c3722Schristos 		log_err("free(): suffix failed %s:%d %s", file, line, func);
5723b6c3722Schristos 		log_err("alloc size is %d", (int)orig);
5733b6c3722Schristos 		log_hex("suffix here", real+lite_pad+orig+sizeof(size_t),
5743b6c3722Schristos 			lite_pad);
5753b6c3722Schristos 		log_hex("  should be", lite_post, lite_pad);
5763b6c3722Schristos 		fatal_exit("alloc assertion failed");
5773b6c3722Schristos 	}
5783b6c3722Schristos 	memset(real, 0xdd, orig+lite_pad*2+sizeof(size_t)); /* mark it */
5793b6c3722Schristos 	free(real);
5803b6c3722Schristos }
5813b6c3722Schristos 
unbound_stat_realloc_lite(void * ptr,size_t size,const char * file,int line,const char * func)5823b6c3722Schristos void *unbound_stat_realloc_lite(void *ptr, size_t size, const char* file,
5833b6c3722Schristos         int line, const char* func)
5843b6c3722Schristos {
5853b6c3722Schristos 	/* always free and realloc (no growing) */
5863b6c3722Schristos 	void* real, *newa;
5873b6c3722Schristos 	size_t orig = 0;
5883b6c3722Schristos 	if(!ptr) {
5893b6c3722Schristos 		/* like malloc() */
5903b6c3722Schristos 		return unbound_stat_malloc_lite(size, file, line, func);
5913b6c3722Schristos 	}
5923b6c3722Schristos 	if(!size) {
5933b6c3722Schristos 		/* like free() */
5943b6c3722Schristos 		unbound_stat_free_lite(ptr, file, line, func);
5953b6c3722Schristos 		return NULL;
5963b6c3722Schristos 	}
5973b6c3722Schristos 	/* change allocation size and copy */
5983b6c3722Schristos 	real = ptr-lite_pad-sizeof(size_t);
5993b6c3722Schristos 	if(memcmp(real, lite_pre, lite_pad) != 0) {
6003b6c3722Schristos 		log_err("realloc(): prefix failed %s:%d %s", file, line, func);
6013b6c3722Schristos 		log_hex("prefix here", real, lite_pad);
6023b6c3722Schristos 		log_hex("  should be", lite_pre, lite_pad);
6033b6c3722Schristos 		fatal_exit("alloc assertion failed");
6043b6c3722Schristos 	}
6053b6c3722Schristos 	memmove(&orig, real+lite_pad, sizeof(size_t));
6063b6c3722Schristos 	if(memcmp(real+lite_pad+orig+sizeof(size_t), lite_post, lite_pad)!=0){
6073b6c3722Schristos 		log_err("realloc(): suffix failed %s:%d %s", file, line, func);
6083b6c3722Schristos 		log_err("alloc size is %d", (int)orig);
6093b6c3722Schristos 		log_hex("suffix here", real+lite_pad+orig+sizeof(size_t),
6103b6c3722Schristos 			lite_pad);
6113b6c3722Schristos 		log_hex("  should be", lite_post, lite_pad);
6123b6c3722Schristos 		fatal_exit("alloc assertion failed");
6133b6c3722Schristos 	}
6143b6c3722Schristos 	/* new alloc and copy over */
6153b6c3722Schristos 	newa = unbound_stat_malloc_lite(size, file, line, func);
6163b6c3722Schristos 	if(!newa)
6173b6c3722Schristos 		return NULL;
6183b6c3722Schristos 	if(orig < size)
6193b6c3722Schristos 		memmove(newa, ptr, orig);
6203b6c3722Schristos 	else	memmove(newa, ptr, size);
6213b6c3722Schristos 	memset(real, 0xdd, orig+lite_pad*2+sizeof(size_t)); /* mark it */
6223b6c3722Schristos 	free(real);
6233b6c3722Schristos 	return newa;
6243b6c3722Schristos }
6253b6c3722Schristos 
unbound_strdup_lite(const char * s,const char * file,int line,const char * func)6263b6c3722Schristos char* unbound_strdup_lite(const char* s, const char* file, int line,
6273b6c3722Schristos         const char* func)
6283b6c3722Schristos {
6293b6c3722Schristos 	/* this routine is made to make sure strdup() uses the malloc_lite */
6303b6c3722Schristos 	size_t l = strlen(s)+1;
6313b6c3722Schristos 	char* n = (char*)unbound_stat_malloc_lite(l, file, line, func);
6323b6c3722Schristos 	if(!n) return NULL;
6333b6c3722Schristos 	memmove(n, s, l);
6343b6c3722Schristos 	return n;
6353b6c3722Schristos }
6363b6c3722Schristos 
unbound_lite_wrapstr(char * s)6373b6c3722Schristos char* unbound_lite_wrapstr(char* s)
6383b6c3722Schristos {
6393b6c3722Schristos 	char* n = unbound_strdup_lite(s, __FILE__, __LINE__, __func__);
6403b6c3722Schristos 	free(s);
6413b6c3722Schristos 	return n;
6423b6c3722Schristos }
6433b6c3722Schristos 
6443b6c3722Schristos #undef sldns_pkt2wire
unbound_lite_pkt2wire(uint8_t ** dest,const sldns_pkt * p,size_t * size)6453b6c3722Schristos sldns_status unbound_lite_pkt2wire(uint8_t **dest, const sldns_pkt *p,
6463b6c3722Schristos 	size_t *size)
6473b6c3722Schristos {
6483b6c3722Schristos 	uint8_t* md = NULL;
6493b6c3722Schristos 	size_t ms = 0;
6503b6c3722Schristos 	sldns_status s = sldns_pkt2wire(&md, p, &ms);
6513b6c3722Schristos 	if(md) {
6523b6c3722Schristos 		*dest = unbound_stat_malloc_lite(ms, __FILE__, __LINE__,
6533b6c3722Schristos 			__func__);
6543b6c3722Schristos 		*size = ms;
6553b6c3722Schristos 		if(!*dest) { free(md); return LDNS_STATUS_MEM_ERR; }
6563b6c3722Schristos 		memcpy(*dest, md, ms);
6573b6c3722Schristos 		free(md);
6583b6c3722Schristos 	} else {
6593b6c3722Schristos 		*dest = NULL;
6603b6c3722Schristos 		*size = 0;
6613b6c3722Schristos 	}
6623b6c3722Schristos 	return s;
6633b6c3722Schristos }
6643b6c3722Schristos 
6653b6c3722Schristos #undef i2d_DSA_SIG
unbound_lite_i2d_DSA_SIG(DSA_SIG * dsasig,unsigned char ** sig)6663b6c3722Schristos int unbound_lite_i2d_DSA_SIG(DSA_SIG* dsasig, unsigned char** sig)
6673b6c3722Schristos {
6683b6c3722Schristos 	unsigned char* n = NULL;
6693b6c3722Schristos 	int r= i2d_DSA_SIG(dsasig, &n);
6703b6c3722Schristos 	if(n) {
6713b6c3722Schristos 		*sig = unbound_stat_malloc_lite((size_t)r, __FILE__, __LINE__,
6723b6c3722Schristos 			__func__);
6733b6c3722Schristos 		if(!*sig) return -1;
6743b6c3722Schristos 		memcpy(*sig, n, (size_t)r);
6753b6c3722Schristos 		free(n);
6763b6c3722Schristos 		return r;
6773b6c3722Schristos 	}
6783b6c3722Schristos 	*sig = NULL;
6793b6c3722Schristos 	return r;
6803b6c3722Schristos }
6813b6c3722Schristos 
6823b6c3722Schristos #endif /* UNBOUND_ALLOC_LITE */
683