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