]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/runtime/memory.c
1 /* Memory management routines.
2 Copyright 2002, 2005, 2006, 2007 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 *, index_type
);
106 export_proto(internal_realloc
);
109 internal_realloc (void *mem
, index_type 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 /* User-allocate, one call for each member of the alloc-list of an
120 ALLOCATE statement. */
123 allocate_size (size_t size
, GFC_INTEGER_4
* stat
)
127 newmem
= malloc (size
? size
: 1);
132 *stat
= ERROR_ALLOCATION
;
136 runtime_error ("ALLOCATE: Out of memory.");
145 extern void *allocate (index_type
, GFC_INTEGER_4
*);
146 export_proto(allocate
);
149 allocate (index_type size
, GFC_INTEGER_4
* stat
)
151 #ifdef GFC_CHECK_MEMORY
152 /* The only time this can happen is the size computed by the
153 frontend wraps around. */
158 *stat
= ERROR_ALLOCATION
;
162 runtime_error ("Attempt to allocate negative amount of memory. "
163 "Possible integer overflow");
166 return allocate_size ((size_t) size
, stat
);
169 /* Function to call in an ALLOCATE statement when the argument is an
170 allocatable array. If the array is currently allocated, it is
171 an error to allocate it again. */
173 extern void *allocate_array (void *, index_type
, GFC_INTEGER_4
*);
174 export_proto(allocate_array
);
177 allocate_array (void *mem
, index_type size
, GFC_INTEGER_4
* stat
)
180 return allocate (size
, stat
);
184 mem
= allocate (size
, stat
);
185 *stat
= ERROR_ALLOCATION
;
189 runtime_error ("Attempting to allocate already allocated array.");
193 /* User-deallocate; pointer is then NULLified by the front-end. */
195 extern void deallocate (void *, GFC_INTEGER_4
*);
196 export_proto(deallocate
);
199 deallocate (void *mem
, GFC_INTEGER_4
* stat
)
209 runtime_error ("Internal: Attempt to DEALLOCATE unallocated memory.");