CACAO
codememory.cpp
Go to the documentation of this file.
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 void codememory_init(void)
57 {
58  TRACESUBSYSTEMINITIALIZATION("codememory_init");
59 
60  /* create mutex for code memory */
61 
62  code_memory_mutex = new Mutex();
63 
64  /* Get the pagesize of this architecture. */
65 
66  pagesize = os::getpagesize();
67 }
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 void *codememory_get(size_t size)
78 {
79  void *p;
80 
81  code_memory_mutex->lock();
82 
83  size = MEMORY_ALIGN(size, ALIGNSIZE);
84 
85  /* check if enough memory is available */
86 
87  if (size > code_memory_size) {
88  /* set default code size */
89 
90  code_memory_size = DEFAULT_CODE_MEMORY_SIZE;
91 
92  /* do we need more? */
93 
94  if (size > code_memory_size)
95  code_memory_size = size;
96 
97  /* align the size of the memory to be allocated */
98 
99  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  MAP_PRIVATE);
109 
110  /* set global code memory pointer */
111 
112  code_memory = p;
113  }
114 
115  /* get a memory chunk of the allocated memory */
116 
117  p = code_memory;
118 
119  code_memory = (void *) ((ptrint) code_memory + size);
120  code_memory_size -= size;
121 
122  code_memory_mutex->unlock();
123 
124  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 void codememory_release(void *p, size_t size)
140 {
141  /* do nothing */
142 }
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  */
static Mutex * code_memory_mutex
Definition: codememory.cpp:44
#define STATISTICS(x)
Wrapper for statistics only code.
Definition: statistics.hpp:975
static size_t pagesize
Definition: codememory.cpp:47
#define max(a, b)
Definition: lsra.hpp:80
Dummy implementation of a mutex.
Definition: mutex-none.hpp:33
JNIEnv jthread jobject jclass jlong size
Definition: jvmti.h:387
#define TRACESUBSYSTEMINITIALIZATION(text)
Definition: options.hpp:258
static size_t code_memory_size
Definition: codememory.cpp:46
void codememory_init(void)
Definition: codememory.cpp:56
void * codememory_get(size_t size)
Definition: codememory.cpp:77
This file contains the statistics framework.
static int getpagesize(void)
Definition: os.hpp:433
#define STAT_REGISTER_GROUP_VAR(type, var, init, name, description, group)
Register an statistics variable and add it to a group.
Definition: statistics.hpp:967
static void * code_memory
Definition: codememory.cpp:45
#define ALIGNSIZE
Definition: gc-none.cpp:50
void codememory_release(void *p, size_t size)
Definition: codememory.cpp:139
static void * mmap_anonymous(void *addr, size_t len, int prot, int flags)
Maps anonymous memory, even on systems not defining MAP_ANON(YMOUS).
Definition: os.cpp:215
#define STAT_DECLARE_GROUP(var)
Declare an external group (or subgroup).
Definition: statistics.hpp:970
#define MEMORY_ALIGN(pos, size)
Definition: memory.hpp:37
void unlock()
Unlocks the given mutex object and checks for errors.
Definition: mutex-none.hpp:36
#define DEFAULT_CODE_MEMORY_SIZE
Definition: codememory.cpp:42
uintptr_t ptrint
Definition: types.hpp:54
void lock()
Locks the given mutex object and checks for errors.
Definition: mutex-none.hpp:35