OSDN Git Service

2007-05-28 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / memory.c
1 /* Memory management routines.
2    Copyright 2002, 2005, 2006 Free Software Foundation, Inc.
3    Contributed by Paul Brook <paul@nowt.org>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
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.
11
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
19 executable.)
20
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.
25
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.  */
30
31 #include "config.h"
32 #include <stdlib.h>
33 #include "libgfortran.h"
34
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 */
40
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
44
45 void *
46 get_mem (size_t n)
47 {
48   void *p;
49
50 #ifdef GFC_CLEAR_MEMORY
51   p = (void *) calloc (1, n);
52 #else
53   p = (void *) malloc (n);
54 #endif
55   if (p == NULL)
56     os_error ("Memory allocation failed");
57
58   return p;
59 }
60
61
62 void
63 free_mem (void *p)
64 {
65   free (p);
66 }
67
68
69 /* Allocate memory for internal (compiler generated) use.  */
70
71 void *
72 internal_malloc_size (size_t size)
73 {
74   if (size == 0)
75     return NULL;
76
77   return get_mem (size);
78 }
79
80
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
83    SIZE is 0.  */
84
85 static void *
86 internal_realloc_size (void *mem, size_t size)
87 {
88   if (size == 0)
89     {
90       if (mem)
91         free (mem);
92       return NULL;
93     }
94
95   if (mem == 0)
96     return get_mem (size);
97
98   mem = realloc (mem, size);
99   if (!mem)
100     os_error ("Out of memory.");
101
102   return mem;
103 }
104
105 extern void *internal_realloc (void *, GFC_INTEGER_4);
106 export_proto(internal_realloc);
107
108 void *
109 internal_realloc (void *mem, GFC_INTEGER_4 size)
110 {
111 #ifdef GFC_CHECK_MEMORY
112   /* Under normal circumstances, this is _never_ going to happen!  */
113   if (size < 0)
114     runtime_error ("Attempt to allocate a negative amount of memory.");
115 #endif
116   return internal_realloc_size (mem, (size_t) size);
117 }
118
119 extern void *internal_realloc64 (void *, GFC_INTEGER_8);
120 export_proto(internal_realloc64);
121
122 void *
123 internal_realloc64 (void *mem, GFC_INTEGER_8 size)
124 {
125 #ifdef GFC_CHECK_MEMORY
126   /* Under normal circumstances, this is _never_ going to happen!  */
127   if (size < 0)
128     runtime_error ("Attempt to allocate a negative amount of memory.");
129 #endif
130   return internal_realloc_size (mem, (size_t) size);
131 }
132
133
134 /* User-allocate, one call for each member of the alloc-list of an
135    ALLOCATE statement. */
136
137 static void *
138 allocate_size (size_t size, GFC_INTEGER_4 * stat)
139 {
140   void *newmem;
141
142   newmem = malloc (size ? size : 1);
143   if (!newmem)
144     {
145       if (stat)
146         {
147           *stat = ERROR_ALLOCATION;
148           return newmem;
149         }
150       else
151         runtime_error ("ALLOCATE: Out of memory.");
152     }
153
154   if (stat)
155     *stat = 0;
156
157   return newmem;
158 }
159
160 extern void *allocate (GFC_INTEGER_4, GFC_INTEGER_4 *);
161 export_proto(allocate);
162
163 void *
164 allocate (GFC_INTEGER_4 size, GFC_INTEGER_4 * stat)
165 {
166   if (size < 0)
167     {
168       if (stat)
169         {
170           *stat = ERROR_ALLOCATION;
171           return NULL;
172         }
173       else
174         runtime_error ("Attempt to allocate negative amount of memory. "
175                        "Possible integer overflow");
176     }
177
178   return allocate_size ((size_t) size, stat);
179 }
180
181 extern void *allocate64 (GFC_INTEGER_8, GFC_INTEGER_4 *);
182 export_proto(allocate64);
183
184 void *
185 allocate64 (GFC_INTEGER_8 size, GFC_INTEGER_4 * stat)
186 {
187   if (size < 0)
188     {
189       if (stat)
190         {
191           *stat = ERROR_ALLOCATION;
192           return NULL;
193         }
194       else
195         runtime_error ("ALLOCATE64: Attempt to allocate negative amount of "
196                        "memory. Possible integer overflow");
197     }
198
199   return allocate_size ((size_t) size, stat);
200 }
201
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.  */
205
206 extern void *allocate_array (void *, GFC_INTEGER_4, GFC_INTEGER_4 *);
207 export_proto(allocate_array);
208
209 void *
210 allocate_array (void *mem, GFC_INTEGER_4 size, GFC_INTEGER_4 * stat)
211 {
212   if (mem == NULL)
213     return allocate (size, stat);
214   if (stat)
215     {
216       free (mem);
217       mem = allocate (size, stat);
218       *stat = ERROR_ALLOCATION;
219       return mem;
220     }
221
222   runtime_error ("Attempting to allocate already allocated array.");
223 }
224
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.  */
228
229 extern void *allocate64_array (void *, GFC_INTEGER_8, GFC_INTEGER_4 *);
230 export_proto(allocate64_array);
231
232 void *
233 allocate64_array (void *mem, GFC_INTEGER_8 size, GFC_INTEGER_4 * stat)
234 {
235   if (mem == NULL)
236     return allocate64 (size, stat);
237   if (stat)
238     {
239       free (mem);
240       mem = allocate (size, stat);
241       *stat = ERROR_ALLOCATION;
242       return mem;
243     }
244
245   runtime_error ("Attempting to allocate already allocated array.");
246 }
247
248 /* User-deallocate; pointer is then NULLified by the front-end. */
249
250 extern void deallocate (void *, GFC_INTEGER_4 *);
251 export_proto(deallocate);
252
253 void
254 deallocate (void *mem, GFC_INTEGER_4 * stat)
255 {
256   if (!mem)
257     {
258       if (stat)
259         {
260           *stat = 1;
261           return;
262         }
263       else
264         runtime_error ("Internal: Attempt to DEALLOCATE unallocated memory.");
265     }
266
267   free (mem);
268
269   if (stat)
270     *stat = 0;
271 }