3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
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 $Id: memory.c 1621 2004-11-30 13:06:55Z twisti $
38 #if defined(__DARWIN__)
39 /* If we compile with -ansi on darwin, <sys/types.h> is not included. So */
40 /* let's do it here. */
41 # include <sys/types.h>
48 #include "mm/memory.h"
49 #include "vm/exceptions.h"
50 #include "vm/global.h"
51 #include "vm/options.h"
52 #include "vm/statistics.h"
53 #include "native/native.h"
55 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
56 # include "threads/native/threads.h"
59 #include "toolbox/logging.h"
62 /********* general types, variables and auxiliary functions *********/
64 static int mmapcodesize = 0;
65 static void *mmapcodeptr = NULL;
68 /*******************************************************************************
70 This structure is used for dump memory allocation if cacao runs without
73 *******************************************************************************/
75 #if !defined(USE_THREADS)
76 static dumpinfo nothreads_dumpinfo;
80 void *mem_mmap(s4 size)
84 size = ALIGN(size, ALIGNSIZE);
86 if (size > mmapcodesize) {
87 mmapcodesize = 0x10000;
89 if (size > mmapcodesize)
92 mmapcodesize = ALIGN(mmapcodesize, getpagesize());
93 mmapcodeptr = mmap(NULL,
94 (size_t) mmapcodesize,
95 PROT_READ | PROT_WRITE | PROT_EXEC,
97 #if defined(HAVE_MAP_ANONYMOUS)
99 #elif defined(HAVE_MAP_ANON)
107 if (mmapcodeptr == MAP_FAILED)
108 throw_cacao_exception_exit(string_java_lang_InternalError,
113 mmapcodeptr = (void *) ((char *) mmapcodeptr + size);
114 mmapcodesize -= size;
120 static void *checked_alloc(s4 size)
122 /* always allocate memory zeroed out */
123 void *m = calloc(size, 1);
126 throw_cacao_exception_exit(string_java_lang_InternalError,
133 void *mem_alloc(s4 size)
141 if (memoryusage > maxmemusage)
142 maxmemusage = memoryusage;
145 return checked_alloc(size);
149 void *mem_realloc(void *src, s4 len1, s4 len2)
155 panic("reallocating memoryblock with address NULL, length != 0");
159 memoryusage = (memoryusage - len1) + len2;
161 dst = realloc(src, len2);
164 throw_cacao_exception_exit(string_java_lang_InternalError,
171 void mem_free(void *m, s4 size)
176 panic("returned memoryblock with address NULL, length != 0");
186 void *dump_alloc(s4 size)
191 /* If no threads are used, the dumpinfo structure is a static structure */
192 /* defined at the top of this file. */
193 #if defined(USE_THREADS)
194 di = &((threadobject *) THREADOBJECT)->dumpinfo;
196 di = ¬hreads_dumpinfo;
202 size = ALIGN(size, ALIGNSIZE);
204 if (di->useddumpsize + size > di->allocateddumpsize) {
205 dumpblock *newdumpblock;
208 /* allocate a new dumplist structure */
209 newdumpblock = checked_alloc(sizeof(dumpblock));
211 /* If requested size is greater than the default, make the new dump */
212 /* block as big as the size requested. Else use the default size. */
213 if (size > DUMPBLOCKSIZE) {
214 newdumpblocksize = size;
217 newdumpblocksize = DUMPBLOCKSIZE;
220 /* allocate dumpblock memory */
221 /*printf("new dumpblock: %d\n", newdumpblocksize);*/
222 newdumpblock->dumpmem = checked_alloc(newdumpblocksize);
224 newdumpblock->prev = di->currentdumpblock;
225 newdumpblock->size = newdumpblocksize;
226 di->currentdumpblock = newdumpblock;
228 /* Used dump size is previously allocated dump size, because the */
229 /* remaining free memory of the previous dump block cannot be used. */
230 /*printf("unused memory: %d\n", allocateddumpsize - useddumpsize);*/
231 di->useddumpsize = di->allocateddumpsize;
233 /* increase the allocated dump size by the size of the new dump block */
234 di->allocateddumpsize += newdumpblocksize;
236 /* the amount of globally allocated dump memory (thread save) */
238 globalallocateddumpsize += newdumpblocksize;
241 /* current dump block base address + the size of the current dump block - */
242 /* the size of the unused memory = new start address */
243 m = di->currentdumpblock->dumpmem + di->currentdumpblock->size -
244 (di->allocateddumpsize - di->useddumpsize);
246 /* increase used dump size by the allocated memory size */
247 di->useddumpsize += size;
250 if (di->useddumpsize > maxdumpsize) {
251 maxdumpsize = di->useddumpsize;
259 void *dump_realloc(void *src, s4 len1, s4 len2)
261 void *dst = dump_alloc(len2);
263 memcpy(dst, src, len1);
269 void dump_release(s4 size)
273 /* If no threads are used, the dumpinfo structure is a static structure */
274 /* defined at the top of this file. */
275 #if defined(USE_THREADS)
276 di = &((threadobject *) THREADOBJECT)->dumpinfo;
278 di = ¬hreads_dumpinfo;
281 if (size < 0 || size > di->useddumpsize)
282 throw_cacao_exception_exit(string_java_lang_InternalError,
283 "Illegal dump release size %d", size);
285 /* reset the used dump size to the size specified */
286 di->useddumpsize = size;
288 while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
289 dumpblock *tmp = di->currentdumpblock;
292 /* XXX TWISTI: can someone explain this to me? */
294 /* Keep the first dumpblock if we don't free memory. Otherwise
295 * a new dumpblock is allocated each time and we run out of
298 if (!oldtop->prev) break;
302 di->allocateddumpsize -= tmp->size;
303 di->currentdumpblock = tmp->prev;
305 /* the amount of globally allocated dump memory (thread save) */
307 globalallocateddumpsize -= tmp->size;
309 /* release the dump memory and the dumpinfo structure */
320 /* If no threads are used, the dumpinfo structure is a static structure */
321 /* defined at the top of this file. */
322 #if defined(USE_THREADS)
323 di = &((threadobject *) THREADOBJECT)->dumpinfo;
325 di = ¬hreads_dumpinfo;
331 return di->useddumpsize;
336 * These are local overrides for various environment variables in Emacs.
337 * Please do not remove this and leave it at the end of the file, where
338 * Emacs will automagically detect them.
339 * ---------------------------------------------------------------------
342 * indent-tabs-mode: t