1 /* src/mm/memory.c - memory management
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 #if defined(__DARWIN__)
36 /* If we compile with -ansi on darwin, <sys/types.h> is not
37 included. So let's do it here. */
38 # include <sys/types.h>
45 #include "mm/memory.h"
47 #include "native/native.h"
49 #include "threads/lock-common.h"
50 #include "threads/thread.h"
52 #include "toolbox/logging.h"
54 #include "vm/exceptions.h"
55 #include "vm/global.h"
56 #include "vm/stringlocal.h"
59 #include "vmcore/options.h"
61 #if defined(ENABLE_STATISTICS)
62 # include "vmcore/statistics.h"
65 #include "vmcore/system.h"
68 /* memory_mprotect *************************************************************
70 Convenience function for mprotect. This function also does error
73 *******************************************************************************/
75 void memory_mprotect(void *addr, size_t len, int prot)
77 if (system_mprotect(addr, len, prot) != 0)
78 vm_abort("memory_mprotect: system_mprotect failed: %s",
83 /* memory_checked_alloc ********************************************************
85 Allocated zeroed-out memory and does an OOM check.
88 XXX If no memory could be allocated, this function justs *exists*.
90 *******************************************************************************/
92 void *memory_checked_alloc(size_t size)
94 /* always allocate memory zeroed out */
96 void *p = calloc(size, 1);
99 vm_abort("memory_checked_alloc: calloc failed: out of memory");
105 void *mem_alloc(int32_t size)
112 #if defined(ENABLE_STATISTICS)
116 if (memoryusage > maxmemusage)
117 maxmemusage = memoryusage;
121 m = memory_checked_alloc(size);
123 #if defined(ENABLE_MEMCHECK)
124 /* XXX we would like to poison the memory, but callers rely on */
125 /* the zeroing. This should change sooner or later. */
126 /* memset(m, MEMORY_CLEAR_BYTE, size); */
133 void *mem_realloc(void *src, int32_t len1, int32_t len2)
137 /* prevent compiler warnings */
143 vm_abort("mem_realloc: reallocating memoryblock with address NULL, length != 0");
145 #if defined(ENABLE_STATISTICS)
147 memoryusage = (memoryusage - len1) + len2;
150 #if defined(ENABLE_MEMCHECK)
152 memset((u1*)dst + len2, MEMORY_CLEAR_BYTE, len1 - len2);
155 dst = realloc(src, len2);
158 vm_abort("mem_realloc: realloc failed: out of memory");
160 #if defined(ENABLE_MEMCHECK)
162 memset((u1*)dst + len1, MEMORY_CLEAR_BYTE, len2 - len1);
169 void mem_free(void *m, int32_t size)
175 log_text("returned memoryblock with address NULL, length != 0");
179 #if defined(ENABLE_STATISTICS)
184 #if defined(ENABLE_MEMCHECK)
185 /* destroy the contents */
186 memset(m, MEMORY_CLEAR_BYTE, size);
193 /* memory_thread ***************************************************************
195 Prints regularly memory statistics.
197 *******************************************************************************/
199 #if defined(ENABLE_THREADS) && !defined(NDEBUG)
200 static void memory_thread(void)
204 /* Prevent compiler warning. */
208 /* If both arguments are specified, use the value of
209 ProfileMemoryUsage. */
211 if (opt_ProfileGCMemoryUsage)
212 seconds = opt_ProfileGCMemoryUsage;
214 if (opt_ProfileMemoryUsage)
215 seconds = opt_ProfileMemoryUsage;
220 threads_sleep(seconds * 1000, 0);
222 # if defined(ENABLE_STATISTICS)
223 /* Print current date and time (only when we print to the
226 if (!opt_ProfileMemoryUsageGNUPlot)
227 statistics_print_date();
229 /* print memory usage */
231 if (opt_ProfileMemoryUsage)
232 statistics_print_memory_usage();
234 /* print GC memory usage */
236 if (opt_ProfileGCMemoryUsage)
237 statistics_print_gc_memory_usage();
244 /* memory_start_thread *********************************************************
246 Starts the memory profiling thread.
248 *******************************************************************************/
250 #if defined(ENABLE_THREADS) && !defined(NDEBUG)
251 bool memory_start_thread(void)
255 name = utf_new_char("Memory Profiler");
257 /* start the memory profiling thread */
259 if (!threads_thread_start_internal(name, memory_thread))
262 /* everything's ok */
270 * These are local overrides for various environment variables in Emacs.
271 * Please do not remove this and leave it at the end of the file, where
272 * Emacs will automagically detect them.
273 * ---------------------------------------------------------------------
276 * indent-tabs-mode: t
280 * vim:noexpandtab:sw=4:ts=4: