1: /*
   2:  *			S T O R A G E . C
   3:  *
   4:  * Ray Tracing program, storage manager.
   5:  *
   6:  *  Functions -
   7:  *	rt_malloc	Allocate storage, with visibility & checking
   8:  *	rt_free		Similarly, free storage
   9:  *	rt_prmem	When debugging, print memory map
  10:  *	calloc, cfree	Which call rt_malloc, rt_free
  11:  *
  12:  *  Author -
  13:  *	Michael John Muuss
  14:  *
  15:  *  Source -
  16:  *	SECAD/VLD Computing Consortium, Bldg 394
  17:  *	The U. S. Army Ballistic Research Laboratory
  18:  *	Aberdeen Proving Ground, Maryland  21005-5066
  19:  *
  20:  *  Copyright Notice -
  21:  *	This software is Copyright (C) 1987 by the United States Army.
  22:  *	All rights reserved.
  23:  */
  24: #if defined(DOSCCS) && !defined(lint)
  25: static char RCSstorage[] = "@(#)$Header: /us1/cr/work/named/named/RCS/storage.c,v 1.1 94/01/01 03:13:33 cr Exp Locker: cr $";
  26: #endif
  27: 
  28: #include <sys/param.h>
  29: #if BSD >= 43
  30: #include <sys/syslog.h>
  31: #else
  32: #include <stdio.h>
  33: #define LOG_ERR 0
  34: #endif BSD
  35: 
  36: #undef malloc
  37: #undef free
  38: 
  39: #define MDB_SIZE    20000
  40: #define MDB_MAGIC   0x12348969
  41: struct memdebug {
  42:     char    *mdb_addr;
  43:     char    *mdb_str;
  44:     int mdb_len;
  45: } rt_mdb[MDB_SIZE];
  46: 
  47: /*
  48:  *			R T _ M A L L O C
  49:  */
  50: char *
  51: rt_malloc(cnt)
  52: unsigned int cnt;
  53: {
  54:     register char *ptr;
  55:     extern char *malloc();
  56: 
  57:     cnt = (cnt+2*sizeof(int)-1)&(~(sizeof(int)-1));
  58:     ptr = malloc(cnt);
  59: 
  60:     if( ptr==(char *)0 ) {
  61:         syslog(LOG_ERR, "rt_malloc: malloc failure");
  62:         abort();
  63:     } else  {
  64:         register struct memdebug *mp = rt_mdb;
  65:         for( ; mp < &rt_mdb[MDB_SIZE]; mp++ )  {
  66:             if( mp->mdb_len > 0 )  continue;
  67:             mp->mdb_addr = ptr;
  68:             mp->mdb_len = cnt;
  69:             mp->mdb_str = "???";
  70:             goto ok;
  71:         }
  72:         syslog(LOG_ERR, "rt_malloc:  memdebug overflow\n");
  73:     }
  74: ok: ;
  75:     {
  76:         register int *ip = (int *)(ptr+cnt-sizeof(int));
  77:         *ip = MDB_MAGIC;
  78:     }
  79:     return(ptr);
  80: }
  81: 
  82: /*
  83:  *			R T _ F R E E
  84:  */
  85: void
  86: rt_free(ptr)
  87: char *ptr;
  88: {
  89:     register struct memdebug *mp = rt_mdb;
  90:     for( ; mp < &rt_mdb[MDB_SIZE]; mp++ )  {
  91:             if( mp->mdb_len <= 0 )  continue;
  92:         if( mp->mdb_addr != ptr )  continue;
  93:         {
  94:             register int *ip = (int *)(ptr+mp->mdb_len-sizeof(int));
  95:             if( *ip != MDB_MAGIC )  {
  96:                 syslog(LOG_ERR, "ERROR rt_free(x%x, %s) corrupted! x%x!=x%x\n", ptr, "???", *ip, MDB_MAGIC);
  97:                 abort();
  98:             }
  99:         }
 100:         mp->mdb_len = 0;    /* successful free */
 101:         goto ok;
 102:     }
 103:     syslog(LOG_ERR, "ERROR rt_free(x%x, %s) bad pointer!\n", ptr, "???");
 104:     abort();
 105: ok: ;
 106: 
 107:     *((int *)ptr) = -1; /* zappo! */
 108:     free(ptr);
 109: }
 110: 
 111: /*
 112:  *			R T _ P R M E M
 113:  *
 114:  *  Print map of memory currently in use.
 115:  */
 116: void
 117: rt_prmem(str)
 118: char *str;
 119: {
 120:     register struct memdebug *mp = rt_mdb;
 121:     register int *ip;
 122: 
 123:     printf("\nRT memory use\t\t%s\n", str);
 124:     for( ; mp < &rt_mdb[MDB_SIZE]; mp++ )  {
 125:         if( mp->mdb_len <= 0 )  continue;
 126:         ip = (int *)(mp->mdb_addr+mp->mdb_len-sizeof(int));
 127:         printf("%7x %5x %s %s\n",
 128:             mp->mdb_addr, mp->mdb_len, mp->mdb_str,
 129:             *ip!=MDB_MAGIC ? "-BAD-" : "" );
 130:         if( *ip != MDB_MAGIC )
 131:             printf("\t%x\t%x\n", *ip, MDB_MAGIC);
 132:     }
 133: }
 134: 
 135: char *
 136: calloc(num, size)
 137:     register unsigned num, size;
 138: {
 139:     extern char *malloc();
 140:     register char *p;
 141: 
 142:     size *= num;
 143:     if (p = rt_malloc(size))
 144:         bzero(p, size);
 145:     return (p);
 146: }
 147: 
 148: cfree(p, num, size)
 149:     char *p;
 150:     unsigned num;
 151:     unsigned size;
 152: {
 153:     rt_free(p);
 154: }
 155: 
 156: #if BSD < 43
 157: openlog() {}
 158: 
 159: syslog(x, str, a, b, c, d, e, f)
 160: int x;
 161: char    *str;
 162: int a, b, c, d, e, f;
 163: {
 164:     fprintf(stderr, str, a, b, c, d, e, f);
 165: }
 166: #endif BSD

Defined functions

cfree defined in line 148; never used
openlog defined in line 157; used 2 times
rt_free defined in line 85; used 1 times
rt_malloc defined in line 50; used 1 times
rt_prmem defined in line 116; never used

Defined variables

RCSstorage defined in line 25; never used
rt_mdb defined in line 45; used 6 times

Defined struct's

memdebug defined in line 41; used 6 times

Defined macros

LOG_ERR defined in line 33; used 4 times
MDB_MAGIC defined in line 40; used 6 times
MDB_SIZE defined in line 39; used 4 times
Last modified: 1994-01-01
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1401
Valid CSS Valid XHTML 1.0 Strict