home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / bind / bind-4.001 / bind-4~ / bind-4.9.3-BETA9 / named / storage.c < prev    next >
C/C++ Source or Header  |  1993-09-07  |  6KB  |  208 lines

  1. /*
  2.  * ++Copyright++ 1985, 1989
  3.  * -
  4.  * Copyright (c) 1985, 1989
  5.  *    The Regents of the University of California.  All rights reserved.
  6.  * 
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  * 3. All advertising materials mentioning features or use of this software
  16.  *    must display the following acknowledgement:
  17.  *     This product includes software developed by the University of
  18.  *     California, Berkeley and its contributors.
  19.  * 4. Neither the name of the University nor the names of its contributors
  20.  *    may be used to endorse or promote products derived from this software
  21.  *    without specific prior written permission.
  22.  * 
  23.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33.  * SUCH DAMAGE.
  34.  * -
  35.  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  36.  * 
  37.  * Permission to use, copy, modify, and distribute this software for any
  38.  * purpose with or without fee is hereby granted, provided that the above
  39.  * copyright notice and this permission notice appear in all copies, and that
  40.  * the name of Digital Equipment Corporation not be used in advertising or
  41.  * publicity pertaining to distribution of the document or software without
  42.  * specific, written prior permission.
  43.  * 
  44.  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  45.  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  46.  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
  47.  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  48.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  49.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  50.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  51.  * SOFTWARE.
  52.  * -
  53.  * --Copyright--
  54.  */
  55.  
  56. #include <sys/param.h>
  57. #include <syslog.h>
  58. #include "../conf/portability.h"
  59. #include "../conf/options.h"
  60.  
  61. #ifdef DSTORAGE
  62. /*
  63.  *            S T O R A G E . C
  64.  *
  65.  * Ray Tracing program, storage manager.
  66.  *
  67.  *  Functions -
  68.  *    rt_malloc    Allocate storage, with visibility & checking
  69.  *    rt_free        Similarly, free storage
  70.  *    rt_prmem    When debugging, print memory map
  71.  *    calloc, cfree    Which call rt_malloc, rt_free
  72.  *
  73.  *  Author -
  74.  *    Michael John Muuss
  75.  *  
  76.  *  Source -
  77.  *    SECAD/VLD Computing Consortium, Bldg 394
  78.  *    The U. S. Army Ballistic Research Laboratory
  79.  *    Aberdeen Proving Ground, Maryland  21005-5066
  80.  *  
  81.  *  Copyright Notice -
  82.  *    This software is Copyright (C) 1987 by the United States Army.
  83.  *    All rights reserved.
  84.  */
  85. #ifndef lint
  86. static char RCSid[] = "$Id: storage.c,v 4.9.1.2 1993/09/08 00:01:17 vixie Exp $";
  87. #endif
  88.  
  89. #undef malloc
  90. #undef free
  91.  
  92. #define MDB_SIZE    20000
  93. #define MDB_MAGIC    0x12348969
  94. struct memdebug {
  95.     char    *mdb_addr;
  96.     char    *mdb_str;
  97.     int    mdb_len;
  98. } rt_mdb[MDB_SIZE];
  99.  
  100. /*
  101.  *            R T _ M A L L O C
  102.  */
  103. char *
  104. rt_malloc(cnt)
  105. unsigned int cnt;
  106. {
  107.     register char *ptr;
  108.  
  109.     cnt = (cnt+2*sizeof(int)-1)&(~(sizeof(int)-1));
  110.     ptr = malloc(cnt);
  111.  
  112.     if( ptr==(char *)0 ) {
  113.         syslog(LOG_ERR, "rt_malloc: malloc failure");
  114.         abort();
  115.     } else     {
  116.         register struct memdebug *mp = rt_mdb;
  117.         for( ; mp < &rt_mdb[MDB_SIZE]; mp++ )  {
  118.             if( mp->mdb_len > 0 )  continue;
  119.             mp->mdb_addr = ptr;
  120.             mp->mdb_len = cnt;
  121.             mp->mdb_str = "???";
  122.             goto ok;
  123.         }
  124.         syslog(LOG_ERR, "rt_malloc:  memdebug overflow\n");
  125.     }
  126. ok:    ;
  127.     {
  128.         register int *ip = (int *)(ptr+cnt-sizeof(int));
  129.         *ip = MDB_MAGIC;
  130.     }
  131.     return(ptr);
  132. }
  133.  
  134. /*
  135.  *            R T _ F R E E
  136.  */
  137. void
  138. rt_free(ptr)
  139. char *ptr;
  140. {
  141.     register struct memdebug *mp = rt_mdb;
  142.     for( ; mp < &rt_mdb[MDB_SIZE]; mp++ )  {
  143.             if( mp->mdb_len <= 0 )  continue;
  144.         if( mp->mdb_addr != ptr )  continue;
  145.         {
  146.             register int *ip = (int *)(ptr+mp->mdb_len-sizeof(int));
  147.             if( *ip != MDB_MAGIC )  {
  148.                 syslog(LOG_ERR, "ERROR rt_free(x%x, %s) corrupted! x%x!=x%x\n", ptr, "???", *ip, MDB_MAGIC);
  149.                 abort();
  150.             }
  151.         }
  152.         mp->mdb_len = 0;    /* successful free */
  153.         goto ok;
  154.     }
  155.     syslog(LOG_ERR, "ERROR rt_free(x%x, %s) bad pointer!\n", ptr, "???");
  156.     abort();
  157. ok:    ;
  158.  
  159.     *((int *)ptr) = -1;    /* zappo! */
  160.     free(ptr);
  161. }
  162.  
  163. /*
  164.  *            R T _ P R M E M
  165.  * 
  166.  *  Print map of memory currently in use.
  167.  */
  168. void
  169. rt_prmem(str)
  170. char *str;
  171. {
  172.     register struct memdebug *mp = rt_mdb;
  173.     register int *ip;
  174.  
  175.     printf("\nRT memory use\t\t%s\n", str);
  176.     for( ; mp < &rt_mdb[MDB_SIZE]; mp++ )  {
  177.         if( mp->mdb_len <= 0 )  continue;
  178.         ip = (int *)(mp->mdb_addr+mp->mdb_len-sizeof(int));
  179.         printf("%7x %5x %s %s\n",
  180.             mp->mdb_addr, mp->mdb_len, mp->mdb_str,
  181.             *ip!=MDB_MAGIC ? "-BAD-" : "" );
  182.         if( *ip != MDB_MAGIC )
  183.             printf("\t%x\t%x\n", *ip, MDB_MAGIC);
  184.     }
  185. }
  186.  
  187. char *
  188. calloc(num, size)
  189.     register unsigned num, size;
  190. {
  191.     register char *p;
  192.  
  193.     size *= num;
  194.     if (p = rt_malloc(size))
  195.         bzero(p, size);
  196.     return (p);
  197. }
  198.  
  199. cfree(p, num, size)
  200.     char *p;
  201.     unsigned num;
  202.     unsigned size;
  203. {
  204.     rt_free(p);
  205. }
  206.  
  207. #endif /*DSTORAGE*/
  208.