Major file restructuring.
[cacao.git] / toolbox / 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 557 2003-11-02 22:51:59Z twisti $
30
31 */
32
33
34 #ifndef _MEMORY_H
35 #define _MEMORY_H
36
37 #include "types.h"
38
39 /* Uncollectable memory which can contain references */
40 void *heap_alloc_uncollectable(u4 bytelen);
41 #define GCNEW(type,num)       heap_alloc_uncollectable(sizeof(type) * (num))
42
43 #define ALIGN(pos,size)       ((((pos) + (size) - 1) / (size)) * (size))
44 #define PADDING(pos,size)     (ALIGN((pos),(size)) - (pos))
45 #define OFFSET(s,el)          ((int) ((size_t) & (((s*) 0)->el)))
46
47
48 #define NEW(type)             ((type*) mem_alloc(sizeof(type)))
49 #define FREE(ptr,type)        mem_free(ptr, sizeof(type))
50
51 #define LNEW(type)            ((type*) lit_mem_alloc(sizeof(type)))
52 #define LFREE(ptr,type)       lit_mem_free(ptr, sizeof(type))
53
54 #define MNEW(type,num)        ((type*) mem_alloc(sizeof(type) * (num)))
55 #define MFREE(ptr,type,num)   mem_free(ptr, sizeof(type) * (num))
56 #define MREALLOC(ptr,type,num1,num2) mem_realloc(ptr, sizeof(type) * (num1), \
57                                                       sizeof(type) * (num2))
58
59 #define DNEW(type)            ((type*) dump_alloc(sizeof(type)))
60 #define DMNEW(type,num)       ((type*) dump_alloc(sizeof(type) * (num)))
61 #define DMREALLOC(ptr,type,num1,num2)  dump_realloc(ptr, sizeof(type) * (num1),\
62                                                          sizeof(type) * (num2))
63
64 #define MCOPY(dest,src,type,num)  memcpy(dest,src, sizeof(type)* (num))
65
66 #ifdef USE_CODEMMAP
67 #define CNEW(type,num)        ((type*) mem_mmap( sizeof(type) * (num)))
68 #define CFREE(ptr,num)
69 #else
70 #define CNEW(type,num)        ((type*) mem_alloc(sizeof(type) * (num)))
71 #define CFREE(ptr,num)        mem_free(ptr, num)
72 #endif
73
74
75 void *mem_alloc(int length);
76 void *mem_mmap(int length);
77 void *lit_mem_alloc(int length);
78 void mem_free(void *m, int length);
79 void lit_mem_free(void *m, int length);
80 void *mem_realloc(void *m, int len1, int len2);
81 long int mem_usage();
82
83 void *dump_alloc(int length);
84 void *dump_realloc(void *m, int len1, int len2);
85 long int dump_size();
86 void dump_release(long int size);
87
88 void mem_usagelog(int givewarnings);
89  
90  
91  
92 /* 
93 ---------------------------- Interface description -----------------------
94
95 There are two possible choices for allocating memory:
96
97         1.   explicit allocating / deallocating
98
99                         mem_alloc ..... allocate a memory block 
100                         mem_free ...... free a memory block
101                         mem_realloc ... change size of a memory block (position may change)
102                         mem_usage ..... amount of allocated memory
103
104
105         2.   explicit allocating, automatic deallocating
106         
107                         dump_alloc .... allocate a memory block in the dump area
108                         dump_realloc .. change size of a memory block (position may change)
109                         dump_size ..... marks the current top of dump
110                         dump_release .. free all memory requested after the mark
111                                         
112         
113 There are some useful macros:
114
115         NEW (type) ....... allocate memory for an element of type `type`
116         FREE (ptr,type) .. free memory
117         
118         MNEW (type,num) .. allocate memory for an array
119         MFREE (ptr,type,num) .. free memory
120         
121         MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
122                                          
123 These macros do the same except they operate on the dump area:
124         
125         DNEW,  DMNEW, DMREALLOC   (there is no DFREE)
126
127
128 -------------------------------------------------------------------------------
129
130 Some more macros:
131
132         ALIGN (pos, size) ... make pos divisible by size. always returns an
133                                                   address >= pos.
134                               
135         
136         OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
137                               
138         MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
139         
140
141 */
142
143 #endif /* _MEMORY_H */
144
145
146 /*
147  * These are local overrides for various environment variables in Emacs.
148  * Please do not remove this and leave it at the end of the file, where
149  * Emacs will automagically detect them.
150  * ---------------------------------------------------------------------
151  * Local variables:
152  * mode: c
153  * indent-tabs-mode: t
154  * c-basic-offset: 4
155  * tab-width: 4
156  * End:
157  */