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 #define malloc_t prefix(malloc_t)
41 typedef struct malloc_t
45 struct malloc_t
*prev
, *next
;
47 /* The start of the block. */
52 /* We try to make sure we don't get memory corruption by checking for
54 #define GFC_MALLOC_MAGIC 0x4d353941 /* "G95M" */
56 #define HEADER_SIZE offsetof (malloc_t, data)
57 #define DATA_POINTER(pheader) (&((pheader)->data))
58 #define DATA_HEADER(pdata) ((malloc_t *)((char *) (pdata) - HEADER_SIZE))
60 /* The root of the circular double linked list for compiler generated
62 static malloc_t mem_root
;
69 /* The root should never be used directly, so don't set the magic. */
71 mem_root
.next
= &mem_root
;
72 mem_root
.prev
= &mem_root
;
77 /* Doesn't actually do any cleaning up, just throws an error if something
78 has got out of sync somewhere. */
81 runtime_cleanup (void)
83 /* Make sure all memory we've allocated is freed on exit. */
84 if (mem_root
.next
!= &mem_root
)
85 runtime_error ("Unfreed memory on program termination");
95 #ifdef GFC_CLEAR_MEMORY
96 p
= (void *) calloc (n
, 1);
100 p
= (void *) malloc (n
);
105 os_error ("Memory allocation failed");
119 /* Allocates a block of memory with a size of N bytes. N does not
120 include the size of the header. */
123 malloc_with_header (size_t n
)
129 newmem
= (malloc_t
*) get_mem (n
);
133 newmem
->magic
= GFC_MALLOC_MAGIC
;
141 /* Allocate memory for internal (compiler generated) use. */
144 internal_malloc_size (size_t size
)
148 newmem
= malloc_with_header (size
);
151 os_error ("Out of memory.");
153 /* Add to end of list. */
154 newmem
->next
= &mem_root
;
155 newmem
->prev
= mem_root
.prev
;
156 mem_root
.prev
->next
= newmem
;
157 mem_root
.prev
= newmem
;
159 return DATA_POINTER (newmem
);
164 internal_malloc (GFC_INTEGER_4 size
)
166 #ifdef GFC_CHECK_MEMORY
167 /* Under normal circumstances, this is _never_ going to happen! */
169 runtime_error ("Attempt to allocate a negative amount of memory.");
172 return internal_malloc_size ((size_t) size
);
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
;
216 /* User-allocate, one call for each member of the alloc-list of an
217 ALLOCATE statement. */
220 allocate_size (void **mem
, size_t size
, GFC_INTEGER_4
* stat
)
225 runtime_error ("Internal: NULL mem pointer in ALLOCATE.");
227 newmem
= malloc_with_header (size
);
236 runtime_error ("ALLOCATE: Out of memory.");
239 /* We don't keep a list of these at the moment, so just link to itself. */
240 newmem
->next
= newmem
;
241 newmem
->prev
= newmem
;
243 (*mem
) = DATA_POINTER (newmem
);
251 allocate (void **mem
, GFC_INTEGER_4 size
, GFC_INTEGER_4
* stat
)
256 runtime_error ("Attempt to allocate negative amount of memory. "
257 "Possible integer overflow");
261 allocate_size (mem
, (size_t) size
, stat
);
266 allocate64 (void **mem
, GFC_INTEGER_8 size
, GFC_INTEGER_4
* stat
)
272 ("ALLOCATE64: Attempt to allocate negative amount of memory. "
273 "Possible integer overflow");
277 allocate_size (mem
, (size_t) size
, stat
);
281 /* User-deallocate; pointer is NULLified. */
284 deallocate (void **mem
, GFC_INTEGER_4
* stat
)
288 runtime_error ("Internal: NULL mem pointer in ALLOCATE.");
300 ("Internal: Attempt to DEALLOCATE unallocated memory.");
305 /* Just use the internal routine. */
306 internal_free (*mem
);