3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
29 Changes: Christian Thalinger
31 $Id: memory.c 3479 2005-10-21 13:08:19Z twisti $
44 #if defined(__DARWIN__)
45 /* If we compile with -ansi on darwin, <sys/types.h> is not included. So */
46 /* let's do it here. */
47 # include <sys/types.h>
55 #include "mm/memory.h"
56 #include "native/native.h"
58 #if defined(USE_THREADS)
59 # if defined(NATIVE_THREADS)
60 # include "threads/native/threads.h"
62 # include "threads/green/threads.h"
66 #include "toolbox/logging.h"
67 #include "vm/exceptions.h"
68 #include "vm/global.h"
69 #include "vm/options.h"
70 #include "vm/statistics.h"
71 #include "vm/stringlocal.h"
74 /*******************************************************************************
76 This structure is used for dump memory allocation if cacao
79 *******************************************************************************/
81 #if !defined(USE_THREADS) || (defined(USE_THREADS) && !defined(NATIVE_THREADS))
82 static dumpinfo _no_threads_dumpinfo;
85 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
86 #define DUMPINFO &((threadobject *) THREADOBJECT)->dumpinfo
88 #define DUMPINFO &_no_threads_dumpinfo
92 static void *checked_alloc(s4 size)
94 /* always allocate memory zeroed out */
96 void *m = calloc(size, 1);
99 throw_cacao_exception_exit(string_java_lang_InternalError,
106 /* memory_cnew *****************************************************************
108 Allocates memory from the heap, aligns it to architecutres PAGESIZE
109 and make the memory read-, write-, and executeable.
111 *******************************************************************************/
113 void *memory_cnew(s4 size)
118 /* get the pagesize of this architecture */
120 pagesize = getpagesize();
122 /* allocate normal heap memory */
124 if ((p = mem_alloc(size + pagesize - 1)) == NULL)
127 /* align the memory allocated to a multiple of PAGESIZE, mprotect
130 p = (void *) (((ptrint) p + pagesize - 1) & ~(pagesize - 1));
132 /* make the memory read-, write-, and executeable */
134 if (mprotect(p, size, PROT_READ | PROT_WRITE | PROT_EXEC) == -1)
135 throw_cacao_exception_exit(string_java_lang_InternalError,
142 void *mem_alloc(s4 size)
147 #if defined(STATISTICS)
151 if (memoryusage > maxmemusage)
152 maxmemusage = memoryusage;
156 return checked_alloc(size);
160 void *mem_realloc(void *src, s4 len1, s4 len2)
166 log_text("reallocating memoryblock with address NULL, length != 0");
171 #if defined(STATISTICS)
173 memoryusage = (memoryusage - len1) + len2;
176 dst = realloc(src, len2);
179 throw_cacao_exception_exit(string_java_lang_InternalError,
186 void mem_free(void *m, s4 size)
192 log_text("returned memoryblock with address NULL, length != 0");
196 #if defined(STATISTICS)
205 /* dump_alloc ******************************************************************
209 *******************************************************************************/
211 void *dump_alloc(s4 size)
213 #if defined(DISABLE_DUMP)
214 /* use malloc memory for dump memory (for debugging only!) */
216 return mem_alloc(size);
221 /* If no threads are used, the dumpinfo structure is a static structure */
222 /* defined at the top of this file. */
229 size = ALIGN(size, ALIGNSIZE);
231 if (di->useddumpsize + size > di->allocateddumpsize) {
232 dumpblock *newdumpblock;
235 /* allocate a new dumplist structure */
237 newdumpblock = checked_alloc(sizeof(dumpblock));
239 /* If requested size is greater than the default, make the new dump */
240 /* block as big as the size requested. Else use the default size. */
242 if (size > DUMPBLOCKSIZE) {
243 newdumpblocksize = size;
246 newdumpblocksize = DUMPBLOCKSIZE;
249 /* allocate dumpblock memory */
251 newdumpblock->dumpmem = checked_alloc(newdumpblocksize);
253 newdumpblock->prev = di->currentdumpblock;
254 newdumpblock->size = newdumpblocksize;
255 di->currentdumpblock = newdumpblock;
257 /* Used dump size is previously allocated dump size, because the */
258 /* remaining free memory of the previous dump block cannot be used. */
260 di->useddumpsize = di->allocateddumpsize;
262 /* increase the allocated dump size by the size of the new dump block */
264 di->allocateddumpsize += newdumpblocksize;
266 #if defined(STATISTICS)
267 /* the amount of globally allocated dump memory (thread save) */
270 globalallocateddumpsize += newdumpblocksize;
274 /* current dump block base address + the size of the current dump block - */
275 /* the size of the unused memory = new start address */
277 m = di->currentdumpblock->dumpmem + di->currentdumpblock->size -
278 (di->allocateddumpsize - di->useddumpsize);
280 /* increase used dump size by the allocated memory size */
282 di->useddumpsize += size;
284 #if defined(STATISTICS)
286 if (di->useddumpsize > maxdumpsize)
287 maxdumpsize = di->useddumpsize;
291 #endif /* defined(DISABLE_DUMP) */
295 /* dump_realloc ****************************************************************
299 *******************************************************************************/
301 void *dump_realloc(void *src, s4 len1, s4 len2)
303 #if defined(DISABLE_DUMP)
304 /* use malloc memory for dump memory (for debugging only!) */
306 return mem_realloc(src, len1, len2);
308 void *dst = dump_alloc(len2);
310 memcpy(dst, src, len1);
317 /* dump_release ****************************************************************
321 *******************************************************************************/
323 void dump_release(s4 size)
325 #if defined(DISABLE_DUMP)
326 /* use malloc memory for dump memory (for debugging only!) */
332 /* If no threads are used, the dumpinfo structure is a static structure */
333 /* defined at the top of this file. */
337 if (size < 0 || size > di->useddumpsize)
338 throw_cacao_exception_exit(string_java_lang_InternalError,
339 "Illegal dump release size %d", size);
341 /* reset the used dump size to the size specified */
343 di->useddumpsize = size;
345 while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
346 dumpblock *tmp = di->currentdumpblock;
349 /* XXX TWISTI: can someone explain this to me? */
351 /* Keep the first dumpblock if we don't free memory. Otherwise
352 * a new dumpblock is allocated each time and we run out of
355 if (!oldtop->prev) break;
359 di->allocateddumpsize -= tmp->size;
360 di->currentdumpblock = tmp->prev;
362 #if defined(STATISTICS)
363 /* the amount of globally allocated dump memory (thread save) */
366 globalallocateddumpsize -= tmp->size;
369 /* release the dump memory and the dumpinfo structure */
374 #endif /* defined(DISABLE_DUMP) */
378 /* dump_size *******************************************************************
382 *******************************************************************************/
386 #if defined(DISABLE_DUMP)
387 /* use malloc memory for dump memory (for debugging only!) */
393 /* If no threads are used, the dumpinfo structure is a static structure */
394 /* defined at the top of this file. */
401 return di->useddumpsize;
402 #endif /* defined(DISABLE_DUMP) */
407 * These are local overrides for various environment variables in Emacs.
408 * Please do not remove this and leave it at the end of the file, where
409 * Emacs will automagically detect them.
410 * ---------------------------------------------------------------------
413 * indent-tabs-mode: t