Line data Source code
1 : /* src/mm/codememory.c - code memory management
2 :
3 : Copyright (C) 1996-2013
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 : #include "mm/codememory.hpp"
26 : #include <stdlib.h> // for NULL
27 : #include <sys/mman.h> // for MAP_PRIVATE, PROT_EXEC, etc
28 : #include "config.h"
29 : #include "mm/memory.hpp" // for MEMORY_ALIGN, ALIGNSIZE
30 : #include "threads/mutex.hpp" // for Mutex
31 : #include "vm/options.hpp"
32 : #include "vm/os.hpp" // for os
33 : #include "vm/types.hpp" // for ptrint
34 : #include "vm/statistics.hpp"
35 :
36 : STAT_DECLARE_GROUP(max_mem_stat)
37 : STAT_DECLARE_GROUP(not_freed_mem_stat)
38 : STAT_REGISTER_GROUP_VAR(int,maxcodememusage,0,"maxcodememusage","max. code memory",max_mem_stat)
39 : STAT_REGISTER_GROUP_VAR(int,codememusage,0,"codememusage","max. code memory",not_freed_mem_stat)
40 : /* global code memory variables ***********************************************/
41 :
42 : #define DEFAULT_CODE_MEMORY_SIZE 128 * 1024 /* defaulting to 128kB */
43 :
44 : static Mutex *code_memory_mutex = NULL;
45 : static void *code_memory = NULL;
46 : static size_t code_memory_size = 0;
47 : static size_t pagesize = 0;
48 :
49 :
50 : /* codememory_init *************************************************************
51 :
52 : Initialize the code memory subsystem.
53 :
54 : *******************************************************************************/
55 :
56 163 : void codememory_init(void)
57 : {
58 163 : TRACESUBSYSTEMINITIALIZATION("codememory_init");
59 :
60 : /* create mutex for code memory */
61 :
62 163 : code_memory_mutex = new Mutex();
63 :
64 : /* Get the pagesize of this architecture. */
65 :
66 163 : pagesize = os::getpagesize();
67 163 : }
68 :
69 :
70 : /* codememory_get **************************************************************
71 :
72 : Allocates memory from the heap via mmap and make the memory read-,
73 : write-, and executeable.
74 :
75 : *******************************************************************************/
76 :
77 522178 : void *codememory_get(size_t size)
78 : {
79 : void *p;
80 :
81 522178 : code_memory_mutex->lock();
82 :
83 522178 : size = MEMORY_ALIGN(size, ALIGNSIZE);
84 :
85 : /* check if enough memory is available */
86 :
87 522178 : if (size > code_memory_size) {
88 : /* set default code size */
89 :
90 491 : code_memory_size = DEFAULT_CODE_MEMORY_SIZE;
91 :
92 : /* do we need more? */
93 :
94 491 : if (size > code_memory_size)
95 1 : code_memory_size = size;
96 :
97 : /* align the size of the memory to be allocated */
98 :
99 491 : code_memory_size = MEMORY_ALIGN(code_memory_size, pagesize);
100 :
101 : STATISTICS(codememusage += code_memory_size);
102 : STATISTICS(maxcodememusage.max(codememusage.get()));
103 :
104 : /* allocate the memory */
105 :
106 : p = os::mmap_anonymous(NULL, code_memory_size,
107 : PROT_READ | PROT_WRITE | PROT_EXEC,
108 491 : MAP_PRIVATE);
109 :
110 : /* set global code memory pointer */
111 :
112 491 : code_memory = p;
113 : }
114 :
115 : /* get a memory chunk of the allocated memory */
116 :
117 522178 : p = code_memory;
118 :
119 522178 : code_memory = (void *) ((ptrint) code_memory + size);
120 522178 : code_memory_size -= size;
121 :
122 522178 : code_memory_mutex->unlock();
123 :
124 522178 : return p;
125 : }
126 :
127 :
128 : /* codememory_release **********************************************************
129 :
130 : Release the code memory and return it to the code memory
131 : management.
132 :
133 : IN:
134 : p ...... pointer to the code memory
135 : size ... size of the code memory
136 :
137 : *******************************************************************************/
138 :
139 0 : void codememory_release(void *p, size_t size)
140 : {
141 : /* do nothing */
142 0 : }
143 :
144 :
145 : /*
146 : * These are local overrides for various environment variables in Emacs.
147 : * Please do not remove this and leave it at the end of the file, where
148 : * Emacs will automagically detect them.
149 : * ---------------------------------------------------------------------
150 : * Local variables:
151 : * mode: c++
152 : * indent-tabs-mode: t
153 : * c-basic-offset: 4
154 : * tab-width: 4
155 : * End:
156 : * vim:noexpandtab:sw=4:ts=4:
157 : */
|