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