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
31 #if defined(__DARWIN__)
32 /* If we compile with -ansi on darwin, <sys/types.h> is not
33 included. So let's do it here. */
34 # include <sys/types.h>
41 #include "mm/memory.h"
43 #include "native/native.h"
45 #include "threads/lock.hpp"
46 #include "threads/thread.hpp"
48 #include "toolbox/logging.h"
50 #include "vm/global.h"
51 #include "vm/string.hpp"
54 #include "vm/options.h"
57 #if defined(ENABLE_STATISTICS)
58 # include "vm/statistics.h"
62 /* memory_mprotect *************************************************************
64 Convenience function for mprotect. This function also does error
67 *******************************************************************************/
69 void memory_mprotect(void *addr, size_t len, int prot)
71 if (os_mprotect(addr, len, prot) != 0)
72 vm_abort_errno("memory_mprotect: os_mprotect failed");
76 /* memory_checked_alloc ********************************************************
78 Allocated zeroed-out memory and does an OOM check.
81 XXX If no memory could be allocated, this function justs *exists*.
83 *******************************************************************************/
85 void *memory_checked_alloc(size_t size)
87 /* always allocate memory zeroed out */
89 void *p = os_calloc(size, 1);
92 vm_abort("memory_checked_alloc: calloc failed: out of memory");
98 void *mem_alloc(int32_t size)
105 #if defined(ENABLE_STATISTICS)
109 if (memoryusage > maxmemusage)
110 maxmemusage = memoryusage;
114 m = memory_checked_alloc(size);
116 #if defined(ENABLE_MEMCHECK)
117 /* XXX we would like to poison the memory, but callers rely on */
118 /* the zeroing. This should change sooner or later. */
119 /* memset(m, MEMORY_CLEAR_BYTE, size); */
126 void *mem_realloc(void *src, int32_t len1, int32_t len2)
130 /* prevent compiler warnings */
136 vm_abort("mem_realloc: reallocating memoryblock with address NULL, length != 0");
138 #if defined(ENABLE_STATISTICS)
140 memoryusage = (memoryusage - len1) + len2;
143 #if defined(ENABLE_MEMCHECK)
145 os_memset((u1*)dst + len2, MEMORY_CLEAR_BYTE, len1 - len2);
148 dst = realloc(src, len2);
151 vm_abort("mem_realloc: realloc failed: out of memory");
153 #if defined(ENABLE_MEMCHECK)
155 os_memset((u1*)dst + len1, MEMORY_CLEAR_BYTE, len2 - len1);
162 void mem_free(void *m, int32_t size)
168 log_text("returned memoryblock with address NULL, length != 0");
172 #if defined(ENABLE_STATISTICS)
177 #if defined(ENABLE_MEMCHECK)
178 /* destroy the contents */
179 os_memset(m, MEMORY_CLEAR_BYTE, size);
186 /* memory_thread ***************************************************************
188 Prints regularly memory statistics.
190 *******************************************************************************/
192 #if defined(ENABLE_THREADS) && !defined(NDEBUG)
193 static void memory_thread(void)
197 /* Prevent compiler warning. */
201 /* If both arguments are specified, use the value of
202 ProfileMemoryUsage. */
204 if (opt_ProfileGCMemoryUsage)
205 seconds = opt_ProfileGCMemoryUsage;
207 if (opt_ProfileMemoryUsage)
208 seconds = opt_ProfileMemoryUsage;
213 threads_sleep(seconds * 1000, 0);
215 # if defined(ENABLE_STATISTICS)
216 /* Print current date and time (only when we print to the
219 if (!opt_ProfileMemoryUsageGNUPlot)
220 statistics_print_date();
222 /* print memory usage */
224 if (opt_ProfileMemoryUsage)
225 statistics_print_memory_usage();
227 /* print GC memory usage */
229 if (opt_ProfileGCMemoryUsage)
230 statistics_print_gc_memory_usage();
237 /* memory_start_thread *********************************************************
239 Starts the memory profiling thread.
241 *******************************************************************************/
243 #if defined(ENABLE_THREADS) && !defined(NDEBUG)
244 bool memory_start_thread(void)
248 name = utf_new_char("Memory Profiler");
250 /* start the memory profiling thread */
252 if (!threads_thread_start_internal(name, memory_thread))
255 /* everything's ok */
263 * These are local overrides for various environment variables in Emacs.
264 * Please do not remove this and leave it at the end of the file, where
265 * Emacs will automagically detect them.
266 * ---------------------------------------------------------------------
269 * indent-tabs-mode: t
273 * vim:noexpandtab:sw=4:ts=4: