1 /* src/mm/memory.c - memory management
3 Copyright (C) 1996-2005, 2006, 2007 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 $Id: memory.c 7341 2007-02-13 00:51:01Z twisti $
40 #if defined(__DARWIN__)
41 /* If we compile with -ansi on darwin, <sys/types.h> is not
42 included. So let's do it here. */
43 # include <sys/types.h>
50 #include "mm/memory.h"
51 #include "native/native.h"
53 #if defined(ENABLE_THREADS)
54 # include "threads/threads-common.h"
56 # include "threads/native/lock.h"
57 # include "threads/native/threads.h"
59 # include "threads/none/lock.h"
62 #include "toolbox/logging.h"
63 #include "vm/exceptions.h"
64 #include "vm/global.h"
65 #include "vm/stringlocal.h"
67 #include "vmcore/options.h"
69 #if defined(ENABLE_STATISTICS)
70 # include "vmcore/statistics.h"
74 /* constants for ENABLE_MEMCHECK **********************************************/
76 #if defined(ENABLE_MEMCHECK)
77 #define MEMORY_CANARY_SIZE 16
78 #define MEMORY_CANARY_FIRST_BYTE 0xca
79 #define MEMORY_CLEAR_BYTE 0xa5
80 #endif /* defined(ENABLE_MEMCHECK) */
83 /*******************************************************************************
85 This structure is used for dump memory allocation if cacao
88 *******************************************************************************/
90 #if !defined(ENABLE_THREADS)
91 static dumpinfo_t _no_threads_dumpinfo;
94 #if defined(ENABLE_THREADS)
95 #define DUMPINFO &((threadobject *) THREADOBJECT)->dumpinfo
97 #define DUMPINFO &_no_threads_dumpinfo
101 /* global code memory variables ***********************************************/
103 #define DEFAULT_CODE_MEMORY_SIZE 128 * 1024 /* defaulting to 128kB */
105 #if defined(ENABLE_THREADS)
106 static java_objectheader *lock_code_memory = NULL;
108 static void *code_memory = NULL;
109 static int code_memory_size = 0;
110 static int pagesize = 0;
113 /* global variables ***********************************************************/
115 #if defined(ENABLE_THREADS)
116 static threadobject *thread_memory;
120 /* memory_init *****************************************************************
122 Initialize the memory subsystem.
124 *******************************************************************************/
126 bool memory_init(void)
128 #if defined(ENABLE_THREADS)
129 /* create lock for code memory */
131 lock_code_memory = NEW(java_objectheader);
133 lock_init_object_lock(lock_code_memory);
136 /* get the pagesize of this architecture */
138 pagesize = getpagesize();
140 /* everything's ok */
146 /* memory_mmap_anon ************************************************************
148 Maps anonymous memory, even on systems not defining
151 *******************************************************************************/
153 void *memory_mmap_anon(void *addr, size_t len, int prot, int flags)
157 #if defined(MAP_ANON) || defined(MAP_ANONYMOUS)
158 p = mmap(addr, len, prot,
159 # if defined(MAP_ANON)
162 MAP_ANONYMOUS | flags,
168 fd = open("/dev/zero", O_RDONLY, 0);
171 vm_abort("memory_mmap_anon: open failed: %s", strerror(errno));
173 p = mmap(addr, len, prot, flags, fd, 0);
176 #if defined(MAP_FAILED)
179 if (p == (void *) -1)
181 vm_abort("memory_mmap_anon: mmap failed: %s", strerror(errno));
187 /* memory_checked_alloc ********************************************************
189 Allocated zeroed-out memory and does an OOM check.
192 XXX If no memory could be allocated, this function justs *exists*.
194 *******************************************************************************/
196 static void *memory_checked_alloc(s4 size)
198 /* always allocate memory zeroed out */
200 void *p = calloc(size, 1);
203 vm_abort("memory_checked_alloc: calloc failed: out of memory");
209 /* memory_cnew *****************************************************************
211 Allocates memory from the heap via mmap and make the memory read-,
212 write-, and executeable.
214 *******************************************************************************/
216 void *memory_cnew(s4 size)
220 LOCK_MONITOR_ENTER(lock_code_memory);
222 size = MEMORY_ALIGN(size, ALIGNSIZE);
224 /* check if enough memory is available */
226 if (size > code_memory_size) {
227 /* set default code size */
229 code_memory_size = DEFAULT_CODE_MEMORY_SIZE;
231 /* do we need more? */
233 if (size > code_memory_size)
234 code_memory_size = size;
236 /* align the size of the memory to be allocated */
238 code_memory_size = MEMORY_ALIGN(code_memory_size, pagesize);
240 #if defined(ENABLE_STATISTICS)
242 codememusage += code_memory_size;
244 if (codememusage > maxcodememusage)
245 maxcodememusage = codememusage;
249 /* allocate the memory */
251 p = memory_mmap_anon(NULL, code_memory_size,
252 PROT_READ | PROT_WRITE | PROT_EXEC,
255 /* set global code memory pointer */
260 /* get a memory chunk of the allocated memory */
264 code_memory = (void *) ((ptrint) code_memory + size);
265 code_memory_size -= size;
267 LOCK_MONITOR_EXIT(lock_code_memory);
273 /* memory_cfree ****************************************************************
275 Frees the code memory pointed to.
277 ATTENTION: This function currently does NOTHING! Because we don't
278 have a memory management for code memory.
280 *******************************************************************************/
282 void memory_cfree(void *p, s4 size)
288 void *mem_alloc(s4 size)
295 #if defined(ENABLE_STATISTICS)
299 if (memoryusage > maxmemusage)
300 maxmemusage = memoryusage;
304 m = memory_checked_alloc(size);
306 #if defined(ENABLE_MEMCHECK)
307 /* XXX we would like to poison the memory, but callers rely on */
308 /* the zeroing. This should change sooner or later. */
309 /* memset(m, MEMORY_CLEAR_BYTE, size); */
316 void *mem_realloc(void *src, s4 len1, s4 len2)
320 /* prevent compiler warnings */
326 vm_abort("mem_realloc: reallocating memoryblock with address NULL, length != 0");
328 #if defined(ENABLE_STATISTICS)
330 memoryusage = (memoryusage - len1) + len2;
333 #if defined(ENABLE_MEMCHECK)
335 memset((u1*)dst + len2, MEMORY_CLEAR_BYTE, len1 - len2);
338 dst = realloc(src, len2);
341 vm_abort("mem_realloc: realloc failed: out of memory");
343 #if defined(ENABLE_MEMCHECK)
345 memset((u1*)dst + len1, MEMORY_CLEAR_BYTE, len2 - len1);
352 void mem_free(void *m, s4 size)
358 log_text("returned memoryblock with address NULL, length != 0");
362 #if defined(ENABLE_STATISTICS)
367 #if defined(ENABLE_MEMCHECK)
368 /* destroy the contents */
369 memset(m, MEMORY_CLEAR_BYTE, size);
376 /* memory_thread ***************************************************************
378 Prints regularly memory statistics.
380 *******************************************************************************/
382 #if defined(ENABLE_THREADS) && !defined(NDEBUG)
383 static void memory_thread(void)
386 /* sleep thread for 2 seconds */
388 threads_sleep(2 * 1000, 0);
390 # if defined(ENABLE_STATISTICS)
391 /* print memory usage */
393 statistics_print_memory_usage();
400 /* memory_start_thread *********************************************************
402 Starts the memory profiling thread.
404 *******************************************************************************/
406 #if defined(ENABLE_THREADS) && !defined(NDEBUG)
407 bool memory_start_thread(void)
411 name = utf_new_char("Memory Profiler");
413 thread_memory = threads_create_thread(name);
415 if (thread_memory == NULL)
418 /* actually start the memory profiling thread */
420 threads_start_thread(thread_memory, memory_thread);
422 /* everything's ok */
429 /* dump_check_canaries *********************************************************
431 Check canaries in dump memory.
434 di...........dumpinfo_t * of the dump area to check
435 bottomsize...dump size down to which the dump area should be checked
436 (specify 0 to check the whole dump area)
439 If any canary has been changed, this function aborts the VM with
442 *******************************************************************************/
444 #if defined(ENABLE_MEMCHECK)
445 void dump_check_canaries(dumpinfo_t *di, s4 bottomsize)
447 dump_allocation_t *da;
451 /* iterate over all dump memory allocations above bottomsize */
453 da = di->allocations;
454 while (da && da->useddumpsize >= bottomsize) {
457 pm = da->mem - MEMORY_CANARY_SIZE;
458 for (i=0; i<MEMORY_CANARY_SIZE; ++i)
459 if (pm[i] != i + MEMORY_CANARY_FIRST_BYTE) {
460 fprintf(stderr, "canary bytes:");
461 for (j=0; j<MEMORY_CANARY_SIZE; ++j)
462 fprintf(stderr, " %02x", pm[j]);
463 fprintf(stderr,"\n");
464 vm_abort("error: dump memory bottom canary killed: "
465 "%p (%d bytes allocated at %p)\n",
466 pm + i, da->size, da->mem);
469 pm = da->mem + da->size;
470 for (i=0; i<MEMORY_CANARY_SIZE; ++i)
471 if (pm[i] != i + MEMORY_CANARY_FIRST_BYTE) {
472 fprintf(stderr, "canary bytes:");
473 for (j=0; j<MEMORY_CANARY_SIZE; ++j)
474 fprintf(stderr, " %02x", pm[j]);
475 fprintf(stderr,"\n");
476 vm_abort("error: dump memory top canary killed: "
477 "%p (%d bytes allocated at %p)\n",
478 pm + i, da->size, da->mem);
484 #endif /* defined(ENABLE_MEMCHECK) */
487 /* dump_alloc ******************************************************************
489 Allocate memory in the dump area.
492 size.........size of block to allocate, in bytes
493 may be zero, in which case NULL is returned
496 pointer to allocated memory, or
497 NULL iff `size` was zero
500 XXX This function uses `memory_checked_alloc`, which *exits* if no
501 memory could be allocated.
504 dump_alloc is thread safe. Each thread has its own dump memory area.
506 dump_alloc is a fast allocator suitable for scratch memory that can be
507 collectively freed when the current activity (eg. compiling) is done.
509 You cannot selectively free dump memory. Before you start allocating it,
510 you remember the current size returned by `dump_size`. Later, when you no
511 longer need the memory, call `dump_release` with the remembered size and
512 all dump memory allocated since the call to `dump_size` will be freed.
514 *******************************************************************************/
516 void *dump_alloc(s4 size)
518 #if defined(DISABLE_DUMP)
520 /* use malloc memory for dump memory (for debugging only!) */
522 return mem_alloc(size);
524 #else /* !defined(DISABLE_DUMP) */
528 #if defined(ENABLE_MEMCHECK)
529 s4 origsize = size; /* needed for the canary system */
532 /* If no threads are used, the dumpinfo structure is a static structure */
533 /* defined at the top of this file. */
540 #if defined(ENABLE_MEMCHECK)
541 size += 2*MEMORY_CANARY_SIZE;
544 size = MEMORY_ALIGN(size, ALIGNSIZE);
546 if (di->useddumpsize + size > di->allocateddumpsize) {
547 dumpblock_t *newdumpblock;
550 /* allocate a new dumplist structure */
552 newdumpblock = memory_checked_alloc(sizeof(dumpblock_t));
554 /* If requested size is greater than the default, make the new dump */
555 /* block as big as the size requested. Else use the default size. */
557 if (size > DUMPBLOCKSIZE) {
558 newdumpblocksize = size;
561 newdumpblocksize = DUMPBLOCKSIZE;
564 /* allocate dumpblock memory */
566 newdumpblock->dumpmem = memory_checked_alloc(newdumpblocksize);
568 newdumpblock->prev = di->currentdumpblock;
569 newdumpblock->size = newdumpblocksize;
570 di->currentdumpblock = newdumpblock;
572 /* Used dump size is previously allocated dump size, because the */
573 /* remaining free memory of the previous dump block cannot be used. */
575 di->useddumpsize = di->allocateddumpsize;
577 /* increase the allocated dump size by the size of the new dump block */
579 di->allocateddumpsize += newdumpblocksize;
581 #if defined(ENABLE_STATISTICS)
582 /* the amount of globally allocated dump memory (thread save) */
585 globalallocateddumpsize += newdumpblocksize;
589 /* current dump block base address + the size of the current dump block - */
590 /* the size of the unused memory = new start address */
592 m = di->currentdumpblock->dumpmem + di->currentdumpblock->size -
593 (di->allocateddumpsize - di->useddumpsize);
595 #if defined(ENABLE_MEMCHECK)
597 dump_allocation_t *da = NEW(dump_allocation_t);
601 /* add the allocation to our linked list of allocations */
603 da->next = di->allocations;
604 da->mem = (u1*) m + MEMORY_CANARY_SIZE;
606 da->useddumpsize = di->useddumpsize;
608 di->allocations = da;
610 /* write the canaries */
613 for (i=0; i<MEMORY_CANARY_SIZE; ++i)
614 pm[i] = i + MEMORY_CANARY_FIRST_BYTE;
615 pm = da->mem + da->size;
616 for (i=0; i<MEMORY_CANARY_SIZE; ++i)
617 pm[i] = i + MEMORY_CANARY_FIRST_BYTE;
619 /* make m point after the bottom canary */
621 m = (u1*)m + MEMORY_CANARY_SIZE;
623 /* clear the memory */
625 memset(m, MEMORY_CLEAR_BYTE, da->size);
627 #endif /* defined(ENABLE_MEMCHECK) */
629 /* increase used dump size by the allocated memory size */
631 di->useddumpsize += size;
633 #if defined(ENABLE_STATISTICS)
635 if (di->useddumpsize > maxdumpsize)
636 maxdumpsize = di->useddumpsize;
641 #endif /* defined(DISABLE_DUMP) */
645 /* dump_realloc ****************************************************************
647 Stupid realloc implementation for dump memory. Avoid, if possible.
649 *******************************************************************************/
651 void *dump_realloc(void *src, s4 len1, s4 len2)
653 #if defined(DISABLE_DUMP)
654 /* use malloc memory for dump memory (for debugging only!) */
656 return mem_realloc(src, len1, len2);
658 void *dst = dump_alloc(len2);
660 memcpy(dst, src, len1);
662 #if defined(ENABLE_MEMCHECK)
663 /* destroy the source */
664 memset(src, MEMORY_CLEAR_BYTE, len1);
672 /* dump_release ****************************************************************
674 Release dump memory above the given size.
677 size........All dump memory above this mark will be freed. Usually
678 `size` will be the return value of a `dump_size` call
682 XXX If the given size is invalid, this function *exits* with an
687 *******************************************************************************/
689 void dump_release(s4 size)
691 #if defined(DISABLE_DUMP)
693 /* use malloc memory for dump memory (for debugging only!) */
697 #else /* !defined(DISABLE_DUMP) */
701 /* If no threads are used, the dumpinfo structure is a static structure */
702 /* defined at the top of this file. */
706 if ((size < 0) || (size > di->useddumpsize))
707 vm_abort("Illegal dump release size: %d", size);
709 #if defined(ENABLE_MEMCHECK)
711 dump_allocation_t *da, *next;
715 dump_check_canaries(di, size);
717 /* iterate over all dump memory allocations about to be released */
719 da = di->allocations;
720 while (da && da->useddumpsize >= size) {
723 /* invalidate the freed memory */
725 memset(da->mem, MEMORY_CLEAR_BYTE, da->size);
727 FREE(da, dump_allocation_t);
731 di->allocations = da;
733 #endif /* defined(ENABLE_MEMCHECK) */
735 /* reset the used dump size to the size specified */
737 di->useddumpsize = size;
739 while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
740 dumpblock_t *tmp = di->currentdumpblock;
742 di->allocateddumpsize -= tmp->size;
743 di->currentdumpblock = tmp->prev;
745 #if defined(ENABLE_STATISTICS)
746 /* the amount of globally allocated dump memory (thread save) */
749 globalallocateddumpsize -= tmp->size;
752 /* release the dump memory and the dumpinfo structure */
758 #endif /* defined(DISABLE_DUMP) */
762 /* dump_size *******************************************************************
764 Return the current size of the dump memory area. See `dump_alloc`.
766 *******************************************************************************/
770 #if defined(DISABLE_DUMP)
771 /* use malloc memory for dump memory (for debugging only!) */
775 #else /* !defined(DISABLE_DUMP) */
779 /* If no threads are used, the dumpinfo structure is a static structure */
780 /* defined at the top of this file. */
787 return di->useddumpsize;
789 #endif /* defined(DISABLE_DUMP) */
794 * These are local overrides for various environment variables in Emacs.
795 * Please do not remove this and leave it at the end of the file, where
796 * Emacs will automagically detect them.
797 * ---------------------------------------------------------------------
800 * indent-tabs-mode: t
804 * vim:noexpandtab:sw=4:ts=4: