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 3205 2005-09-19 09:02:55Z twisti $
41 #if defined(__DARWIN__)
42 /* If we compile with -ansi on darwin, <sys/types.h> is not included. So */
43 /* let's do it here. */
44 # include <sys/types.h>
51 # include <sys/mman.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 /********* general types, variables and auxiliary functions *********/
77 static int mmapcodesize = 0;
78 static void *mmapcodeptr = NULL;
82 /*******************************************************************************
84 This structure is used for dump memory allocation if cacao runs
87 *******************************************************************************/
89 #if !defined(USE_THREADS) || (defined(USE_THREADS) && !defined(NATIVE_THREADS))
90 static dumpinfo _no_threads_dumpinfo;
93 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
94 #define DUMPINFO &((threadobject *) THREADOBJECT)->dumpinfo
96 #define DUMPINFO &_no_threads_dumpinfo
101 void *mem_mmap(s4 size)
105 size = ALIGN(size, ALIGNSIZE);
107 if (size > mmapcodesize) {
108 mmapcodesize = 0x10000;
110 if (size > mmapcodesize)
113 mmapcodesize = ALIGN(mmapcodesize, getpagesize());
114 mmapcodeptr = mmap(NULL,
115 (size_t) mmapcodesize,
116 PROT_READ | PROT_WRITE | PROT_EXEC,
118 #if defined(HAVE_MAP_ANONYMOUS)
120 #elif defined(HAVE_MAP_ANON)
128 if (mmapcodeptr == MAP_FAILED)
129 throw_cacao_exception_exit(string_java_lang_InternalError,
134 mmapcodeptr = (void *) ((char *) mmapcodeptr + size);
135 mmapcodesize -= size;
142 static void *checked_alloc(s4 size)
144 /* always allocate memory zeroed out */
145 void *m = calloc(size, 1);
148 throw_cacao_exception_exit(string_java_lang_InternalError,
155 void *mem_alloc(s4 size)
160 #if defined(STATISTICS)
164 if (memoryusage > maxmemusage)
165 maxmemusage = memoryusage;
169 return checked_alloc(size);
173 void *mem_realloc(void *src, s4 len1, s4 len2)
179 log_text("reallocating memoryblock with address NULL, length != 0");
184 #if defined(STATISTICS)
186 memoryusage = (memoryusage - len1) + len2;
189 dst = realloc(src, len2);
192 throw_cacao_exception_exit(string_java_lang_InternalError,
199 void mem_free(void *m, s4 size)
205 log_text("returned memoryblock with address NULL, length != 0");
209 #if defined(STATISTICS)
218 /* dump_alloc ******************************************************************
222 *******************************************************************************/
224 void *dump_alloc(s4 size)
226 #if defined(DISABLE_DUMP)
227 /* use malloc memory for dump memory (for debugging only!) */
229 return mem_alloc(size);
234 /* If no threads are used, the dumpinfo structure is a static structure */
235 /* defined at the top of this file. */
242 size = ALIGN(size, ALIGNSIZE);
244 if (di->useddumpsize + size > di->allocateddumpsize) {
245 dumpblock *newdumpblock;
248 /* allocate a new dumplist structure */
250 newdumpblock = checked_alloc(sizeof(dumpblock));
252 /* If requested size is greater than the default, make the new dump */
253 /* block as big as the size requested. Else use the default size. */
255 if (size > DUMPBLOCKSIZE) {
256 newdumpblocksize = size;
259 newdumpblocksize = DUMPBLOCKSIZE;
262 /* allocate dumpblock memory */
264 newdumpblock->dumpmem = checked_alloc(newdumpblocksize);
266 newdumpblock->prev = di->currentdumpblock;
267 newdumpblock->size = newdumpblocksize;
268 di->currentdumpblock = newdumpblock;
270 /* Used dump size is previously allocated dump size, because the */
271 /* remaining free memory of the previous dump block cannot be used. */
273 di->useddumpsize = di->allocateddumpsize;
275 /* increase the allocated dump size by the size of the new dump block */
277 di->allocateddumpsize += newdumpblocksize;
279 #if defined(STATISTICS)
280 /* the amount of globally allocated dump memory (thread save) */
283 globalallocateddumpsize += newdumpblocksize;
287 /* current dump block base address + the size of the current dump block - */
288 /* the size of the unused memory = new start address */
290 m = di->currentdumpblock->dumpmem + di->currentdumpblock->size -
291 (di->allocateddumpsize - di->useddumpsize);
293 /* increase used dump size by the allocated memory size */
295 di->useddumpsize += size;
297 #if defined(STATISTICS)
299 if (di->useddumpsize > maxdumpsize)
300 maxdumpsize = di->useddumpsize;
304 #endif /* defined(DISABLE_DUMP) */
308 /* dump_realloc ****************************************************************
312 *******************************************************************************/
314 void *dump_realloc(void *src, s4 len1, s4 len2)
316 #if defined(DISABLE_DUMP)
317 /* use malloc memory for dump memory (for debugging only!) */
319 return mem_realloc(src, len1, len2);
321 void *dst = dump_alloc(len2);
323 memcpy(dst, src, len1);
330 /* dump_release ****************************************************************
334 *******************************************************************************/
336 void dump_release(s4 size)
338 #if defined(DISABLE_DUMP)
339 /* use malloc memory for dump memory (for debugging only!) */
345 /* If no threads are used, the dumpinfo structure is a static structure */
346 /* defined at the top of this file. */
350 if (size < 0 || size > di->useddumpsize)
351 throw_cacao_exception_exit(string_java_lang_InternalError,
352 "Illegal dump release size %d", size);
354 /* reset the used dump size to the size specified */
356 di->useddumpsize = size;
358 while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
359 dumpblock *tmp = di->currentdumpblock;
362 /* XXX TWISTI: can someone explain this to me? */
364 /* Keep the first dumpblock if we don't free memory. Otherwise
365 * a new dumpblock is allocated each time and we run out of
368 if (!oldtop->prev) break;
372 di->allocateddumpsize -= tmp->size;
373 di->currentdumpblock = tmp->prev;
375 #if defined(STATISTICS)
376 /* the amount of globally allocated dump memory (thread save) */
379 globalallocateddumpsize -= tmp->size;
382 /* release the dump memory and the dumpinfo structure */
387 #endif /* defined(DISABLE_DUMP) */
391 /* dump_size *******************************************************************
395 *******************************************************************************/
399 #if defined(DISABLE_DUMP)
400 /* use malloc memory for dump memory (for debugging only!) */
406 /* If no threads are used, the dumpinfo structure is a static structure */
407 /* defined at the top of this file. */
414 return di->useddumpsize;
415 #endif /* defined(DISABLE_DUMP) */
420 * These are local overrides for various environment variables in Emacs.
421 * Please do not remove this and leave it at the end of the file, where
422 * Emacs will automagically detect them.
423 * ---------------------------------------------------------------------
426 * indent-tabs-mode: t