Added GCNEW
[cacao.git] / src / mm / memory.h
1 /************************* toolbox/memory.h ************************************
2
3         Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
4
5         See file COPYRIGHT for information on usage and disclaimer of warranties
6
7         Macros for memory management
8
9         Authors: Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at
10
11         Last Change: 1996/10/03
12
13 *******************************************************************************/
14
15 #define CODEMMAP
16
17 #ifdef USE_BOEHM
18 /* Uncollectable memory which can contain references */
19 #define GCNEW(type,num) heap_alloc_uncollectable(sizeof(type) * (num))
20 #endif
21
22 #define ALIGN(pos,size)       ( ( ((pos)+(size)-1) / (size))*(size) )
23 #define PADDING(pos,size)     ( ALIGN((pos),(size)) - (pos) )
24 #define OFFSET(s,el)          ( (int) ( (size_t) &( ((s*)0) -> el ) ) )
25
26
27 #define NEW(type)             ((type*) mem_alloc ( sizeof(type) ))
28 #define FREE(ptr,type)        mem_free (ptr, sizeof(type) )
29
30 #define LNEW(type)             ((type*) lit_mem_alloc ( sizeof(type) ))
31 #define LFREE(ptr,type)        lit_mem_free (ptr, sizeof(type) )
32
33 #define MNEW(type,num)        ((type*) mem_alloc ( sizeof(type) * (num) ))
34 #define MFREE(ptr,type,num)   mem_free (ptr, sizeof(type) * (num) )
35 #define MREALLOC(ptr,type,num1,num2) mem_realloc (ptr, sizeof(type) * (num1), \
36                                                        sizeof(type) * (num2) )
37
38 #define DNEW(type)            ((type*) mem_alloc ( sizeof(type) ))
39 #define DMNEW(type,num)       ((type*) mem_alloc ( sizeof(type) * (num) ))
40 #define DMREALLOC(ptr,type,num1,num2)  mem_realloc (ptr, sizeof(type)*(num1),\
41                                                        sizeof(type) * (num2) )
42
43 #define MCOPY(dest,src,type,num)  memcpy (dest,src, sizeof(type)* (num) )
44
45 #ifdef CODEMMAP
46 #define CNEW(type,num)        ((type*) mem_mmap ( sizeof(type) * (num) ))
47 #define CFREE(ptr,num)
48 #else
49 #define CNEW(type,num)        ((type*) mem_alloc ( sizeof(type) * (num) ))
50 #define CFREE(ptr,num)        mem_free (ptr, num)
51 #endif
52
53 void *mem_alloc(int length);
54 void *mem_mmap(int length);
55 void *lit_mem_alloc(int length);
56 void mem_free(void *m, int length);
57 void lit_mem_free(void *m, int length);
58 void *mem_realloc(void *m, int len1, int len2);
59 long int mem_usage();
60
61 void *dump_alloc(int length);
62 void *dump_realloc(void *m, int len1, int len2);
63 long int dump_size();
64 void dump_release(long int size);
65
66 void mem_usagelog(int givewarnings);
67  
68  
69  
70 /* 
71 ---------------------------- Interface description -----------------------
72
73 There are two possible choices for allocating memory:
74
75         1.   explicit allocating / deallocating
76
77                         mem_alloc ..... allocate a memory block 
78                         mem_free ...... free a memory block
79                         mem_realloc ... change size of a memory block (position may change)
80                         mem_usage ..... amount of allocated memory
81
82
83         2.   explicit allocating, automatic deallocating
84         
85                         dump_alloc .... allocate a memory block in the dump area
86                         dump_realloc .. change size of a memory block (position may change)
87                         dump_size ..... marks the current top of dump
88                         dump_release .. free all memory requested after the mark
89                                         
90         
91 There are some useful macros:
92
93         NEW (type) ....... allocate memory for an element of type `type`
94         FREE (ptr,type) .. free memory
95         
96         MNEW (type,num) .. allocate memory for an array
97         MFREE (ptr,type,num) .. free memory
98         
99         MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
100                                          
101 These macros do the same except they operate on the dump area:
102         
103         DNEW,  DMNEW, DMREALLOC   (there is no DFREE)
104
105
106 -------------------------------------------------------------------------------
107
108 Some more macros:
109
110         ALIGN (pos, size) ... make pos divisible by size. always returns an
111                                                   address >= pos.
112                               
113         
114         OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
115                               
116         MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
117         
118
119 */