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