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