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
31 #include "threads/mutex.hpp"
32 #include "threads/thread.hpp"
34 #include "mm/codememory.h"
35 #include "mm/memory.hpp"
37 #include "vm/global.h"
38 #include "vm/options.h"
41 #if defined(ENABLE_STATISTICS)
42 # include "vm/statistics.h"
46 /* global code memory variables ***********************************************/
48 #define DEFAULT_CODE_MEMORY_SIZE 128 * 1024 /* defaulting to 128kB */
50 #if defined(ENABLE_THREADS)
51 static Mutex *code_memory_mutex = NULL;
53 static void *code_memory = NULL;
54 static int code_memory_size = 0;
55 static int pagesize = 0;
58 /* codememory_init *************************************************************
60 Initialize the code memory subsystem.
62 *******************************************************************************/
64 void codememory_init(void)
66 TRACESUBSYSTEMINITIALIZATION("codememory_init");
68 #if defined(ENABLE_THREADS)
69 /* create mutex for code memory */
71 code_memory_mutex = Mutex_new();
74 /* Get the pagesize of this architecture. */
76 pagesize = os_getpagesize();
80 /* codememory_get **************************************************************
82 Allocates memory from the heap via mmap and make the memory read-,
83 write-, and executeable.
85 *******************************************************************************/
87 void *codememory_get(size_t size)
91 Mutex_lock(code_memory_mutex);
93 size = MEMORY_ALIGN(size, ALIGNSIZE);
95 /* check if enough memory is available */
97 if (size > code_memory_size) {
98 /* set default code size */
100 code_memory_size = DEFAULT_CODE_MEMORY_SIZE;
102 /* do we need more? */
104 if (size > code_memory_size)
105 code_memory_size = size;
107 /* align the size of the memory to be allocated */
109 code_memory_size = MEMORY_ALIGN(code_memory_size, pagesize);
111 #if defined(ENABLE_STATISTICS)
113 codememusage += code_memory_size;
115 if (codememusage > maxcodememusage)
116 maxcodememusage = codememusage;
120 /* allocate the memory */
122 p = os_mmap_anonymous(NULL, code_memory_size,
123 PROT_READ | PROT_WRITE | PROT_EXEC,
126 /* set global code memory pointer */
131 /* get a memory chunk of the allocated memory */
135 code_memory = (void *) ((ptrint) code_memory + size);
136 code_memory_size -= size;
138 Mutex_unlock(code_memory_mutex);
144 /* codememory_release **********************************************************
146 Release the code memory and return it to the code memory
150 p ...... pointer to the code memory
151 size ... size of the code memory
153 *******************************************************************************/
155 void codememory_release(void *p, size_t size)
162 * These are local overrides for various environment variables in Emacs.
163 * Please do not remove this and leave it at the end of the file, where
164 * Emacs will automagically detect them.
165 * ---------------------------------------------------------------------
168 * indent-tabs-mode: t
172 * vim:noexpandtab:sw=4:ts=4: