3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
29 Changes: Christian Thalinger
31 $Id: memory.c 4357 2006-01-22 23:33:38Z 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 exceptions_throw_outofmemory_exit();
144 /* memory_cnew *****************************************************************
146 Allocates memory from the heap, aligns it to architecutres PAGESIZE
147 and make the memory read-, write-, and executeable.
149 *******************************************************************************/
151 void *memory_cnew(s4 size)
156 #if defined(USE_THREADS)
157 builtin_monitorenter(codememlock);
160 size = ALIGN(size, ALIGNSIZE);
162 /* check if enough memory is available */
164 if (size > codememsize) {
165 /* set default code size */
167 codememsize = DEFAULT_CODEMEM_SIZE;
169 /* do we need more? */
171 if (size > codememsize)
174 /* get the pagesize of this architecture */
176 pagesize = getpagesize();
178 /* allocate normal heap memory */
180 if ((p = memory_checked_alloc(codememsize + pagesize - 1)) == NULL)
183 #if defined(ENABLE_STATISTICS)
185 codememusage += codememsize + pagesize - 1;
187 if (codememusage > maxcodememusage)
188 maxcodememusage = codememusage;
192 /* align the memory allocated to a multiple of PAGESIZE,
193 mprotect requires this */
195 p = (void *) (((ptrint) p + pagesize - 1) & ~(pagesize - 1));
197 /* make the memory read-, write-, and executeable */
199 if (mprotect(p, codememsize, PROT_READ | PROT_WRITE | PROT_EXEC) == -1)
200 throw_cacao_exception_exit(string_java_lang_InternalError,
203 /* set global code memory pointer */
208 /* get a memory chunk of the allocated memory */
211 codememptr = (void *) ((ptrint) codememptr + size);
214 #if defined(USE_THREADS)
215 builtin_monitorexit(codememlock);
222 void *mem_alloc(s4 size)
227 #if defined(ENABLE_STATISTICS)
231 if (memoryusage > maxmemusage)
232 maxmemusage = memoryusage;
236 return memory_checked_alloc(size);
240 void *mem_realloc(void *src, s4 len1, s4 len2)
246 log_text("reallocating memoryblock with address NULL, length != 0");
251 #if defined(ENABLE_STATISTICS)
253 memoryusage = (memoryusage - len1) + len2;
256 dst = realloc(src, len2);
259 exceptions_throw_outofmemory_exit();
265 void mem_free(void *m, s4 size)
271 log_text("returned memoryblock with address NULL, length != 0");
275 #if defined(ENABLE_STATISTICS)
284 /* dump_alloc ******************************************************************
288 *******************************************************************************/
290 void *dump_alloc(s4 size)
292 #if defined(DISABLE_DUMP)
293 /* use malloc memory for dump memory (for debugging only!) */
295 return mem_alloc(size);
300 /* If no threads are used, the dumpinfo structure is a static structure */
301 /* defined at the top of this file. */
308 size = ALIGN(size, ALIGNSIZE);
310 if (di->useddumpsize + size > di->allocateddumpsize) {
311 dumpblock *newdumpblock;
314 /* allocate a new dumplist structure */
316 newdumpblock = memory_checked_alloc(sizeof(dumpblock));
318 /* If requested size is greater than the default, make the new dump */
319 /* block as big as the size requested. Else use the default size. */
321 if (size > DUMPBLOCKSIZE) {
322 newdumpblocksize = size;
325 newdumpblocksize = DUMPBLOCKSIZE;
328 /* allocate dumpblock memory */
330 newdumpblock->dumpmem = memory_checked_alloc(newdumpblocksize);
332 newdumpblock->prev = di->currentdumpblock;
333 newdumpblock->size = newdumpblocksize;
334 di->currentdumpblock = newdumpblock;
336 /* Used dump size is previously allocated dump size, because the */
337 /* remaining free memory of the previous dump block cannot be used. */
339 di->useddumpsize = di->allocateddumpsize;
341 /* increase the allocated dump size by the size of the new dump block */
343 di->allocateddumpsize += newdumpblocksize;
345 #if defined(ENABLE_STATISTICS)
346 /* the amount of globally allocated dump memory (thread save) */
349 globalallocateddumpsize += newdumpblocksize;
353 /* current dump block base address + the size of the current dump block - */
354 /* the size of the unused memory = new start address */
356 m = di->currentdumpblock->dumpmem + di->currentdumpblock->size -
357 (di->allocateddumpsize - di->useddumpsize);
359 /* increase used dump size by the allocated memory size */
361 di->useddumpsize += size;
363 #if defined(ENABLE_STATISTICS)
365 if (di->useddumpsize > maxdumpsize)
366 maxdumpsize = di->useddumpsize;
370 #endif /* defined(DISABLE_DUMP) */
374 /* dump_realloc ****************************************************************
378 *******************************************************************************/
380 void *dump_realloc(void *src, s4 len1, s4 len2)
382 #if defined(DISABLE_DUMP)
383 /* use malloc memory for dump memory (for debugging only!) */
385 return mem_realloc(src, len1, len2);
387 void *dst = dump_alloc(len2);
389 memcpy(dst, src, len1);
396 /* dump_release ****************************************************************
400 *******************************************************************************/
402 void dump_release(s4 size)
404 #if defined(DISABLE_DUMP)
405 /* use malloc memory for dump memory (for debugging only!) */
411 /* If no threads are used, the dumpinfo structure is a static structure */
412 /* defined at the top of this file. */
416 if (size < 0 || size > di->useddumpsize)
417 throw_cacao_exception_exit(string_java_lang_InternalError,
418 "Illegal dump release size %d", size);
420 /* reset the used dump size to the size specified */
422 di->useddumpsize = size;
424 while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
425 dumpblock *tmp = di->currentdumpblock;
428 /* XXX TWISTI: can someone explain this to me? */
430 /* Keep the first dumpblock if we don't free memory. Otherwise
431 * a new dumpblock is allocated each time and we run out of
434 if (!oldtop->prev) break;
438 di->allocateddumpsize -= tmp->size;
439 di->currentdumpblock = tmp->prev;
441 #if defined(ENABLE_STATISTICS)
442 /* the amount of globally allocated dump memory (thread save) */
445 globalallocateddumpsize -= tmp->size;
448 /* release the dump memory and the dumpinfo structure */
453 #endif /* defined(DISABLE_DUMP) */
457 /* dump_size *******************************************************************
461 *******************************************************************************/
465 #if defined(DISABLE_DUMP)
466 /* use malloc memory for dump memory (for debugging only!) */
472 /* If no threads are used, the dumpinfo structure is a static structure */
473 /* defined at the top of this file. */
480 return di->useddumpsize;
481 #endif /* defined(DISABLE_DUMP) */
486 * These are local overrides for various environment variables in Emacs.
487 * Please do not remove this and leave it at the end of the file, where
488 * Emacs will automagically detect them.
489 * ---------------------------------------------------------------------
492 * indent-tabs-mode: t