GNU header update.
[cacao.git] / src / mm / memory.h
1 /* toolbox/memory.h - macros for memory management
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Reinhard Grafl
28
29    $Id: memory.h 1735 2004-12-07 14:33:27Z twisti $
30
31 */
32
33
34 #ifndef _MEMORY_H
35 #define _MEMORY_H
36
37 #include <string.h>
38
39 #include "arch.h"
40 #include "types.h"
41
42
43 /* 
44 ---------------------------- Interface description -----------------------
45
46 There are two possible choices for allocating memory:
47
48         1.   explicit allocating / deallocating
49
50                         mem_alloc ..... allocate a memory block 
51                         mem_free ...... free a memory block
52                         mem_realloc ... change size of a memory block (position may change)
53                         mem_usage ..... amount of allocated memory
54
55
56         2.   explicit allocating, automatic deallocating
57         
58                         dump_alloc .... allocate a memory block in the dump area
59                         dump_realloc .. change size of a memory block (position may change)
60                         dump_size ..... marks the current top of dump
61                         dump_release .. free all memory requested after the mark
62                                         
63         
64 There are some useful macros:
65
66         NEW (type) ....... allocate memory for an element of type `type`
67         FREE (ptr,type) .. free memory
68         
69         MNEW (type,num) .. allocate memory for an array
70         MFREE (ptr,type,num) .. free memory
71         
72         MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
73                                          
74 These macros do the same except they operate on the dump area:
75         
76         DNEW,  DMNEW, DMREALLOC   (there is no DFREE)
77
78
79 -------------------------------------------------------------------------------
80
81 Some more macros:
82
83         ALIGN (pos, size) ... make pos divisible by size. always returns an
84                                                   address >= pos.
85                               
86         
87         OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
88                               
89         MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
90         
91
92 */
93
94
95 #define DUMPBLOCKSIZE    2 << 13    /* 2 * 8192 bytes */
96 #define ALIGNSIZE        8
97
98
99 /* dumpblock *******************************************************************
100
101    TODO
102
103 *******************************************************************************/
104
105 typedef struct dumpblock dumpblock;
106
107 struct dumpblock {
108         dumpblock *prev;
109         u1        *dumpmem;
110         s4         size;
111 };
112
113
114 /* dumpinfo ********************************************************************
115
116    DOCUMENT ME!
117
118 *******************************************************************************/
119
120 typedef struct dumpinfo dumpinfo;
121
122 struct dumpinfo {
123         dumpblock *currentdumpblock;
124         s4         allocateddumpsize;
125         s4         useddumpsize;
126 };
127
128
129 /* Uncollectable memory which can contain references */
130
131 #define GCNEW(type,num)       ((type *) heap_alloc_uncollectable(sizeof(type) * (num)))
132 #define GCFREE(ptr)           heap_free((ptr))
133
134 #define ALIGN(pos,size)       ((((pos) + (size) - 1) / (size)) * (size))
135 #define PADDING(pos,size)     (ALIGN((pos),(size)) - (pos))
136 #define OFFSET(s,el)          ((int) ((size_t) & (((s*) 0)->el)))
137
138
139 #define NEW(type)             ((type *) mem_alloc(sizeof(type)))
140 #define FREE(ptr,type)        mem_free((ptr), sizeof(type))
141
142 #define MNEW(type,num)        ((type *) mem_alloc(sizeof(type) * (num)))
143 #define MFREE(ptr,type,num)   mem_free((ptr), sizeof(type) * (num))
144 #define MREALLOC(ptr,type,num1,num2) mem_realloc((ptr), sizeof(type) * (num1), \
145                                                         sizeof(type) * (num2))
146
147 #define DNEW(type)            ((type *) dump_alloc(sizeof(type)))
148 #define DMNEW(type,num)       ((type *) dump_alloc(sizeof(type) * (num)))
149 #define DMREALLOC(ptr,type,num1,num2) dump_realloc((ptr), sizeof(type) * (num1), \
150                                                           sizeof(type) * (num2))
151
152 #define MCOPY(dest,src,type,num) memcpy((dest), (src), sizeof(type) * (num))
153 #define MSET(ptr,byte,type,num) memset((ptr), (byte), sizeof(type) * (num))
154
155 #if defined(USE_CODEMMAP)
156 #define CNEW(type,num)        ((type *) mem_mmap(sizeof(type) * (num)))
157 #define CFREE(ptr,num)        /* nothing */
158 #else
159 #define CNEW(type,num)        ((type *) mem_alloc(sizeof(type) * (num)))
160 #define CFREE(ptr,num)        mem_free((ptr), (num))
161 #endif
162
163
164 /* function prototypes ********************************************************/
165
166 void *mem_mmap(s4 size);
167 void *mem_alloc(s4 size);
168 void  mem_free(void *m, s4 size);
169 void *mem_realloc(void *src, s4 len1, s4 len2);
170
171 void *dump_alloc(s4 size);
172 void *dump_realloc(void *src, s4 len1, s4 len2);
173 s4    dump_size();
174 void  dump_release(s4 size);
175
176 #endif /* _MEMORY_H */
177
178
179 /*
180  * These are local overrides for various environment variables in Emacs.
181  * Please do not remove this and leave it at the end of the file, where
182  * Emacs will automagically detect them.
183  * ---------------------------------------------------------------------
184  * Local variables:
185  * mode: c
186  * indent-tabs-mode: t
187  * c-basic-offset: 4
188  * tab-width: 4
189  * End:
190  */