* src/mm/memory.cpp,
[cacao.git] / src / mm / codememory.c
1 /* src/mm/codememory.c - code memory management
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
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.
12
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.
17
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
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <sys/mman.h> /* REMOVEME */
31
32 #include "threads/mutex.hpp"
33 #include "threads/thread.hpp"
34
35 #include "mm/codememory.h"
36 #include "mm/memory.hpp"
37
38 #include "vm/global.h"
39 #include "vm/options.h"
40 #include "vm/os.hpp"
41
42 #if defined(ENABLE_STATISTICS)
43 # include "vm/statistics.h"
44 #endif
45
46
47 /* global code memory variables ***********************************************/
48
49 #define DEFAULT_CODE_MEMORY_SIZE    128 * 1024 /* defaulting to 128kB         */
50
51 #if defined(ENABLE_THREADS)
52 static Mutex *code_memory_mutex = NULL;
53 #endif
54 static void  *code_memory       = NULL;
55 static int    code_memory_size  = 0;
56 static int    pagesize          = 0;
57
58
59 /* codememory_init *************************************************************
60
61    Initialize the code memory subsystem.
62
63 *******************************************************************************/
64
65 void codememory_init(void)
66 {
67         TRACESUBSYSTEMINITIALIZATION("codememory_init");
68
69 #if defined(ENABLE_THREADS)
70         /* create mutex for code memory */
71
72         code_memory_mutex = Mutex_new();
73 #endif
74
75         /* Get the pagesize of this architecture. */
76
77         pagesize = os_getpagesize();
78 }
79
80
81 /* codememory_get **************************************************************
82
83    Allocates memory from the heap via mmap and make the memory read-,
84    write-, and executeable.
85
86 *******************************************************************************/
87
88 void *codememory_get(size_t size)
89 {
90         void *p;
91
92         Mutex_lock(code_memory_mutex);
93
94         size = MEMORY_ALIGN(size, ALIGNSIZE);
95
96         /* check if enough memory is available */
97
98         if (size > code_memory_size) {
99                 /* set default code size */
100
101                 code_memory_size = DEFAULT_CODE_MEMORY_SIZE;
102
103                 /* do we need more? */
104
105                 if (size > code_memory_size)
106                         code_memory_size = size;
107
108                 /* align the size of the memory to be allocated */
109
110                 code_memory_size = MEMORY_ALIGN(code_memory_size, pagesize);
111
112 #if defined(ENABLE_STATISTICS)
113                 if (opt_stat) {
114                         codememusage += code_memory_size;
115
116                         if (codememusage > maxcodememusage)
117                                 maxcodememusage = codememusage;
118                 }
119 #endif
120
121                 /* allocate the memory */
122
123                 p = os_mmap_anonymous(NULL, code_memory_size,
124                                                           PROT_READ | PROT_WRITE | PROT_EXEC,
125                                                           MAP_PRIVATE);
126
127                 /* set global code memory pointer */
128
129                 code_memory = p;
130         }
131
132         /* get a memory chunk of the allocated memory */
133
134         p = code_memory;
135
136         code_memory       = (void *) ((ptrint) code_memory + size);
137         code_memory_size -= size;
138
139         Mutex_unlock(code_memory_mutex);
140
141         return p;
142 }
143
144
145 /* codememory_release **********************************************************
146
147    Release the code memory and return it to the code memory
148    management.
149
150    IN:
151        p ...... pointer to the code memory
152            size ... size of the code memory
153
154 *******************************************************************************/
155
156 void codememory_release(void *p, size_t size)
157 {
158         /* do nothing */
159 }
160
161
162 /*
163  * These are local overrides for various environment variables in Emacs.
164  * Please do not remove this and leave it at the end of the file, where
165  * Emacs will automagically detect them.
166  * ---------------------------------------------------------------------
167  * Local variables:
168  * mode: c
169  * indent-tabs-mode: t
170  * c-basic-offset: 4
171  * tab-width: 4
172  * End:
173  * vim:noexpandtab:sw=4:ts=4:
174  */