2 * util/alloc.c - memory allocation service.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains memory allocation functions.
43 #include "util/alloc.h"
44 #include "util/regional.h"
45 #include "util/data/packed_rrset.h"
46 #include "util/fptr_wlist.h"
48 /** custom size of cached regional blocks */
49 #define ALLOC_REG_SIZE 16384
50 /** number of bits for ID part of uint64, rest for number of threads. */
51 #define THRNUM_SHIFT 48 /* for 65k threads, 2^48 rrsets per thr. */
53 /** setup new special type */
55 alloc_setup_special(alloc_special_type* t)
57 memset(t, 0, sizeof(*t));
58 lock_rw_init(&t->entry.lock);
62 /** prealloc some entries in the cache. To minimize contention.
63 * Result is 1 lock per alloc_max newly created entries.
64 * @param alloc: the structure to fill up.
67 prealloc_setup(struct alloc_cache* alloc)
69 alloc_special_type* p;
71 for(i=0; i<ALLOC_SPECIAL_MAX; i++) {
72 if(!(p = (alloc_special_type*)malloc(
73 sizeof(alloc_special_type)))) {
74 log_err("prealloc: out of memory");
77 alloc_setup_special(p);
78 alloc_set_special_next(p, alloc->quar);
84 /** prealloc region blocks */
86 prealloc_blocks(struct alloc_cache* alloc, size_t num)
90 for(i=0; i<num; i++) {
91 r = regional_create_custom(ALLOC_REG_SIZE);
93 log_err("prealloc blocks: out of memory");
96 r->next = (char*)alloc->reg_list;
98 alloc->num_reg_blocks ++;
103 alloc_init(struct alloc_cache* alloc, struct alloc_cache* super,
106 memset(alloc, 0, sizeof(*alloc));
107 alloc->super = super;
108 alloc->thread_num = thread_num;
109 alloc->next_id = (uint64_t)thread_num; /* in steps, so that type */
110 alloc->next_id <<= THRNUM_SHIFT; /* of *_id is used. */
111 alloc->last_id = 1; /* so no 64bit constants, */
112 alloc->last_id <<= THRNUM_SHIFT; /* or implicit 'int' ops. */
113 alloc->last_id -= 1; /* for compiler portability. */
114 alloc->last_id |= alloc->next_id;
115 alloc->next_id += 1; /* because id=0 is special. */
116 alloc->max_reg_blocks = 100;
117 alloc->num_reg_blocks = 0;
118 alloc->reg_list = NULL;
119 alloc->cleanup = NULL;
120 alloc->cleanup_arg = NULL;
122 prealloc_blocks(alloc, alloc->max_reg_blocks);
124 lock_quick_init(&alloc->lock);
125 lock_protect(&alloc->lock, alloc, sizeof(*alloc));
130 alloc_clear(struct alloc_cache* alloc)
132 alloc_special_type* p, *np;
133 struct regional* r, *nr;
137 lock_quick_destroy(&alloc->lock);
139 if(alloc->super && alloc->quar) {
140 /* push entire list into super */
142 while(alloc_special_next(p)) /* find last */
143 p = alloc_special_next(p);
144 lock_quick_lock(&alloc->super->lock);
145 alloc_set_special_next(p, alloc->super->quar);
146 alloc->super->quar = alloc->quar;
147 alloc->super->num_quar += alloc->num_quar;
148 lock_quick_unlock(&alloc->super->lock);
153 np = alloc_special_next(p);
154 /* deinit special type */
155 lock_rw_destroy(&p->entry.lock);
164 nr = (struct regional*)r->next;
168 alloc->reg_list = NULL;
169 alloc->num_reg_blocks = 0;
173 alloc_get_id(struct alloc_cache* alloc)
175 uint64_t id = alloc->next_id++;
176 if(id == alloc->last_id) {
177 log_warn("rrset alloc: out of 64bit ids. Clearing cache.");
178 fptr_ok(fptr_whitelist_alloc_cleanup(alloc->cleanup));
179 (*alloc->cleanup)(alloc->cleanup_arg);
181 /* start back at first number */ /* like in alloc_init*/
182 alloc->next_id = (uint64_t)alloc->thread_num;
183 alloc->next_id <<= THRNUM_SHIFT; /* in steps for comp. */
184 alloc->next_id += 1; /* portability. */
185 /* and generate new and safe id */
186 id = alloc->next_id++;
192 alloc_special_obtain(struct alloc_cache* alloc)
194 alloc_special_type* p;
196 /* see if in local cache */
199 alloc->quar = alloc_special_next(p);
201 p->id = alloc_get_id(alloc);
204 /* see if in global cache */
206 /* could maybe grab alloc_max/2 entries in one go,
207 * but really, isn't that just as fast as this code? */
208 lock_quick_lock(&alloc->super->lock);
209 if((p = alloc->super->quar)) {
210 alloc->super->quar = alloc_special_next(p);
211 alloc->super->num_quar--;
213 lock_quick_unlock(&alloc->super->lock);
215 p->id = alloc_get_id(alloc);
220 prealloc_setup(alloc);
221 if(!(p = (alloc_special_type*)malloc(sizeof(alloc_special_type)))) {
222 log_err("alloc_special_obtain: out of memory");
225 alloc_setup_special(p);
226 p->id = alloc_get_id(alloc);
230 /** push mem and some more items to the super */
232 pushintosuper(struct alloc_cache* alloc, alloc_special_type* mem)
235 alloc_special_type *p = alloc->quar;
237 log_assert(alloc && alloc->super &&
238 alloc->num_quar >= ALLOC_SPECIAL_MAX);
239 /* push ALLOC_SPECIAL_MAX/2 after mem */
240 alloc_set_special_next(mem, alloc->quar);
241 for(i=1; i<ALLOC_SPECIAL_MAX/2; i++) {
242 p = alloc_special_next(p);
244 alloc->quar = alloc_special_next(p);
245 alloc->num_quar -= ALLOC_SPECIAL_MAX/2;
247 /* dump mem+list into the super quar list */
248 lock_quick_lock(&alloc->super->lock);
249 alloc_set_special_next(p, alloc->super->quar);
250 alloc->super->quar = mem;
251 alloc->super->num_quar += ALLOC_SPECIAL_MAX/2 + 1;
252 lock_quick_unlock(&alloc->super->lock);
253 /* so 1 lock per mem+alloc/2 deletes */
257 alloc_special_release(struct alloc_cache* alloc, alloc_special_type* mem)
263 lock_quick_lock(&alloc->lock); /* superalloc needs locking */
266 alloc_special_clean(mem);
267 if(alloc->super && alloc->num_quar >= ALLOC_SPECIAL_MAX) {
268 /* push it to the super structure */
269 pushintosuper(alloc, mem);
273 alloc_set_special_next(mem, alloc->quar);
277 lock_quick_unlock(&alloc->lock);
282 alloc_stats(struct alloc_cache* alloc)
284 log_info("%salloc: %d in cache, %d blocks.", alloc->super?"":"sup",
285 (int)alloc->num_quar, (int)alloc->num_reg_blocks);
288 size_t alloc_get_mem(struct alloc_cache* alloc)
290 alloc_special_type* p;
291 size_t s = sizeof(*alloc);
293 lock_quick_lock(&alloc->lock); /* superalloc needs locking */
295 s += sizeof(alloc_special_type) * alloc->num_quar;
296 for(p = alloc->quar; p; p = alloc_special_next(p)) {
297 s += lock_get_mem(&p->entry.lock);
299 s += alloc->num_reg_blocks * ALLOC_REG_SIZE;
301 lock_quick_unlock(&alloc->lock);
307 alloc_reg_obtain(struct alloc_cache* alloc)
309 if(alloc->num_reg_blocks > 0) {
310 struct regional* r = alloc->reg_list;
311 alloc->reg_list = (struct regional*)r->next;
313 alloc->num_reg_blocks--;
316 return regional_create_custom(ALLOC_REG_SIZE);
320 alloc_reg_release(struct alloc_cache* alloc, struct regional* r)
322 if(alloc->num_reg_blocks >= alloc->max_reg_blocks) {
327 regional_free_all(r);
328 log_assert(r->next == NULL);
329 r->next = (char*)alloc->reg_list;
331 alloc->num_reg_blocks++;
335 alloc_set_id_cleanup(struct alloc_cache* alloc, void (*cleanup)(void*),
338 alloc->cleanup = cleanup;
339 alloc->cleanup_arg = arg;
342 /** global debug value to keep track of total memory mallocs */
343 size_t unbound_mem_alloc = 0;
344 /** global debug value to keep track of total memory frees */
345 size_t unbound_mem_freed = 0;
346 #ifdef UNBOUND_ALLOC_STATS
347 /** special value to know if the memory is being tracked */
348 uint64_t mem_special = (uint64_t)0xfeed43327766abcdLL;
352 /** malloc with stats */
353 void *unbound_stat_malloc(size_t size)
356 if(size == 0) size = 1;
357 res = malloc(size+16);
358 if(!res) return NULL;
359 unbound_mem_alloc += size;
360 log_info("stat %p=malloc(%u)", res+16, (unsigned)size);
361 memcpy(res, &size, sizeof(size));
362 memcpy(res+8, &mem_special, sizeof(mem_special));
369 #define INT_MAX (((int)-1)>>1)
371 /** calloc with stats */
372 void *unbound_stat_calloc(size_t nmemb, size_t size)
376 if(nmemb != 0 && INT_MAX/nmemb < size)
377 return NULL; /* integer overflow check */
378 s = (nmemb*size==0)?(size_t)1:nmemb*size;
379 res = calloc(1, s+16);
380 if(!res) return NULL;
381 log_info("stat %p=calloc(%u, %u)", res+16, (unsigned)nmemb, (unsigned)size);
382 unbound_mem_alloc += s;
383 memcpy(res, &s, sizeof(s));
384 memcpy(res+8, &mem_special, sizeof(mem_special));
390 /** free with stats */
391 void unbound_stat_free(void *ptr)
395 if(memcmp(ptr-8, &mem_special, sizeof(mem_special)) != 0) {
400 memcpy(&s, ptr, sizeof(s));
401 log_info("stat free(%p) size %u", ptr+16, (unsigned)s);
403 unbound_mem_freed += s;
409 /** realloc with stats */
410 void *unbound_stat_realloc(void *ptr, size_t size)
414 if(!ptr) return unbound_stat_malloc(size);
415 if(memcmp(ptr-8, &mem_special, sizeof(mem_special)) != 0) {
416 return realloc(ptr, size);
419 unbound_stat_free(ptr);
423 memcpy(&cursz, ptr, sizeof(cursz));
425 /* nothing changes */
428 res = malloc(size+16);
429 if(!res) return NULL;
430 unbound_mem_alloc += size;
431 unbound_mem_freed += cursz;
432 log_info("stat realloc(%p, %u) from %u", ptr+16, (unsigned)size, (unsigned)cursz);
434 memcpy(res+16, ptr+16, size);
435 } else if(size > cursz) {
436 memcpy(res+16, ptr+16, cursz);
440 memcpy(res, &size, sizeof(size));
441 memcpy(res+8, &mem_special, sizeof(mem_special));
445 /** log to file where alloc was done */
446 void *unbound_stat_malloc_log(size_t size, const char* file, int line,
449 log_info("%s:%d %s malloc(%u)", file, line, func, (unsigned)size);
450 return unbound_stat_malloc(size);
453 /** log to file where alloc was done */
454 void *unbound_stat_calloc_log(size_t nmemb, size_t size, const char* file,
455 int line, const char* func)
457 log_info("%s:%d %s calloc(%u, %u)", file, line, func,
458 (unsigned) nmemb, (unsigned)size);
459 return unbound_stat_calloc(nmemb, size);
462 /** log to file where free was done */
463 void unbound_stat_free_log(void *ptr, const char* file, int line,
466 if(ptr && memcmp(ptr-8, &mem_special, sizeof(mem_special)) == 0) {
468 memcpy(&s, ptr-16, sizeof(s));
469 log_info("%s:%d %s free(%p) size %u",
470 file, line, func, ptr, (unsigned)s);
472 log_info("%s:%d %s unmatched free(%p)", file, line, func, ptr);
473 unbound_stat_free(ptr);
476 /** log to file where alloc was done */
477 void *unbound_stat_realloc_log(void *ptr, size_t size, const char* file,
478 int line, const char* func)
480 log_info("%s:%d %s realloc(%p, %u)", file, line, func,
481 ptr, (unsigned)size);
482 return unbound_stat_realloc(ptr, size);
485 #endif /* UNBOUND_ALLOC_STATS */
486 #ifdef UNBOUND_ALLOC_LITE
491 /** length of prefix and suffix */
492 static size_t lite_pad = 16;
493 /** prefix value to check */
494 static char* lite_pre = "checkfront123456";
495 /** suffix value to check */
496 static char* lite_post= "checkafter123456";
498 void *unbound_stat_malloc_lite(size_t size, const char* file, int line,
501 /* [prefix .. len .. actual data .. suffix] */
502 void* res = malloc(size+lite_pad*2+sizeof(size_t));
503 if(!res) return NULL;
504 memmove(res, lite_pre, lite_pad);
505 memmove(res+lite_pad, &size, sizeof(size_t));
506 memset(res+lite_pad+sizeof(size_t), 0x1a, size); /* init the memory */
507 memmove(res+lite_pad+size+sizeof(size_t), lite_post, lite_pad);
508 return res+lite_pad+sizeof(size_t);
511 void *unbound_stat_calloc_lite(size_t nmemb, size_t size, const char* file,
512 int line, const char* func)
516 if(nmemb != 0 && INT_MAX/nmemb < size)
517 return NULL; /* integer overflow check */
519 res = malloc(req+lite_pad*2+sizeof(size_t));
520 if(!res) return NULL;
521 memmove(res, lite_pre, lite_pad);
522 memmove(res+lite_pad, &req, sizeof(size_t));
523 memset(res+lite_pad+sizeof(size_t), 0, req);
524 memmove(res+lite_pad+req+sizeof(size_t), lite_post, lite_pad);
525 return res+lite_pad+sizeof(size_t);
528 void unbound_stat_free_lite(void *ptr, const char* file, int line,
534 real = ptr-lite_pad-sizeof(size_t);
535 if(memcmp(real, lite_pre, lite_pad) != 0) {
536 log_err("free(): prefix failed %s:%d %s", file, line, func);
537 log_hex("prefix here", real, lite_pad);
538 log_hex(" should be", lite_pre, lite_pad);
539 fatal_exit("alloc assertion failed");
541 memmove(&orig, real+lite_pad, sizeof(size_t));
542 if(memcmp(real+lite_pad+orig+sizeof(size_t), lite_post, lite_pad)!=0){
543 log_err("free(): suffix failed %s:%d %s", file, line, func);
544 log_err("alloc size is %d", (int)orig);
545 log_hex("suffix here", real+lite_pad+orig+sizeof(size_t),
547 log_hex(" should be", lite_post, lite_pad);
548 fatal_exit("alloc assertion failed");
550 memset(real, 0xdd, orig+lite_pad*2+sizeof(size_t)); /* mark it */
554 void *unbound_stat_realloc_lite(void *ptr, size_t size, const char* file,
555 int line, const char* func)
557 /* always free and realloc (no growing) */
562 return unbound_stat_malloc_lite(size, file, line, func);
566 unbound_stat_free_lite(ptr, file, line, func);
569 /* change allocation size and copy */
570 real = ptr-lite_pad-sizeof(size_t);
571 if(memcmp(real, lite_pre, lite_pad) != 0) {
572 log_err("realloc(): prefix failed %s:%d %s", file, line, func);
573 log_hex("prefix here", real, lite_pad);
574 log_hex(" should be", lite_pre, lite_pad);
575 fatal_exit("alloc assertion failed");
577 memmove(&orig, real+lite_pad, sizeof(size_t));
578 if(memcmp(real+lite_pad+orig+sizeof(size_t), lite_post, lite_pad)!=0){
579 log_err("realloc(): suffix failed %s:%d %s", file, line, func);
580 log_err("alloc size is %d", (int)orig);
581 log_hex("suffix here", real+lite_pad+orig+sizeof(size_t),
583 log_hex(" should be", lite_post, lite_pad);
584 fatal_exit("alloc assertion failed");
586 /* new alloc and copy over */
587 newa = unbound_stat_malloc_lite(size, file, line, func);
591 memmove(newa, ptr, orig);
592 else memmove(newa, ptr, size);
593 memset(real, 0xdd, orig+lite_pad*2+sizeof(size_t)); /* mark it */
598 char* unbound_strdup_lite(const char* s, const char* file, int line,
601 /* this routine is made to make sure strdup() uses the malloc_lite */
602 size_t l = strlen(s)+1;
603 char* n = (char*)unbound_stat_malloc_lite(l, file, line, func);
609 char* unbound_lite_wrapstr(char* s)
611 char* n = unbound_strdup_lite(s, __FILE__, __LINE__, __func__);
616 #undef sldns_pkt2wire
617 sldns_status unbound_lite_pkt2wire(uint8_t **dest, const sldns_pkt *p,
622 sldns_status s = sldns_pkt2wire(&md, p, &ms);
624 *dest = unbound_stat_malloc_lite(ms, __FILE__, __LINE__,
627 if(!*dest) { free(md); return LDNS_STATUS_MEM_ERR; }
628 memcpy(*dest, md, ms);
638 int unbound_lite_i2d_DSA_SIG(DSA_SIG* dsasig, unsigned char** sig)
640 unsigned char* n = NULL;
641 int r= i2d_DSA_SIG(dsasig, &n);
643 *sig = unbound_stat_malloc_lite((size_t)r, __FILE__, __LINE__,
646 memcpy(*sig, n, (size_t)r);
654 #endif /* UNBOUND_ALLOC_LITE */