merged volatile memory barriers
[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
31 #include "threads/mutex.hpp"
32 #include "threads/thread.hpp"
33
34 #include "mm/codememory.h"
35 #include "mm/memory.hpp"
36
37 #include "vm/global.h"
38 #include "vm/options.h"
39 #include "vm/os.hpp"
40
41 #if defined(ENABLE_STATISTICS)
42 # include "vm/statistics.h"
43 #endif
44
45
46 /* global code memory variables ***********************************************/
47
48 #define DEFAULT_CODE_MEMORY_SIZE    128 * 1024 /* defaulting to 128kB         */
49
50 #if defined(ENABLE_THREADS)
51 static Mutex *code_memory_mutex = NULL;
52 #endif
53 static void  *code_memory       = NULL;
54 static int    code_memory_size  = 0;
55 static int    pagesize          = 0;
56
57
58 /* codememory_init *************************************************************
59
60    Initialize the code memory subsystem.
61
62 *******************************************************************************/
63
64 void codememory_init(void)
65 {
66         TRACESUBSYSTEMINITIALIZATION("codememory_init");
67
68 #if defined(ENABLE_THREADS)
69         /* create mutex for code memory */
70
71         code_memory_mutex = Mutex_new();
72 #endif
73
74         /* Get the pagesize of this architecture. */
75
76         pagesize = os_getpagesize();
77 }
78
79
80 /* codememory_get **************************************************************
81
82    Allocates memory from the heap via mmap and make the memory read-,
83    write-, and executeable.
84
85 *******************************************************************************/
86
87 void *codememory_get(size_t size)
88 {
89         void *p;
90
91         Mutex_lock(code_memory_mutex);
92
93         size = MEMORY_ALIGN(size, ALIGNSIZE);
94
95         /* check if enough memory is available */
96
97         if (size > code_memory_size) {
98                 /* set default code size */
99
100                 code_memory_size = DEFAULT_CODE_MEMORY_SIZE;
101
102                 /* do we need more? */
103
104                 if (size > code_memory_size)
105                         code_memory_size = size;
106
107                 /* align the size of the memory to be allocated */
108
109                 code_memory_size = MEMORY_ALIGN(code_memory_size, pagesize);
110
111 #if defined(ENABLE_STATISTICS)
112                 if (opt_stat) {
113                         codememusage += code_memory_size;
114
115                         if (codememusage > maxcodememusage)
116                                 maxcodememusage = codememusage;
117                 }
118 #endif
119
120                 /* allocate the memory */
121
122                 p = os_mmap_anonymous(NULL, code_memory_size,
123                                                           PROT_READ | PROT_WRITE | PROT_EXEC,
124                                                           MAP_PRIVATE);
125
126                 /* set global code memory pointer */
127
128                 code_memory = p;
129         }
130
131         /* get a memory chunk of the allocated memory */
132
133         p = code_memory;
134
135         code_memory       = (void *) ((ptrint) code_memory + size);
136         code_memory_size -= size;
137
138         Mutex_unlock(code_memory_mutex);
139
140         return p;
141 }
142
143
144 /* codememory_release **********************************************************
145
146    Release the code memory and return it to the code memory
147    management.
148
149    IN:
150        p ...... pointer to the code memory
151            size ... size of the code memory
152
153 *******************************************************************************/
154
155 void codememory_release(void *p, size_t size)
156 {
157         /* do nothing */
158 }
159
160
161 /*
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  * ---------------------------------------------------------------------
166  * Local variables:
167  * mode: c
168  * indent-tabs-mode: t
169  * c-basic-offset: 4
170  * tab-width: 4
171  * End:
172  * vim:noexpandtab:sw=4:ts=4:
173  */