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