home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdlib / MemDoTrace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-22  |  3.8 KB  |  147 lines

  1. /* 
  2.  * MemDoTrace.c --
  3.  *
  4.  *    Source code for the "MemDoTrace" procedure, which is used
  5.  *    internally by the memory allocator.  See memInt.h for overall
  6.  *    information about how the allocator works..
  7.  *
  8.  * Copyright 1988 Regents of the University of California
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software and its documentation for any purpose and without
  11.  * fee is hereby granted, provided that the above copyright
  12.  * notice appear in all copies.  The University of California
  13.  * makes no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without
  15.  * express or implied warranty.
  16.  */
  17.  
  18. #ifndef lint
  19. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/MemDoTrace.c,v 1.1 88/05/20 15:49:16 ouster Exp $ SPRITE (Berkeley)";
  20. #endif not lint
  21.  
  22. #define MEM_TRACE 1
  23.  
  24. #include "memInt.h"
  25.  
  26. /*
  27.  * The array below holds information about what to trace, and how.
  28.  */
  29.  
  30. MemTraceElement    memTraceArray[MAX_NUM_TRACE_SIZES];
  31. int        memNumSizesToTrace = 0;
  32.  
  33. /*
  34.  *----------------------------------------------------------------------
  35.  *
  36.  * PrintTrace --
  37.  *
  38.  *    Print out the given trace information about a memory trace record.
  39.  *
  40.  * Results:
  41.  *    None.
  42.  *
  43.  * Side effects:
  44.  *    None.
  45.  *
  46.  *----------------------------------------------------------------------
  47.  */
  48.  
  49. INTERNAL static void
  50. PrintTrace(allocated, infoPtr, curPC)
  51.     Boolean        allocated;    /* If TRUE, we are called by Mem_Alloc,
  52.                      * otherwise by Mem_Free. */
  53.     register Address    infoPtr;    /* Address of admin. info. */
  54.     Address        curPC;        /* If called by Mem_Free, PC of
  55.                      * call to Mem_Free, NULL otherwise. */
  56. {
  57.     if (allocated) {
  58.     (*memPrintProc)(memPrintData,
  59.         "malloc: PC=0x%x  addr=0x%x  size=%d\n",
  60.         GET_PC(infoPtr), infoPtr+sizeof(AdminInfo), 
  61.         GET_ORIG_SIZE(infoPtr));
  62.     } else {
  63.     (*memPrintProc)(memPrintData,
  64.         "free:  PC=0x%x  addr=0x%x  size=%d *\n",
  65.         curPC, infoPtr+sizeof(AdminInfo), GET_ORIG_SIZE(infoPtr));
  66.     }
  67. }
  68.  
  69. /*
  70.  *----------------------------------------------------------------------
  71.  *
  72.  * MemDoTrace --
  73.  *
  74.  *    Print and/or store a trace record.  Called by malloc and free.
  75.  *
  76.  * Results:
  77.  *    None.
  78.  *
  79.  * Side effects:
  80.  *    None.
  81.  *
  82.  *----------------------------------------------------------------------
  83.  */
  84.  
  85. void
  86. MemDoTrace(allocated, infoPtr, curPC, size)
  87.     Boolean        allocated;    /* If TRUE, we are called by malloc,
  88.                      * otherwise by free. */
  89.     register Address    infoPtr;    /* Address of admin. info. */
  90.     Address        curPC;        /* If called by free, PC of call to
  91.                      * free, NULL otherwise. */
  92.     int            size;        /* Size actually allocated. */
  93. {
  94.     int                i, j;
  95.     int                origSize;
  96.     Address            callerPC;
  97.     register MemTraceElement    *trPtr;
  98.  
  99.     if (memNumSizesToTrace == -1) {
  100.     PrintTrace(allocated, infoPtr, curPC);
  101.     return;
  102.     }
  103.  
  104.     callerPC = GET_PC(infoPtr);
  105.  
  106.     origSize = GET_ORIG_SIZE(infoPtr);
  107.  
  108.     for (i = 0, trPtr = memTraceArray; i < memNumSizesToTrace;
  109.         i++, trPtr++) {
  110.     if (trPtr->traceInfo.flags & MEM_DONT_USE_ORIG_SIZE) {
  111.         if (trPtr->traceInfo.size != size) {
  112.         continue;
  113.         }
  114.     } else if (trPtr->traceInfo.size != origSize) {
  115.         continue;
  116.     }
  117.     if (trPtr->traceInfo.flags & MEM_PRINT_TRACE) {
  118.         PrintTrace(allocated, infoPtr, curPC);
  119.     }
  120.     if (trPtr->traceInfo.flags & MEM_STORE_TRACE) {
  121.         if (trPtr->traceInfo.flags & MEM_TRACE_NOT_INIT) {
  122.         for (j = 0; j < MAX_CALLERS_TO_TRACE; j++) {
  123.             trPtr->allocInfo[j].numBlocks = 0;
  124.         }
  125.         trPtr->traceInfo.flags &= ~MEM_TRACE_NOT_INIT;
  126.         }
  127.         for (j = 0; j < MAX_CALLERS_TO_TRACE; j++) {
  128.         if (trPtr->allocInfo[j].numBlocks == 0) {
  129.             if (allocated) {
  130.             trPtr->allocInfo[j].callerPC = callerPC;
  131.             trPtr->allocInfo[j].numBlocks = 1;
  132.             }
  133.             break;
  134.         } else if (trPtr->allocInfo[j].callerPC == callerPC) {
  135.             if (allocated) {
  136.             trPtr->allocInfo[j].numBlocks++;
  137.             } else {
  138.             trPtr->allocInfo[j].numBlocks--;
  139.             }
  140.             break;
  141.         }
  142.         }
  143.     }
  144.     break;
  145.     }
  146. }
  147.