* src/mm/memory.h (ALIGN): Renamed to MEMORY_ALIGN.
[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 5868 2006-10-30 11:21:36Z edwin $
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 /* dumpinfo *******************************************************************/
68
69 struct dumpinfo_t {
70         dumpblock_t *currentdumpblock;
71         s4           allocateddumpsize;
72         s4           useddumpsize;
73 };
74
75
76 /* internal includes **********************************************************/
77
78 #include "mm/boehm.h"
79
80
81 /* 
82 ---------------------------- Interface description -----------------------
83
84 There are two possible choices for allocating memory:
85
86         1.   explicit allocating / deallocating
87
88                         mem_alloc ..... allocate a memory block 
89                         mem_free ...... free a memory block
90                         mem_realloc ... change size of a memory block (position may change)
91                         mem_usage ..... amount of allocated memory
92
93
94         2.   explicit allocating, automatic deallocating
95         
96                         dump_alloc .... allocate a memory block in the dump area
97                         dump_realloc .. change size of a memory block (position may change)
98                         dump_size ..... marks the current top of dump
99                         dump_release .. free all memory requested after the mark
100                                         
101         
102 There are some useful macros:
103
104         NEW (type) ....... allocate memory for an element of type `type`
105         FREE (ptr,type) .. free memory
106         
107         MNEW (type,num) .. allocate memory for an array
108         MFREE (ptr,type,num) .. free memory
109         
110         MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
111                                          
112 These macros do the same except they operate on the dump area:
113         
114         DNEW,  DMNEW, DMREALLOC   (there is no DFREE)
115
116
117 -------------------------------------------------------------------------------
118
119 Some more macros:
120
121         MEMORY_ALIGN (pos, size) ... make pos divisible by size. always returns an
122                                  address >= pos.
123                               
124         
125         OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
126                               
127         MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
128         
129
130 */
131
132 #define MEMORY_ALIGN(pos,size) ((((pos) + (size) - 1) / (size)) * (size))
133 #define PADDING(pos,size)     (MEMORY_ALIGN((pos),(size)) - (pos))
134 #define OFFSET(s,el)          ((s4) ((ptrint) &(((s*) 0)->el)))
135
136 #if !defined(DISABLE_GC)
137
138 #define NEW(type)             ((type *) mem_alloc(sizeof(type)))
139 #define FREE(ptr,type)        mem_free((ptr), sizeof(type))
140
141 #define MNEW(type,num)        ((type *) mem_alloc(sizeof(type) * (num)))
142 #define MFREE(ptr,type,num)   mem_free((ptr), sizeof(type) * (num))
143
144 #define MREALLOC(ptr,type,num1,num2) mem_realloc((ptr), sizeof(type) * (num1), \
145                                                         sizeof(type) * (num2))
146
147 #else
148
149 #define NEW(type)             GCNEW(type)
150 #define FREE(ptr,type)        GCFREE(ptr)
151
152 #define MNEW(type,num)        GCMNEW(type,num)
153 #define MFREE(ptr,type,num)   GCFREE(ptr)
154
155 #define MREALLOC(ptr,type,num1,num2) nogc_realloc((ptr), sizeof(type) * (num1), \
156                                                         sizeof(type) * (num2))
157
158 #endif
159
160 #define DNEW(type)            ((type *) dump_alloc(sizeof(type)))
161 #define DMNEW(type,num)       ((type *) dump_alloc(sizeof(type) * (num)))
162 #define DMREALLOC(ptr,type,num1,num2) dump_realloc((ptr), sizeof(type) * (num1), \
163                                                           sizeof(type) * (num2))
164
165 #define MCOPY(dest,src,type,num) memcpy((dest), (src), sizeof(type) * (num))
166 #define MSET(ptr,byte,type,num) memset((ptr), (byte), sizeof(type) * (num))
167 #define MZERO(ptr,type,num)     MSET(ptr,0,type,num)
168 #define MMOVE(dest,src,type,num) memmove((dest), (src), sizeof(type) * (num))
169
170 #define CNEW(type,num)        ((type *) memory_cnew(sizeof(type) * (num)))
171 #define CFREE(ptr,num)        memory_cfree((ptr),(num))
172
173
174 /* GC macros ******************************************************************/
175
176 /* Uncollectable memory which can contain references */
177
178 #define GCNEW_UNCOLLECTABLE(type,num) ((type *) heap_alloc_uncollectable(sizeof(type) * (num)))
179
180 #define GCNEW(type)           heap_allocate(sizeof(type), true, NULL)
181 #define GCMNEW(type,num)      heap_allocate(sizeof(type) * (num), true, NULL)
182
183 #define GCFREE(ptr)           heap_free((ptr))
184
185
186 /* function prototypes ********************************************************/
187
188 /* initializes the memory subsystem */
189 bool memory_init(void);
190
191 void *memory_cnew(s4 size);
192 void  memory_cfree(void *p, s4 size);
193
194 void *mem_alloc(s4 size);
195 void  mem_free(void *m, s4 size);
196 void *mem_realloc(void *src, s4 len1, s4 len2);
197
198 void *dump_alloc(s4 size);
199 void *dump_realloc(void *src, s4 len1, s4 len2);
200 s4    dump_size(void);
201 void  dump_release(s4 size);
202
203 #endif /* _MEMORY_H */
204
205
206 /*
207  * These are local overrides for various environment variables in Emacs.
208  * Please do not remove this and leave it at the end of the file, where
209  * Emacs will automagically detect them.
210  * ---------------------------------------------------------------------
211  * Local variables:
212  * mode: c
213  * indent-tabs-mode: t
214  * c-basic-offset: 4
215  * tab-width: 4
216  * End:
217  */