home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbmalloc.zip / xheap.c < prev    next >
C/C++ Source or Header  |  1993-01-04  |  4KB  |  129 lines

  1.  
  2. /*
  3.  * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  4.  *
  5.  * This software may be distributed freely as long as the following conditions
  6.  * are met:
  7.  *         * the distribution, or any derivative thereof, may not be
  8.  *          included as part of a commercial product
  9.  *        * full source code is provided including this copyright
  10.  *        * there is no charge for the software itself (there may be
  11.  *          a minimal charge for the copying or distribution effort)
  12.  *        * this copyright notice is not modified or removed from any
  13.  *          source file
  14.  */
  15. #ifndef lint
  16. static char rcs_hdr[] = "$Id: xheap.c,v 1.4 1992/08/22 16:27:13 cpcahil Exp $";
  17. #endif
  18.  
  19. /***********************************************************
  20. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  21. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  22.  
  23.                         All Rights Reserved
  24.  
  25. Permission to use, copy, modify, and distribute this software and its 
  26. documentation for any purpose and without fee is hereby granted, 
  27. provided that the above copyright notice appear in all copies and that
  28. both that copyright notice and this permission notice appear in 
  29. supporting documentation, and that the names of Digital or MIT not be
  30. used in advertising or publicity pertaining to distribution of the
  31. software without specific, written prior permission.  
  32.  
  33. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  34. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  35. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  36. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  37. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  38. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  39. SOFTWARE.
  40.  
  41. ******************************************************************/
  42.  
  43. /*
  44.  * X Toolkit Memory Allocation Routines
  45.  *
  46.  * Uses Xlib memory management, which is spec'd to be re-entrant.
  47.  */
  48.  
  49. #if FOUND_X_INTRINSIC
  50. #include "X11/Intrinsic.h"
  51. #endif
  52.  
  53. #include "mallocin.h"
  54.  
  55. #ifndef NULL
  56. #define NULL ((char *)0)
  57. #endif
  58.  
  59. void _XtHeapInit(heap)
  60.     Heap*    heap;
  61. {
  62.     heap->start = NULL;
  63.     heap->bytes_remaining = 0;
  64. }
  65.  
  66. #ifndef HEAP_SEGMENT_SIZE
  67. #define HEAP_SEGMENT_SIZE 1492
  68. #endif
  69.  
  70. char* _XtHeapAlloc(heap, bytes)
  71.     Heap*    heap;
  72.     Cardinal    bytes;
  73. {
  74.     register char* heap_loc;
  75.     if (heap == (Heap *) NULL) return XtMalloc(bytes);
  76.     if (heap->bytes_remaining < (int)bytes) {
  77.     if ((bytes + sizeof(char*)) >= (HEAP_SEGMENT_SIZE>>1)) {
  78.         /* preserve current segment; insert this one in front */
  79. #ifdef _TRACE_HEAP
  80.         printf( "allocating large segment (%d bytes) on heap %#x\n",
  81.             bytes, heap );
  82. #endif
  83.         heap_loc = XtMalloc(bytes + sizeof(char*));
  84.         if (heap->start) {
  85.         *(char**)heap_loc = *(char**)heap->start;
  86.         *(char**)heap->start = heap_loc;
  87.         }
  88.         else {
  89.         *(char**)heap_loc = NULL;
  90.         heap->start = heap_loc;
  91.         }
  92.         return heap_loc + sizeof(char*);
  93.     }
  94.     /* else discard remainder of this segment */
  95. #ifdef _TRACE_HEAP
  96.     printf( "allocating new segment on heap %#x\n", heap );
  97. #endif
  98.     heap_loc = XtMalloc((unsigned)HEAP_SEGMENT_SIZE);
  99.     *(char**)heap_loc = heap->start;
  100.     heap->start = heap_loc;
  101.     heap->current = heap_loc + sizeof(char*);
  102.     heap->bytes_remaining = HEAP_SEGMENT_SIZE - sizeof(char*);
  103.     }
  104. #ifdef WORD64
  105.     /* round to nearest 8-byte boundary */
  106.     bytes = (bytes + 7) & (~7);
  107. #else
  108.     /* round to nearest 4-byte boundary */
  109.     bytes = (bytes + 3) & (~3);
  110. #endif /* WORD64 */
  111.     heap_loc = heap->current;
  112.     heap->current += bytes;
  113.     heap->bytes_remaining -= bytes; /* can be negative, if rounded */
  114.     return heap_loc;
  115. }
  116.  
  117. void _XtHeapFree(heap)
  118.     Heap*    heap;
  119. {
  120.     char* segment = heap->start;
  121.     while (segment != NULL) {
  122.     char* next_segment = *(char**)segment;
  123.     XtFree(segment);
  124.     segment = next_segment;
  125.     }
  126.     heap->start = NULL;
  127.     heap->bytes_remaining = 0;
  128. }
  129.