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