* configure.ac: New switch for disabling -O2 (--disable-optimizations).
[cacao.git] / src / mm / memory.hpp
1 /* src/mm/memory.hpp - macros for 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 #ifndef _MEMORY_H
27 #define _MEMORY_H
28
29 #include "config.h"
30
31 #include <stdint.h>
32
33
34 // Align the size of memory allocations to this size.
35 #define ALIGNSIZE 8
36 #define MEMORY_ALIGN(pos,size) ((((pos) + (size) - 1) / (size)) * (size))
37
38
39 // Constants for ENABLE_MEMCHECK.
40
41 #if defined(ENABLE_MEMCHECK)
42 #define MEMORY_CANARY_SIZE          16
43 #define MEMORY_CANARY_FIRST_BYTE    0xca
44 #define MEMORY_CLEAR_BYTE           0xa5
45 #endif
46
47
48 // Includes.
49 #include "mm/codememory.h"
50 #include "mm/dumpmemory.hpp"
51 #include "mm/gc.hpp"
52
53
54 /* 
55 ---------------------------- Interface description -----------------------
56
57 There are two possible choices for allocating memory:
58
59         1.   explicit allocating / deallocating
60
61                         mem_alloc ..... allocate a memory block 
62                         mem_free ...... free a memory block
63                         mem_realloc ... change size of a memory block (position may change)
64                         mem_usage ..... amount of allocated memory
65
66 There are some useful macros:
67
68         NEW (type) ....... allocate memory for an element of type `type`
69         FREE (ptr,type) .. free memory
70         
71         MNEW (type,num) .. allocate memory for an array
72         MFREE (ptr,type,num) .. free memory
73         
74         MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
75                                          
76 -------------------------------------------------------------------------------
77
78 Some more macros:
79
80         MEMORY_ALIGN (pos, size) ... make pos divisible by size. always returns an
81                                  address >= pos.
82                               
83         
84         OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
85                               
86         MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
87         
88
89 */
90
91 #define PADDING(pos,size)     (MEMORY_ALIGN((pos),(size)) - (pos))
92 #define OFFSET(s,el)          ((int32_t) ((ptrint) &(((s*) 0)->el)))
93
94
95 #define NEW(type)             ((type *) mem_alloc(sizeof(type)))
96 #define FREE(ptr,type)        mem_free((ptr), sizeof(type))
97
98 #define MNEW(type,num)        ((type *) mem_alloc(sizeof(type) * (num)))
99 #define MFREE(ptr,type,num)   mem_free((ptr), sizeof(type) * (num))
100
101 #define MREALLOC(ptr,type,num1,num2) mem_realloc((ptr), sizeof(type) * (num1), \
102                                                         sizeof(type) * (num2))
103
104
105 #define MCOPY(dest,src,type,num) memcpy((dest), (src), sizeof(type) * (num))
106 #define MSET(ptr,byte,type,num) memset((ptr), (byte), sizeof(type) * (num))
107 #define MZERO(ptr,type,num)     MSET(ptr,0,type,num)
108 #define MMOVE(dest,src,type,num) memmove((dest), (src), sizeof(type) * (num))
109
110
111 /* GC macros (boehm only) *****************************************************/
112
113 #if defined(ENABLE_GC_BOEHM)
114
115 /* Uncollectable memory which can contain references */
116
117 #define GCNEW_UNCOLLECTABLE(type,num) ((type *) heap_alloc_uncollectable(sizeof(type) * (num)))
118
119 #define GCNEW(type)           heap_alloc(sizeof(type), true, NULL, true)
120 #define GCMNEW(type,num)      heap_alloc(sizeof(type) * (num), true, NULL, true)
121
122 #define GCFREE(ptr)           heap_free((ptr))
123
124 #endif
125
126
127 /* function prototypes ********************************************************/
128
129 #ifdef __cplusplus
130 extern "C" {
131 #endif
132
133 bool  memory_init(void);
134
135 void  memory_mprotect(void *addr, size_t len, int prot);
136
137 void *memory_checked_alloc(size_t size);
138
139 void *memory_cnew(int32_t size);
140 void  memory_cfree(void *p, int32_t size);
141
142 void *mem_alloc(int32_t size);
143 void  mem_free(void *m, int32_t size);
144 void *mem_realloc(void *src, int32_t len1, int32_t len2);
145
146 #if defined(ENABLE_THREADS)
147 bool  memory_start_thread(void);
148 #endif
149
150 #ifdef __cplusplus
151 }
152 #endif
153
154 #endif /* _MEMORY_H */
155
156
157 /*
158  * These are local overrides for various environment variables in Emacs.
159  * Please do not remove this and leave it at the end of the file, where
160  * Emacs will automagically detect them.
161  * ---------------------------------------------------------------------
162  * Local variables:
163  * mode: c++
164  * indent-tabs-mode: t
165  * c-basic-offset: 4
166  * tab-width: 4
167  * End:
168  * vim:noexpandtab:sw=4:ts=4:
169  */