home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / SYSTEM / GC / MACOS.C < prev    next >
C/C++ Source or Header  |  1994-12-02  |  3KB  |  99 lines

  1. /*
  2.     MacOS.c
  3.     
  4.     Some routines for the Macintosh OS port of the Hans-J. Boehm, Alan J. Demers
  5.     garbage collector.
  6.     
  7.     <Revision History>
  8.     
  9.     11/22/94  pcb  StripAddress the temporary memory handle for 24-bit mode.
  10.     11/30/94  pcb  Tracking all memory usage so we can deallocate it all at once.
  11.     
  12.     by Patrick C. Beard.
  13.  */
  14. /* Boehm, July 28, 1994 10:35 am PDT */
  15.  
  16. #include <Resources.h>
  17. #include <Memory.h>
  18. #include <LowMem.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. // use 'CODE' resource 0 to get exact location of the beginning of global space.
  24.  
  25. typedef struct {
  26.     unsigned long aboveA5;
  27.     unsigned long belowA5;
  28.     unsigned long JTSize;
  29.     unsigned long JTOffset;
  30. } *CodeZeroPtr, **CodeZeroHandle;
  31.  
  32. void* GC_MacGetDataStart()
  33. {
  34.     CodeZeroHandle code0 = (CodeZeroHandle)GetResource('CODE', 0);
  35.     if (code0) {
  36.         long belowA5Size = (**code0).belowA5;
  37.         ReleaseResource((Handle)code0);
  38.         return (LMGetCurrentA5() - belowA5Size);
  39.     }
  40.     fprintf(stderr, "Couldn't load the jump table.");
  41.     exit(-1);
  42.     return 0;
  43. }
  44.  
  45. /* track the use of temporary memory so it can be freed all at once. */
  46.  
  47. typedef struct TemporaryMemoryBlock TemporaryMemoryBlock, **TemporaryMemoryHandle;
  48.  
  49. struct TemporaryMemoryBlock {
  50.     TemporaryMemoryHandle nextBlock;
  51.     char data[];
  52. };
  53.  
  54. TemporaryMemoryHandle theTemporaryMemory = NULL;
  55.  
  56. static void GC_MacFreeTemporaryMemory(void);
  57.  
  58. Ptr GC_MacTemporaryNewPtr(Size size, Boolean clearMemory)
  59. {
  60.     static Boolean firstTime = true;
  61.     OSErr result;
  62.     TemporaryMemoryHandle tempMemBlock;
  63.     Ptr tempPtr = nil;
  64.  
  65.     tempMemBlock = (TemporaryMemoryHandle)TempNewHandle(size + sizeof(TemporaryMemoryBlock), &result);
  66.     if (tempMemBlock && result == noErr) {
  67.         HLockHi((Handle)tempMemBlock);
  68.         tempPtr = (**tempMemBlock).data;
  69.         if (clearMemory) memset(tempPtr, 0, size);
  70.         tempPtr = StripAddress(tempPtr);
  71.  
  72.         // keep track of the allocated blocks.
  73.         (**tempMemBlock).nextBlock = theTemporaryMemory;
  74.         theTemporaryMemory = tempMemBlock;
  75.     }
  76.     
  77.     // install an exit routine to clean up the memory used at the end.
  78.     if (firstTime) {
  79.         atexit(&GC_MacFreeTemporaryMemory);
  80.         firstTime = false;
  81.     }
  82.     
  83.     return tempPtr;
  84. }
  85.  
  86. static void GC_MacFreeTemporaryMemory()
  87. {
  88.     long totalMemoryUsed = 0;
  89.     TemporaryMemoryHandle tempMemBlock = theTemporaryMemory;
  90.     while (tempMemBlock != NULL) {
  91.         TemporaryMemoryHandle nextBlock = (**tempMemBlock).nextBlock;
  92.         totalMemoryUsed += GetHandleSize((Handle)tempMemBlock);
  93.         DisposeHandle((Handle)tempMemBlock);
  94.         tempMemBlock = nextBlock;
  95.     }
  96.     theTemporaryMemory = NULL;
  97.     fprintf(stdout, "[total memory used:  %ld bytes.]\n", totalMemoryUsed);
  98. }
  99.