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 1672 2004-12-03 16:41:52Z 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)
56 # if defined(NATIVE_THREADS)
57 # include "threads/native/threads.h"
59 # include "threads/green/threads.h"
63 #include "toolbox/logging.h"
66 /********* general types, variables and auxiliary functions *********/
68 static int mmapcodesize = 0;
69 static void *mmapcodeptr = NULL;
72 /*******************************************************************************
74 This structure is used for dump memory allocation if cacao runs without
77 *******************************************************************************/
79 #if !defined(USE_THREADS) || (defined(USE_THREADS) && !defined(NATIVE_THREADS))
80 static dumpinfo nothreads_dumpinfo;
84 void *mem_mmap(s4 size)
88 size = ALIGN(size, ALIGNSIZE);
90 if (size > mmapcodesize) {
91 mmapcodesize = 0x10000;
93 if (size > mmapcodesize)
96 mmapcodesize = ALIGN(mmapcodesize, getpagesize());
97 mmapcodeptr = mmap(NULL,
98 (size_t) mmapcodesize,
99 PROT_READ | PROT_WRITE | PROT_EXEC,
101 #if defined(HAVE_MAP_ANONYMOUS)
103 #elif defined(HAVE_MAP_ANON)
111 if (mmapcodeptr == MAP_FAILED)
112 throw_cacao_exception_exit(string_java_lang_InternalError,
117 mmapcodeptr = (void *) ((char *) mmapcodeptr + size);
118 mmapcodesize -= size;
124 static void *checked_alloc(s4 size)
126 /* always allocate memory zeroed out */
127 void *m = calloc(size, 1);
130 throw_cacao_exception_exit(string_java_lang_InternalError,
137 void *mem_alloc(s4 size)
145 if (memoryusage > maxmemusage)
146 maxmemusage = memoryusage;
149 return checked_alloc(size);
153 void *mem_realloc(void *src, s4 len1, s4 len2)
159 panic("reallocating memoryblock with address NULL, length != 0");
163 memoryusage = (memoryusage - len1) + len2;
165 dst = realloc(src, len2);
168 throw_cacao_exception_exit(string_java_lang_InternalError,
175 void mem_free(void *m, s4 size)
180 panic("returned memoryblock with address NULL, length != 0");
190 void *dump_alloc(s4 size)
195 /* If no threads are used, the dumpinfo structure is a static structure */
196 /* defined at the top of this file. */
197 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
198 di = &((threadobject *) THREADOBJECT)->dumpinfo;
200 di = ¬hreads_dumpinfo;
206 size = ALIGN(size, ALIGNSIZE);
208 if (di->useddumpsize + size > di->allocateddumpsize) {
209 dumpblock *newdumpblock;
212 /* allocate a new dumplist structure */
213 newdumpblock = checked_alloc(sizeof(dumpblock));
215 /* If requested size is greater than the default, make the new dump */
216 /* block as big as the size requested. Else use the default size. */
217 if (size > DUMPBLOCKSIZE) {
218 newdumpblocksize = size;
221 newdumpblocksize = DUMPBLOCKSIZE;
224 /* allocate dumpblock memory */
225 /*printf("new dumpblock: %d\n", newdumpblocksize);*/
226 newdumpblock->dumpmem = checked_alloc(newdumpblocksize);
228 newdumpblock->prev = di->currentdumpblock;
229 newdumpblock->size = newdumpblocksize;
230 di->currentdumpblock = newdumpblock;
232 /* Used dump size is previously allocated dump size, because the */
233 /* remaining free memory of the previous dump block cannot be used. */
234 /*printf("unused memory: %d\n", allocateddumpsize - useddumpsize);*/
235 di->useddumpsize = di->allocateddumpsize;
237 /* increase the allocated dump size by the size of the new dump block */
238 di->allocateddumpsize += newdumpblocksize;
240 /* the amount of globally allocated dump memory (thread save) */
242 globalallocateddumpsize += newdumpblocksize;
245 /* current dump block base address + the size of the current dump block - */
246 /* the size of the unused memory = new start address */
247 m = di->currentdumpblock->dumpmem + di->currentdumpblock->size -
248 (di->allocateddumpsize - di->useddumpsize);
250 /* increase used dump size by the allocated memory size */
251 di->useddumpsize += size;
254 if (di->useddumpsize > maxdumpsize) {
255 maxdumpsize = di->useddumpsize;
263 void *dump_realloc(void *src, s4 len1, s4 len2)
265 void *dst = dump_alloc(len2);
267 memcpy(dst, src, len1);
273 void dump_release(s4 size)
277 /* If no threads are used, the dumpinfo structure is a static structure */
278 /* defined at the top of this file. */
279 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
280 di = &((threadobject *) THREADOBJECT)->dumpinfo;
282 di = ¬hreads_dumpinfo;
285 if (size < 0 || size > di->useddumpsize)
286 throw_cacao_exception_exit(string_java_lang_InternalError,
287 "Illegal dump release size %d", size);
289 /* reset the used dump size to the size specified */
290 di->useddumpsize = size;
292 while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
293 dumpblock *tmp = di->currentdumpblock;
296 /* XXX TWISTI: can someone explain this to me? */
298 /* Keep the first dumpblock if we don't free memory. Otherwise
299 * a new dumpblock is allocated each time and we run out of
302 if (!oldtop->prev) break;
306 di->allocateddumpsize -= tmp->size;
307 di->currentdumpblock = tmp->prev;
309 /* the amount of globally allocated dump memory (thread save) */
311 globalallocateddumpsize -= tmp->size;
313 /* release the dump memory and the dumpinfo structure */
324 /* If no threads are used, the dumpinfo structure is a static structure */
325 /* defined at the top of this file. */
326 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
327 di = &((threadobject *) THREADOBJECT)->dumpinfo;
329 di = ¬hreads_dumpinfo;
335 return di->useddumpsize;
340 * These are local overrides for various environment variables in Emacs.
341 * Please do not remove this and leave it at the end of the file, where
342 * Emacs will automagically detect them.
343 * ---------------------------------------------------------------------
346 * indent-tabs-mode: t