CACAO
tlh.cpp
Go to the documentation of this file.
1 /* src/mm/tlh.c
2 
3  Copyright (C) 2008-2013
4  CACAOVM - Verein zu Foerderung der freien virtuellen Machine 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 #include "mm/tlh.hpp"
25 #include <assert.h> // for assert
26 #include <sys/mman.h> // for mmap, munmap, MAP_ANONYMOUS, etc
27 #include "config.h" // for SIZEOF_VOID_P
28 #include "mm/memory.hpp" // for MZERO
29 
30 static const int TLH_MAX_SIZE = (20 * 1024 * 1024);
31 
32 static inline bool tlh_avail(tlh_t *tlh, unsigned n) {
33  /*
34  --- --- --- ---
35  ^ end
36  ^ top
37  ^ top + 2
38  */
39  return (tlh->top + n) <= tlh->end;
40 }
41 
42 void tlh_init(tlh_t *tlh) {
43 
44  void *heap = mmap(
45  NULL,
47  PROT_READ|PROT_WRITE,
48  MAP_ANONYMOUS|MAP_PRIVATE,
49  -1,
50  0
51  );
52 
53  if (heap == MAP_FAILED) {
54  /* The top pointer points to end, so all allocations will fail. */
55  tlh->start = NULL;
56  tlh->end = NULL;
57  tlh->top = NULL;
58  tlh->base = NULL;
59  } else {
60  tlh->start = (uint8_t *)heap;
61  tlh->top = tlh->start;
62  tlh->base = tlh->start;
63  tlh->end = tlh->start + TLH_MAX_SIZE;
64  }
65 
66  tlh->overflows = 0;
67 }
68 
69 void tlh_destroy(tlh_t *tlh) {
70  int res = munmap(tlh->start, TLH_MAX_SIZE);
71  if (res == -1) {
72  /* TODO */
73  assert(0);
74  }
75  tlh->start = NULL;
76  tlh->end = NULL;
77  tlh->top = NULL;
78  tlh->base = NULL;
79 }
80 
81 void tlh_add_frame(tlh_t *tlh) {
82  if (tlh_avail(tlh, SIZEOF_VOID_P)) {
83  *(uint8_t **)tlh->top = tlh->base;
84  tlh->base = tlh->top;
85  tlh->top += SIZEOF_VOID_P;
86  } else {
87  tlh->overflows += 1;
88  }
89 }
90 
91 void tlh_remove_frame(tlh_t *tlh) {
92  if (tlh->overflows > 0) {
93  tlh->overflows -= 1;
94  } else {
95  tlh->top = tlh->base;
96  tlh->base = *(uint8_t **)tlh->top;
97  }
98 }
99 
100 void *tlh_alloc(tlh_t *tlh, size_t size) {
101  void *ret;
102  if (tlh_avail(tlh, size)) {
103  ret = tlh->top;
104  tlh->top += size;
105  MZERO(ret, char, size);
106  } else {
107  ret = NULL;
108  }
109  return ret;
110 }
111 
112 /*
113  * These are local overrides for various environment variables in Emacs.
114  * Please do not remove this and leave it at the end of the file, where
115  * Emacs will automagically detect them.
116  * ---------------------------------------------------------------------
117  * Local variables:
118  * mode: c++
119  * indent-tabs-mode: t
120  * c-basic-offset: 4
121  * tab-width: 4
122  * End:
123  * vim:noexpandtab:sw=4:ts=4:
124  */
uint8_t * top
Definition: tlh.hpp:37
static const int TLH_MAX_SIZE
Definition: tlh.cpp:30
Definition: tlh.hpp:34
uint8_t * start
Definition: tlh.hpp:35
JNIEnv jthread jobject jclass jlong size
Definition: jvmti.h:387
#define MZERO(ptr, type, num)
Definition: memory.hpp:105
uint8_t * end
Definition: tlh.hpp:36
void tlh_destroy(tlh_t *tlh)
Definition: tlh.cpp:69
void tlh_remove_frame(tlh_t *tlh)
Definition: tlh.cpp:91
void * tlh_alloc(tlh_t *tlh, size_t size)
Definition: tlh.cpp:100
void tlh_init(tlh_t *tlh)
Definition: tlh.cpp:42
void tlh_add_frame(tlh_t *tlh)
Definition: tlh.cpp:81
uint8_t * base
Definition: tlh.hpp:38
static bool tlh_avail(tlh_t *tlh, unsigned n)
Definition: tlh.cpp:32
unsigned overflows
Definition: tlh.hpp:39