1 /* Memory management routines.
2 Copyright 2002, 2005, 2006 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., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, 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
50 #ifdef GFC_CLEAR_MEMORY
51 p = (void *) calloc (1, n);
53 p = (void *) malloc (n);
56 os_error ("Memory allocation failed");
69 /* Allocate memory for internal (compiler generated) use. */
72 internal_malloc_size (size_t size)
77 return get_mem (size);
81 /* Reallocate internal memory MEM so it has SIZE bytes of data.
82 Allocate a new block if MEM is zero, and free the block if
86 internal_realloc_size (void *mem, size_t size)
96 return get_mem (size);
98 mem = realloc (mem, size);
100 os_error ("Out of memory.");
105 extern void *internal_realloc (void *, GFC_INTEGER_4);
106 export_proto(internal_realloc);
109 internal_realloc (void *mem, GFC_INTEGER_4 size)
111 #ifdef GFC_CHECK_MEMORY
112 /* Under normal circumstances, this is _never_ going to happen! */
114 runtime_error ("Attempt to allocate a negative amount of memory.");
116 return internal_realloc_size (mem, (size_t) size);
119 extern void *internal_realloc64 (void *, GFC_INTEGER_8);
120 export_proto(internal_realloc64);
123 internal_realloc64 (void *mem, GFC_INTEGER_8 size)
125 #ifdef GFC_CHECK_MEMORY
126 /* Under normal circumstances, this is _never_ going to happen! */
128 runtime_error ("Attempt to allocate a negative amount of memory.");
130 return internal_realloc_size (mem, (size_t) size);
134 /* User-allocate, one call for each member of the alloc-list of an
135 ALLOCATE statement. */
138 allocate_size (size_t size, GFC_INTEGER_4 * stat)
142 newmem = malloc (size ? size : 1);
147 *stat = ERROR_ALLOCATION;
151 runtime_error ("ALLOCATE: Out of memory.");
160 extern void *allocate (GFC_INTEGER_4, GFC_INTEGER_4 *);
161 export_proto(allocate);
164 allocate (GFC_INTEGER_4 size, GFC_INTEGER_4 * stat)
170 *stat = ERROR_ALLOCATION;
174 runtime_error ("Attempt to allocate negative amount of memory. "
175 "Possible integer overflow");
178 return allocate_size ((size_t) size, stat);
181 extern void *allocate64 (GFC_INTEGER_8, GFC_INTEGER_4 *);
182 export_proto(allocate64);
185 allocate64 (GFC_INTEGER_8 size, GFC_INTEGER_4 * stat)
191 *stat = ERROR_ALLOCATION;
195 runtime_error ("ALLOCATE64: Attempt to allocate negative amount of "
196 "memory. Possible integer overflow");
199 return allocate_size ((size_t) size, stat);
202 /* Function to call in an ALLOCATE statement when the argument is an
203 allocatable array. If the array is currently allocated, it is
204 an error to allocate it again. 32-bit version. */
206 extern void *allocate_array (void *, GFC_INTEGER_4, GFC_INTEGER_4 *);
207 export_proto(allocate_array);
210 allocate_array (void *mem, GFC_INTEGER_4 size, GFC_INTEGER_4 * stat)
213 return allocate (size, stat);
217 mem = allocate (size, stat);
218 *stat = ERROR_ALLOCATION;
222 runtime_error ("Attempting to allocate already allocated array.");
225 /* Function to call in an ALLOCATE statement when the argument is an
226 allocatable array. If the array is currently allocated, it is
227 an error to allocate it again. 64-bit version. */
229 extern void *allocate64_array (void *, GFC_INTEGER_8, GFC_INTEGER_4 *);
230 export_proto(allocate64_array);
233 allocate64_array (void *mem, GFC_INTEGER_8 size, GFC_INTEGER_4 * stat)
236 return allocate64 (size, stat);
240 mem = allocate (size, stat);
241 *stat = ERROR_ALLOCATION;
245 runtime_error ("Attempting to allocate already allocated array.");
248 /* User-deallocate; pointer is then NULLified by the front-end. */
250 extern void deallocate (void *, GFC_INTEGER_4 *);
251 export_proto(deallocate);
254 deallocate (void *mem, GFC_INTEGER_4 * stat)
264 runtime_error ("Internal: Attempt to DEALLOCATE unallocated memory.");