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 3552 2005-11-03 20:43:17Z 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 /* global code memory variables ***********************************************/
94 #define DEFAULT_CODEMEM_SIZE 128 * 1024 /* defaulting to 128kB */
96 #if defined(USE_THREADS)
97 static java_objectheader *codememlock = NULL;
99 static int codememsize = 0;
100 static void *codememptr = NULL;
103 /* memory_init *****************************************************************
105 Initialize the memory subsystem.
107 *******************************************************************************/
109 bool memory_init(void)
111 #if defined(USE_THREADS)
112 codememlock = NEW(java_objectheader);
114 # if defined(NATIVE_THREADS)
115 initObjectLock(codememlock);
119 /* everything's ok */
125 /* memory_checked_alloc ********************************************************
127 Allocated zeroed-out memory and does an OOM check.
129 *******************************************************************************/
131 static void *memory_checked_alloc(s4 size)
133 /* always allocate memory zeroed out */
135 void *p = calloc(size, 1);
138 throw_cacao_exception_exit(string_java_lang_InternalError,
145 /* memory_cnew *****************************************************************
147 Allocates memory from the heap, aligns it to architecutres PAGESIZE
148 and make the memory read-, write-, and executeable.
150 *******************************************************************************/
152 void *memory_cnew(s4 size)
157 #if defined(USE_THREADS)
158 builtin_monitorenter(codememlock);
161 size = ALIGN(size, ALIGNSIZE);
163 /* check if enough memory is available */
165 if (size > codememsize) {
166 /* set default code size */
168 codememsize = DEFAULT_CODEMEM_SIZE;
170 /* do we need more? */
172 if (size > codememsize)
175 /* get the pagesize of this architecture */
177 pagesize = getpagesize();
179 /* allocate normal heap memory */
181 if ((p = memory_checked_alloc(codememsize + pagesize - 1)) == NULL)
184 #if defined(STATISTICS)
186 codememusage += codememsize + pagesize - 1;
188 if (codememusage > maxcodememusage)
189 maxcodememusage = codememusage;
193 /* align the memory allocated to a multiple of PAGESIZE,
194 mprotect requires this */
196 p = (void *) (((ptrint) p + pagesize - 1) & ~(pagesize - 1));
198 /* make the memory read-, write-, and executeable */
200 if (mprotect(p, codememsize, PROT_READ | PROT_WRITE | PROT_EXEC) == -1)
201 throw_cacao_exception_exit(string_java_lang_InternalError,
204 /* set global code memory pointer */
209 /* get a memory chunk of the allocated memory */
212 codememptr = (void *) ((ptrint) codememptr + size);
215 #if defined(USE_THREADS)
216 builtin_monitorexit(codememlock);
223 void *mem_alloc(s4 size)
228 #if defined(STATISTICS)
232 if (memoryusage > maxmemusage)
233 maxmemusage = memoryusage;
237 return memory_checked_alloc(size);
241 void *mem_realloc(void *src, s4 len1, s4 len2)
247 log_text("reallocating memoryblock with address NULL, length != 0");
252 #if defined(STATISTICS)
254 memoryusage = (memoryusage - len1) + len2;
257 dst = realloc(src, len2);
260 throw_cacao_exception_exit(string_java_lang_InternalError,
267 void mem_free(void *m, s4 size)
273 log_text("returned memoryblock with address NULL, length != 0");
277 #if defined(STATISTICS)
286 /* dump_alloc ******************************************************************
290 *******************************************************************************/
292 void *dump_alloc(s4 size)
294 #if defined(DISABLE_DUMP)
295 /* use malloc memory for dump memory (for debugging only!) */
297 return mem_alloc(size);
302 /* If no threads are used, the dumpinfo structure is a static structure */
303 /* defined at the top of this file. */
310 size = ALIGN(size, ALIGNSIZE);
312 if (di->useddumpsize + size > di->allocateddumpsize) {
313 dumpblock *newdumpblock;
316 /* allocate a new dumplist structure */
318 newdumpblock = memory_checked_alloc(sizeof(dumpblock));
320 /* If requested size is greater than the default, make the new dump */
321 /* block as big as the size requested. Else use the default size. */
323 if (size > DUMPBLOCKSIZE) {
324 newdumpblocksize = size;
327 newdumpblocksize = DUMPBLOCKSIZE;
330 /* allocate dumpblock memory */
332 newdumpblock->dumpmem = memory_checked_alloc(newdumpblocksize);
334 newdumpblock->prev = di->currentdumpblock;
335 newdumpblock->size = newdumpblocksize;
336 di->currentdumpblock = newdumpblock;
338 /* Used dump size is previously allocated dump size, because the */
339 /* remaining free memory of the previous dump block cannot be used. */
341 di->useddumpsize = di->allocateddumpsize;
343 /* increase the allocated dump size by the size of the new dump block */
345 di->allocateddumpsize += newdumpblocksize;
347 #if defined(STATISTICS)
348 /* the amount of globally allocated dump memory (thread save) */
351 globalallocateddumpsize += newdumpblocksize;
355 /* current dump block base address + the size of the current dump block - */
356 /* the size of the unused memory = new start address */
358 m = di->currentdumpblock->dumpmem + di->currentdumpblock->size -
359 (di->allocateddumpsize - di->useddumpsize);
361 /* increase used dump size by the allocated memory size */
363 di->useddumpsize += size;
365 #if defined(STATISTICS)
367 if (di->useddumpsize > maxdumpsize)
368 maxdumpsize = di->useddumpsize;
372 #endif /* defined(DISABLE_DUMP) */
376 /* dump_realloc ****************************************************************
380 *******************************************************************************/
382 void *dump_realloc(void *src, s4 len1, s4 len2)
384 #if defined(DISABLE_DUMP)
385 /* use malloc memory for dump memory (for debugging only!) */
387 return mem_realloc(src, len1, len2);
389 void *dst = dump_alloc(len2);
391 memcpy(dst, src, len1);
398 /* dump_release ****************************************************************
402 *******************************************************************************/
404 void dump_release(s4 size)
406 #if defined(DISABLE_DUMP)
407 /* use malloc memory for dump memory (for debugging only!) */
413 /* If no threads are used, the dumpinfo structure is a static structure */
414 /* defined at the top of this file. */
418 if (size < 0 || size > di->useddumpsize)
419 throw_cacao_exception_exit(string_java_lang_InternalError,
420 "Illegal dump release size %d", size);
422 /* reset the used dump size to the size specified */
424 di->useddumpsize = size;
426 while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
427 dumpblock *tmp = di->currentdumpblock;
430 /* XXX TWISTI: can someone explain this to me? */
432 /* Keep the first dumpblock if we don't free memory. Otherwise
433 * a new dumpblock is allocated each time and we run out of
436 if (!oldtop->prev) break;
440 di->allocateddumpsize -= tmp->size;
441 di->currentdumpblock = tmp->prev;
443 #if defined(STATISTICS)
444 /* the amount of globally allocated dump memory (thread save) */
447 globalallocateddumpsize -= tmp->size;
450 /* release the dump memory and the dumpinfo structure */
455 #endif /* defined(DISABLE_DUMP) */
459 /* dump_size *******************************************************************
463 *******************************************************************************/
467 #if defined(DISABLE_DUMP)
468 /* use malloc memory for dump memory (for debugging only!) */
474 /* If no threads are used, the dumpinfo structure is a static structure */
475 /* defined at the top of this file. */
482 return di->useddumpsize;
483 #endif /* defined(DISABLE_DUMP) */
488 * These are local overrides for various environment variables in Emacs.
489 * Please do not remove this and leave it at the end of the file, where
490 * Emacs will automagically detect them.
491 * ---------------------------------------------------------------------
494 * indent-tabs-mode: t