OSDN Git Service

* acinclude.m4 (LIBGFOR_CHECK_ATTRIBUTE_VISIBILITY): New.
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / memory.c
1 /* Memory mamagement routines.
2    Copyright 2002 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 (libgfor).
6
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.
11
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.
16
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.  */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include "libgfortran.h"
25
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
31
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
35
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
39    seperate list.  */
40 typedef struct malloc_t
41 {
42   int magic;
43   int marker;
44   struct malloc_t *prev, *next;
45
46   /* The start of the block.  */
47   void *data;
48 }
49 malloc_t;
50
51 /* We try to make sure we don't get memory corruption by checking for
52    a magic number.  */
53 #define GFC_MALLOC_MAGIC 0x4d353941     /* "G95M" */
54
55 #define HEADER_SIZE offsetof (malloc_t, data)
56 #define DATA_POINTER(pheader) (&((pheader)->data))
57 #define DATA_HEADER(pdata) ((malloc_t *)((char *) (pdata) - HEADER_SIZE))
58
59 /* The root of the circular double linked list for compiler generated
60    malloc calls.  */
61 static malloc_t mem_root = {
62         .next = &mem_root,
63         .prev = &mem_root
64 };
65
66 #if 0
67 /* ??? Disabled because, well, it wasn't being called before transforming
68    it to a destructor, and turning it on causes testsuite failures.  */
69 /* Doesn't actually do any cleaning up, just throws an error if something
70    has got out of sync somewhere.  */
71
72 static void __attribute__((destructor))
73 runtime_cleanup (void)
74 {
75   /* Make sure all memory we've allocated is freed on exit.  */
76   if (mem_root.next != &mem_root)
77     runtime_error ("Unfreed memory on program termination");
78 }
79 #endif
80
81
82 void *
83 get_mem (size_t n)
84 {
85   void *p;
86
87 #ifdef GFC_CLEAR_MEMORY
88   p = (void *) calloc (1, n);
89 #else
90   p = (void *) malloc (n);
91 #endif
92   if (p == NULL)
93     os_error ("Memory allocation failed");
94
95   return p;
96 }
97
98
99 void
100 free_mem (void *p)
101 {
102   free (p);
103 }
104
105
106 /* Allocates a block of memory with a size of N bytes.  N does not
107    include the size of the header.  */
108
109 static malloc_t *
110 malloc_with_header (size_t n)
111 {
112   malloc_t *newmem;
113
114   n = n + HEADER_SIZE;
115
116   newmem = (malloc_t *) get_mem (n);
117
118   if (newmem)
119     {
120       newmem->magic = GFC_MALLOC_MAGIC;
121       newmem->marker = 0;
122     }
123
124   return newmem;
125 }
126
127
128 /* Allocate memory for internal (compiler generated) use.  */
129
130 void *
131 internal_malloc_size (size_t size)
132 {
133   malloc_t *newmem;
134
135   newmem = malloc_with_header (size);
136
137   if (!newmem)
138     os_error ("Out of memory.");
139
140   /* Add to end of list.  */
141   newmem->next = &mem_root;
142   newmem->prev = mem_root.prev;
143   mem_root.prev->next = newmem;
144   mem_root.prev = newmem;
145
146   return DATA_POINTER (newmem);
147 }
148
149 extern void *internal_malloc (GFC_INTEGER_4);
150 export_proto(internal_malloc);
151
152 void *
153 internal_malloc (GFC_INTEGER_4 size)
154 {
155 #ifdef GFC_CHECK_MEMORY
156   /* Under normal circumstances, this is _never_ going to happen!  */
157   if (size < 0)
158     runtime_error ("Attempt to allocate a negative amount of memory.");
159
160 #endif
161   return internal_malloc_size ((size_t) size);
162 }
163
164 extern void *internal_malloc64 (GFC_INTEGER_8);
165 export_proto(internal_malloc64);
166
167 void *
168 internal_malloc64 (GFC_INTEGER_8 size)
169 {
170 #ifdef GFC_CHECK_MEMORY
171   /* Under normal circumstances, this is _never_ going to happen!  */
172   if (size < 0)
173     runtime_error ("Attempt to allocate a negative amount of memory.");
174 #endif
175   return internal_malloc_size ((size_t) size);
176 }
177
178
179 /* Free internally allocated memory.  Pointer is NULLified.  Also used to
180    free user allocated memory.  */
181 /* TODO: keep a list of previously allocated blocks and reuse them.  */
182
183 void
184 internal_free (void *mem)
185 {
186   malloc_t *m;
187
188   if (!mem)
189     runtime_error ("Internal: Possible double free of temporary.");
190
191   m = DATA_HEADER (mem);
192
193   if (m->magic != GFC_MALLOC_MAGIC)
194     runtime_error ("Internal: No magic memblock marker.  "
195                    "Possible memory corruption");
196
197   /* Move markers up the chain, so they don't get lost.  */
198   m->prev->marker += m->marker;
199   /* Remove from list.  */
200   m->prev->next = m->next;
201   m->next->prev = m->prev;
202
203   free (m);
204 }
205 iexport(internal_free);
206
207
208 /* User-allocate, one call for each member of the alloc-list of an
209    ALLOCATE statement. */
210
211 static void
212 allocate_size (void **mem, size_t size, GFC_INTEGER_4 * stat)
213 {
214   malloc_t *newmem;
215
216   if (!mem)
217     runtime_error ("Internal: NULL mem pointer in ALLOCATE.");
218
219   newmem = malloc_with_header (size);
220   if (!newmem)
221     {
222       if (stat)
223         {
224           *stat = 1;
225           return;
226         }
227       else
228         runtime_error ("ALLOCATE: Out of memory.");
229     }
230
231   /* We don't keep a list of these at the moment, so just link to itself. */
232   newmem->next = newmem;
233   newmem->prev = newmem;
234
235   (*mem) = DATA_POINTER (newmem);
236
237   if (stat)
238     *stat = 0;
239 }
240
241 extern void allocate (void **, GFC_INTEGER_4, GFC_INTEGER_4 *);
242 export_proto(allocate);
243
244 void
245 allocate (void **mem, GFC_INTEGER_4 size, GFC_INTEGER_4 * stat)
246 {
247   if (size < 0)
248     {
249       runtime_error ("Attempt to allocate negative amount of memory.  "
250                      "Possible integer overflow");
251       abort ();
252     }
253
254   allocate_size (mem, (size_t) size, stat);
255 }
256
257 extern void allocate64 (void **, GFC_INTEGER_8, GFC_INTEGER_4 *);
258 export_proto(allocate64);
259
260 void
261 allocate64 (void **mem, GFC_INTEGER_8 size, GFC_INTEGER_4 * stat)
262 {
263   if (size < 0)
264     {
265       runtime_error
266         ("ALLOCATE64: Attempt to allocate negative amount of memory. "
267          "Possible integer overflow");
268       abort ();
269     }
270
271   allocate_size (mem, (size_t) size, stat);
272 }
273
274
275 /* User-deallocate; pointer is NULLified. */
276
277 extern void deallocate (void **, GFC_INTEGER_4 *);
278 export_proto(deallocate);
279
280 void
281 deallocate (void **mem, GFC_INTEGER_4 * stat)
282 {
283   if (!mem)
284     runtime_error ("Internal: NULL mem pointer in ALLOCATE.");
285
286   if (!*mem)
287     {
288       if (stat)
289         {
290           *stat = 1;
291           return;
292         }
293       else
294         {
295           runtime_error
296             ("Internal: Attempt to DEALLOCATE unallocated memory.");
297           abort ();
298         }
299     }
300
301   /* Just use the internal routine.  */
302   internal_free (*mem);
303   *mem = NULL;
304
305   if (stat)
306     *stat = 0;
307 }