3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file guardedalloc/intern/mallocn.c
37 * Copyright (C) 2001 NaN Technologies B.V.
38 * Guarded memory allocation, and boundary-write detection.
42 #include <string.h> /* memcpy */
44 #include <sys/types.h>
45 /* Blame Microsoft for LLP64 and no inttypes.h, quick workaround needed: */
47 #define SIZET_FORMAT "%I64u"
48 #define SIZET_ARG(a) ((unsigned long long)(a))
50 #define SIZET_FORMAT "%lu"
51 #define SIZET_ARG(a) ((unsigned long)(a))
61 #include "MEM_guardedalloc.h"
63 /* Only for debugging:
64 * lets you count the allocations so as to find the allocator of unfreed memory
65 * in situations where the leak is predictable */
67 // #define DEBUG_MEMCOUNTER
69 #ifdef DEBUG_MEMCOUNTER
70 #define DEBUG_MEMCOUNTER_ERROR_VAL 0 /* set this to the value that isnt being freed */
71 static int _mallocn_count = 0;
74 static void memcount_raise(const char *name)
76 fprintf(stderr, "%s: memcount-leak, %d\n", name, _mallocn_count);
80 /* --------------------------------------------------------------------- */
82 /* --------------------------------------------------------------------- */
83 /* all memory chunks are put in linked lists */
84 typedef struct localLink
86 struct localLink *next,*prev;
89 typedef struct localListBase
94 /* note: keep this struct aligned (e.g., irix/gcc) - Hos */
95 typedef struct MemHead {
98 struct MemHead *next,*prev;
100 const char * nextname;
102 int mmap; /* if true, memory was mmapped */
103 #ifdef DEBUG_MEMCOUNTER
108 typedef struct MemTail {
113 /* --------------------------------------------------------------------- */
114 /* local functions */
115 /* --------------------------------------------------------------------- */
117 static void addtail(volatile localListBase *listbase, void *vlink);
118 static void remlink(volatile localListBase *listbase, void *vlink);
119 static void rem_memblock(MemHead *memh);
120 static void MemorY_ErroR(const char *block, const char *error);
121 static const char *check_memlist(MemHead *memh);
123 /* --------------------------------------------------------------------- */
124 /* locally used defines */
125 /* --------------------------------------------------------------------- */
127 #if defined( __sgi) || defined (__sun) || defined (__sun__) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || (defined (__APPLE__) && !defined(__LITTLE_ENDIAN__))
128 #define MAKE_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) )
130 #define MAKE_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) )
133 #define MEMTAG1 MAKE_ID('M', 'E', 'M', 'O')
134 #define MEMTAG2 MAKE_ID('R', 'Y', 'B', 'L')
135 #define MEMTAG3 MAKE_ID('O', 'C', 'K', '!')
136 #define MEMFREE MAKE_ID('F', 'R', 'E', 'E')
138 #define MEMNEXT(x) ((MemHead *)(((char *) x) - ((char *) & (((MemHead *)0)->next))))
140 /* --------------------------------------------------------------------- */
142 /* --------------------------------------------------------------------- */
145 static volatile int totblock= 0;
146 static volatile uintptr_t mem_in_use= 0, mmap_in_use= 0, peak_mem = 0;
148 static volatile struct localListBase _membase;
149 static volatile struct localListBase *membase = &_membase;
150 static void (*error_callback)(const char *) = NULL;
151 static void (*thread_lock_callback)(void) = NULL;
152 static void (*thread_unlock_callback)(void) = NULL;
154 static int malloc_debug_memset= 0;
169 /* --------------------------------------------------------------------- */
171 /* --------------------------------------------------------------------- */
173 static void print_error(const char *str, ...)
179 vsnprintf(buf, sizeof(buf), str, ap);
181 buf[sizeof(buf) - 1] = '\0';
183 if (error_callback) error_callback(buf);
186 static void mem_lock_thread(void)
188 if (thread_lock_callback)
189 thread_lock_callback();
192 static void mem_unlock_thread(void)
194 if (thread_unlock_callback)
195 thread_unlock_callback();
198 int MEM_check_memory_integrity()
200 const char* err_val = NULL;
202 /* check_memlist starts from the front, and runs until it finds
203 * the requested chunk. For this test, that's the last one. */
204 listend = membase->last;
206 err_val = check_memlist(listend);
208 if (err_val == 0) return 0;
213 void MEM_set_error_callback(void (*func)(const char *))
215 error_callback = func;
218 void MEM_set_lock_callback(void (*lock)(void), void (*unlock)(void))
220 thread_lock_callback = lock;
221 thread_unlock_callback = unlock;
224 void MEM_set_memory_debug(void)
226 malloc_debug_memset= 1;
229 size_t MEM_allocN_len(void *vmemh)
232 MemHead *memh= vmemh;
240 void *MEM_dupallocN(void *vmemh)
245 MemHead *memh= vmemh;
249 newp= MEM_mapallocN(memh->len, "dupli_mapalloc");
251 newp= MEM_mallocN(memh->len, "dupli_alloc");
253 if (newp == NULL) return NULL;
255 memcpy(newp, vmemh, memh->len);
261 void *MEM_reallocN(void *vmemh, size_t len)
266 MemHead *memh= vmemh;
269 newp= MEM_mallocN(len, memh->name);
272 memcpy(newp, vmemh, len);
274 memcpy(newp, vmemh, memh->len);
283 static void make_memhead_header(MemHead *memh, size_t len, const char *str)
287 memh->tag1 = MEMTAG1;
292 memh->tag2 = MEMTAG2;
294 memt = (MemTail *)(((char *) memh) + sizeof(MemHead) + len);
295 memt->tag3 = MEMTAG3;
297 addtail(membase,&memh->next);
298 if (memh->next) memh->nextname = MEMNEXT(memh->next)->name;
303 peak_mem = mem_in_use > peak_mem ? mem_in_use : peak_mem;
306 void *MEM_mallocN(size_t len, const char *str)
312 len = (len + 3 ) & ~3; /* allocate in units of 4 */
314 memh= (MemHead *)malloc(len+sizeof(MemHead)+sizeof(MemTail));
317 make_memhead_header(memh, len, str);
319 if(malloc_debug_memset && len)
320 memset(memh+1, 255, len);
322 #ifdef DEBUG_MEMCOUNTER
323 if(_mallocn_count==DEBUG_MEMCOUNTER_ERROR_VAL)
324 memcount_raise("MEM_mallocN");
325 memh->_count= _mallocn_count++;
330 print_error("Malloc returns null: len=" SIZET_FORMAT " in %s, total %u\n", SIZET_ARG(len), str, mem_in_use);
334 void *MEM_callocN(size_t len, const char *str)
340 len = (len + 3 ) & ~3; /* allocate in units of 4 */
342 memh= (MemHead *)calloc(len+sizeof(MemHead)+sizeof(MemTail),1);
345 make_memhead_header(memh, len, str);
347 #ifdef DEBUG_MEMCOUNTER
348 if(_mallocn_count==DEBUG_MEMCOUNTER_ERROR_VAL)
349 memcount_raise("MEM_callocN");
350 memh->_count= _mallocn_count++;
355 print_error("Calloc returns null: len=" SIZET_FORMAT " in %s, total %u\n", SIZET_ARG(len), str, mem_in_use);
359 /* note; mmap returns zero'd memory */
360 void *MEM_mapallocN(size_t len, const char *str)
366 len = (len + 3 ) & ~3; /* allocate in units of 4 */
373 fd = open("/dev/zero", O_RDWR);
375 memh= mmap(0, len+sizeof(MemHead)+sizeof(MemTail),
376 PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
380 memh= mmap(0, len+sizeof(MemHead)+sizeof(MemTail),
381 PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
384 if(memh!=(MemHead *)-1) {
385 make_memhead_header(memh, len, str);
388 peak_mem = mmap_in_use > peak_mem ? mmap_in_use : peak_mem;
390 #ifdef DEBUG_MEMCOUNTER
391 if(_mallocn_count==DEBUG_MEMCOUNTER_ERROR_VAL)
392 memcount_raise("MEM_mapallocN");
393 memh->_count= _mallocn_count++;
399 print_error("Mapalloc returns null, fallback to regular malloc: len=" SIZET_FORMAT " in %s, total %u\n", SIZET_ARG(len), str, mmap_in_use);
400 return MEM_callocN(len, str);
404 /* Memory statistics print */
405 typedef struct MemPrintBlock {
411 static int compare_name(const void *p1, const void *p2)
413 const MemPrintBlock *pb1= (const MemPrintBlock*)p1;
414 const MemPrintBlock *pb2= (const MemPrintBlock*)p2;
416 return strcmp(pb1->name, pb2->name);
419 static int compare_len(const void *p1, const void *p2)
421 const MemPrintBlock *pb1= (const MemPrintBlock*)p1;
422 const MemPrintBlock *pb2= (const MemPrintBlock*)p2;
424 if(pb1->len < pb2->len)
426 else if(pb1->len == pb2->len)
432 void MEM_printmemlist_stats()
435 MemPrintBlock *pb, *printblock;
440 /* put memory blocks into array */
441 printblock= malloc(sizeof(MemPrintBlock)*totblock);
446 membl = membase->first;
447 if (membl) membl = MEMNEXT(membl);
450 pb->name= membl->name;
458 membl= MEMNEXT(membl->next);
462 /* sort by name and add together blocks with the same name */
463 qsort(printblock, totpb, sizeof(MemPrintBlock), compare_name);
464 for(a=0, b=0; a<totpb; a++) {
468 else if(strcmp(printblock[a].name, printblock[b].name) == 0) {
469 printblock[b].len += printblock[a].len;
470 printblock[b].items++;
474 memcpy(&printblock[b], &printblock[a], sizeof(MemPrintBlock));
479 /* sort by length and print */
480 qsort(printblock, totpb, sizeof(MemPrintBlock), compare_len);
481 printf("\ntotal memory len: %.3f MB\n", (double)mem_in_use/(double)(1024*1024));
482 printf(" ITEMS TOTAL-MiB AVERAGE-KiB TYPE\n");
483 for(a=0, pb=printblock; a<totpb; a++, pb++)
484 printf("%6d (%8.3f %8.3f) %s\n", pb->items, (double)pb->len/(double)(1024*1024), (double)pb->len/1024.0/(double)pb->items, pb->name);
490 #if 0 /* GLIBC only */
495 /* Prints in python syntax for easy */
496 static void MEM_printmemlist_internal( int pydict )
502 membl = membase->first;
503 if (membl) membl = MEMNEXT(membl);
506 print_error("# membase_debug.py\n");
507 print_error("membase = [\\\n");
511 fprintf(stderr, "{'len':" SIZET_FORMAT ", 'name':'''%s''', 'pointer':'%p'},\\\n", SIZET_ARG(membl->len), membl->name, (void *)(membl+1));
513 #ifdef DEBUG_MEMCOUNTER
514 print_error("%s len: " SIZET_FORMAT " %p, count: %d\n", membl->name, SIZET_ARG(membl->len), membl+1, membl->_count);
516 print_error("%s len: " SIZET_FORMAT " %p\n", membl->name, SIZET_ARG(membl->len), membl+1);
520 membl= MEMNEXT(membl->next);
524 fprintf(stderr, "]\n\n");
528 "for mb_item in membase:\n"
529 "\tmb_item_user_size = mb_userinfo.setdefault(mb_item['name'], [0,0])\n"
530 "\tmb_item_user_size[0] += 1 # Add a user\n"
531 "\tmb_item_user_size[1] += mb_item['len'] # Increment the size\n"
532 "\ttotmem += mb_item['len']\n"
533 "print '(membase) items:', len(membase), '| unique-names:', len(mb_userinfo), '| total-mem:', totmem\n"
534 "mb_userinfo_sort = mb_userinfo.items()\n"
535 "for sort_name, sort_func in (('size', lambda a: -a[1][1]), ('users', lambda a: -a[1][0]), ('name', lambda a: a[0])):\n"
536 "\tprint '\\nSorting by:', sort_name\n"
537 "\tmb_userinfo_sort.sort(key = sort_func)\n"
538 "\tfor item in mb_userinfo_sort:\n"
539 "\t\tprint 'name:%%s, users:%%i, len:%%i' %% (item[0], item[1][0], item[1][1])\n"
546 void MEM_callbackmemlist(void (*func)(void*)) {
551 membl = membase->first;
552 if (membl) membl = MEMNEXT(membl);
557 membl= MEMNEXT(membl->next);
564 short MEM_testN(void *vmemh) {
569 membl = membase->first;
570 if (membl) membl = MEMNEXT(membl);
573 if (vmemh == membl+1) {
579 membl= MEMNEXT(membl->next);
585 print_error("Memoryblock %p: pointer not in memlist\n", vmemh);
589 void MEM_printmemlist( void ) {
590 MEM_printmemlist_internal(0);
592 void MEM_printmemlist_pydict( void ) {
593 MEM_printmemlist_internal(1);
596 short MEM_freeN(void *vmemh) /* anders compileertie niet meer */
600 MemHead *memh= vmemh;
604 MemorY_ErroR("free","attempt to free NULL pointer");
605 /* print_error(err_stream, "%d\n", (memh+4000)->tag1); */
609 if(sizeof(intptr_t)==8) {
610 if (((intptr_t) memh) & 0x7) {
611 MemorY_ErroR("free","attempt to free illegal pointer");
616 if (((intptr_t) memh) & 0x3) {
617 MemorY_ErroR("free","attempt to free illegal pointer");
623 if(memh->tag1 == MEMFREE && memh->tag2 == MEMFREE) {
624 MemorY_ErroR(memh->name,"double free");
629 if ((memh->tag1 == MEMTAG1) && (memh->tag2 == MEMTAG2) && ((memh->len & 0x3) == 0)) {
630 memt = (MemTail *)(((char *) memh) + sizeof(MemHead) + memh->len);
631 if (memt->tag3 == MEMTAG3){
633 memh->tag1 = MEMFREE;
634 memh->tag2 = MEMFREE;
635 memt->tag3 = MEMFREE;
644 MemorY_ErroR(memh->name,"end corrupt");
645 name = check_memlist(memh);
647 if (name != memh->name) MemorY_ErroR(name,"is also corrupt");
651 name = check_memlist(memh);
653 MemorY_ErroR("free","pointer not in memlist");
655 MemorY_ErroR(name,"error in header");
659 /* here a DUMP should happen */
666 /* --------------------------------------------------------------------- */
667 /* local functions */
668 /* --------------------------------------------------------------------- */
670 static void addtail(volatile localListBase *listbase, void *vlink)
672 struct localLink *link= vlink;
674 if (link == 0) return;
675 if (listbase == 0) return;
678 link->prev = listbase->last;
680 if (listbase->last) ((struct localLink *)listbase->last)->next = link;
681 if (listbase->first == 0) listbase->first = link;
682 listbase->last = link;
685 static void remlink(volatile localListBase *listbase, void *vlink)
687 struct localLink *link= vlink;
689 if (link == 0) return;
690 if (listbase == 0) return;
692 if (link->next) link->next->prev = link->prev;
693 if (link->prev) link->prev->next = link->next;
695 if (listbase->last == link) listbase->last = link->prev;
696 if (listbase->first == link) listbase->first = link->next;
699 static void rem_memblock(MemHead *memh)
701 remlink(membase,&memh->next);
704 MEMNEXT(memh->prev)->nextname = MEMNEXT(memh->next)->name;
706 MEMNEXT(memh->prev)->nextname = NULL;
710 mem_in_use -= memh->len;
713 mmap_in_use -= memh->len;
714 if (munmap(memh, memh->len + sizeof(MemHead) + sizeof(MemTail)))
715 printf("Couldn't unmap memory %s\n", memh->name);
718 if(malloc_debug_memset && memh->len)
719 memset(memh+1, 255, memh->len);
724 static void MemorY_ErroR(const char *block, const char *error)
726 print_error("Memoryblock %s: %s\n",block, error);
729 static const char *check_memlist(MemHead *memh)
731 MemHead *forw,*back,*forwok,*backok;
734 forw = membase->first;
735 if (forw) forw = MEMNEXT(forw);
738 if (forw->tag1 != MEMTAG1 || forw->tag2 != MEMTAG2) break;
740 if (forw->next) forw = MEMNEXT(forw->next);
744 back = (MemHead *) membase->last;
745 if (back) back = MEMNEXT(back);
748 if (back->tag1 != MEMTAG1 || back->tag2 != MEMTAG2) break;
750 if (back->prev) back = MEMNEXT(back->prev);
754 if (forw != back) return ("MORE THAN 1 MEMORYBLOCK CORRUPT");
756 if (forw == 0 && back == 0){
757 /* geen foute headers gevonden dan maar op zoek naar memblock*/
759 forw = membase->first;
760 if (forw) forw = MEMNEXT(forw);
763 if (forw == memh) break;
764 if (forw->tag1 != MEMTAG1 || forw->tag2 != MEMTAG2) break;
766 if (forw->next) forw = MEMNEXT(forw->next);
769 if (forw == 0) return (0);
771 back = (MemHead *) membase->last;
772 if (back) back = MEMNEXT(back);
775 if (back == memh) break;
776 if (back->tag1 != MEMTAG1 || back->tag2 != MEMTAG2) break;
778 if (back->prev) back = MEMNEXT(back->prev);
783 if (forwok) name = forwok->nextname;
784 else name = "No name found";
787 /* voor alle zekerheid wordt dit block maar uit de lijst gehaald */
790 forwok->next = (MemHead *)&backok->next;
791 backok->prev = (MemHead *)&forwok->next;
792 forwok->nextname = backok->name;
795 membase->last = (struct localLink *) &forwok->next;
796 /* membase->last = (struct Link *) &forwok->next; */
801 membase->first = &backok->next;
803 membase->first = membase->last = 0;
807 MemorY_ErroR(name,"Additional error in header");
808 return("Additional error in header");
814 uintptr_t MEM_get_peak_memory(void)
819 _peak_mem = peak_mem;
825 void MEM_reset_peak_memory(void)
832 uintptr_t MEM_get_memory_in_use(void)
834 uintptr_t _mem_in_use;
837 _mem_in_use= mem_in_use;
843 uintptr_t MEM_get_mapped_memory_in_use(void)
845 uintptr_t _mmap_in_use;
848 _mmap_in_use= mmap_in_use;
854 int MEM_get_memory_blocks_in_use(void)
866 const char *MEM_name_ptr(void *vmemh)
869 MemHead *memh= vmemh;
874 return "MEM_name_ptr(NULL)";