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 2494 2005-05-21 15:07:00Z 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 "mm/memory.h"
52 #include "native/native.h"
54 #if defined(USE_THREADS)
55 # if defined(NATIVE_THREADS)
56 # include "threads/native/threads.h"
58 # include "threads/green/threads.h"
62 #include "toolbox/logging.h"
63 #include "vm/exceptions.h"
64 #include "vm/global.h"
65 #include "vm/options.h"
66 #include "vm/statistics.h"
67 #include "vm/stringlocal.h"
70 /********* general types, variables and auxiliary functions *********/
72 static int mmapcodesize = 0;
73 static void *mmapcodeptr = NULL;
76 /*******************************************************************************
78 This structure is used for dump memory allocation if cacao runs without
81 *******************************************************************************/
83 #if !defined(USE_THREADS) || (defined(USE_THREADS) && !defined(NATIVE_THREADS))
84 static dumpinfo nothreads_dumpinfo;
88 void *mem_mmap(s4 size)
92 size = ALIGN(size, ALIGNSIZE);
94 if (size > mmapcodesize) {
95 mmapcodesize = 0x10000;
97 if (size > mmapcodesize)
100 mmapcodesize = ALIGN(mmapcodesize, getpagesize());
101 mmapcodeptr = mmap(NULL,
102 (size_t) mmapcodesize,
103 PROT_READ | PROT_WRITE | PROT_EXEC,
105 #if defined(HAVE_MAP_ANONYMOUS)
107 #elif defined(HAVE_MAP_ANON)
115 if (mmapcodeptr == MAP_FAILED)
116 throw_cacao_exception_exit(string_java_lang_InternalError,
121 mmapcodeptr = (void *) ((char *) mmapcodeptr + size);
122 mmapcodesize -= size;
128 static void *checked_alloc(s4 size)
130 /* always allocate memory zeroed out */
131 void *m = calloc(size, 1);
134 throw_cacao_exception_exit(string_java_lang_InternalError,
141 void *mem_alloc(s4 size)
146 #if defined(STATISTICS)
150 if (memoryusage > maxmemusage)
151 maxmemusage = memoryusage;
155 return checked_alloc(size);
159 void *mem_realloc(void *src, s4 len1, s4 len2)
165 log_text("reallocating memoryblock with address NULL, length != 0");
170 #if defined(STATISTICS)
172 memoryusage = (memoryusage - len1) + len2;
175 dst = realloc(src, len2);
178 throw_cacao_exception_exit(string_java_lang_InternalError,
185 void mem_free(void *m, s4 size)
191 log_text("returned memoryblock with address NULL, length != 0");
195 #if defined(STATISTICS)
204 void *dump_alloc(s4 size)
209 /* If no threads are used, the dumpinfo structure is a static structure */
210 /* defined at the top of this file. */
211 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
212 di = &((threadobject *) THREADOBJECT)->dumpinfo;
214 di = ¬hreads_dumpinfo;
220 size = ALIGN(size, ALIGNSIZE);
222 if (di->useddumpsize + size > di->allocateddumpsize) {
223 dumpblock *newdumpblock;
226 /* allocate a new dumplist structure */
227 newdumpblock = checked_alloc(sizeof(dumpblock));
229 /* If requested size is greater than the default, make the new dump */
230 /* block as big as the size requested. Else use the default size. */
231 if (size > DUMPBLOCKSIZE) {
232 newdumpblocksize = size;
235 newdumpblocksize = DUMPBLOCKSIZE;
238 /* allocate dumpblock memory */
239 /*printf("new dumpblock: %d\n", newdumpblocksize);*/
240 newdumpblock->dumpmem = checked_alloc(newdumpblocksize);
242 newdumpblock->prev = di->currentdumpblock;
243 newdumpblock->size = newdumpblocksize;
244 di->currentdumpblock = newdumpblock;
246 /* Used dump size is previously allocated dump size, because the */
247 /* remaining free memory of the previous dump block cannot be used. */
248 /*printf("unused memory: %d\n", allocateddumpsize - useddumpsize);*/
249 di->useddumpsize = di->allocateddumpsize;
251 /* increase the allocated dump size by the size of the new dump block */
252 di->allocateddumpsize += newdumpblocksize;
254 #if defined(STATISTICS)
255 /* the amount of globally allocated dump memory (thread save) */
257 globalallocateddumpsize += newdumpblocksize;
261 /* current dump block base address + the size of the current dump block - */
262 /* the size of the unused memory = new start address */
263 m = di->currentdumpblock->dumpmem + di->currentdumpblock->size -
264 (di->allocateddumpsize - di->useddumpsize);
266 /* increase used dump size by the allocated memory size */
267 di->useddumpsize += size;
269 #if defined(STATISTICS)
271 if (di->useddumpsize > maxdumpsize) {
272 maxdumpsize = di->useddumpsize;
281 void *dump_realloc(void *src, s4 len1, s4 len2)
283 void *dst = dump_alloc(len2);
285 memcpy(dst, src, len1);
291 void dump_release(s4 size)
295 /* If no threads are used, the dumpinfo structure is a static structure */
296 /* defined at the top of this file. */
297 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
298 di = &((threadobject *) THREADOBJECT)->dumpinfo;
300 di = ¬hreads_dumpinfo;
303 if (size < 0 || size > di->useddumpsize)
304 throw_cacao_exception_exit(string_java_lang_InternalError,
305 "Illegal dump release size %d", size);
307 /* reset the used dump size to the size specified */
308 di->useddumpsize = size;
310 while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
311 dumpblock *tmp = di->currentdumpblock;
314 /* XXX TWISTI: can someone explain this to me? */
316 /* Keep the first dumpblock if we don't free memory. Otherwise
317 * a new dumpblock is allocated each time and we run out of
320 if (!oldtop->prev) break;
324 di->allocateddumpsize -= tmp->size;
325 di->currentdumpblock = tmp->prev;
327 #if defined(STATISTICS)
328 /* the amount of globally allocated dump memory (thread save) */
330 globalallocateddumpsize -= tmp->size;
333 /* release the dump memory and the dumpinfo structure */
344 /* If no threads are used, the dumpinfo structure is a static structure */
345 /* defined at the top of this file. */
346 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
347 di = &((threadobject *) THREADOBJECT)->dumpinfo;
349 di = ¬hreads_dumpinfo;
355 return di->useddumpsize;
360 * These are local overrides for various environment variables in Emacs.
361 * Please do not remove this and leave it at the end of the file, where
362 * Emacs will automagically detect them.
363 * ---------------------------------------------------------------------
366 * indent-tabs-mode: t