1 /* src/mm/codememory.c - code 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
30 #include <sys/mman.h> /* REMOVEME */
32 #include "threads/lock-common.h"
33 #include "threads/threads-common.h"
35 #include "mm/codememory.h"
36 #include "mm/memory.h"
38 #include "vmcore/options.h"
40 #if defined(ENABLE_STATISTICS)
41 # include "vmcore/statistics.h"
44 #include "vmcore/system.h"
46 #include "vm/global.h"
49 /* global code memory variables ***********************************************/
51 #define DEFAULT_CODE_MEMORY_SIZE 128 * 1024 /* defaulting to 128kB */
53 #if defined(ENABLE_THREADS)
54 static java_object_t *lock_code_memory = NULL;
56 static void *code_memory = NULL;
57 static int code_memory_size = 0;
58 static int pagesize = 0;
61 /* codememory_init *************************************************************
63 Initialize the code memory subsystem.
65 *******************************************************************************/
67 void codememory_init(void)
69 #if defined(ENABLE_THREADS)
70 /* create lock for code memory */
72 lock_code_memory = NEW(java_object_t);
74 lock_init_object_lock(lock_code_memory);
77 /* Get the pagesize of this architecture. */
79 pagesize = system_getpagesize();
83 /* codememory_get **************************************************************
85 Allocates memory from the heap via mmap and make the memory read-,
86 write-, and executeable.
88 *******************************************************************************/
90 void *codememory_get(size_t size)
94 LOCK_MONITOR_ENTER(lock_code_memory);
96 size = MEMORY_ALIGN(size, ALIGNSIZE);
98 /* check if enough memory is available */
100 if (size > code_memory_size) {
101 /* set default code size */
103 code_memory_size = DEFAULT_CODE_MEMORY_SIZE;
105 /* do we need more? */
107 if (size > code_memory_size)
108 code_memory_size = size;
110 /* align the size of the memory to be allocated */
112 code_memory_size = MEMORY_ALIGN(code_memory_size, pagesize);
114 #if defined(ENABLE_STATISTICS)
116 codememusage += code_memory_size;
118 if (codememusage > maxcodememusage)
119 maxcodememusage = codememusage;
123 /* allocate the memory */
125 p = system_mmap_anonymous(NULL, code_memory_size,
126 PROT_READ | PROT_WRITE | PROT_EXEC,
129 /* set global code memory pointer */
134 /* get a memory chunk of the allocated memory */
138 code_memory = (void *) ((ptrint) code_memory + size);
139 code_memory_size -= size;
141 LOCK_MONITOR_EXIT(lock_code_memory);
147 /* codememory_release **********************************************************
149 Release the code memory and return it to the code memory
153 p ...... pointer to the code memory
154 size ... size of the code memory
156 *******************************************************************************/
158 void codememory_release(void *p, size_t size)
165 * These are local overrides for various environment variables in Emacs.
166 * Please do not remove this and leave it at the end of the file, where
167 * Emacs will automagically detect them.
168 * ---------------------------------------------------------------------
171 * indent-tabs-mode: t
175 * vim:noexpandtab:sw=4:ts=4: