home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xt / Alloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  9.3 KB  |  422 lines

  1. /* $XConsortium: Alloc.c,v 1.47 92/02/21 13:11:23 converse Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  5. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  6.  
  7.                         All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its 
  10. documentation for any purpose and without fee is hereby granted, 
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in 
  13. supporting documentation, and that the names of Digital or MIT not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.  
  16.  
  17. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  19. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27. /*
  28.  * X Toolkit Memory Allocation Routines
  29.  *
  30.  * Uses Xlib memory management, which is spec'd to be re-entrant.
  31.  */
  32.  
  33. #include "IntrinsicI.h"
  34. #undef _XBCOPYFUNC
  35.  
  36. #ifndef X_NOT_STDC_ENV
  37. #include <stdlib.h>
  38. #else
  39. char *malloc(), *realloc(), *calloc();
  40. #endif
  41. #if defined(macII) && !defined(__STDC__)  /* stdlib.h fails to define these */
  42. char *malloc(), *realloc(), *calloc();
  43. #endif /* macII */
  44.  
  45. #ifdef MALLOC_0_RETURNS_NULL
  46. #define Xmalloc(size) malloc(((size) > 0 ? (size) : 1))
  47. #define Xrealloc(ptr, size) realloc((ptr), ((size) > 0 ? (size) : 1))
  48. #define Xcalloc(nelem, elsize) calloc(((nelem) > 0 ? (nelem) : 1), (elsize))
  49. #else
  50. #define Xmalloc(size) malloc((size))
  51. #define Xrealloc(ptr, size) realloc((ptr), (size))
  52. #define Xcalloc(nelem, elsize) calloc((nelem), (elsize))
  53. #endif
  54. #define Xfree(ptr) free(ptr)
  55.  
  56. #ifdef _XNEEDBCOPYFUNC
  57. void _XtBCopy(b1, b2, length)
  58.     register char *b1, *b2;
  59.     register length;
  60. {
  61.     if (b1 < b2) {
  62.     b2 += length;
  63.     b1 += length;
  64.     while (length--)
  65.         *--b2 = *--b1;
  66.     } else {
  67.     while (length--)
  68.         *b2++ = *b1++;
  69.     }
  70. }
  71. #endif
  72.  
  73. void _XtAllocError(type)
  74.     String type;
  75. {
  76.     Cardinal num_params = 1;
  77.     if (type == NULL) type = "local memory allocation";
  78.     XtErrorMsg("allocError", type, XtCXtToolkitError,
  79.            "Cannot perform %s", &type, &num_params);
  80. }
  81.  
  82. void _XtHeapInit(heap)
  83.     Heap*    heap;
  84. {
  85.     heap->start = NULL;
  86.     heap->bytes_remaining = 0;
  87. }
  88.  
  89. #ifndef XTTRACEMEMORY
  90.  
  91. char *XtMalloc(size)
  92.     unsigned size;
  93. {
  94.     char *ptr;
  95.     if ((ptr = Xmalloc(size)) == NULL)
  96.         _XtAllocError("malloc");
  97.  
  98.     return(ptr);
  99. }
  100.  
  101. char *XtRealloc(ptr, size)
  102.     char     *ptr;
  103.     unsigned size;
  104. {
  105.    if (ptr == NULL) return(XtMalloc(size));
  106.    else if ((ptr = Xrealloc(ptr, size)) == NULL)
  107.     _XtAllocError("realloc");
  108.  
  109.    return(ptr);
  110. }
  111.  
  112. char *XtCalloc(num, size)
  113.     unsigned num, size;
  114. {
  115.     char *ptr;
  116.  
  117. #ifdef MALLOC_0_RETURNS_NULL
  118.     if (!size)
  119.     num = size = 1;
  120. #endif
  121.     if ((ptr = Xcalloc(num, size)) == NULL)
  122.     _XtAllocError("calloc");
  123.  
  124.     return(ptr);
  125. }
  126.  
  127. void XtFree(ptr)
  128.     char *ptr;
  129. {
  130.    if (ptr != NULL) Xfree(ptr);
  131. }
  132.  
  133. #ifndef HEAP_SEGMENT_SIZE
  134. #define HEAP_SEGMENT_SIZE 1492
  135. #endif
  136.  
  137. char* _XtHeapAlloc(heap, bytes)
  138.     Heap*    heap;
  139.     Cardinal    bytes;
  140. {
  141.     register char* heap_loc;
  142.     if (heap == NULL) return XtMalloc(bytes);
  143.     if (heap->bytes_remaining < (int)bytes) {
  144.     if ((bytes + sizeof(char*)) >= (HEAP_SEGMENT_SIZE>>1)) {
  145.         /* preserve current segment; insert this one in front */
  146. #ifdef _TRACE_HEAP
  147.         printf( "allocating large segment (%d bytes) on heap %#x\n",
  148.             bytes, heap );
  149. #endif
  150.         heap_loc = XtMalloc(bytes + sizeof(char*));
  151.         if (heap->start) {
  152.         *(char**)heap_loc = *(char**)heap->start;
  153.         *(char**)heap->start = heap_loc;
  154.         }
  155.         else {
  156.         *(char**)heap_loc = NULL;
  157.         heap->start = heap_loc;
  158.         }
  159.         return heap_loc + sizeof(char*);
  160.     }
  161.     /* else discard remainder of this segment */
  162. #ifdef _TRACE_HEAP
  163.     printf( "allocating new segment on heap %#x\n", heap );
  164. #endif
  165.     heap_loc = XtMalloc((unsigned)HEAP_SEGMENT_SIZE);
  166.     *(char**)heap_loc = heap->start;
  167.     heap->start = heap_loc;
  168.     heap->current = heap_loc + sizeof(char*);
  169.     heap->bytes_remaining = HEAP_SEGMENT_SIZE - sizeof(char*);
  170.     }
  171. #ifdef WORD64
  172.     /* round to nearest 8-byte boundary */
  173.     bytes = (bytes + 7) & (~7);
  174. #else
  175.     /* round to nearest 4-byte boundary */
  176.     bytes = (bytes + 3) & (~3);
  177. #endif /* WORD64 */
  178.     heap_loc = heap->current;
  179.     heap->current += bytes;
  180.     heap->bytes_remaining -= bytes; /* can be negative, if rounded */
  181.     return heap_loc;
  182. }
  183.  
  184. void _XtHeapFree(heap)
  185.     Heap*    heap;
  186. {
  187.     char* segment = heap->start;
  188.     while (segment != NULL) {
  189.     char* next_segment = *(char**)segment;
  190.     XtFree(segment);
  191.     segment = next_segment;
  192.     }
  193.     heap->start = NULL;
  194.     heap->bytes_remaining = 0;
  195. }
  196.  
  197. #else
  198.  
  199. /*
  200.  * X Toolkit Memory Trace Allocation Routines
  201.  */
  202.  
  203. #undef XtMalloc
  204. #undef XtRealloc
  205. #undef XtCalloc
  206. #undef XtFree
  207.  
  208. typedef struct _Stats *StatsPtr;
  209. typedef struct _Stats {
  210.     StatsPtr prev, next;
  211.     char *file;
  212.     int line;
  213.     unsigned size;
  214.     unsigned long seq;
  215.     XtPointer heap;
  216. } Stats;
  217.  
  218. static StatsPtr XtMemory = (StatsPtr)NULL;
  219. static unsigned long ActiveXtMemory = 0;
  220. static unsigned long XtSeqId = 0;
  221. static unsigned long XtSeqBreakpoint = ~0;
  222.  
  223. #define StatsSize(n) ((((n) + 3) & ~3) + sizeof(Stats))
  224. #define ToStats(ptr) ((StatsPtr)(ptr - sizeof(Stats)))
  225. #define ToMem(ptr) (((char *)ptr) + sizeof(Stats))
  226.  
  227. #define CHAIN(ptr,len,hp) \
  228.     ptr->next = XtMemory; \
  229.     if (XtMemory) \
  230.         XtMemory->prev = ptr; \
  231.     XtMemory = ptr; \
  232.     ptr->prev = (StatsPtr)NULL; \
  233.     ptr->file = file; \
  234.     ptr->line = line; \
  235.     ptr->size = len; \
  236.     ptr->heap = hp; \
  237.     if (file) \
  238.     ActiveXtMemory += len; \
  239.     ptr->seq = XtSeqId; \
  240.     if (XtSeqId == XtSeqBreakpoint) \
  241.     _XtBreakpoint(ptr); \
  242.     XtSeqId++
  243.  
  244. /*ARGUSED*/
  245. static void _XtBreakpoint(mem)
  246.     StatsPtr mem;
  247. {
  248.     mem->seq = XtSeqId; /* avoid being optimized out of existence */
  249. }
  250.  
  251. char *_XtMalloc(size, file, line)
  252.     unsigned size;
  253.     char *file;
  254.     int line;
  255. {
  256.     StatsPtr ptr;
  257.     unsigned newsize;
  258.  
  259.     newsize = StatsSize(size);
  260.     if ((ptr = (StatsPtr)Xmalloc(newsize)) == NULL)
  261.         _XtAllocError("malloc");
  262.     CHAIN(ptr, size, NULL);
  263.     return(ToMem(ptr));
  264. }
  265.  
  266. char *XtMalloc(size)
  267.     unsigned size;
  268. {
  269.     return _XtMalloc(size, (char *)NULL, 0);
  270. }
  271.  
  272. char *_XtRealloc(ptr, size, file, line)
  273.     char     *ptr;
  274.     unsigned size;
  275.     char *file;
  276.     int line;
  277. {
  278.    char *newptr;
  279.  
  280.    newptr = _XtMalloc(size, file, line);
  281.    if (ptr) {
  282.        unsigned copysize = ToStats(ptr)->size;
  283.        if (copysize > size) copysize = size;
  284.        bcopy(ptr, newptr, copysize);
  285.        _XtFree(ptr);
  286.    }
  287.    return(newptr);
  288. }
  289.  
  290. char *XtRealloc(ptr, size)
  291.     char     *ptr;
  292.     unsigned size;
  293. {
  294.     return _XtRealloc(ptr, size, (char *)NULL, 0);
  295. }
  296.  
  297. char *_XtCalloc(num, size, file, line)
  298.     unsigned num, size;
  299.     char *file;
  300.     int line;
  301. {
  302.     StatsPtr ptr;
  303.     unsigned total, newsize;
  304.  
  305.     total = num * size;
  306.     newsize = StatsSize(total);
  307.     if ((ptr = (StatsPtr)Xcalloc(newsize, 1)) == NULL)
  308.         _XtAllocError("calloc");
  309.     CHAIN(ptr, total, NULL);
  310.     return(ToMem(ptr));
  311. }
  312.  
  313. char *XtCalloc(num, size)
  314.     unsigned num, size;
  315. {
  316.     return _XtCalloc(num, size, (char *)NULL, 0);
  317. }
  318.  
  319. Boolean _XtIsValidPointer(ptr)
  320.     char *ptr;
  321. {
  322.     register StatsPtr mem;
  323.     register StatsPtr stp = ToStats(ptr);
  324.  
  325.     for (mem = XtMemory; mem; mem = mem->next) {
  326.     if (mem == stp)
  327.         return True;
  328.     }
  329.     return False;
  330. }
  331.  
  332. Boolean _XtValidateMemory = False;
  333.  
  334. void _XtFree(ptr)
  335.     char *ptr;
  336. {
  337.    register StatsPtr stp;
  338.  
  339.    if (ptr) {
  340.        if (_XtValidateMemory && !_XtIsValidPointer(ptr))
  341.        abort();
  342.        stp = ToStats(ptr);
  343.        if (stp->file)
  344.        ActiveXtMemory -= stp->size;
  345.        if (stp->prev)
  346.        stp->prev->next = stp->next;
  347.        else
  348.        XtMemory = stp->next;
  349.        if (stp->next)
  350.        stp->next->prev = stp->prev;
  351.        Xfree((char *)stp);
  352.    }
  353. }
  354.  
  355. void XtFree(ptr)
  356.     char *ptr;
  357. {
  358.    _XtFree(ptr);
  359. }
  360.  
  361. char *_XtHeapMalloc(heap, size, file, line)
  362.     Heap *heap;
  363.     Cardinal size;
  364.     char *file;
  365.     int line;
  366. {
  367.     StatsPtr ptr;
  368.     unsigned newsize;
  369.     XtPointer hp = (XtPointer) heap;
  370.  
  371.     newsize = StatsSize(size);
  372.     if ((ptr = (StatsPtr)Xmalloc(newsize)) == NULL)
  373.         _XtAllocError("malloc");
  374.     CHAIN(ptr, size, hp);
  375.     return(ToMem(ptr));
  376. }
  377.  
  378. void _XtHeapFree(heap)
  379.     register XtPointer heap;
  380. {
  381.     register StatsPtr mem, next;
  382.  
  383.     for (mem = XtMemory; mem; mem = next) {
  384.     next = mem->next;
  385.     if (mem->heap == heap) {
  386.         if (mem->file)
  387.         ActiveXtMemory -= mem->size;
  388.         if (mem->prev)
  389.         mem->prev->next = next;
  390.         else
  391.         XtMemory = next;
  392.         if (next)
  393.         next->prev = mem->prev;
  394.         Xfree((char *)mem);
  395.     }
  396.     }
  397. }
  398.  
  399. #include <stdio.h>
  400.  
  401. void _XtPrintMemory(filename)
  402. char * filename;
  403. {
  404.     register StatsPtr mem;
  405.     FILE *f;
  406.  
  407.     if (filename == NULL)
  408.     f = stderr;
  409.     else 
  410.     f = fopen(filename, "w");
  411.     fprintf(f, "total size: %d\n", ActiveXtMemory);
  412.     for (mem = XtMemory; mem; mem = mem->next) {
  413.     if (mem->file)
  414.         fprintf(f, "size: %6d  seq: %5d  %12s(%4d)  %s\n",
  415.             mem->size, mem->seq,
  416.             mem->file, mem->line, mem->heap ? "heap" : "");
  417.     }
  418.     if (filename) fclose(f);
  419. }
  420.  
  421. #endif  /* XTTRACEMEMORY */
  422.