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 1499 2004-11-12 15:52:35Z twisti $
39 #if defined(__DARWIN__)
40 /* If we compile with -ansi on darwin, <sys/types.h> is not included. So */
41 /* let's do it here. */
42 # include <sys/types.h>
48 #include "exceptions.h"
52 #include "statistics.h"
53 #include "threads/nativethread.h"
54 #include "toolbox/logging.h"
55 #include "toolbox/memory.h"
58 /********* general types, variables and auxiliary functions *********/
60 static int mmapcodesize = 0;
61 static void *mmapcodeptr = NULL;
64 /*******************************************************************************
66 This structure is used for dump memory allocation if cacao runs without
69 *******************************************************************************/
71 #if !defined(USE_THREADS)
72 static dumpinfo nothreads_dumpinfo;
76 void *mem_mmap(int size)
80 size = ALIGN(size, ALIGNSIZE);
82 if (size > mmapcodesize) {
83 mmapcodesize = 0x10000;
85 if (size > mmapcodesize)
88 mmapcodesize = ALIGN(mmapcodesize, getpagesize());
89 mmapcodeptr = mmap(NULL,
90 (size_t) mmapcodesize,
91 PROT_READ | PROT_WRITE | PROT_EXEC,
93 #if defined(HAVE_MAP_ANONYMOUS)
95 #elif defined(HAVE_MAP_ANON)
103 if (mmapcodeptr == MAP_FAILED)
104 throw_cacao_exception_exit(string_java_lang_InternalError,
109 mmapcodeptr = (void *) ((char *) mmapcodeptr + size);
110 mmapcodesize -= size;
116 static void *checked_alloc(int size)
118 /* always allocate memory zeroed out */
119 void *m = calloc(size, 1);
122 throw_cacao_exception_exit(string_java_lang_InternalError,
129 void *mem_alloc(int size)
137 if (memoryusage > maxmemusage)
138 maxmemusage = memoryusage;
141 return checked_alloc(size);
145 void *mem_realloc(void *src, int len1, int len2)
151 panic("reallocating memoryblock with address NULL, length != 0");
155 memoryusage = (memoryusage - len1) + len2;
157 dst = realloc(src, len2);
160 throw_cacao_exception_exit(string_java_lang_InternalError,
167 void mem_free(void *m, int size)
172 panic("returned memoryblock with address NULL, length != 0");
182 void *dump_alloc(int size)
187 /* If no threads are used, the dumpinfo structure is a static structure */
188 /* defined at the top of this file. */
189 #if defined(USE_THREADS)
190 di = &((threadobject *) THREADOBJECT)->dumpinfo;
192 di = ¬hreads_dumpinfo;
198 size = ALIGN(size, ALIGNSIZE);
200 if (di->useddumpsize + size > di->allocateddumpsize) {
201 dumpblock *newdumpblock;
204 /* allocate a new dumplist structure */
205 newdumpblock = checked_alloc(sizeof(dumpblock));
207 /* If requested size is greater than the default, make the new dump */
208 /* block as big as the size requested. Else use the default size. */
209 if (size > DUMPBLOCKSIZE) {
210 newdumpblocksize = size;
213 newdumpblocksize = DUMPBLOCKSIZE;
216 /* allocate dumpblock memory */
217 /*printf("new dumpblock: %d\n", newdumpblocksize);*/
218 newdumpblock->dumpmem = checked_alloc(newdumpblocksize);
220 newdumpblock->prev = di->currentdumpblock;
221 newdumpblock->size = newdumpblocksize;
222 di->currentdumpblock = newdumpblock;
224 /* Used dump size is previously allocated dump size, because the */
225 /* remaining free memory of the previous dump block cannot be used. */
226 /*printf("unused memory: %d\n", allocateddumpsize - useddumpsize);*/
227 di->useddumpsize = di->allocateddumpsize;
229 /* increase the allocated dump size by the size of the new dump block */
230 di->allocateddumpsize += newdumpblocksize;
232 /* the amount of globally allocated dump memory (thread save) */
234 globalallocateddumpsize += newdumpblocksize;
237 /* current dump block base address + the size of the current dump block - */
238 /* the size of the unused memory = new start address */
239 m = di->currentdumpblock->dumpmem + di->currentdumpblock->size -
240 (di->allocateddumpsize - di->useddumpsize);
242 /* increase used dump size by the allocated memory size */
243 di->useddumpsize += size;
246 if (di->useddumpsize > maxdumpsize) {
247 maxdumpsize = di->useddumpsize;
255 void *dump_realloc(void *src, int len1, int len2)
257 void *dst = dump_alloc(len2);
259 memcpy(dst, src, len1);
265 void dump_release(int size)
269 /* If no threads are used, the dumpinfo structure is a static structure */
270 /* defined at the top of this file. */
271 #if defined(USE_THREADS)
272 di = &((threadobject *) THREADOBJECT)->dumpinfo;
274 di = ¬hreads_dumpinfo;
277 if (size < 0 || size > di->useddumpsize)
278 throw_cacao_exception_exit(string_java_lang_InternalError,
279 "Illegal dump release size %d", size);
281 /* reset the used dump size to the size specified */
282 di->useddumpsize = size;
284 while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
285 dumpblock *tmp = di->currentdumpblock;
288 /* XXX TWISTI: can someone explain this to me? */
290 /* Keep the first dumpblock if we don't free memory. Otherwise
291 * a new dumpblock is allocated each time and we run out of
294 if (!oldtop->prev) break;
298 di->allocateddumpsize -= tmp->size;
299 di->currentdumpblock = tmp->prev;
301 /* the amount of globally allocated dump memory (thread save) */
303 globalallocateddumpsize -= tmp->size;
305 /* release the dump memory and the dumpinfo structure */
316 /* If no threads are used, the dumpinfo structure is a static structure */
317 /* defined at the top of this file. */
318 #if defined(USE_THREADS)
319 di = &((threadobject *) THREADOBJECT)->dumpinfo;
321 di = ¬hreads_dumpinfo;
324 return di->useddumpsize;
329 * These are local overrides for various environment variables in Emacs.
330 * Please do not remove this and leave it at the end of the file, where
331 * Emacs will automagically detect them.
332 * ---------------------------------------------------------------------
335 * indent-tabs-mode: t