1 /* Memory mamagement routines.
2 Copyright 2002 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfor).
7 Libgfor is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 Libgfor is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "libgfortran.h"
26 /* If GFC_CLEAR_MEMORY is defined, the memory allocation routines will
27 return memory that is guaranteed to be set to zero. This can have
28 a severe efficiency penalty, so it should never be set if good
29 performance is desired, but it can help when you're debugging code. */
30 #define GFC_CLEAR_MEMORY
32 /* If GFC_CHECK_MEMORY is defined, we do some sanity checks at runtime.
33 This causes small overhead, but again, it also helps debugging. */
34 #define GFC_CHECK_MEMORY
36 /* We use a double linked list of these structures to keep track of
37 the memory we allocate internally. We could also use this for user
38 allocated memory (ALLOCATE/DEALLOCATE). This should be stored in a
40 typedef struct malloc_t
44 struct malloc_t *prev, *next;
46 /* The start of the block. */
51 /* We try to make sure we don't get memory corruption by checking for
53 #define GFC_MALLOC_MAGIC 0x4d353941 /* "G95M" */
55 #define HEADER_SIZE offsetof (malloc_t, data)
56 #define DATA_POINTER(pheader) (&((pheader)->data))
57 #define DATA_HEADER(pdata) ((malloc_t *)((char *) (pdata) - HEADER_SIZE))
59 /* The root of the circular double linked list for compiler generated
61 static malloc_t mem_root = {
67 /* ??? Disabled because, well, it wasn't being called before transforming
68 it to a destructor, and turning it on causes testsuite failures. */
69 /* Doesn't actually do any cleaning up, just throws an error if something
70 has got out of sync somewhere. */
72 static void __attribute__((destructor))
73 runtime_cleanup (void)
75 /* Make sure all memory we've allocated is freed on exit. */
76 if (mem_root.next != &mem_root)
77 runtime_error ("Unfreed memory on program termination");
87 #ifdef GFC_CLEAR_MEMORY
88 p = (void *) calloc (1, n);
90 p = (void *) malloc (n);
93 os_error ("Memory allocation failed");
106 /* Allocates a block of memory with a size of N bytes. N does not
107 include the size of the header. */
110 malloc_with_header (size_t n)
116 newmem = (malloc_t *) get_mem (n);
120 newmem->magic = GFC_MALLOC_MAGIC;
128 /* Allocate memory for internal (compiler generated) use. */
131 internal_malloc_size (size_t size)
135 newmem = malloc_with_header (size);
138 os_error ("Out of memory.");
140 /* Add to end of list. */
141 newmem->next = &mem_root;
142 newmem->prev = mem_root.prev;
143 mem_root.prev->next = newmem;
144 mem_root.prev = newmem;
146 return DATA_POINTER (newmem);
149 extern void *internal_malloc (GFC_INTEGER_4);
150 export_proto(internal_malloc);
153 internal_malloc (GFC_INTEGER_4 size)
155 #ifdef GFC_CHECK_MEMORY
156 /* Under normal circumstances, this is _never_ going to happen! */
158 runtime_error ("Attempt to allocate a negative amount of memory.");
161 return internal_malloc_size ((size_t) size);
164 extern void *internal_malloc64 (GFC_INTEGER_8);
165 export_proto(internal_malloc64);
168 internal_malloc64 (GFC_INTEGER_8 size)
170 #ifdef GFC_CHECK_MEMORY
171 /* Under normal circumstances, this is _never_ going to happen! */
173 runtime_error ("Attempt to allocate a negative amount of memory.");
175 return internal_malloc_size ((size_t) size);
179 /* Free internally allocated memory. Pointer is NULLified. Also used to
180 free user allocated memory. */
181 /* TODO: keep a list of previously allocated blocks and reuse them. */
184 internal_free (void *mem)
189 runtime_error ("Internal: Possible double free of temporary.");
191 m = DATA_HEADER (mem);
193 if (m->magic != GFC_MALLOC_MAGIC)
194 runtime_error ("Internal: No magic memblock marker. "
195 "Possible memory corruption");
197 /* Move markers up the chain, so they don't get lost. */
198 m->prev->marker += m->marker;
199 /* Remove from list. */
200 m->prev->next = m->next;
201 m->next->prev = m->prev;
205 iexport(internal_free);
208 /* User-allocate, one call for each member of the alloc-list of an
209 ALLOCATE statement. */
212 allocate_size (void **mem, size_t size, GFC_INTEGER_4 * stat)
217 runtime_error ("Internal: NULL mem pointer in ALLOCATE.");
219 newmem = malloc_with_header (size);
228 runtime_error ("ALLOCATE: Out of memory.");
231 /* We don't keep a list of these at the moment, so just link to itself. */
232 newmem->next = newmem;
233 newmem->prev = newmem;
235 (*mem) = DATA_POINTER (newmem);
241 extern void allocate (void **, GFC_INTEGER_4, GFC_INTEGER_4 *);
242 export_proto(allocate);
245 allocate (void **mem, GFC_INTEGER_4 size, GFC_INTEGER_4 * stat)
249 runtime_error ("Attempt to allocate negative amount of memory. "
250 "Possible integer overflow");
254 allocate_size (mem, (size_t) size, stat);
257 extern void allocate64 (void **, GFC_INTEGER_8, GFC_INTEGER_4 *);
258 export_proto(allocate64);
261 allocate64 (void **mem, GFC_INTEGER_8 size, GFC_INTEGER_4 * stat)
266 ("ALLOCATE64: Attempt to allocate negative amount of memory. "
267 "Possible integer overflow");
271 allocate_size (mem, (size_t) size, stat);
275 /* User-deallocate; pointer is NULLified. */
277 extern void deallocate (void **, GFC_INTEGER_4 *);
278 export_proto(deallocate);
281 deallocate (void **mem, GFC_INTEGER_4 * stat)
284 runtime_error ("Internal: NULL mem pointer in ALLOCATE.");
296 ("Internal: Attempt to DEALLOCATE unallocated memory.");
301 /* Just use the internal routine. */
302 internal_free (*mem);