3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
31 $Id: memory.c 6286 2007-01-10 10:03:38Z twisti $
46 #if defined(__DARWIN__)
47 /* If we compile with -ansi on darwin, <sys/types.h> is not
48 included. So let's do it here. */
49 # include <sys/types.h>
56 #include "mm/memory.h"
57 #include "native/native.h"
59 #if defined(ENABLE_THREADS)
60 # include "threads/native/lock.h"
61 # include "threads/native/threads.h"
63 # include "threads/none/lock.h"
66 #include "toolbox/logging.h"
67 #include "vm/exceptions.h"
68 #include "vm/global.h"
69 #include "vm/options.h"
70 #include "vm/statistics.h"
71 #include "vm/stringlocal.h"
75 /* constants for ENABLE_MEMCHECK **********************************************/
77 #if defined(ENABLE_MEMCHECK)
78 #define MEMORY_CANARY_SIZE 16
79 #define MEMORY_CANARY_FIRST_BYTE 0xca
80 #define MEMORY_CLEAR_BYTE 0xa5
81 #endif /* defined(ENABLE_MEMCHECK) */
84 /*******************************************************************************
86 This structure is used for dump memory allocation if cacao
89 *******************************************************************************/
91 #if !defined(ENABLE_THREADS)
92 static dumpinfo_t _no_threads_dumpinfo;
95 #if defined(ENABLE_THREADS)
96 #define DUMPINFO &((threadobject *) THREADOBJECT)->dumpinfo
98 #define DUMPINFO &_no_threads_dumpinfo
102 /* global code memory variables ***********************************************/
104 #define DEFAULT_CODE_MEMORY_SIZE 128 * 1024 /* defaulting to 128kB */
106 #if defined(ENABLE_THREADS)
107 static java_objectheader *lock_code_memory = NULL;
109 static void *code_memory = NULL;
110 static int code_memory_size = 0;
111 static int pagesize = 0;
114 /* memory_init *****************************************************************
116 Initialize the memory subsystem.
118 *******************************************************************************/
120 bool memory_init(void)
122 #if defined(ENABLE_THREADS)
123 lock_code_memory = NEW(java_objectheader);
125 lock_init_object_lock(lock_code_memory);
128 /* get the pagesize of this architecture */
130 pagesize = getpagesize();
132 /* everything's ok */
138 /* memory_mmap_anon ************************************************************
140 Maps anonymous memory, even on systems not defining
143 *******************************************************************************/
145 void *memory_mmap_anon(void *addr, size_t len, int prot, int flags)
149 #if defined(MAP_ANON) || defined(MAP_ANONYMOUS)
150 p = mmap(addr, len, prot,
151 # if defined(MAP_ANON)
154 MAP_ANONYMOUS | flags,
160 fd = open("/dev/zero", O_RDONLY, 0);
163 vm_abort("memory_mmap_anon: open failed: %s", strerror(errno));
165 p = mmap(addr, len, prot, flags, fd, 0);
168 #if defined(MAP_FAILED)
171 if (p == (void *) -1)
173 vm_abort("memory_mmap_anon: mmap failed: %s", strerror(errno));
179 /* memory_checked_alloc ********************************************************
181 Allocated zeroed-out memory and does an OOM check.
184 XXX If no memory could be allocated, this function justs *exists*.
186 *******************************************************************************/
188 static void *memory_checked_alloc(s4 size)
190 /* always allocate memory zeroed out */
192 void *p = calloc(size, 1);
195 vm_abort("memory_checked_alloc: calloc failed: out of memory");
201 /* memory_cnew *****************************************************************
203 Allocates memory from the heap via mmap and make the memory read-,
204 write-, and executeable.
206 *******************************************************************************/
208 void *memory_cnew(s4 size)
212 LOCK_MONITOR_ENTER(lock_code_memory);
214 size = MEMORY_ALIGN(size, ALIGNSIZE);
216 /* check if enough memory is available */
218 if (size > code_memory_size) {
219 /* set default code size */
221 code_memory_size = DEFAULT_CODE_MEMORY_SIZE;
223 /* do we need more? */
225 if (size > code_memory_size)
226 code_memory_size = size;
228 /* align the size of the memory to be allocated */
230 code_memory_size = MEMORY_ALIGN(code_memory_size, pagesize);
232 #if defined(ENABLE_STATISTICS)
234 codememusage += code_memory_size;
236 if (codememusage > maxcodememusage)
237 maxcodememusage = codememusage;
241 /* allocate the memory */
243 p = memory_mmap_anon(NULL, code_memory_size,
244 PROT_READ | PROT_WRITE | PROT_EXEC,
247 /* set global code memory pointer */
252 /* get a memory chunk of the allocated memory */
256 code_memory = (void *) ((ptrint) code_memory + size);
257 code_memory_size -= size;
259 LOCK_MONITOR_EXIT(lock_code_memory);
265 /* memory_cfree ****************************************************************
267 Frees the code memory pointed to.
269 ATTENTION: This function currently does NOTHING! Because we don't
270 have a memory management for code memory.
272 *******************************************************************************/
274 void memory_cfree(void *p, s4 size)
280 void *mem_alloc(s4 size)
287 #if defined(ENABLE_STATISTICS)
291 if (memoryusage > maxmemusage)
292 maxmemusage = memoryusage;
296 m = memory_checked_alloc(size);
298 #if defined(ENABLE_MEMCHECK)
299 /* XXX we would like to poison the memory, but callers rely on */
300 /* the zeroing. This should change sooner or later. */
301 /* memset(m, MEMORY_CLEAR_BYTE, size); */
308 void *mem_realloc(void *src, s4 len1, s4 len2)
314 vm_abort("mem_realloc: reallocating memoryblock with address NULL, length != 0");
316 #if defined(ENABLE_STATISTICS)
318 memoryusage = (memoryusage - len1) + len2;
321 #if defined(ENABLE_MEMCHECK)
323 memset((u1*)dst + len2, MEMORY_CLEAR_BYTE, len1 - len2);
326 dst = realloc(src, len2);
329 vm_abort("mem_realloc: realloc failed: out of memory");
331 #if defined(ENABLE_MEMCHECK)
333 memset((u1*)dst + len1, MEMORY_CLEAR_BYTE, len2 - len1);
340 void mem_free(void *m, s4 size)
346 log_text("returned memoryblock with address NULL, length != 0");
350 #if defined(ENABLE_STATISTICS)
355 #if defined(ENABLE_MEMCHECK)
356 /* destroy the contents */
357 memset(m, MEMORY_CLEAR_BYTE, size);
364 /* dump_check_canaries *********************************************************
366 Check canaries in dump memory.
369 di...........dumpinfo_t * of the dump area to check
370 bottomsize...dump size down to which the dump area should be checked
371 (specify 0 to check the whole dump area)
374 If any canary has been changed, this function aborts the VM with
377 *******************************************************************************/
379 #if defined(ENABLE_MEMCHECK)
380 void dump_check_canaries(dumpinfo_t *di, s4 bottomsize)
382 dump_allocation_t *da;
386 /* iterate over all dump memory allocations above bottomsize */
388 da = di->allocations;
389 while (da && da->useddumpsize >= bottomsize) {
392 pm = da->mem - MEMORY_CANARY_SIZE;
393 for (i=0; i<MEMORY_CANARY_SIZE; ++i)
394 if (pm[i] != i + MEMORY_CANARY_FIRST_BYTE) {
395 fprintf(stderr, "canary bytes:");
396 for (j=0; j<MEMORY_CANARY_SIZE; ++j)
397 fprintf(stderr, " %02x", pm[j]);
398 fprintf(stderr,"\n");
399 vm_abort("error: dump memory bottom canary killed: "
400 "%p (%d bytes allocated at %p)\n",
401 pm + i, da->size, da->mem);
404 pm = da->mem + da->size;
405 for (i=0; i<MEMORY_CANARY_SIZE; ++i)
406 if (pm[i] != i + MEMORY_CANARY_FIRST_BYTE) {
407 fprintf(stderr, "canary bytes:");
408 for (j=0; j<MEMORY_CANARY_SIZE; ++j)
409 fprintf(stderr, " %02x", pm[j]);
410 fprintf(stderr,"\n");
411 vm_abort("error: dump memory top canary killed: "
412 "%p (%d bytes allocated at %p)\n",
413 pm + i, da->size, da->mem);
419 #endif /* defined(ENABLE_MEMCHECK) */
422 /* dump_alloc ******************************************************************
424 Allocate memory in the dump area.
427 size.........size of block to allocate, in bytes
428 may be zero, in which case NULL is returned
431 pointer to allocated memory, or
432 NULL iff `size` was zero
435 XXX This function uses `memory_checked_alloc`, which *exits* if no
436 memory could be allocated.
439 dump_alloc is thread safe. Each thread has its own dump memory area.
441 dump_alloc is a fast allocator suitable for scratch memory that can be
442 collectively freed when the current activity (eg. compiling) is done.
444 You cannot selectively free dump memory. Before you start allocating it,
445 you remember the current size returned by `dump_size`. Later, when you no
446 longer need the memory, call `dump_release` with the remembered size and
447 all dump memory allocated since the call to `dump_size` will be freed.
449 *******************************************************************************/
451 void *dump_alloc(s4 size)
453 #if defined(DISABLE_DUMP)
455 /* use malloc memory for dump memory (for debugging only!) */
457 return mem_alloc(size);
459 #else /* !defined(DISABLE_DUMP) */
463 #if defined(ENABLE_MEMCHECK)
464 s4 origsize = size; /* needed for the canary system */
467 /* If no threads are used, the dumpinfo structure is a static structure */
468 /* defined at the top of this file. */
475 #if defined(ENABLE_MEMCHECK)
476 size += 2*MEMORY_CANARY_SIZE;
479 size = MEMORY_ALIGN(size, ALIGNSIZE);
481 if (di->useddumpsize + size > di->allocateddumpsize) {
482 dumpblock_t *newdumpblock;
485 /* allocate a new dumplist structure */
487 newdumpblock = memory_checked_alloc(sizeof(dumpblock_t));
489 /* If requested size is greater than the default, make the new dump */
490 /* block as big as the size requested. Else use the default size. */
492 if (size > DUMPBLOCKSIZE) {
493 newdumpblocksize = size;
496 newdumpblocksize = DUMPBLOCKSIZE;
499 /* allocate dumpblock memory */
501 newdumpblock->dumpmem = memory_checked_alloc(newdumpblocksize);
503 newdumpblock->prev = di->currentdumpblock;
504 newdumpblock->size = newdumpblocksize;
505 di->currentdumpblock = newdumpblock;
507 /* Used dump size is previously allocated dump size, because the */
508 /* remaining free memory of the previous dump block cannot be used. */
510 di->useddumpsize = di->allocateddumpsize;
512 /* increase the allocated dump size by the size of the new dump block */
514 di->allocateddumpsize += newdumpblocksize;
516 #if defined(ENABLE_STATISTICS)
517 /* the amount of globally allocated dump memory (thread save) */
520 globalallocateddumpsize += newdumpblocksize;
524 /* current dump block base address + the size of the current dump block - */
525 /* the size of the unused memory = new start address */
527 m = di->currentdumpblock->dumpmem + di->currentdumpblock->size -
528 (di->allocateddumpsize - di->useddumpsize);
530 #if defined(ENABLE_MEMCHECK)
532 dump_allocation_t *da = NEW(dump_allocation_t);
536 /* add the allocation to our linked list of allocations */
538 da->next = di->allocations;
539 da->mem = (u1*) m + MEMORY_CANARY_SIZE;
541 da->useddumpsize = di->useddumpsize;
543 di->allocations = da;
545 /* write the canaries */
548 for (i=0; i<MEMORY_CANARY_SIZE; ++i)
549 pm[i] = i + MEMORY_CANARY_FIRST_BYTE;
550 pm = da->mem + da->size;
551 for (i=0; i<MEMORY_CANARY_SIZE; ++i)
552 pm[i] = i + MEMORY_CANARY_FIRST_BYTE;
554 /* make m point after the bottom canary */
556 m = (u1*)m + MEMORY_CANARY_SIZE;
558 /* clear the memory */
560 memset(m, MEMORY_CLEAR_BYTE, da->size);
562 #endif /* defined(ENABLE_MEMCHECK) */
564 /* increase used dump size by the allocated memory size */
566 di->useddumpsize += size;
568 #if defined(ENABLE_STATISTICS)
570 if (di->useddumpsize > maxdumpsize)
571 maxdumpsize = di->useddumpsize;
576 #endif /* defined(DISABLE_DUMP) */
580 /* dump_realloc ****************************************************************
582 Stupid realloc implementation for dump memory. Avoid, if possible.
584 *******************************************************************************/
586 void *dump_realloc(void *src, s4 len1, s4 len2)
588 #if defined(DISABLE_DUMP)
589 /* use malloc memory for dump memory (for debugging only!) */
591 return mem_realloc(src, len1, len2);
593 void *dst = dump_alloc(len2);
595 memcpy(dst, src, len1);
597 #if defined(ENABLE_MEMCHECK)
598 /* destroy the source */
599 memset(src, MEMORY_CLEAR_BYTE, len1);
607 /* dump_release ****************************************************************
609 Release dump memory above the given size.
612 size........All dump memory above this mark will be freed. Usually
613 `size` will be the return value of a `dump_size` call
617 XXX If the given size is invalid, this function *exits* with an
622 *******************************************************************************/
624 void dump_release(s4 size)
626 #if defined(DISABLE_DUMP)
628 /* use malloc memory for dump memory (for debugging only!) */
632 #else /* !defined(DISABLE_DUMP) */
636 /* If no threads are used, the dumpinfo structure is a static structure */
637 /* defined at the top of this file. */
641 if ((size < 0) || (size > di->useddumpsize))
642 vm_abort("Illegal dump release size: %d", size);
644 #if defined(ENABLE_MEMCHECK)
646 dump_allocation_t *da, *next;
650 dump_check_canaries(di, size);
652 /* iterate over all dump memory allocations about to be released */
654 da = di->allocations;
655 while (da && da->useddumpsize >= size) {
658 /* invalidate the freed memory */
660 memset(da->mem, MEMORY_CLEAR_BYTE, da->size);
662 FREE(da, dump_allocation_t);
666 di->allocations = da;
668 #endif /* defined(ENABLE_MEMCHECK) */
670 /* reset the used dump size to the size specified */
672 di->useddumpsize = size;
674 while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
675 dumpblock_t *tmp = di->currentdumpblock;
677 di->allocateddumpsize -= tmp->size;
678 di->currentdumpblock = tmp->prev;
680 #if defined(ENABLE_STATISTICS)
681 /* the amount of globally allocated dump memory (thread save) */
684 globalallocateddumpsize -= tmp->size;
687 /* release the dump memory and the dumpinfo structure */
693 #endif /* defined(DISABLE_DUMP) */
697 /* dump_size *******************************************************************
699 Return the current size of the dump memory area. See `dump_alloc`.
701 *******************************************************************************/
705 #if defined(DISABLE_DUMP)
706 /* use malloc memory for dump memory (for debugging only!) */
710 #else /* !defined(DISABLE_DUMP) */
714 /* If no threads are used, the dumpinfo structure is a static structure */
715 /* defined at the top of this file. */
722 return di->useddumpsize;
724 #endif /* defined(DISABLE_DUMP) */
729 * These are local overrides for various environment variables in Emacs.
730 * Please do not remove this and leave it at the end of the file, where
731 * Emacs will automagically detect them.
732 * ---------------------------------------------------------------------
735 * indent-tabs-mode: t
739 * vim:noexpandtab:sw=4:ts=4: