* Merged with default branch at rev 16f3633aaa5a.
[cacao.git] / src / mm / memory.h
1 /* src/mm/memory.h - macros for memory management
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #ifndef _MEMORY_H
29 #define _MEMORY_H
30
31 /* forward typedefs ***********************************************************/
32
33 typedef struct dumpblock_t dumpblock_t;
34 typedef struct dumpinfo_t  dumpinfo_t;
35
36 #include "config.h"
37
38 #include <string.h>
39
40 #include "vm/types.h"
41
42
43 /* ATTENTION: We need to define dumpblock_t and dumpinfo_t before
44    internal includes, as we need dumpinfo_t as nested structure in
45    threadobject. */
46
47 /* dumpblock ******************************************************************/
48
49 #define DUMPBLOCKSIZE    2 << 13    /* 2 * 8192 bytes */
50 #define ALIGNSIZE        8
51
52 struct dumpblock_t {
53         dumpblock_t *prev;
54         u1          *dumpmem;
55         s4           size;
56 };
57
58
59 /* dump_allocation *************************************************************
60
61    This struct is used to record dump memory allocations for ENABLE_MEMCHECK.
62
63 *******************************************************************************/
64
65 #if defined(ENABLE_MEMCHECK)
66 typedef struct dump_allocation_t dump_allocation_t;
67
68 struct dump_allocation_t {
69         dump_allocation_t *next;
70         u1                *mem;
71         s4                 useddumpsize;
72         s4                 size;
73 };
74 #endif
75
76
77 /* dumpinfo *******************************************************************/
78
79 struct dumpinfo_t {
80         dumpblock_t       *currentdumpblock;        /* the current top-most block */
81         s4                 allocateddumpsize;     /* allocated bytes in this area */
82         s4                 useddumpsize;          /* used bytes in this dump area */
83 #if defined(ENABLE_MEMCHECK)
84         dump_allocation_t *allocations;       /* list of allocations in this area */
85 #endif
86 };
87
88
89 /* constants for ENABLE_MEMCHECK **********************************************/
90
91 #if defined(ENABLE_MEMCHECK)
92 #define MEMORY_CANARY_SIZE          16
93 #define MEMORY_CANARY_FIRST_BYTE    0xca
94 #define MEMORY_CLEAR_BYTE           0xa5
95 #endif /* defined(ENABLE_MEMCHECK) */
96
97
98 /* internal includes **********************************************************/
99
100 #include "mm/gc-common.h"
101
102
103 /* 
104 ---------------------------- Interface description -----------------------
105
106 There are two possible choices for allocating memory:
107
108         1.   explicit allocating / deallocating
109
110                         mem_alloc ..... allocate a memory block 
111                         mem_free ...... free a memory block
112                         mem_realloc ... change size of a memory block (position may change)
113                         mem_usage ..... amount of allocated memory
114
115
116         2.   explicit allocating, automatic deallocating
117         
118                         dump_alloc .... allocate a memory block in the dump area
119                         dump_realloc .. change size of a memory block (position may change)
120                         dump_size ..... marks the current top of dump
121                         dump_release .. free all memory requested after the mark
122                                         
123         
124 There are some useful macros:
125
126         NEW (type) ....... allocate memory for an element of type `type`
127         FREE (ptr,type) .. free memory
128         
129         MNEW (type,num) .. allocate memory for an array
130         MFREE (ptr,type,num) .. free memory
131         
132         MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
133                                          
134 These macros do the same except they operate on the dump area:
135         
136         DNEW,  DMNEW, DMREALLOC   (there is no DFREE)
137
138
139 -------------------------------------------------------------------------------
140
141 Some more macros:
142
143         MEMORY_ALIGN (pos, size) ... make pos divisible by size. always returns an
144                                  address >= pos.
145                               
146         
147         OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
148                               
149         MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
150         
151
152 */
153
154 #define MEMORY_ALIGN(pos,size) ((((pos) + (size) - 1) / (size)) * (size))
155 #define PADDING(pos,size)     (MEMORY_ALIGN((pos),(size)) - (pos))
156 #define OFFSET(s,el)          ((s4) ((ptrint) &(((s*) 0)->el)))
157
158
159 #define NEW(type)             ((type *) mem_alloc(sizeof(type)))
160 #define FREE(ptr,type)        mem_free((ptr), sizeof(type))
161
162 #define MNEW(type,num)        ((type *) mem_alloc(sizeof(type) * (num)))
163 #define MFREE(ptr,type,num)   mem_free((ptr), sizeof(type) * (num))
164
165 #define MREALLOC(ptr,type,num1,num2) mem_realloc((ptr), sizeof(type) * (num1), \
166                                                         sizeof(type) * (num2))
167
168
169 #define DNEW(type)            ((type *) dump_alloc(sizeof(type)))
170 #define DMNEW(type,num)       ((type *) dump_alloc(sizeof(type) * (num)))
171 #define DMREALLOC(ptr,type,num1,num2) dump_realloc((ptr), sizeof(type) * (num1), \
172                                                           sizeof(type) * (num2))
173
174 #define MCOPY(dest,src,type,num) memcpy((dest), (src), sizeof(type) * (num))
175 #define MSET(ptr,byte,type,num) memset((ptr), (byte), sizeof(type) * (num))
176 #define MZERO(ptr,type,num)     MSET(ptr,0,type,num)
177 #define MMOVE(dest,src,type,num) memmove((dest), (src), sizeof(type) * (num))
178
179 #define CNEW(type,num)        ((type *) memory_cnew(sizeof(type) * (num)))
180 #define CFREE(ptr,num)        memory_cfree((ptr),(num))
181
182
183 /* GC macros ******************************************************************/
184
185 #if !defined(ENABLE_GC_CACAO)
186
187 /* Uncollectable memory which can contain references */
188
189 #define GCNEW_UNCOLLECTABLE(type,num) ((type *) heap_alloc_uncollectable(sizeof(type) * (num)))
190
191 #define GCNEW(type)           heap_alloc(sizeof(type), true, NULL, true)
192 #define GCMNEW(type,num)      heap_alloc(sizeof(type) * (num), true, NULL, true)
193
194 #define GCFREE(ptr)           heap_free((ptr))
195
196 #endif
197
198
199 /* function prototypes ********************************************************/
200
201 /* initializes the memory subsystem */
202 bool memory_init(void);
203
204 void *memory_mmap_anon(void *addr, size_t len, int prot, int flags);
205
206 void *memory_cnew(s4 size);
207 void  memory_cfree(void *p, s4 size);
208
209 void *mem_alloc(s4 size);
210 void  mem_free(void *m, s4 size);
211 void *mem_realloc(void *src, s4 len1, s4 len2);
212
213 #if defined(ENABLE_THREADS)
214 bool  memory_start_thread(void);
215 #endif
216
217 void *dump_alloc(s4 size);
218 void *dump_realloc(void *src, s4 len1, s4 len2);
219 s4    dump_size(void);
220 void  dump_release(s4 size);
221
222 #endif /* _MEMORY_H */
223
224
225 /*
226  * These are local overrides for various environment variables in Emacs.
227  * Please do not remove this and leave it at the end of the file, where
228  * Emacs will automagically detect them.
229  * ---------------------------------------------------------------------
230  * Local variables:
231  * mode: c
232  * indent-tabs-mode: t
233  * c-basic-offset: 4
234  * tab-width: 4
235  * End:
236  * vim:noexpandtab:sw=4:ts=4:
237  */