Home | History | Annotate | Line # | Download | only in raidframe
rf_debugMem.c revision 1.2
      1 /*	$NetBSD: rf_debugMem.c,v 1.2 1999/01/26 02:33:54 oster Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author: Daniel Stodolsky, Mark Holland, Jim Zelenka
      7  *
      8  * Permission to use, copy, modify and distribute this software and
      9  * its documentation is hereby granted, provided that both the copyright
     10  * notice and this permission notice appear in all copies of the
     11  * software, derivative works or modified versions, and any portions
     12  * thereof, and that both notices appear in supporting documentation.
     13  *
     14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  *
     18  * Carnegie Mellon requests users of this software to return to
     19  *
     20  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  *  School of Computer Science
     22  *  Carnegie Mellon University
     23  *  Pittsburgh PA 15213-3890
     24  *
     25  * any improvements or extensions that they make and grant Carnegie the
     26  * rights to redistribute these changes.
     27  */
     28 
     29 /* debugMem.c:  memory usage debugging stuff.
     30  * Malloc, Calloc, and Free are #defined everywhere
     31  * to do_malloc, do_calloc, and do_free.
     32  *
     33  * if RF_UTILITY is nonzero, it means were compiling one of the
     34  * raidframe utility programs, such as rfctrl or smd.  In this
     35  * case, we eliminate all references to the threads package
     36  * and to the allocation list stuff.
     37  */
     38 
     39 #include "rf_types.h"
     40 #include "rf_sys.h"
     41 
     42 #if RF_UTILITY == 0
     43 #include "rf_threadstuff.h"
     44 #include "rf_threadid.h"
     45 #include "rf_options.h"
     46 #else /* RF_UTILITY == 0 */
     47 #include "rf_utility.h"
     48 #endif /* RF_UTILITY == 0 */
     49 
     50 #include "rf_debugMem.h"
     51 #include "rf_general.h"
     52 
     53 static long tot_mem_in_use = 0, max_mem = 0;
     54 
     55 /* Hash table of information about memory allocations */
     56 #define RF_MH_TABLESIZE 1000
     57 
     58 struct mh_struct {
     59     void              *address;
     60     int                size;
     61     int                line;
     62     char              *filen;
     63     char               allocated;
     64     struct mh_struct  *next;
     65 };
     66 static struct mh_struct *mh_table[RF_MH_TABLESIZE];
     67 RF_DECLARE_MUTEX(rf_debug_mem_mutex)
     68 static int mh_table_initialized=0;
     69 
     70 static void memory_hash_insert(void *addr, int size, int line, char *filen);
     71 static int memory_hash_remove(void *addr, int sz);
     72 
     73 #ifndef _KERNEL                    /* no redzones or "real_" routines in the kernel */
     74 
     75 static void rf_redzone_free_failed(void *ptr, int size, int line, char *file);
     76 
     77 void *rf_real_redzone_malloc(_size_)
     78   int  _size_;
     79 {
     80   char *p;
     81 
     82   rf_validate_mh_table();
     83   p = malloc((_size_)+16);
     84   if (p == NULL)
     85     return(p);
     86   RF_ASSERT (p);
     87   *((long *) p) = (_size_) ;
     88   ((char *) p)[(_size_)+8] = '!';
     89   ((char *) p)[(_size_)+15] = '!';
     90   p += 8;
     91   return(p);
     92 }
     93 
     94 void *rf_real_redzone_calloc(_n_,_size_)
     95 int _n_,_size_;
     96 {
     97   char *p;
     98   int _sz_;
     99 
    100   rf_validate_mh_table();
    101   _sz_  = (_n_) * (_size_);
    102   p = malloc((_sz_)+16);
    103   if (p == NULL)
    104     return(p);
    105   bzero(p,(_sz_)+16);
    106   *((long *) p) = (_sz_) ;
    107   ((char *) p)[(_sz_)+8] = '!';
    108   ((char *) p)[(_sz_)+15] = '!';
    109   p += 8;
    110   return(p);
    111 }
    112 
    113 void rf_real_redzone_free(p, line, filen)
    114 char *p;
    115 int line;
    116 char *filen;
    117 {
    118   unsigned long _size_;
    119 
    120   rf_validate_mh_table();
    121   p -= 8;
    122   _size_ = *((long *) p);
    123   if  ((((char *) p)[(_size_)+8] != '!') || (((char *) p)[(_size_)+15] != '!'))
    124     rf_redzone_free_failed(p,(_size_),line,filen);
    125   free(p);
    126 }
    127 
    128 unsigned long rf_mem_alloc = 0;
    129 
    130 char *rf_real_Malloc(size, line, file)
    131   int    size;
    132   int    line;
    133   char  *file;
    134 {
    135   void *pp;
    136   char *p;
    137   int tid;
    138 
    139   RF_LOCK_MUTEX(rf_debug_mem_mutex);
    140   rf_redzone_malloc(pp, size);
    141   p = pp;
    142   if (p == NULL) {
    143     RF_ERRORMSG3("Unable to malloc %d bytes at line %d file %s\n", size,
    144       line, file);
    145   }
    146   if (rf_memAmtDebug) {
    147     rf_mem_alloc += size;
    148     printf("%lu    size %d %s:%d\n", rf_mem_alloc, size, file, line);
    149   }
    150 #if RF_UTILITY == 0
    151   if (rf_memDebug > 1) {
    152     rf_get_threadid(tid);
    153     printf("[%d] malloc 0x%lx - 0x%lx (%d) %s %d\n", tid, p, p+size, size,
    154         file, line);
    155   }
    156 #endif /* RF_UTILITY == 0 */
    157   if (rf_memDebug)
    158     rf_record_malloc(p, size, line, file);
    159   RF_UNLOCK_MUTEX(rf_debug_mem_mutex);
    160   return(p);
    161 }
    162 
    163 #if RF_UTILITY == 0
    164 char *rf_real_MallocAndAdd(size, alist, line, file)
    165   int                  size;
    166   RF_AllocListElem_t  *alist;
    167   int                  line;
    168   char                *file;
    169 {
    170 	void *pp;
    171 	char *p;
    172 	int tid;
    173 
    174 	RF_LOCK_MUTEX(rf_debug_mem_mutex);
    175 	rf_redzone_malloc(pp, size);
    176 	p = pp;
    177 	if (p == NULL) {
    178 		RF_ERRORMSG3("Unable to malloc %d bytes at line %d file %s\n", size,
    179 			line, file);
    180 	}
    181 	if (rf_memAmtDebug) {
    182 		rf_mem_alloc += size;
    183 		printf("%lu    size %d %s:%d\n", rf_mem_alloc, size, file, line);
    184 	}
    185 	if (rf_memDebug > 1) {
    186 		rf_get_threadid(tid);
    187 		printf("[%d] malloc+add 0x%lx - 0x%lx (%d) %s %d\n", tid, p, p+size,
    188 			size, file, line);
    189 	}
    190 	if (alist) {
    191 		rf_real_AddToAllocList(alist, pp, size, 0);
    192 	}
    193 	if (rf_memDebug)
    194 		rf_record_malloc(p, size, line, file);
    195 	RF_UNLOCK_MUTEX(rf_debug_mem_mutex);
    196 	return(p);
    197 }
    198 #endif /* RF_UTILITY == 0 */
    199 
    200 char *rf_real_Calloc(nel, elsz, line, file)
    201   int    nel;
    202   int    elsz;
    203   int    line;
    204   char  *file;
    205 {
    206   int tid, size;
    207   void *pp;
    208   char *p;
    209 
    210   size = nel * elsz;
    211   RF_LOCK_MUTEX(rf_debug_mem_mutex);
    212   rf_redzone_calloc(pp, nel, elsz);
    213   p = pp;
    214   if (p == NULL) {
    215     RF_ERRORMSG4("Unable to calloc %d objects of size %d at line %d file %s\n",
    216       nel, elsz, line, file);
    217     return(NULL);
    218   }
    219   if (rf_memAmtDebug) {
    220     rf_mem_alloc += size;
    221     printf("%lu    size %d %s:%d\n", rf_mem_alloc, size, file, line);
    222   }
    223 #if RF_UTILITY == 0
    224   if (rf_memDebug > 1) {
    225     rf_get_threadid(tid);
    226     printf("[%d] calloc 0x%lx - 0x%lx (%d,%d) %s %d\n", tid, p, p+size, nel,
    227         elsz, file, line);
    228   }
    229 #endif /* RF_UTILITY == 0 */
    230   if (rf_memDebug) {
    231     rf_record_malloc(p, size, line, file);
    232   }
    233   RF_UNLOCK_MUTEX(rf_debug_mem_mutex);
    234   return(p);
    235 }
    236 
    237 #if RF_UTILITY == 0
    238 char *rf_real_CallocAndAdd(nel, elsz, alist, line, file)
    239   int                  nel;
    240   int                  elsz;
    241   RF_AllocListElem_t  *alist;
    242   int                  line;
    243   char                *file;
    244 {
    245 	int tid, size;
    246 	void *pp;
    247 	char *p;
    248 
    249 	size = nel * elsz;
    250 	RF_LOCK_MUTEX(rf_debug_mem_mutex);
    251 	rf_redzone_calloc(pp, nel, elsz);
    252 	p = pp;
    253 	if (p == NULL) {
    254 		RF_ERRORMSG4("Unable to calloc %d objs of size %d at line %d file %s\n",
    255 			nel, elsz, line, file);
    256 		return(NULL);
    257 	}
    258 	if (rf_memAmtDebug) {
    259 		rf_mem_alloc += size;
    260 		printf("%lu    size %d %s:%d\n", rf_mem_alloc, size, file, line);
    261 	}
    262 	if (rf_memDebug > 1) {
    263 		rf_get_threadid(tid);
    264 		printf("[%d] calloc+add 0x%lx - 0x%lx (%d,%d) %s %d\n", tid, p,
    265 			p+size, nel, elsz, file, line);
    266 	}
    267 	if (alist) {
    268 		rf_real_AddToAllocList(alist, pp, size, 0);
    269 	}
    270 	if (rf_memDebug)
    271 		rf_record_malloc(p, size, line, file);
    272 	RF_UNLOCK_MUTEX(rf_debug_mem_mutex);
    273 	return(p);
    274 }
    275 #endif /* RF_UTILITY == 0 */
    276 
    277 void rf_real_Free(p, sz, line, file)
    278   void  *p;
    279   int    sz;
    280   int    line;
    281   char  *file;
    282 {
    283   int tid;
    284 
    285 #if RF_UTILITY == 0
    286   if (rf_memDebug > 1) {
    287     rf_get_threadid(tid);
    288     printf("[%d] free 0x%lx - 0x%lx (%d) %s %d\n", tid, p, ((char *)p)+sz, sz,
    289       file, line);
    290   }
    291 #endif /* RF_UTILITY == 0 */
    292   RF_LOCK_MUTEX(rf_debug_mem_mutex);
    293   if (rf_memAmtDebug) {
    294     rf_mem_alloc -= sz;
    295     printf("%lu  - size %d %s:%d\n", rf_mem_alloc, sz, file, line);
    296   }
    297   if (rf_memDebug) {
    298     rf_unrecord_malloc(p,sz);
    299   }
    300   rf_redzone_free(p);
    301   RF_UNLOCK_MUTEX(rf_debug_mem_mutex);
    302 }
    303 
    304 void rf_validate_mh_table()
    305 {
    306   int i, size;
    307   struct mh_struct *p;
    308   char *cp;
    309 
    310   return;
    311   for (i=0; i<RF_MH_TABLESIZE; i++) {
    312     for (p=mh_table[i]; p; p=p->next) if (p->allocated) {
    313       cp = ((char *) p->address) - 8;
    314       size = *((long *) cp);
    315       if  ((((char *) cp)[(size)+8] != '!') || (((char *) cp)[(size)+15] != '!')) {
    316 	rf_redzone_free_failed(cp,(size),__LINE__,__FILE__);
    317       }
    318     }
    319   }
    320 }
    321 
    322 static void rf_redzone_free_failed(ptr,size,line,file)
    323   void  *ptr;
    324   int    size;
    325   int    line;
    326   char  *file;
    327 {
    328   RF_ERRORMSG4("Free of 0x%lx (recorded size %d) at %d of %s detected redzone overrun\n",ptr,size,line,file);
    329   RF_ASSERT(0);
    330 }
    331 
    332 #endif /* !_KERNEL */
    333 
    334 void rf_record_malloc(p, size, line, filen)
    335 void *p;
    336 int size, line;
    337 char *filen;
    338 {
    339   RF_ASSERT(size != 0);
    340 
    341   /*RF_LOCK_MUTEX(rf_debug_mem_mutex);*/
    342   memory_hash_insert(p, size, line, filen);
    343   tot_mem_in_use += size;
    344   /*RF_UNLOCK_MUTEX(rf_debug_mem_mutex);*/
    345   if ( (long) p == rf_memDebugAddress) {
    346     printf("Allocate: debug address allocated from line %d file %s\n",line,filen);
    347   }
    348 }
    349 
    350 void rf_unrecord_malloc(p, sz)
    351 void *p;
    352 int sz;
    353 {
    354   int size;
    355 
    356   /*RF_LOCK_MUTEX(rf_debug_mem_mutex);*/
    357   size = memory_hash_remove(p, sz);
    358   tot_mem_in_use -= size;
    359   /*RF_UNLOCK_MUTEX(rf_debug_mem_mutex);*/
    360   if ( (long) p == rf_memDebugAddress) {
    361     printf("Free: Found debug address\n");         /* this is really only a flag line for gdb */
    362   }
    363 }
    364 
    365 void rf_print_unfreed()
    366 {
    367   int i, foundone=0;
    368   struct mh_struct *p;
    369 
    370   for (i=0; i<RF_MH_TABLESIZE; i++) {
    371     for (p=mh_table[i]; p; p=p->next) if (p->allocated) {
    372       if (!foundone) printf("\n\nThere are unfreed memory locations at program shutdown:\n");
    373       foundone = 1;
    374       printf("Addr 0x%lx Size %d line %d file %s\n",
    375 	     (long)p->address,p->size,p->line,p->filen);
    376     }
    377   }
    378   if (tot_mem_in_use) {
    379     printf("%ld total bytes in use\n", tot_mem_in_use);
    380   }
    381 }
    382 
    383 int rf_ConfigureDebugMem(listp)
    384   RF_ShutdownList_t  **listp;
    385 {
    386   int i, rc;
    387 
    388   rc = rf_create_managed_mutex(listp, &rf_debug_mem_mutex);
    389   if (rc) {
    390     RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    391       __LINE__, rc);
    392     return(rc);
    393   }
    394   if (rf_memDebug) {
    395     for (i=0; i<RF_MH_TABLESIZE; i++)
    396       mh_table[i] = NULL;
    397     mh_table_initialized=1;
    398   }
    399   return(0);
    400 }
    401 
    402 #define HASHADDR(_a_)      ( (((unsigned long) _a_)>>3) % RF_MH_TABLESIZE )
    403 
    404 static void memory_hash_insert(addr, size, line, filen)
    405 void *addr;
    406 int size, line;
    407 char *filen;
    408 {
    409     unsigned long bucket = HASHADDR(addr);
    410     struct mh_struct *p;
    411 
    412     RF_ASSERT(mh_table_initialized);
    413 
    414     /* search for this address in the hash table */
    415     for (p=mh_table[bucket]; p && (p->address != addr); p=p->next);
    416     if (!p) {
    417       RF_Malloc(p,sizeof(struct mh_struct),(struct mh_struct *));
    418       RF_ASSERT(p);
    419       p->next = mh_table[bucket];
    420       mh_table[bucket] = p;
    421       p->address = addr;
    422       p->allocated = 0;
    423     }
    424     if (p->allocated) {
    425       printf("ERROR:  reallocated address 0x%lx from line %d, file %s without intervening free\n",(long) addr, line, filen);
    426       printf("        last allocated from line %d file %s\n",p->line, p->filen);
    427       RF_ASSERT(0);
    428     }
    429     p->size = size; p->line = line; p->filen = filen;
    430     p->allocated = 1;
    431 }
    432 
    433 static int memory_hash_remove(addr, sz)
    434 void *addr;
    435 int sz;
    436 {
    437     unsigned long bucket = HASHADDR(addr);
    438     struct mh_struct *p;
    439 
    440     RF_ASSERT(mh_table_initialized);
    441     for (p=mh_table[bucket]; p && (p->address != addr); p=p->next);
    442     if (!p) {
    443       printf("ERROR:  freeing never-allocated address 0x%lx\n",(long) addr);
    444       RF_PANIC();
    445     }
    446     if (!p->allocated) {
    447       printf("ERROR:  freeing unallocated address 0x%lx.  Last allocation line %d file %s\n",(long) addr, p->line, p->filen);
    448       RF_PANIC();
    449     }
    450     if (sz > 0 && p->size != sz) {         /* you can suppress this error by using a negative value as the size to free */
    451       printf("ERROR:  incorrect size at free for address 0x%lx: is %d should be %d.  Alloc at line %d of file %s\n",(unsigned long) addr, sz, p->size,p->line, p->filen);
    452       RF_PANIC();
    453     }
    454     p->allocated = 0;
    455     return(p->size);
    456 }
    457 
    458 void rf_ReportMaxMem()
    459 {
    460     printf("Max memory used:  %d bytes\n",(int)max_mem);
    461 }
    462