26f6d8479cfbecb713909757dbacd96982ef7d83
[cacao.git] / src / mm / memory.h
1 /* src/mm/memory.h - macros for memory management
2
3    Copyright (C) 1996-2005, 2006 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    Contact: cacao@cacaojvm.org
26
27    Authors: Reinhard Grafl
28
29    Changes: Christian Thalinger
30
31    $Id: memory.h 7615 2007-03-29 23:10:59Z michi $
32
33 */
34
35
36 #ifndef _MEMORY_H
37 #define _MEMORY_H
38
39 /* forward typedefs ***********************************************************/
40
41 typedef struct dumpblock_t dumpblock_t;
42 typedef struct dumpinfo_t  dumpinfo_t;
43
44 #include "config.h"
45
46 #include <string.h>
47
48 #include "vm/types.h"
49
50
51 /* ATTENTION: We need to define dumpblock_t and dumpinfo_t before
52    internal includes, as we need dumpinfo_t as nested structure in
53    threadobject. */
54
55 /* dumpblock ******************************************************************/
56
57 #define DUMPBLOCKSIZE    2 << 13    /* 2 * 8192 bytes */
58 #define ALIGNSIZE        8
59
60 struct dumpblock_t {
61         dumpblock_t *prev;
62         u1          *dumpmem;
63         s4           size;
64 };
65
66
67 /* dump_allocation *************************************************************
68
69    This struct is used to record dump memory allocations for ENABLE_MEMCHECK.
70
71 *******************************************************************************/
72
73 #if defined(ENABLE_MEMCHECK)
74 typedef struct dump_allocation_t dump_allocation_t;
75
76 struct dump_allocation_t {
77         dump_allocation_t *next;
78         u1                *mem;
79         s4                 useddumpsize;
80         s4                 size;
81 };
82 #endif
83
84
85 /* dumpinfo *******************************************************************/
86
87 struct dumpinfo_t {
88         dumpblock_t       *currentdumpblock;        /* the current top-most block */
89         s4                 allocateddumpsize;     /* allocated bytes in this area */
90         s4                 useddumpsize;          /* used bytes in this dump area */
91 #if defined(ENABLE_MEMCHECK)
92         dump_allocation_t *allocations;       /* list of allocations in this area */
93 #endif
94 };
95
96
97 /* constants for ENABLE_MEMCHECK **********************************************/
98
99 #if defined(ENABLE_MEMCHECK)
100 #define MEMORY_CANARY_SIZE          16
101 #define MEMORY_CANARY_FIRST_BYTE    0xca
102 #define MEMORY_CLEAR_BYTE           0xa5
103 #endif /* defined(ENABLE_MEMCHECK) */
104
105
106 /* internal includes **********************************************************/
107
108 #include "mm/gc-common.h"
109
110
111 /* 
112 ---------------------------- Interface description -----------------------
113
114 There are two possible choices for allocating memory:
115
116         1.   explicit allocating / deallocating
117
118                         mem_alloc ..... allocate a memory block 
119                         mem_free ...... free a memory block
120                         mem_realloc ... change size of a memory block (position may change)
121                         mem_usage ..... amount of allocated memory
122
123
124         2.   explicit allocating, automatic deallocating
125         
126                         dump_alloc .... allocate a memory block in the dump area
127                         dump_realloc .. change size of a memory block (position may change)
128                         dump_size ..... marks the current top of dump
129                         dump_release .. free all memory requested after the mark
130                                         
131         
132 There are some useful macros:
133
134         NEW (type) ....... allocate memory for an element of type `type`
135         FREE (ptr,type) .. free memory
136         
137         MNEW (type,num) .. allocate memory for an array
138         MFREE (ptr,type,num) .. free memory
139         
140         MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
141                                          
142 These macros do the same except they operate on the dump area:
143         
144         DNEW,  DMNEW, DMREALLOC   (there is no DFREE)
145
146
147 -------------------------------------------------------------------------------
148
149 Some more macros:
150
151         MEMORY_ALIGN (pos, size) ... make pos divisible by size. always returns an
152                                  address >= pos.
153                               
154         
155         OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
156                               
157         MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
158         
159
160 */
161
162 #define MEMORY_ALIGN(pos,size) ((((pos) + (size) - 1) / (size)) * (size))
163 #define PADDING(pos,size)     (MEMORY_ALIGN((pos),(size)) - (pos))
164 #define OFFSET(s,el)          ((s4) ((ptrint) &(((s*) 0)->el)))
165
166
167 #define NEW(type)             ((type *) mem_alloc(sizeof(type)))
168 #define FREE(ptr,type)        mem_free((ptr), sizeof(type))
169
170 #define MNEW(type,num)        ((type *) mem_alloc(sizeof(type) * (num)))
171 #define MFREE(ptr,type,num)   mem_free((ptr), sizeof(type) * (num))
172
173 #define MREALLOC(ptr,type,num1,num2) mem_realloc((ptr), sizeof(type) * (num1), \
174                                                         sizeof(type) * (num2))
175
176
177 #define DNEW(type)            ((type *) dump_alloc(sizeof(type)))
178 #define DMNEW(type,num)       ((type *) dump_alloc(sizeof(type) * (num)))
179 #define DMREALLOC(ptr,type,num1,num2) dump_realloc((ptr), sizeof(type) * (num1), \
180                                                           sizeof(type) * (num2))
181
182 #define MCOPY(dest,src,type,num) memcpy((dest), (src), sizeof(type) * (num))
183 #define MSET(ptr,byte,type,num) memset((ptr), (byte), sizeof(type) * (num))
184 #define MZERO(ptr,type,num)     MSET(ptr,0,type,num)
185 #define MMOVE(dest,src,type,num) memmove((dest), (src), sizeof(type) * (num))
186
187 #define CNEW(type,num)        ((type *) memory_cnew(sizeof(type) * (num)))
188 #define CFREE(ptr,num)        memory_cfree((ptr),(num))
189
190
191 /* GC macros ******************************************************************/
192
193 #if !defined(ENABLE_GC_CACAO)
194
195 /* Uncollectable memory which can contain references */
196
197 #define GCNEW_UNCOLLECTABLE(type,num) ((type *) heap_alloc_uncollectable(sizeof(type) * (num)))
198
199 #define GCNEW(type)           heap_alloc(sizeof(type), true, NULL, true)
200 #define GCMNEW(type,num)      heap_alloc(sizeof(type) * (num), true, NULL, true)
201
202 #define GCFREE(ptr)           heap_free((ptr))
203
204 #endif
205
206
207 /* function prototypes ********************************************************/
208
209 /* initializes the memory subsystem */
210 bool memory_init(void);
211
212 void *memory_mmap_anon(void *addr, size_t len, int prot, int flags);
213
214 void *memory_cnew(s4 size);
215 void  memory_cfree(void *p, s4 size);
216
217 void *mem_alloc(s4 size);
218 void  mem_free(void *m, s4 size);
219 void *mem_realloc(void *src, s4 len1, s4 len2);
220
221 #if defined(ENABLE_THREADS)
222 bool  memory_start_thread(void);
223 #endif
224
225 void *dump_alloc(s4 size);
226 void *dump_realloc(void *src, s4 len1, s4 len2);
227 s4    dump_size(void);
228 void  dump_release(s4 size);
229
230 #endif /* _MEMORY_H */
231
232
233 /*
234  * These are local overrides for various environment variables in Emacs.
235  * Please do not remove this and leave it at the end of the file, where
236  * Emacs will automagically detect them.
237  * ---------------------------------------------------------------------
238  * Local variables:
239  * mode: c
240  * indent-tabs-mode: t
241  * c-basic-offset: 4
242  * tab-width: 4
243  * End:
244  * vim:noexpandtab:sw=4:ts=4:
245  */