a68g-mem.c
1 //! @file a68g-mem.c
2 //! @author J. Marcel van der Veer
3
4 //! @section Copyright
5 //!
6 //! This file is part of Algol68G - an Algol 68 compiler-interpreter.
7 //! Copyright 2001-2025 J. Marcel van der Veer [algol68g@xs4all.nl].
8
9 //! @section License
10 //!
11 //! This program is free software; you can redistribute it and/or modify it
12 //! under the terms of the GNU General Public License as published by the
13 //! Free Software Foundation; either version 3 of the License, or
14 //! (at your option) any later version.
15 //!
16 //! This program is distributed in the hope that it will be useful, but
17 //! WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 //! or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 //! more details. You should have received a copy of the GNU General Public
20 //! License along with this program. If not, see [http://www.gnu.org/licenses/].
21
22 //! @section Synopsis
23 //!
24 //! Low-level memory management.
25
26 // A68G memory is organised following below schema.
27 //
28 // +------------+----------+-----------+---------+-------------+----------------+
29 // | HEAP | | STACK |
30 // | FIXED HEAP | A68 HEAP | TEMP HEAP | HANDLES | FRAME STACK | ARGUMENT STACK |
31 // +------------+----------+-----------+---------+-------------+----------------+
32 // ^ ^ ^
33 // A68G_HP A68G_FP A68G_SP
34 //
35 // The HEAP starts at the lowest address, and the ARGUMENT STACK ends at the highest address.
36 //
37 // The FIXED HEAP contains permanent data stored during compilation.
38 // The A68 HEAP contains data stored by HEAP generators in the running A68 program.
39 // The TEMP HEAP stores temporary run-time data in the heap.
40 // HANDLES store A68 REFs. This way the heap can be swept and compacted.
41 //
42 // After compilation, the 'heap pointer' starts where 'fixed heap pointer' ended.
43 //
44 // The 'heap pointer' counts up and the 'temp heap pointer' counts down.
45 // Should they collide, the heap is full.
46 // This way there is no need to reserve space for the temp heap a priori.
47 // The STACK could follow the same mechanism to have a joined space for
48 // the FRAME STACK and ARGUMENT STACK, but is not implemented like that.
49 //
50 // The FRAME STACK contains local variables according the lexical levels.
51 // The ARGUMENT STACK is for evaluation of expressions and procedure arguments/results.
52 //
53 // The garbage collector only acts in between units.
54 // Care must be taken to not sweep intermediate HEAP data pointed to from the STACK.
55 // Note that A68G stores row elements in the heap.
56 // If a row is local, only the descriptor is in the FRAME STACK.
57
58 #include "a68g.h"
59 #include "a68g-prelude.h"
60
61 //! Reasonable limits for chunk sizes.
62
63 void storage_limit (size_t n)
64 {
65 ABEND ((n) > MAX_MEM_SIZE, ERROR_MEMORY_FULL, __func__);
66 }
67
68 //! @brief Initialise C and A68 heap management.
69
70 void init_heap (void)
71 {
72 // Note that some overhead is allowed for every size that can be specified.
73 size_t heap_a_size = A68G_ALIGN (A68G (heap_size) + A68G (storage_overhead));
74 size_t handle_a_size = A68G_ALIGN (A68G (handle_pool_size) + A68G (storage_overhead));
75 size_t frame_a_size = A68G_ALIGN (A68G (frame_stack_size) + A68G (storage_overhead));
76 size_t expr_a_size = A68G_ALIGN (A68G (expr_stack_size) + A68G (storage_overhead));
77 storage_limit (heap_a_size);
78 storage_limit (handle_a_size);
79 storage_limit (frame_a_size);
80 storage_limit (expr_a_size);
81 size_t total_size = A68G_ALIGN (heap_a_size + handle_a_size + frame_a_size + expr_a_size);
82 storage_limit (total_size);
83 // Reserve the space.
84 errno = 0;
85 BYTE_T *core = (BYTE_T *) (A68G_ALIGN_T *) a68g_alloc (total_size, __func__, __LINE__);
86 if (core == NO_BYTE || errno != 0) {
87 ABEND (A68G_TRUE, ERROR_MEMORY_FULL, __func__);
88 }
89 // Initialise.
90 A68G_HEAP = NO_BYTE;
91 A68G_HANDLES = NO_BYTE;
92 A68G_STACK = NO_BYTE;
93 A68G_SP = 0;
94 A68G_FP = 0;
95 A68G_HP = 0;
96 A68G_GLOBALS = 0;
97 A68G_HEAP = & (core[0]);
98 A68G_HANDLES = & (A68G_HEAP[heap_a_size]);
99 A68G_STACK = & (A68G_HANDLES[handle_a_size]);
100 A68G (fixed_heap_pointer) = A68G_ALIGNMENT;
101 A68G (temp_heap_pointer) = heap_a_size;
102 A68G (frame_start) = 0;
103 A68G (frame_end) = A68G (frame_start) + frame_a_size;
104 A68G (stack_start) = A68G (frame_end);
105 A68G (stack_end) = A68G (stack_start) + expr_a_size;
106 ABEND (errno != 0, ERROR_MEMORY_FULL, __func__);
107 }
108
109 //! @brief aligned allocation.
110
111 void *a68g_alloc (size_t len, const char *f, int line)
112 {
113 // We need this since malloc aligns to "standard C types".
114 // __float128 is not a standard type, apparently ...
115 //
116 (void) f;
117 (void) line;
118 storage_limit (len);
119 void *p = NULL;
120 size_t align = sizeof (A68G_ALIGN_T);
121 int save = errno;
122 errno = 0;
123 // Allocation is platform dependent.
124 #if defined (BUILD_WIN32) || defined (BUILD_WIN64)
125 p = _aligned_malloc (len, align);
126 ABEND (p == NULL || errno != 0, "cannot allocate memory", __func__);
127 #elif defined (HAVE_POSIX_MEMALIGN)
128 errno = posix_memalign (&p, align, len);
129 if (errno != 0) {
130 p = NULL;
131 }
132 #elif defined (HAVE_ALIGNED_ALLOC) // Glibc version of posix_memalign.
133 if (align < sizeof (void *)) {
134 errno = EINVAL;
135 } else {
136 p = aligned_alloc (align, len);
137 }
138 #else
139 p = malloc (len); // Aude audenda.
140 #endif
141 //
142 if (p == (void *) NULL || errno != 0) {
143 static BUFFER msg;
144 if (len > GIGABYTE) {
145 a68g_bufprt (msg, SNPRINTF_SIZE, "request for " A68G_LU " GB", len / GIGABYTE);
146 } else if (len > MEGABYTE) {
147 a68g_bufprt (msg, SNPRINTF_SIZE, "request for " A68G_LU " MB", len / MEGABYTE);
148 } else {
149 a68g_bufprt (msg, SNPRINTF_SIZE, "request for " A68G_LU " bytes", len);
150 }
151 ABEND (A68G_TRUE, ERROR_MEMORY_FULL, msg);
152 }
153 errno = save;
154 return p;
155 }
156
157 void a68g_free (void *z)
158 {
159 if (z != NULL) {
160 #if defined (BUILD_WIN32) || defined (BUILD_WIN64)
161 _aligned_free (z); // On WIN32/64, free cannot deallocate _aligned_malloc
162 #else
163 free (z);
164 #endif
165 }
166 }
167
168 //! @brief Give pointer to block of "s" bytes.
169
170 BYTE_T *get_heap_space (size_t s)
171 {
172 ABEND (s == 0, ERROR_INVALID_SIZE, __func__);
173 BYTE_T *z = (BYTE_T *) (A68G_ALIGN_T *) a68g_alloc (A68G_ALIGN (s), __func__, __LINE__);
174 ABEND (z == NO_BYTE, ERROR_MEMORY_FULL, __func__);
175 return z;
176 }
177
178 //! @brief Make a new copy of concatenated strings.
179
180 char *new_string (char *t, ...)
181 {
182 va_list vl;
183 va_start (vl, t);
184 char *q = t;
185 if (q == NO_TEXT) {
186 va_end (vl);
187 return NO_TEXT;
188 }
189 size_t len = 0;
190 while (q != NO_TEXT) {
191 len += strlen (q);
192 q = va_arg (vl, char *);
193 }
194 va_end (vl);
195 len++;
196 char *z = (char *) get_heap_space (len);
197 z[0] = NULL_CHAR;
198 q = t;
199 va_start (vl, t);
200 while (q != NO_TEXT) {
201 a68g_bufcat (z, q, len);
202 q = va_arg (vl, char *);
203 }
204 va_end (vl);
205 return z;
206 }
207
208 //! @brief Make a new copy of "t".
209
210 char *new_fixed_string (char *t)
211 {
212 size_t n = strlen (t) + 1;
213 char *z = (char *) get_fixed_heap_space (n);
214 a68g_bufcpy (z, t, n);
215 return z;
216 }
217
218 //! @brief Make a new copy of "t".
219
220 char *new_temp_string (char *t)
221 {
222 size_t n = strlen (t) + 1;
223 char *z = (char *) get_temp_heap_space (n);
224 a68g_bufcpy (z, t, n);
225 return z;
226 }
227
228 //! @brief Get (preferably fixed) heap space.
229
230 BYTE_T *get_fixed_heap_space (size_t s)
231 {
232 if (A68G (heap_is_fluid)) {
233 BYTE_T *z = HEAP_ADDRESS (A68G (fixed_heap_pointer));
234 A68G (fixed_heap_pointer) += A68G_ALIGN (s);
235 // Allow for extra storage for diagnostics etcetera.
236 ABEND (((A68G (fixed_heap_pointer) + A68G (storage_overhead)) >= A68G (temp_heap_pointer)), ERROR_MEMORY_FULL, __func__);
237 return z;
238 } else {
239 return get_heap_space (s);
240 }
241 }
242
243 //! @brief Get (preferably temporary) heap space.
244
245 BYTE_T *get_temp_heap_space (size_t s)
246 {
247 if (A68G (heap_is_fluid)) {
248 A68G (temp_heap_pointer) -= A68G_ALIGN (s);
249 // Allow for extra storage for diagnostics etcetera.
250 ABEND (((A68G (fixed_heap_pointer) + A68G (storage_overhead)) >= A68G (temp_heap_pointer)), ERROR_MEMORY_FULL, __func__);
251 return HEAP_ADDRESS (A68G (temp_heap_pointer));
252 } else {
253 return get_heap_space (s);
254 }
255 }
256
257 //! @brief Get size of stack segment.
258
259 void get_stack_size (void)
260 {
261 #if defined (BUILD_WIN32) || defined (BUILD_WIN64)
262 A68G (stack_size) = MEGABYTE; // Guestimate
263 #else
264 errno = 0;
265 // Some systems do not implement RLIMIT_STACK so if getrlimit fails, we do not abend.
266 struct rlimit limits;
267 if (! (getrlimit (RLIMIT_STACK, &limits) == 0 && errno == 0)) {
268 A68G (stack_size) = MEGABYTE;
269 }
270 A68G (stack_size) = (size_t) (RLIM_CUR (&limits) < RLIM_MAX (&limits) ? RLIM_CUR (&limits) : RLIM_MAX (&limits));
271 // A heuristic in case getrlimit yields extreme numbers: the frame stack is
272 // assumed to fill at a rate comparable to the C stack, so the C stack needs
273 // not be larger than the frame stack. This may not be true.
274 if (A68G (stack_size) < KILOBYTE || (A68G (stack_size) > 96 * MEGABYTE && A68G (stack_size) > A68G (frame_stack_size)) ) {
275 A68G (stack_size) = A68G (frame_stack_size);
276 }
277 #endif
278 A68G (stack_limit) = (A68G (stack_size) > (4 * A68G (storage_overhead)) ? (A68G (stack_size) - A68G (storage_overhead)) : A68G (stack_size) / 2);
279 }
280
281 //! @brief Free heap allocated by genie.
282
283 void genie_free (NODE_T *p)
284 {
285 for (; p != NO_NODE; FORWARD (p)) {
286 genie_free (SUB (p));
287 if (GINFO (p) != NO_GINFO) {
288 a68g_free (CONSTANT (GINFO (p)) );
289 CONSTANT (GINFO (p)) = NO_CONSTANT;
290 a68g_free (COMPILE_NAME (GINFO (p)) );
291 COMPILE_NAME (GINFO (p)) = NO_TEXT;
292 }
293 }
294 }
295
296 //! @brief Free heap allocated by genie.
297
298 void free_syntax_tree (NODE_T *p)
299 {
300 for (; p != NO_NODE; FORWARD (p)) {
301 free_syntax_tree (SUB (p));
302 a68g_free (NPRAGMENT (p));
303 NPRAGMENT (p) = NO_TEXT;
304 DIAGNOSTIC_T *d = DIAGNOSTICS (LINE (INFO (p)) );
305 while (d != NO_DIAGNOSTIC) {
306 a68g_free (TEXT (d));
307 DIAGNOSTIC_T *stale = d;
308 FORWARD (d);
309 a68g_free (stale);
310 }
311 DIAGNOSTICS (LINE (INFO (p)) ) = NO_DIAGNOSTIC;
312 }
313 }
© 2002-2025 J.M. van der Veer (jmvdveer@xs4all.nl)
|