Debian Codegraph @

gc/extra/MacOS.c

raw
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 02/10/96 pcb Added routine to perform a final collection when
12 unloading shared library.
13
14 by Patrick C. Beard.
15 */
16 /* Boehm, February 15, 1996 2:55 pm PST */
17
18 #include <Resources.h>
19 #include <Memory.h>
20 #include <LowMem.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #define GC_BUILD
26 #include "gc.h"
27 #include "private/gc_priv.h"
28
29 /* use 'CODE' resource 0 to get exact location of the beginning of global space. */
30
31 typedef struct {
32 unsigned long aboveA5;
33 unsigned long belowA5;
34 unsigned long JTSize;
35 unsigned long JTOffset;
36 } *CodeZeroPtr, **CodeZeroHandle;
37
38 void* GC_MacGetDataStart(void)
39 {
40 CodeZeroHandle code0 = (CodeZeroHandle)GetResource('CODE', 0);
41 if (code0) {
42 long belowA5Size = (**code0).belowA5;
43 ReleaseResource((Handle)code0);
44 return (LMGetCurrentA5() - belowA5Size);
45 }
46 fprintf(stderr, "Couldn't load the jump table.");
47 exit(-1);
48 # if !defined(CPPCHECK)
49 return 0; /* to avoid compiler complain about missing return */
50 # endif
51 }
52
53 #ifdef USE_TEMPORARY_MEMORY
54
55 /* track the use of temporary memory so it can be freed all at once. */
56
57 typedef struct TemporaryMemoryBlock TemporaryMemoryBlock, **TemporaryMemoryHandle;
58
59 struct TemporaryMemoryBlock {
60 TemporaryMemoryHandle nextBlock;
61 char data[];
62 };
63
64 static TemporaryMemoryHandle theTemporaryMemory = NULL;
65
66 void GC_MacFreeTemporaryMemory(void);
67
68 Ptr GC_MacTemporaryNewPtr(size_t size, Boolean clearMemory)
69 {
70 # if !defined(SHARED_LIBRARY_BUILD)
71 static Boolean firstTime = true;
72 # endif
73 OSErr result;
74 TemporaryMemoryHandle tempMemBlock;
75 Ptr tempPtr = nil;
76
77 tempMemBlock = (TemporaryMemoryHandle)TempNewHandle(size + sizeof(TemporaryMemoryBlock), &result);
78 if (tempMemBlock && result == noErr) {
79 HLockHi((Handle)tempMemBlock);
80 tempPtr = (**tempMemBlock).data;
81 if (clearMemory) memset(tempPtr, 0, size);
82 tempPtr = StripAddress(tempPtr);
83
84 /* keep track of the allocated blocks. */
85 (**tempMemBlock).nextBlock = theTemporaryMemory;
86 theTemporaryMemory = tempMemBlock;
87 }
88
89 # if !defined(SHARED_LIBRARY_BUILD)
90 /* install an exit routine to clean up the memory used at the end. */
91 if (firstTime) {
92 atexit(&GC_MacFreeTemporaryMemory);
93 firstTime = false;
94 }
95 # endif
96
97 return tempPtr;
98 }
99
100 static void perform_final_collection(void)
101 {
102 unsigned i;
103 word last_fo_entries = 0;
104
105 /* adjust the stack bottom, because CFM calls us from another stack
106 location. */
107 GC_stackbottom = (ptr_t)&i;
108
109 /* try to collect and finalize everything in sight */
110 for (i = 0; i < 2 || GC_fo_entries < last_fo_entries; i++) {
111 last_fo_entries = GC_fo_entries;
112 GC_gcollect();
113 }
114 }
115
116
117 void GC_MacFreeTemporaryMemory(void)
118 {
119 # if defined(SHARED_LIBRARY_BUILD)
120 /* if possible, collect all memory, and invoke all finalizers. */
121 perform_final_collection();
122 # endif
123
124 if (theTemporaryMemory != NULL) {
125 # if !defined(SHARED_LIBRARY_BUILD)
126 long totalMemoryUsed = 0;
127 # endif
128 TemporaryMemoryHandle tempMemBlock = theTemporaryMemory;
129 while (tempMemBlock /* != NULL */) {
130 TemporaryMemoryHandle nextBlock = (**tempMemBlock).nextBlock;
131 # if !defined(SHARED_LIBRARY_BUILD)
132 totalMemoryUsed += GetHandleSize((Handle)tempMemBlock);
133 # endif
134 DisposeHandle((Handle)tempMemBlock);
135 tempMemBlock = nextBlock;
136 }
137 theTemporaryMemory = NULL;
138
139 # if !defined(SHARED_LIBRARY_BUILD)
140 if (GC_print_stats) {
141 fprintf(stdout, "[total memory used: %ld bytes.]\n",
142 totalMemoryUsed);
143 fprintf(stdout, "[total collections: %lu]\n",
144 (unsigned long)GC_gc_no);
145 }
146 # endif
147 }
148 }
149
150 #endif /* USE_TEMPORARY_MEMORY */
151
152 #if __option(far_data)
153
154 void* GC_MacGetDataEnd(void)
155 {
156 CodeZeroHandle code0 = (CodeZeroHandle)GetResource('CODE', 0);
157 if (code0) {
158 long aboveA5Size = (**code0).aboveA5;
159 ReleaseResource((Handle)code0);
160 return (LMGetCurrentA5() + aboveA5Size);
161 }
162 fprintf(stderr, "Couldn't load the jump table.");
163 exit(-1);
164 # if !defined(CPPCHECK)
165 return 0;
166 # endif
167 }
168
169 #endif /* __option(far_data) */