- removed literal allocate functions
[cacao.git] / src / mm / memory.c
1 /* toolbox/memory.c - 
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Reinhard Grafl
28
29    $Id: memory.c 1437 2004-11-05 09:51:07Z twisti $
30
31 */
32
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <assert.h>
37 #include <string.h>
38 #include <sys/mman.h>
39 #include <unistd.h>
40
41 #include "exceptions.h"
42 #include "global.h"
43 #include "native.h"
44 #include "options.h"
45 #include "statistics.h"
46 #include "threads/nativethread.h"
47 #include "toolbox/logging.h"
48 #include "toolbox/memory.h"
49
50
51 /********* general types, variables and auxiliary functions *********/
52
53 static int mmapcodesize = 0;
54 static void *mmapcodeptr = NULL;
55
56
57 /*******************************************************************************
58
59     This structure is used for dump memory allocation if cacao runs without
60     threads.
61
62 *******************************************************************************/
63
64 #if !defined(USE_THREADS)
65 static dumpinfo nothreads_dumpinfo;
66 #endif
67
68
69 void *mem_mmap(int size)
70 {
71         void *m;
72
73         size = ALIGN(size, ALIGNSIZE);
74
75         if (size > mmapcodesize) {
76                 mmapcodesize = 0x10000;
77
78                 if (size > mmapcodesize)
79                         mmapcodesize = size;
80
81                 mmapcodesize = ALIGN(mmapcodesize, getpagesize());
82                 mmapcodeptr = mmap(NULL,
83                                                    (size_t) mmapcodesize,
84                                                    PROT_READ | PROT_WRITE | PROT_EXEC,
85                                                    MAP_PRIVATE | MAP_ANONYMOUS,
86                                                    -1,
87                                                    (off_t) 0);
88
89                 if (mmapcodeptr == MAP_FAILED)
90                         throw_cacao_exception_exit(string_java_lang_InternalError,
91                                                                            "Out of memory");
92         }
93
94         m = mmapcodeptr;
95         mmapcodeptr = (void *) ((char *) mmapcodeptr + size);
96         mmapcodesize -= size;
97
98         return m;
99 }
100
101
102 static void *checked_alloc(int size)
103 {
104         /* always allocate memory zeroed out */
105         void *m = calloc(size, 1);
106
107         if (!m)
108                 throw_cacao_exception_exit(string_java_lang_InternalError,
109                                                                    "Out of memory");
110
111         return m;
112 }
113
114
115 void *mem_alloc(int size)
116 {
117         if (size == 0)
118                 return NULL;
119
120         if (opt_stat) {
121                 memoryusage += size;
122
123                 if (memoryusage > maxmemusage)
124                         maxmemusage = memoryusage;
125         }
126         
127         return checked_alloc(size);
128 }
129
130
131 void *mem_realloc(void *src, int len1, int len2)
132 {
133         void *dst;
134
135         if (!src) {
136                 if (len1 != 0)
137                         panic("reallocating memoryblock with address NULL, length != 0");
138         }
139
140         if (opt_stat)
141                 memoryusage = (memoryusage - len1) + len2;
142
143         dst = realloc(src, len2);
144
145         if (!dst)
146                 throw_cacao_exception_exit(string_java_lang_InternalError,
147                                                                    "Out of memory");
148
149         return dst;
150 }
151
152
153 void mem_free(void *m, int size)
154 {
155         if (!m) {
156                 if (size == 0)
157                         return;
158                 panic("returned memoryblock with address NULL, length != 0");
159         }
160
161         if (opt_stat)
162                 memoryusage -= size;
163
164         free(m);
165 }
166
167
168 void *dump_alloc(int size)
169 {
170         void *m;
171         dumpinfo *di;
172
173         /* If no threads are used, the dumpinfo structure is a static structure   */
174         /* defined at the top of this file.                                       */
175 #if defined(USE_THREADS)
176         di = &((threadobject *) THREADOBJECT)->dumpinfo;
177 #else
178         di = &nothreads_dumpinfo;
179 #endif
180
181         if (size == 0)
182                 return NULL;
183
184         size = ALIGN(size, ALIGNSIZE);
185
186         if (di->useddumpsize + size > di->allocateddumpsize) {
187                 dumpblock *newdumpblock;
188                 s4 newdumpblocksize;
189
190                 /* allocate a new dumplist structure */
191                 newdumpblock = checked_alloc(sizeof(dumpblock));
192
193                 /* If requested size is greater than the default, make the new dump   */
194                 /* block as big as the size requested. Else use the default size.     */
195                 if (size > DUMPBLOCKSIZE) {
196                         newdumpblocksize = size;
197
198                 } else {
199                         newdumpblocksize = DUMPBLOCKSIZE;
200                 }
201
202                 /* allocate dumpblock memory */
203                 //printf("new dumpblock: %d\n", newdumpblocksize);
204                 newdumpblock->dumpmem = checked_alloc(newdumpblocksize);
205
206                 newdumpblock->prev = di->currentdumpblock;
207                 newdumpblock->size = newdumpblocksize;
208                 di->currentdumpblock = newdumpblock;
209
210                 /* Used dump size is previously allocated dump size, because the      */
211                 /* remaining free memory of the previous dump block cannot be used.   */
212                 //printf("unused memory: %d\n", allocateddumpsize - useddumpsize);
213                 di->useddumpsize = di->allocateddumpsize;
214
215                 /* increase the allocated dump size by the size of the new dump block */
216                 di->allocateddumpsize += newdumpblocksize;
217
218                 /* the amount of globally allocated dump memory (thread save)         */
219                 if (opt_stat)
220                         globalallocateddumpsize += newdumpblocksize;
221         }
222
223         /* current dump block base address + the size of the current dump block - */
224         /* the size of the unused memory = new start address                      */
225         m = di->currentdumpblock->dumpmem + di->currentdumpblock->size -
226                 (di->allocateddumpsize - di->useddumpsize);
227
228         /* increase used dump size by the allocated memory size                   */
229         di->useddumpsize += size;
230
231         if (opt_stat) {
232                 if (di->useddumpsize > maxdumpsize) {
233                         maxdumpsize = di->useddumpsize;
234                 }
235         }
236                 
237         return m;
238 }   
239
240
241 void *dump_realloc(void *src, int len1, int len2)
242 {
243         void *dst = dump_alloc(len2);
244
245         memcpy(dst, src, len1);
246
247         return dst;
248 }
249
250
251 void dump_release(int size)
252 {
253         dumpinfo *di;
254
255         /* If no threads are used, the dumpinfo structure is a static structure   */
256         /* defined at the top of this file.                                       */
257 #if defined(USE_THREADS)
258         di = &((threadobject *) THREADOBJECT)->dumpinfo;
259 #else
260         di = &nothreads_dumpinfo;
261 #endif
262
263         if (size < 0 || size > di->useddumpsize)
264                 throw_cacao_exception_exit(string_java_lang_InternalError,
265                                                                    "Illegal dump release size %d", size);
266
267         /* reset the used dump size to the size specified                         */
268         di->useddumpsize = size;
269
270         while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
271                 dumpblock *tmp = di->currentdumpblock;
272
273 #if 0
274                 /* XXX TWISTI: can someone explain this to me? */
275 #ifdef TRACECALLARGS
276                 /* Keep the first dumpblock if we don't free memory. Otherwise
277                  * a new dumpblock is allocated each time and we run out of
278                  * memory.
279                  */
280                 if (!oldtop->prev) break;
281 #endif
282 #endif
283
284                 di->allocateddumpsize -= tmp->size;
285                 di->currentdumpblock = tmp->prev;
286
287                 /* the amount of globally allocated dump memory (thread save)         */
288                 if (opt_stat)
289                         globalallocateddumpsize -= tmp->size;
290
291                 /* release the dump memory and the dumpinfo structure                 */
292                 free(tmp->dumpmem);
293                 free(tmp);
294         }
295 }
296
297
298 long dump_size()
299 {
300         dumpinfo *di;
301
302         /* If no threads are used, the dumpinfo structure is a static structure   */
303         /* defined at the top of this file.                                       */
304 #if defined(USE_THREADS)
305         di = &((threadobject *) THREADOBJECT)->dumpinfo;
306 #else
307         di = &nothreads_dumpinfo;
308 #endif
309
310         return di->useddumpsize;
311 }
312
313
314 /*
315  * These are local overrides for various environment variables in Emacs.
316  * Please do not remove this and leave it at the end of the file, where
317  * Emacs will automagically detect them.
318  * ---------------------------------------------------------------------
319  * Local variables:
320  * mode: c
321  * indent-tabs-mode: t
322  * c-basic-offset: 4
323  * tab-width: 4
324  * End:
325  */