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 (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA. */
33 #include "libgfortran.h"
35 /* If GFC_CLEAR_MEMORY is defined, the memory allocation routines will
36 return memory that is guaranteed to be set to zero. This can have
37 a severe efficiency penalty, so it should never be set if good
38 performance is desired, but it can help when you're debugging code. */
39 #define GFC_CLEAR_MEMORY
41 /* If GFC_CHECK_MEMORY is defined, we do some sanity checks at runtime.
42 This causes small overhead, but again, it also helps debugging. */
43 #define GFC_CHECK_MEMORY
45 /* We use a double linked list of these structures to keep track of
46 the memory we allocate internally. We could also use this for user
47 allocated memory (ALLOCATE/DEALLOCATE). This should be stored in a
49 typedef struct malloc_t
53 struct malloc_t *prev, *next;
55 /* The start of the block. */
60 /* We try to make sure we don't get memory corruption by checking for
62 #define GFC_MALLOC_MAGIC 0x4d353941 /* "G95M" */
64 #define HEADER_SIZE offsetof (malloc_t, data)
65 #define DATA_POINTER(pheader) (&((pheader)->data))
66 #define DATA_HEADER(pdata) ((malloc_t *)((char *) (pdata) - HEADER_SIZE))
68 /* The root of the circular double linked list for compiler generated
70 static malloc_t mem_root = {
76 /* ??? Disabled because, well, it wasn't being called before transforming
77 it to a destructor, and turning it on causes testsuite failures. */
78 /* Doesn't actually do any cleaning up, just throws an error if something
79 has got out of sync somewhere. */
81 static void __attribute__((destructor))
82 runtime_cleanup (void)
84 /* Make sure all memory we've allocated is freed on exit. */
85 if (mem_root.next != &mem_root)
86 runtime_error ("Unfreed memory on program termination");
96 #ifdef GFC_CLEAR_MEMORY
97 p = (void *) calloc (1, n);
99 p = (void *) malloc (n);
102 os_error ("Memory allocation failed");
115 /* Allocates a block of memory with a size of N bytes. N does not
116 include the size of the header. */
119 malloc_with_header (size_t n)
125 newmem = (malloc_t *) get_mem (n);
129 newmem->magic = GFC_MALLOC_MAGIC;
137 /* Allocate memory for internal (compiler generated) use. */
140 internal_malloc_size (size_t size)
144 newmem = malloc_with_header (size);
147 os_error ("Out of memory.");
149 /* Add to end of list. */
150 newmem->next = &mem_root;
151 newmem->prev = mem_root.prev;
152 mem_root.prev->next = newmem;
153 mem_root.prev = newmem;
155 return DATA_POINTER (newmem);
158 extern void *internal_malloc (GFC_INTEGER_4);
159 export_proto(internal_malloc);
162 internal_malloc (GFC_INTEGER_4 size)
164 #ifdef GFC_CHECK_MEMORY
165 /* Under normal circumstances, this is _never_ going to happen! */
167 runtime_error ("Attempt to allocate a negative amount of memory.");
170 return internal_malloc_size ((size_t) size);
173 extern void *internal_malloc64 (GFC_INTEGER_8);
174 export_proto(internal_malloc64);
177 internal_malloc64 (GFC_INTEGER_8 size)
179 #ifdef GFC_CHECK_MEMORY
180 /* Under normal circumstances, this is _never_ going to happen! */
182 runtime_error ("Attempt to allocate a negative amount of memory.");
184 return internal_malloc_size ((size_t) size);
188 /* Free internally allocated memory. Pointer is NULLified. Also used to
189 free user allocated memory. */
190 /* TODO: keep a list of previously allocated blocks and reuse them. */
193 internal_free (void *mem)
198 runtime_error ("Internal: Possible double free of temporary.");
200 m = DATA_HEADER (mem);
202 if (m->magic != GFC_MALLOC_MAGIC)
203 runtime_error ("Internal: No magic memblock marker. "
204 "Possible memory corruption");
206 /* Move markers up the chain, so they don't get lost. */
207 m->prev->marker += m->marker;
208 /* Remove from list. */
209 m->prev->next = m->next;
210 m->next->prev = m->prev;
214 iexport(internal_free);
217 /* User-allocate, one call for each member of the alloc-list of an
218 ALLOCATE statement. */
221 allocate_size (void **mem, size_t size, GFC_INTEGER_4 * stat)
226 runtime_error ("Internal: NULL mem pointer in ALLOCATE.");
228 newmem = malloc_with_header (size);
237 runtime_error ("ALLOCATE: Out of memory.");
240 /* We don't keep a list of these at the moment, so just link to itself. */
241 newmem->next = newmem;
242 newmem->prev = newmem;
244 (*mem) = DATA_POINTER (newmem);
250 extern void allocate (void **, GFC_INTEGER_4, GFC_INTEGER_4 *);
251 export_proto(allocate);
254 allocate (void **mem, GFC_INTEGER_4 size, GFC_INTEGER_4 * stat)
258 runtime_error ("Attempt to allocate negative amount of memory. "
259 "Possible integer overflow");
263 allocate_size (mem, (size_t) size, stat);
266 extern void allocate64 (void **, GFC_INTEGER_8, GFC_INTEGER_4 *);
267 export_proto(allocate64);
270 allocate64 (void **mem, GFC_INTEGER_8 size, GFC_INTEGER_4 * stat)
275 ("ALLOCATE64: Attempt to allocate negative amount of memory. "
276 "Possible integer overflow");
280 allocate_size (mem, (size_t) size, stat);
284 /* User-deallocate; pointer is NULLified. */
286 extern void deallocate (void **, GFC_INTEGER_4 *);
287 export_proto(deallocate);
290 deallocate (void **mem, GFC_INTEGER_4 * stat)
293 runtime_error ("Internal: NULL mem pointer in ALLOCATE.");
305 ("Internal: Attempt to DEALLOCATE unallocated memory.");
310 /* Just use the internal routine. */
311 internal_free (*mem);