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