1238106Sdes/*
2238106Sdes * util/alloc.c - 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
24266114Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25266114Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26266114Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27266114Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28266114Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29266114Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30266114Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31266114Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32266114Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33266114Sdes * 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
42238106Sdes#include "config.h"
43238106Sdes#include "util/alloc.h"
44238106Sdes#include "util/regional.h"
45238106Sdes#include "util/data/packed_rrset.h"
46238106Sdes#include "util/fptr_wlist.h"
47238106Sdes
48238106Sdes/** custom size of cached regional blocks */
49238106Sdes#define ALLOC_REG_SIZE	16384
50238106Sdes/** number of bits for ID part of uint64, rest for number of threads. */
51238106Sdes#define THRNUM_SHIFT	48	/* for 65k threads, 2^48 rrsets per thr. */
52238106Sdes
53238106Sdes/** setup new special type */
54238106Sdesstatic void
55356345Scyalloc_setup_special(alloc_special_type* t)
56238106Sdes{
57238106Sdes	memset(t, 0, sizeof(*t));
58238106Sdes	lock_rw_init(&t->entry.lock);
59238106Sdes	t->entry.key = t;
60238106Sdes}
61238106Sdes
62238106Sdes/** prealloc some entries in the cache. To minimize contention.
63238106Sdes * Result is 1 lock per alloc_max newly created entries.
64238106Sdes * @param alloc: the structure to fill up.
65238106Sdes */
66238106Sdesstatic void
67307729Sdesprealloc_setup(struct alloc_cache* alloc)
68238106Sdes{
69356345Scy	alloc_special_type* p;
70238106Sdes	int i;
71238106Sdes	for(i=0; i<ALLOC_SPECIAL_MAX; i++) {
72356345Scy		if(!(p = (alloc_special_type*)malloc(
73356345Scy			sizeof(alloc_special_type)))) {
74238106Sdes			log_err("prealloc: out of memory");
75238106Sdes			return;
76238106Sdes		}
77238106Sdes		alloc_setup_special(p);
78238106Sdes		alloc_set_special_next(p, alloc->quar);
79238106Sdes		alloc->quar = p;
80238106Sdes		alloc->num_quar++;
81238106Sdes	}
82238106Sdes}
83238106Sdes
84238106Sdes/** prealloc region blocks */
85238106Sdesstatic void
86238106Sdesprealloc_blocks(struct alloc_cache* alloc, size_t num)
87238106Sdes{
88238106Sdes	size_t i;
89238106Sdes	struct regional* r;
90238106Sdes	for(i=0; i<num; i++) {
91238106Sdes		r = regional_create_custom(ALLOC_REG_SIZE);
92238106Sdes		if(!r) {
93238106Sdes			log_err("prealloc blocks: out of memory");
94238106Sdes			return;
95238106Sdes		}
96238106Sdes		r->next = (char*)alloc->reg_list;
97238106Sdes		alloc->reg_list = r;
98238106Sdes		alloc->num_reg_blocks ++;
99238106Sdes	}
100238106Sdes}
101238106Sdes
102238106Sdesvoid
103238106Sdesalloc_init(struct alloc_cache* alloc, struct alloc_cache* super,
104238106Sdes	int thread_num)
105238106Sdes{
106238106Sdes	memset(alloc, 0, sizeof(*alloc));
107238106Sdes	alloc->super = super;
108238106Sdes	alloc->thread_num = thread_num;
109238106Sdes	alloc->next_id = (uint64_t)thread_num; 	/* in steps, so that type */
110238106Sdes	alloc->next_id <<= THRNUM_SHIFT; 	/* of *_id is used. */
111238106Sdes	alloc->last_id = 1; 			/* so no 64bit constants, */
112238106Sdes	alloc->last_id <<= THRNUM_SHIFT; 	/* or implicit 'int' ops. */
113238106Sdes	alloc->last_id -= 1; 			/* for compiler portability. */
114238106Sdes	alloc->last_id |= alloc->next_id;
115238106Sdes	alloc->next_id += 1;			/* because id=0 is special. */
116238106Sdes	alloc->max_reg_blocks = 100;
117238106Sdes	alloc->num_reg_blocks = 0;
118238106Sdes	alloc->reg_list = NULL;
119238106Sdes	alloc->cleanup = NULL;
120238106Sdes	alloc->cleanup_arg = NULL;
121238106Sdes	if(alloc->super)
122238106Sdes		prealloc_blocks(alloc, alloc->max_reg_blocks);
123238106Sdes	if(!alloc->super) {
124238106Sdes		lock_quick_init(&alloc->lock);
125238106Sdes		lock_protect(&alloc->lock, alloc, sizeof(*alloc));
126238106Sdes	}
127238106Sdes}
128238106Sdes
129356345Scy/** free the special list */
130356345Scystatic void
131356345Scyalloc_clear_special_list(struct alloc_cache* alloc)
132356345Scy{
133356345Scy	alloc_special_type* p, *np;
134356345Scy	/* free */
135356345Scy	p = alloc->quar;
136356345Scy	while(p) {
137356345Scy		np = alloc_special_next(p);
138356345Scy		/* deinit special type */
139356345Scy		lock_rw_destroy(&p->entry.lock);
140356345Scy		free(p);
141356345Scy		p = np;
142356345Scy	}
143356345Scy}
144356345Scy
145356345Scyvoid
146356345Scyalloc_clear_special(struct alloc_cache* alloc)
147356345Scy{
148356345Scy	if(!alloc->super) {
149356345Scy		lock_quick_lock(&alloc->lock);
150356345Scy	}
151356345Scy	alloc_clear_special_list(alloc);
152356345Scy	alloc->quar = 0;
153356345Scy	alloc->num_quar = 0;
154356345Scy	if(!alloc->super) {
155356345Scy		lock_quick_unlock(&alloc->lock);
156356345Scy	}
157356345Scy}
158356345Scy
159238106Sdesvoid
160238106Sdesalloc_clear(struct alloc_cache* alloc)
161238106Sdes{
162356345Scy	alloc_special_type* p;
163238106Sdes	struct regional* r, *nr;
164238106Sdes	if(!alloc)
165238106Sdes		return;
166238106Sdes	if(!alloc->super) {
167238106Sdes		lock_quick_destroy(&alloc->lock);
168238106Sdes	}
169238106Sdes	if(alloc->super && alloc->quar) {
170238106Sdes		/* push entire list into super */
171238106Sdes		p = alloc->quar;
172238106Sdes		while(alloc_special_next(p)) /* find last */
173238106Sdes			p = alloc_special_next(p);
174238106Sdes		lock_quick_lock(&alloc->super->lock);
175238106Sdes		alloc_set_special_next(p, alloc->super->quar);
176238106Sdes		alloc->super->quar = alloc->quar;
177238106Sdes		alloc->super->num_quar += alloc->num_quar;
178238106Sdes		lock_quick_unlock(&alloc->super->lock);
179238106Sdes	} else {
180356345Scy		alloc_clear_special_list(alloc);
181238106Sdes	}
182238106Sdes	alloc->quar = 0;
183238106Sdes	alloc->num_quar = 0;
184238106Sdes	r = alloc->reg_list;
185238106Sdes	while(r) {
186238106Sdes		nr = (struct regional*)r->next;
187238106Sdes		free(r);
188238106Sdes		r = nr;
189238106Sdes	}
190238106Sdes	alloc->reg_list = NULL;
191238106Sdes	alloc->num_reg_blocks = 0;
192238106Sdes}
193238106Sdes
194238106Sdesuint64_t
195238106Sdesalloc_get_id(struct alloc_cache* alloc)
196238106Sdes{
197238106Sdes	uint64_t id = alloc->next_id++;
198238106Sdes	if(id == alloc->last_id) {
199238106Sdes		log_warn("rrset alloc: out of 64bit ids. Clearing cache.");
200238106Sdes		fptr_ok(fptr_whitelist_alloc_cleanup(alloc->cleanup));
201238106Sdes		(*alloc->cleanup)(alloc->cleanup_arg);
202238106Sdes
203238106Sdes		/* start back at first number */   	/* like in alloc_init*/
204238106Sdes		alloc->next_id = (uint64_t)alloc->thread_num;
205238106Sdes		alloc->next_id <<= THRNUM_SHIFT; 	/* in steps for comp. */
206238106Sdes		alloc->next_id += 1;			/* portability. */
207238106Sdes		/* and generate new and safe id */
208238106Sdes		id = alloc->next_id++;
209238106Sdes	}
210238106Sdes	return id;
211238106Sdes}
212238106Sdes
213356345Scyalloc_special_type*
214238106Sdesalloc_special_obtain(struct alloc_cache* alloc)
215238106Sdes{
216356345Scy	alloc_special_type* p;
217238106Sdes	log_assert(alloc);
218238106Sdes	/* see if in local cache */
219238106Sdes	if(alloc->quar) {
220238106Sdes		p = alloc->quar;
221238106Sdes		alloc->quar = alloc_special_next(p);
222238106Sdes		alloc->num_quar--;
223238106Sdes		p->id = alloc_get_id(alloc);
224238106Sdes		return p;
225238106Sdes	}
226238106Sdes	/* see if in global cache */
227238106Sdes	if(alloc->super) {
228238106Sdes		/* could maybe grab alloc_max/2 entries in one go,
229238106Sdes		 * but really, isn't that just as fast as this code? */
230238106Sdes		lock_quick_lock(&alloc->super->lock);
231238106Sdes		if((p = alloc->super->quar)) {
232238106Sdes			alloc->super->quar = alloc_special_next(p);
233238106Sdes			alloc->super->num_quar--;
234238106Sdes		}
235238106Sdes		lock_quick_unlock(&alloc->super->lock);
236238106Sdes		if(p) {
237238106Sdes			p->id = alloc_get_id(alloc);
238238106Sdes			return p;
239238106Sdes		}
240238106Sdes	}
241238106Sdes	/* allocate new */
242307729Sdes	prealloc_setup(alloc);
243356345Scy	if(!(p = (alloc_special_type*)malloc(sizeof(alloc_special_type)))) {
244238106Sdes		log_err("alloc_special_obtain: out of memory");
245238106Sdes		return NULL;
246238106Sdes	}
247238106Sdes	alloc_setup_special(p);
248238106Sdes	p->id = alloc_get_id(alloc);
249238106Sdes	return p;
250238106Sdes}
251238106Sdes
252238106Sdes/** push mem and some more items to the super */
253238106Sdesstatic void
254356345Scypushintosuper(struct alloc_cache* alloc, alloc_special_type* mem)
255238106Sdes{
256238106Sdes	int i;
257356345Scy	alloc_special_type *p = alloc->quar;
258238106Sdes	log_assert(p);
259238106Sdes	log_assert(alloc && alloc->super &&
260238106Sdes		alloc->num_quar >= ALLOC_SPECIAL_MAX);
261238106Sdes	/* push ALLOC_SPECIAL_MAX/2 after mem */
262238106Sdes	alloc_set_special_next(mem, alloc->quar);
263238106Sdes	for(i=1; i<ALLOC_SPECIAL_MAX/2; i++) {
264238106Sdes		p = alloc_special_next(p);
265238106Sdes	}
266238106Sdes	alloc->quar = alloc_special_next(p);
267238106Sdes	alloc->num_quar -= ALLOC_SPECIAL_MAX/2;
268238106Sdes
269238106Sdes	/* dump mem+list into the super quar list */
270238106Sdes	lock_quick_lock(&alloc->super->lock);
271238106Sdes	alloc_set_special_next(p, alloc->super->quar);
272238106Sdes	alloc->super->quar = mem;
273238106Sdes	alloc->super->num_quar += ALLOC_SPECIAL_MAX/2 + 1;
274238106Sdes	lock_quick_unlock(&alloc->super->lock);
275238106Sdes	/* so 1 lock per mem+alloc/2 deletes */
276238106Sdes}
277238106Sdes
278238106Sdesvoid
279356345Scyalloc_special_release(struct alloc_cache* alloc, alloc_special_type* mem)
280238106Sdes{
281238106Sdes	log_assert(alloc);
282238106Sdes	if(!mem)
283238106Sdes		return;
284238106Sdes	if(!alloc->super) {
285238106Sdes		lock_quick_lock(&alloc->lock); /* superalloc needs locking */
286238106Sdes	}
287238106Sdes
288238106Sdes	alloc_special_clean(mem);
289238106Sdes	if(alloc->super && alloc->num_quar >= ALLOC_SPECIAL_MAX) {
290238106Sdes		/* push it to the super structure */
291238106Sdes		pushintosuper(alloc, mem);
292238106Sdes		return;
293238106Sdes	}
294238106Sdes
295238106Sdes	alloc_set_special_next(mem, alloc->quar);
296238106Sdes	alloc->quar = mem;
297238106Sdes	alloc->num_quar++;
298238106Sdes	if(!alloc->super) {
299238106Sdes		lock_quick_unlock(&alloc->lock);
300238106Sdes	}
301238106Sdes}
302238106Sdes
303238106Sdesvoid
304238106Sdesalloc_stats(struct alloc_cache* alloc)
305238106Sdes{
306238106Sdes	log_info("%salloc: %d in cache, %d blocks.", alloc->super?"":"sup",
307238106Sdes		(int)alloc->num_quar, (int)alloc->num_reg_blocks);
308238106Sdes}
309238106Sdes
310238106Sdessize_t alloc_get_mem(struct alloc_cache* alloc)
311238106Sdes{
312356345Scy	alloc_special_type* p;
313238106Sdes	size_t s = sizeof(*alloc);
314238106Sdes	if(!alloc->super) {
315238106Sdes		lock_quick_lock(&alloc->lock); /* superalloc needs locking */
316238106Sdes	}
317356345Scy	s += sizeof(alloc_special_type) * alloc->num_quar;
318238106Sdes	for(p = alloc->quar; p; p = alloc_special_next(p)) {
319238106Sdes		s += lock_get_mem(&p->entry.lock);
320238106Sdes	}
321238106Sdes	s += alloc->num_reg_blocks * ALLOC_REG_SIZE;
322238106Sdes	if(!alloc->super) {
323238106Sdes		lock_quick_unlock(&alloc->lock);
324238106Sdes	}
325238106Sdes	return s;
326238106Sdes}
327238106Sdes
328238106Sdesstruct regional*
329238106Sdesalloc_reg_obtain(struct alloc_cache* alloc)
330238106Sdes{
331238106Sdes	if(alloc->num_reg_blocks > 0) {
332238106Sdes		struct regional* r = alloc->reg_list;
333238106Sdes		alloc->reg_list = (struct regional*)r->next;
334238106Sdes		r->next = NULL;
335238106Sdes		alloc->num_reg_blocks--;
336238106Sdes		return r;
337238106Sdes	}
338238106Sdes	return regional_create_custom(ALLOC_REG_SIZE);
339238106Sdes}
340238106Sdes
341238106Sdesvoid
342238106Sdesalloc_reg_release(struct alloc_cache* alloc, struct regional* r)
343238106Sdes{
344238106Sdes	if(alloc->num_reg_blocks >= alloc->max_reg_blocks) {
345238106Sdes		regional_destroy(r);
346238106Sdes		return;
347238106Sdes	}
348238106Sdes	if(!r) return;
349238106Sdes	regional_free_all(r);
350238106Sdes	log_assert(r->next == NULL);
351238106Sdes	r->next = (char*)alloc->reg_list;
352238106Sdes	alloc->reg_list = r;
353238106Sdes	alloc->num_reg_blocks++;
354238106Sdes}
355238106Sdes
356238106Sdesvoid
357238106Sdesalloc_set_id_cleanup(struct alloc_cache* alloc, void (*cleanup)(void*),
358238106Sdes        void* arg)
359238106Sdes{
360238106Sdes	alloc->cleanup = cleanup;
361238106Sdes	alloc->cleanup_arg = arg;
362238106Sdes}
363238106Sdes
364238106Sdes/** global debug value to keep track of total memory mallocs */
365238106Sdessize_t unbound_mem_alloc = 0;
366238106Sdes/** global debug value to keep track of total memory frees */
367238106Sdessize_t unbound_mem_freed = 0;
368238106Sdes#ifdef UNBOUND_ALLOC_STATS
369238106Sdes/** special value to know if the memory is being tracked */
370238106Sdesuint64_t mem_special = (uint64_t)0xfeed43327766abcdLL;
371238106Sdes#ifdef malloc
372238106Sdes#undef malloc
373238106Sdes#endif
374238106Sdes/** malloc with stats */
375238106Sdesvoid *unbound_stat_malloc(size_t size)
376238106Sdes{
377238106Sdes	void* res;
378238106Sdes	if(size == 0) size = 1;
379356345Scy	log_assert(size <= SIZE_MAX-16);
380238106Sdes	res = malloc(size+16);
381238106Sdes	if(!res) return NULL;
382238106Sdes	unbound_mem_alloc += size;
383238106Sdes	log_info("stat %p=malloc(%u)", res+16, (unsigned)size);
384238106Sdes	memcpy(res, &size, sizeof(size));
385238106Sdes	memcpy(res+8, &mem_special, sizeof(mem_special));
386238106Sdes	return res+16;
387238106Sdes}
388238106Sdes#ifdef calloc
389238106Sdes#undef calloc
390238106Sdes#endif
391287917Sdes#ifndef INT_MAX
392287917Sdes#define INT_MAX (((int)-1)>>1)
393287917Sdes#endif
394238106Sdes/** calloc with stats */
395238106Sdesvoid *unbound_stat_calloc(size_t nmemb, size_t size)
396238106Sdes{
397287917Sdes	size_t s;
398287917Sdes	void* res;
399287917Sdes	if(nmemb != 0 && INT_MAX/nmemb < size)
400287917Sdes		return NULL; /* integer overflow check */
401287917Sdes	s = (nmemb*size==0)?(size_t)1:nmemb*size;
402356345Scy	log_assert(s <= SIZE_MAX-16);
403287917Sdes	res = calloc(1, s+16);
404238106Sdes	if(!res) return NULL;
405238106Sdes	log_info("stat %p=calloc(%u, %u)", res+16, (unsigned)nmemb, (unsigned)size);
406238106Sdes	unbound_mem_alloc += s;
407238106Sdes	memcpy(res, &s, sizeof(s));
408238106Sdes	memcpy(res+8, &mem_special, sizeof(mem_special));
409238106Sdes	return res+16;
410238106Sdes}
411238106Sdes#ifdef free
412238106Sdes#undef free
413238106Sdes#endif
414238106Sdes/** free with stats */
415238106Sdesvoid unbound_stat_free(void *ptr)
416238106Sdes{
417238106Sdes	size_t s;
418238106Sdes	if(!ptr) return;
419238106Sdes	if(memcmp(ptr-8, &mem_special, sizeof(mem_special)) != 0) {
420238106Sdes		free(ptr);
421238106Sdes		return;
422238106Sdes	}
423238106Sdes	ptr-=16;
424238106Sdes	memcpy(&s, ptr, sizeof(s));
425238106Sdes	log_info("stat free(%p) size %u", ptr+16, (unsigned)s);
426238106Sdes	memset(ptr+8, 0, 8);
427238106Sdes	unbound_mem_freed += s;
428238106Sdes	free(ptr);
429238106Sdes}
430238106Sdes#ifdef realloc
431238106Sdes#undef realloc
432238106Sdes#endif
433238106Sdes/** realloc with stats */
434238106Sdesvoid *unbound_stat_realloc(void *ptr, size_t size)
435238106Sdes{
436238106Sdes	size_t cursz;
437238106Sdes	void* res;
438238106Sdes	if(!ptr) return unbound_stat_malloc(size);
439238106Sdes	if(memcmp(ptr-8, &mem_special, sizeof(mem_special)) != 0) {
440238106Sdes		return realloc(ptr, size);
441238106Sdes	}
442238106Sdes	if(size==0) {
443238106Sdes		unbound_stat_free(ptr);
444238106Sdes		return NULL;
445238106Sdes	}
446238106Sdes	ptr -= 16;
447238106Sdes	memcpy(&cursz, ptr, sizeof(cursz));
448238106Sdes	if(cursz == size) {
449238106Sdes		/* nothing changes */
450238106Sdes		return ptr;
451238106Sdes	}
452356345Scy	log_assert(size <= SIZE_MAX-16);
453238106Sdes	res = malloc(size+16);
454238106Sdes	if(!res) return NULL;
455238106Sdes	unbound_mem_alloc += size;
456238106Sdes	unbound_mem_freed += cursz;
457238106Sdes	log_info("stat realloc(%p, %u) from %u", ptr+16, (unsigned)size, (unsigned)cursz);
458238106Sdes	if(cursz > size) {
459238106Sdes		memcpy(res+16, ptr+16, size);
460238106Sdes	} else if(size > cursz) {
461238106Sdes		memcpy(res+16, ptr+16, cursz);
462238106Sdes	}
463238106Sdes	memset(ptr+8, 0, 8);
464238106Sdes	free(ptr);
465238106Sdes	memcpy(res, &size, sizeof(size));
466238106Sdes	memcpy(res+8, &mem_special, sizeof(mem_special));
467238106Sdes	return res+16;
468238106Sdes}
469238106Sdes
470238106Sdes/** log to file where alloc was done */
471238106Sdesvoid *unbound_stat_malloc_log(size_t size, const char* file, int line,
472238106Sdes        const char* func)
473238106Sdes{
474238106Sdes	log_info("%s:%d %s malloc(%u)", file, line, func, (unsigned)size);
475238106Sdes	return unbound_stat_malloc(size);
476238106Sdes}
477238106Sdes
478238106Sdes/** log to file where alloc was done */
479238106Sdesvoid *unbound_stat_calloc_log(size_t nmemb, size_t size, const char* file,
480238106Sdes        int line, const char* func)
481238106Sdes{
482238106Sdes	log_info("%s:%d %s calloc(%u, %u)", file, line, func,
483238106Sdes		(unsigned) nmemb, (unsigned)size);
484238106Sdes	return unbound_stat_calloc(nmemb, size);
485238106Sdes}
486238106Sdes
487238106Sdes/** log to file where free was done */
488238106Sdesvoid unbound_stat_free_log(void *ptr, const char* file, int line,
489238106Sdes        const char* func)
490238106Sdes{
491238106Sdes	if(ptr && memcmp(ptr-8, &mem_special, sizeof(mem_special)) == 0) {
492238106Sdes		size_t s;
493238106Sdes		memcpy(&s, ptr-16, sizeof(s));
494238106Sdes		log_info("%s:%d %s free(%p) size %u",
495238106Sdes			file, line, func, ptr, (unsigned)s);
496238106Sdes	} else
497238106Sdes		log_info("%s:%d %s unmatched free(%p)", file, line, func, ptr);
498238106Sdes	unbound_stat_free(ptr);
499238106Sdes}
500238106Sdes
501238106Sdes/** log to file where alloc was done */
502238106Sdesvoid *unbound_stat_realloc_log(void *ptr, size_t size, const char* file,
503238106Sdes        int line, const char* func)
504238106Sdes{
505238106Sdes	log_info("%s:%d %s realloc(%p, %u)", file, line, func,
506238106Sdes		ptr, (unsigned)size);
507238106Sdes	return unbound_stat_realloc(ptr, size);
508238106Sdes}
509238106Sdes
510238106Sdes#endif /* UNBOUND_ALLOC_STATS */
511238106Sdes#ifdef UNBOUND_ALLOC_LITE
512238106Sdes#undef malloc
513238106Sdes#undef calloc
514238106Sdes#undef free
515238106Sdes#undef realloc
516238106Sdes/** length of prefix and suffix */
517238106Sdesstatic size_t lite_pad = 16;
518238106Sdes/** prefix value to check */
519238106Sdesstatic char* lite_pre = "checkfront123456";
520238106Sdes/** suffix value to check */
521238106Sdesstatic char* lite_post= "checkafter123456";
522238106Sdes
523238106Sdesvoid *unbound_stat_malloc_lite(size_t size, const char* file, int line,
524238106Sdes        const char* func)
525238106Sdes{
526238106Sdes	/*  [prefix .. len .. actual data .. suffix] */
527356345Scy	void* res;
528356345Scy	log_assert(size <= SIZE_MAX-(lite_pad*2+sizeof(size_t)));
529356345Scy	res = malloc(size+lite_pad*2+sizeof(size_t));
530238106Sdes	if(!res) return NULL;
531238106Sdes	memmove(res, lite_pre, lite_pad);
532238106Sdes	memmove(res+lite_pad, &size, sizeof(size_t));
533238106Sdes	memset(res+lite_pad+sizeof(size_t), 0x1a, size); /* init the memory */
534238106Sdes	memmove(res+lite_pad+size+sizeof(size_t), lite_post, lite_pad);
535238106Sdes	return res+lite_pad+sizeof(size_t);
536238106Sdes}
537238106Sdes
538238106Sdesvoid *unbound_stat_calloc_lite(size_t nmemb, size_t size, const char* file,
539238106Sdes        int line, const char* func)
540238106Sdes{
541287917Sdes	size_t req;
542287917Sdes	void* res;
543287917Sdes	if(nmemb != 0 && INT_MAX/nmemb < size)
544287917Sdes		return NULL; /* integer overflow check */
545287917Sdes	req = nmemb * size;
546356345Scy	log_assert(req <= SIZE_MAX-(lite_pad*2+sizeof(size_t)));
547287917Sdes	res = malloc(req+lite_pad*2+sizeof(size_t));
548238106Sdes	if(!res) return NULL;
549238106Sdes	memmove(res, lite_pre, lite_pad);
550238106Sdes	memmove(res+lite_pad, &req, sizeof(size_t));
551238106Sdes	memset(res+lite_pad+sizeof(size_t), 0, req);
552238106Sdes	memmove(res+lite_pad+req+sizeof(size_t), lite_post, lite_pad);
553238106Sdes	return res+lite_pad+sizeof(size_t);
554238106Sdes}
555238106Sdes
556238106Sdesvoid unbound_stat_free_lite(void *ptr, const char* file, int line,
557238106Sdes        const char* func)
558238106Sdes{
559238106Sdes	void* real;
560238106Sdes	size_t orig = 0;
561238106Sdes	if(!ptr) return;
562238106Sdes	real = ptr-lite_pad-sizeof(size_t);
563238106Sdes	if(memcmp(real, lite_pre, lite_pad) != 0) {
564238106Sdes		log_err("free(): prefix failed %s:%d %s", file, line, func);
565238106Sdes		log_hex("prefix here", real, lite_pad);
566238106Sdes		log_hex("  should be", lite_pre, lite_pad);
567238106Sdes		fatal_exit("alloc assertion failed");
568238106Sdes	}
569238106Sdes	memmove(&orig, real+lite_pad, sizeof(size_t));
570238106Sdes	if(memcmp(real+lite_pad+orig+sizeof(size_t), lite_post, lite_pad)!=0){
571238106Sdes		log_err("free(): suffix failed %s:%d %s", file, line, func);
572238106Sdes		log_err("alloc size is %d", (int)orig);
573238106Sdes		log_hex("suffix here", real+lite_pad+orig+sizeof(size_t),
574238106Sdes			lite_pad);
575238106Sdes		log_hex("  should be", lite_post, lite_pad);
576238106Sdes		fatal_exit("alloc assertion failed");
577238106Sdes	}
578238106Sdes	memset(real, 0xdd, orig+lite_pad*2+sizeof(size_t)); /* mark it */
579238106Sdes	free(real);
580238106Sdes}
581238106Sdes
582238106Sdesvoid *unbound_stat_realloc_lite(void *ptr, size_t size, const char* file,
583238106Sdes        int line, const char* func)
584238106Sdes{
585238106Sdes	/* always free and realloc (no growing) */
586238106Sdes	void* real, *newa;
587238106Sdes	size_t orig = 0;
588238106Sdes	if(!ptr) {
589238106Sdes		/* like malloc() */
590238106Sdes		return unbound_stat_malloc_lite(size, file, line, func);
591238106Sdes	}
592238106Sdes	if(!size) {
593238106Sdes		/* like free() */
594238106Sdes		unbound_stat_free_lite(ptr, file, line, func);
595238106Sdes		return NULL;
596238106Sdes	}
597238106Sdes	/* change allocation size and copy */
598238106Sdes	real = ptr-lite_pad-sizeof(size_t);
599238106Sdes	if(memcmp(real, lite_pre, lite_pad) != 0) {
600238106Sdes		log_err("realloc(): prefix failed %s:%d %s", file, line, func);
601238106Sdes		log_hex("prefix here", real, lite_pad);
602238106Sdes		log_hex("  should be", lite_pre, lite_pad);
603238106Sdes		fatal_exit("alloc assertion failed");
604238106Sdes	}
605238106Sdes	memmove(&orig, real+lite_pad, sizeof(size_t));
606238106Sdes	if(memcmp(real+lite_pad+orig+sizeof(size_t), lite_post, lite_pad)!=0){
607238106Sdes		log_err("realloc(): suffix failed %s:%d %s", file, line, func);
608238106Sdes		log_err("alloc size is %d", (int)orig);
609238106Sdes		log_hex("suffix here", real+lite_pad+orig+sizeof(size_t),
610238106Sdes			lite_pad);
611238106Sdes		log_hex("  should be", lite_post, lite_pad);
612238106Sdes		fatal_exit("alloc assertion failed");
613238106Sdes	}
614238106Sdes	/* new alloc and copy over */
615238106Sdes	newa = unbound_stat_malloc_lite(size, file, line, func);
616238106Sdes	if(!newa)
617238106Sdes		return NULL;
618238106Sdes	if(orig < size)
619238106Sdes		memmove(newa, ptr, orig);
620238106Sdes	else	memmove(newa, ptr, size);
621238106Sdes	memset(real, 0xdd, orig+lite_pad*2+sizeof(size_t)); /* mark it */
622238106Sdes	free(real);
623238106Sdes	return newa;
624238106Sdes}
625238106Sdes
626238106Sdeschar* unbound_strdup_lite(const char* s, const char* file, int line,
627238106Sdes        const char* func)
628238106Sdes{
629238106Sdes	/* this routine is made to make sure strdup() uses the malloc_lite */
630238106Sdes	size_t l = strlen(s)+1;
631238106Sdes	char* n = (char*)unbound_stat_malloc_lite(l, file, line, func);
632238106Sdes	if(!n) return NULL;
633238106Sdes	memmove(n, s, l);
634238106Sdes	return n;
635238106Sdes}
636238106Sdes
637238106Sdeschar* unbound_lite_wrapstr(char* s)
638238106Sdes{
639238106Sdes	char* n = unbound_strdup_lite(s, __FILE__, __LINE__, __func__);
640238106Sdes	free(s);
641238106Sdes	return n;
642238106Sdes}
643238106Sdes
644266114Sdes#undef sldns_pkt2wire
645266114Sdessldns_status unbound_lite_pkt2wire(uint8_t **dest, const sldns_pkt *p,
646238106Sdes	size_t *size)
647238106Sdes{
648238106Sdes	uint8_t* md = NULL;
649238106Sdes	size_t ms = 0;
650266114Sdes	sldns_status s = sldns_pkt2wire(&md, p, &ms);
651238106Sdes	if(md) {
652238106Sdes		*dest = unbound_stat_malloc_lite(ms, __FILE__, __LINE__,
653238106Sdes			__func__);
654238106Sdes		*size = ms;
655238106Sdes		if(!*dest) { free(md); return LDNS_STATUS_MEM_ERR; }
656238106Sdes		memcpy(*dest, md, ms);
657238106Sdes		free(md);
658238106Sdes	} else {
659238106Sdes		*dest = NULL;
660238106Sdes		*size = 0;
661238106Sdes	}
662238106Sdes	return s;
663238106Sdes}
664238106Sdes
665238106Sdes#undef i2d_DSA_SIG
666238106Sdesint unbound_lite_i2d_DSA_SIG(DSA_SIG* dsasig, unsigned char** sig)
667238106Sdes{
668238106Sdes	unsigned char* n = NULL;
669238106Sdes	int r= i2d_DSA_SIG(dsasig, &n);
670238106Sdes	if(n) {
671238106Sdes		*sig = unbound_stat_malloc_lite((size_t)r, __FILE__, __LINE__,
672238106Sdes			__func__);
673238106Sdes		if(!*sig) return -1;
674238106Sdes		memcpy(*sig, n, (size_t)r);
675238106Sdes		free(n);
676238106Sdes		return r;
677238106Sdes	}
678238106Sdes	*sig = NULL;
679238106Sdes	return r;
680238106Sdes}
681238106Sdes
682238106Sdes#endif /* UNBOUND_ALLOC_LITE */
683