The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/raidframe/rf_debugMem.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: rf_debugMem.c,v 1.14 2003/12/30 21:59:03 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@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 
   34 #include <sys/cdefs.h>
   35 __KERNEL_RCSID(0, "$NetBSD: rf_debugMem.c,v 1.14 2003/12/30 21:59:03 oster Exp $");
   36 
   37 #include <dev/raidframe/raidframevar.h>
   38 
   39 #include "rf_threadstuff.h"
   40 #include "rf_options.h"
   41 #include "rf_debugMem.h"
   42 #include "rf_general.h"
   43 
   44 #if RF_DEBUG_MEM
   45 
   46 static long tot_mem_in_use = 0;
   47 
   48 /* Hash table of information about memory allocations */
   49 #define RF_MH_TABLESIZE 1000
   50 
   51 struct mh_struct {
   52         void   *address;
   53         int     size;
   54         int     line;
   55         char   *filen;
   56         char    allocated;
   57         struct mh_struct *next;
   58 };
   59 static struct mh_struct *mh_table[RF_MH_TABLESIZE];
   60 RF_DECLARE_MUTEX(rf_debug_mem_mutex)
   61 static int mh_table_initialized = 0;
   62 
   63 static void memory_hash_insert(void *addr, int size, int line, char *filen);
   64 static int memory_hash_remove(void *addr, int sz);
   65 
   66 void 
   67 rf_record_malloc(void *p, int size, int line, char *filen)
   68 {
   69         RF_ASSERT(size != 0);
   70 
   71         /* RF_LOCK_MUTEX(rf_debug_mem_mutex); */
   72         memory_hash_insert(p, size, line, filen);
   73         tot_mem_in_use += size;
   74         /* RF_UNLOCK_MUTEX(rf_debug_mem_mutex); */
   75         if ((long) p == rf_memDebugAddress) {
   76                 printf("Allocate: debug address allocated from line %d file %s\n", line, filen);
   77         }
   78 }
   79 
   80 void 
   81 rf_unrecord_malloc(void *p, int sz)
   82 {
   83         int     size;
   84 
   85         /* RF_LOCK_MUTEX(rf_debug_mem_mutex); */
   86         size = memory_hash_remove(p, sz);
   87         tot_mem_in_use -= size;
   88         /* RF_UNLOCK_MUTEX(rf_debug_mem_mutex); */
   89         if ((long) p == rf_memDebugAddress) {
   90                 printf("Free: Found debug address\n");  /* this is really only a
   91                                                          * flag line for gdb */
   92         }
   93 }
   94 
   95 void 
   96 rf_print_unfreed()
   97 {
   98         int     i, foundone = 0;
   99         struct mh_struct *p;
  100 
  101         for (i = 0; i < RF_MH_TABLESIZE; i++) {
  102                 for (p = mh_table[i]; p; p = p->next)
  103                         if (p->allocated) {
  104                                 if (!foundone)
  105                                         printf("\n\nThere are unfreed memory locations at program shutdown:\n");
  106                                 foundone = 1;
  107                                 printf("Addr 0x%lx Size %d line %d file %s\n",
  108                                     (long) p->address, p->size, p->line, p->filen);
  109                         }
  110         }
  111         if (tot_mem_in_use) {
  112                 printf("%ld total bytes in use\n", tot_mem_in_use);
  113         }
  114 }
  115 #endif /* RF_DEBUG_MEM */
  116 
  117 int 
  118 rf_ConfigureDebugMem(RF_ShutdownList_t **listp)
  119 {
  120 #if RF_DEBUG_MEM
  121         int     i, rc;
  122 
  123         rf_mutex_init(&rf_debug_mem_mutex);
  124         if (rf_memDebug) {
  125                 for (i = 0; i < RF_MH_TABLESIZE; i++)
  126                         mh_table[i] = NULL;
  127                 mh_table_initialized = 1;
  128         }
  129 #endif
  130         return (0);
  131 }
  132 
  133 #if RF_DEBUG_MEM
  134 
  135 #define HASHADDR(_a_)      ( (((unsigned long) _a_)>>3) % RF_MH_TABLESIZE )
  136 
  137 static void 
  138 memory_hash_insert(void *addr, int size, int line, char *filen)
  139 {
  140         unsigned long bucket = HASHADDR(addr);
  141         struct mh_struct *p;
  142 
  143         RF_ASSERT(mh_table_initialized);
  144 
  145         /* search for this address in the hash table */
  146         for (p = mh_table[bucket]; p && (p->address != addr); p = p->next);
  147         if (!p) {
  148                 RF_Malloc(p, sizeof(struct mh_struct), (struct mh_struct *));
  149                 RF_ASSERT(p);
  150                 p->next = mh_table[bucket];
  151                 mh_table[bucket] = p;
  152                 p->address = addr;
  153                 p->allocated = 0;
  154         }
  155         if (p->allocated) {
  156                 printf("ERROR:  reallocated address 0x%lx from line %d, file %s without intervening free\n", (long) addr, line, filen);
  157                 printf("        last allocated from line %d file %s\n", p->line, p->filen);
  158                 RF_ASSERT(0);
  159         }
  160         p->size = size;
  161         p->line = line;
  162         p->filen = filen;
  163         p->allocated = 1;
  164 }
  165 
  166 static int 
  167 memory_hash_remove(void *addr, int sz)
  168 {
  169         unsigned long bucket = HASHADDR(addr);
  170         struct mh_struct *p;
  171 
  172         RF_ASSERT(mh_table_initialized);
  173         for (p = mh_table[bucket]; p && (p->address != addr); p = p->next);
  174         if (!p) {
  175                 printf("ERROR:  freeing never-allocated address 0x%lx\n", (long) addr);
  176                 RF_PANIC();
  177         }
  178         if (!p->allocated) {
  179                 printf("ERROR:  freeing unallocated address 0x%lx.  Last allocation line %d file %s\n", (long) addr, p->line, p->filen);
  180                 RF_PANIC();
  181         }
  182         if (sz > 0 && p->size != sz) {  /* you can suppress this error by
  183                                          * using a negative value as the size
  184                                          * to free */
  185                 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);
  186                 RF_PANIC();
  187         }
  188         p->allocated = 0;
  189         return (p->size);
  190 }
  191 #endif /* RF_DEBUG_MEM */
  192 
  193 

Cache object: ab26ecd3203dfa54bbf4666f41af6a94


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.