300677645bbd0dd424fa657684bc56b535d0066e
[cacao.git] / src / mm / memory.h
1 /* src/mm/memory.h - 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
30 #include "config.h"
31
32 #include <string.h>
33
34 #include "vm/types.h"
35
36 #include "mm/codememory.h"
37 #include "mm/dumpmemory.h"
38
39
40 /* constants for ENABLE_MEMCHECK **********************************************/
41
42 #if defined(ENABLE_MEMCHECK)
43 #define MEMORY_CANARY_SIZE          16
44 #define MEMORY_CANARY_FIRST_BYTE    0xca
45 #define MEMORY_CLEAR_BYTE           0xa5
46 #endif /* defined(ENABLE_MEMCHECK) */
47
48
49 /* internal includes **********************************************************/
50
51 #include "mm/gc-common.h"
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
67         2.   explicit allocating, automatic deallocating
68         
69                         dump_alloc .... allocate a memory block in the dump area
70                         dump_realloc .. change size of a memory block (position may change)
71                         dump_size ..... marks the current top of dump
72                         dump_release .. free all memory requested after the mark
73                                         
74         
75 There are some useful macros:
76
77         NEW (type) ....... allocate memory for an element of type `type`
78         FREE (ptr,type) .. free memory
79         
80         MNEW (type,num) .. allocate memory for an array
81         MFREE (ptr,type,num) .. free memory
82         
83         MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
84                                          
85 These macros do the same except they operate on the dump area:
86         
87         DNEW,  DMNEW, DMREALLOC   (there is no DFREE)
88
89
90 -------------------------------------------------------------------------------
91
92 Some more macros:
93
94         MEMORY_ALIGN (pos, size) ... make pos divisible by size. always returns an
95                                  address >= pos.
96                               
97         
98         OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
99                               
100         MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
101         
102
103 */
104
105 #define MEMORY_ALIGN(pos,size) ((((pos) + (size) - 1) / (size)) * (size))
106 #define PADDING(pos,size)     (MEMORY_ALIGN((pos),(size)) - (pos))
107 #define OFFSET(s,el)          ((s4) ((ptrint) &(((s*) 0)->el)))
108
109
110 #define NEW(type)             ((type *) mem_alloc(sizeof(type)))
111 #define FREE(ptr,type)        mem_free((ptr), sizeof(type))
112
113 #define MNEW(type,num)        ((type *) mem_alloc(sizeof(type) * (num)))
114 #define MFREE(ptr,type,num)   mem_free((ptr), sizeof(type) * (num))
115
116 #define MREALLOC(ptr,type,num1,num2) mem_realloc((ptr), sizeof(type) * (num1), \
117                                                         sizeof(type) * (num2))
118
119
120 #define MCOPY(dest,src,type,num) memcpy((dest), (src), sizeof(type) * (num))
121 #define MSET(ptr,byte,type,num) memset((ptr), (byte), sizeof(type) * (num))
122 #define MZERO(ptr,type,num)     MSET(ptr,0,type,num)
123 #define MMOVE(dest,src,type,num) memmove((dest), (src), sizeof(type) * (num))
124
125
126 /* GC macros (boehm only) *****************************************************/
127
128 #if defined(ENABLE_GC_BOEHM)
129
130 /* Uncollectable memory which can contain references */
131
132 #define GCNEW_UNCOLLECTABLE(type,num) ((type *) heap_alloc_uncollectable(sizeof(type) * (num)))
133
134 #define GCNEW(type)           heap_alloc(sizeof(type), true, NULL, true)
135 #define GCMNEW(type,num)      heap_alloc(sizeof(type) * (num), true, NULL, true)
136
137 #define GCFREE(ptr)           heap_free((ptr))
138
139 #endif
140
141
142 /* function prototypes ********************************************************/
143
144 bool  memory_init(void);
145
146 void  memory_mprotect(void *addr, size_t len, int prot);
147
148 void *memory_checked_alloc(size_t size);
149
150 void *memory_cnew(s4 size);
151 void  memory_cfree(void *p, s4 size);
152
153 void *mem_alloc(s4 size);
154 void  mem_free(void *m, s4 size);
155 void *mem_realloc(void *src, s4 len1, s4 len2);
156
157 #if defined(ENABLE_THREADS)
158 bool  memory_start_thread(void);
159 #endif
160
161 #endif /* _MEMORY_H */
162
163
164 /*
165  * These are local overrides for various environment variables in Emacs.
166  * Please do not remove this and leave it at the end of the file, where
167  * Emacs will automagically detect them.
168  * ---------------------------------------------------------------------
169  * Local variables:
170  * mode: c
171  * indent-tabs-mode: t
172  * c-basic-offset: 4
173  * tab-width: 4
174  * End:
175  * vim:noexpandtab:sw=4:ts=4:
176  */