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 1494 2004-11-12 13:34:26Z twisti $
41 #include "exceptions.h"
45 #include "statistics.h"
46 #include "threads/nativethread.h"
47 #include "toolbox/logging.h"
48 #include "toolbox/memory.h"
51 /********* general types, variables and auxiliary functions *********/
53 static int mmapcodesize = 0;
54 static void *mmapcodeptr = NULL;
57 /*******************************************************************************
59 This structure is used for dump memory allocation if cacao runs without
62 *******************************************************************************/
64 #if !defined(USE_THREADS)
65 static dumpinfo nothreads_dumpinfo;
69 void *mem_mmap(int size)
73 size = ALIGN(size, ALIGNSIZE);
75 if (size > mmapcodesize) {
76 mmapcodesize = 0x10000;
78 if (size > mmapcodesize)
81 mmapcodesize = ALIGN(mmapcodesize, getpagesize());
82 mmapcodeptr = mmap(NULL,
83 (size_t) mmapcodesize,
84 PROT_READ | PROT_WRITE | PROT_EXEC,
86 #if defined(HAVE_MAP_ANONYMOUS)
88 #elif defined(HAVE_MAP_ANON)
96 if (mmapcodeptr == MAP_FAILED)
97 throw_cacao_exception_exit(string_java_lang_InternalError,
102 mmapcodeptr = (void *) ((char *) mmapcodeptr + size);
103 mmapcodesize -= size;
109 static void *checked_alloc(int size)
111 /* always allocate memory zeroed out */
112 void *m = calloc(size, 1);
115 throw_cacao_exception_exit(string_java_lang_InternalError,
122 void *mem_alloc(int size)
130 if (memoryusage > maxmemusage)
131 maxmemusage = memoryusage;
134 return checked_alloc(size);
138 void *mem_realloc(void *src, int len1, int len2)
144 panic("reallocating memoryblock with address NULL, length != 0");
148 memoryusage = (memoryusage - len1) + len2;
150 dst = realloc(src, len2);
153 throw_cacao_exception_exit(string_java_lang_InternalError,
160 void mem_free(void *m, int size)
165 panic("returned memoryblock with address NULL, length != 0");
175 void *dump_alloc(int size)
180 /* If no threads are used, the dumpinfo structure is a static structure */
181 /* defined at the top of this file. */
182 #if defined(USE_THREADS)
183 di = &((threadobject *) THREADOBJECT)->dumpinfo;
185 di = ¬hreads_dumpinfo;
191 size = ALIGN(size, ALIGNSIZE);
193 if (di->useddumpsize + size > di->allocateddumpsize) {
194 dumpblock *newdumpblock;
197 /* allocate a new dumplist structure */
198 newdumpblock = checked_alloc(sizeof(dumpblock));
200 /* If requested size is greater than the default, make the new dump */
201 /* block as big as the size requested. Else use the default size. */
202 if (size > DUMPBLOCKSIZE) {
203 newdumpblocksize = size;
206 newdumpblocksize = DUMPBLOCKSIZE;
209 /* allocate dumpblock memory */
210 /*printf("new dumpblock: %d\n", newdumpblocksize);*/
211 newdumpblock->dumpmem = checked_alloc(newdumpblocksize);
213 newdumpblock->prev = di->currentdumpblock;
214 newdumpblock->size = newdumpblocksize;
215 di->currentdumpblock = newdumpblock;
217 /* Used dump size is previously allocated dump size, because the */
218 /* remaining free memory of the previous dump block cannot be used. */
219 /*printf("unused memory: %d\n", allocateddumpsize - useddumpsize);*/
220 di->useddumpsize = di->allocateddumpsize;
222 /* increase the allocated dump size by the size of the new dump block */
223 di->allocateddumpsize += newdumpblocksize;
225 /* the amount of globally allocated dump memory (thread save) */
227 globalallocateddumpsize += newdumpblocksize;
230 /* current dump block base address + the size of the current dump block - */
231 /* the size of the unused memory = new start address */
232 m = di->currentdumpblock->dumpmem + di->currentdumpblock->size -
233 (di->allocateddumpsize - di->useddumpsize);
235 /* increase used dump size by the allocated memory size */
236 di->useddumpsize += size;
239 if (di->useddumpsize > maxdumpsize) {
240 maxdumpsize = di->useddumpsize;
248 void *dump_realloc(void *src, int len1, int len2)
250 void *dst = dump_alloc(len2);
252 memcpy(dst, src, len1);
258 void dump_release(int size)
262 /* If no threads are used, the dumpinfo structure is a static structure */
263 /* defined at the top of this file. */
264 #if defined(USE_THREADS)
265 di = &((threadobject *) THREADOBJECT)->dumpinfo;
267 di = ¬hreads_dumpinfo;
270 if (size < 0 || size > di->useddumpsize)
271 throw_cacao_exception_exit(string_java_lang_InternalError,
272 "Illegal dump release size %d", size);
274 /* reset the used dump size to the size specified */
275 di->useddumpsize = size;
277 while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
278 dumpblock *tmp = di->currentdumpblock;
281 /* XXX TWISTI: can someone explain this to me? */
283 /* Keep the first dumpblock if we don't free memory. Otherwise
284 * a new dumpblock is allocated each time and we run out of
287 if (!oldtop->prev) break;
291 di->allocateddumpsize -= tmp->size;
292 di->currentdumpblock = tmp->prev;
294 /* the amount of globally allocated dump memory (thread save) */
296 globalallocateddumpsize -= tmp->size;
298 /* release the dump memory and the dumpinfo structure */
309 /* If no threads are used, the dumpinfo structure is a static structure */
310 /* defined at the top of this file. */
311 #if defined(USE_THREADS)
312 di = &((threadobject *) THREADOBJECT)->dumpinfo;
314 di = ¬hreads_dumpinfo;
317 return di->useddumpsize;
322 * These are local overrides for various environment variables in Emacs.
323 * Please do not remove this and leave it at the end of the file, where
324 * Emacs will automagically detect them.
325 * ---------------------------------------------------------------------
328 * indent-tabs-mode: t