48e4ccc0945af68e1e587f2504db0542678ee0e4
[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 #include "types.h"
18
19 #ifdef USE_BOEHM
20 /* Uncollectable memory which can contain references */
21 void *heap_alloc_uncollectable(u4 bytelen);
22 #define GCNEW(type,num) heap_alloc_uncollectable(sizeof(type) * (num))
23 #endif
24
25 #define ALIGN(pos,size)       ( ( ((pos)+(size)-1) / (size))*(size) )
26 #define PADDING(pos,size)     ( ALIGN((pos),(size)) - (pos) )
27 #define OFFSET(s,el)          ( (int) ( (size_t) &( ((s*)0) -> el ) ) )
28
29
30 #define NEW(type)             ((type*) mem_alloc ( sizeof(type) ))
31 #define FREE(ptr,type)        mem_free (ptr, sizeof(type) )
32
33 #define LNEW(type)             ((type*) lit_mem_alloc ( sizeof(type) ))
34 #define LFREE(ptr,type)        lit_mem_free (ptr, sizeof(type) )
35
36 #define MNEW(type,num)        ((type*) mem_alloc ( sizeof(type) * (num) ))
37 #define MFREE(ptr,type,num)   mem_free (ptr, sizeof(type) * (num) )
38 #define MREALLOC(ptr,type,num1,num2) mem_realloc (ptr, sizeof(type) * (num1), \
39                                                        sizeof(type) * (num2) )
40
41 #define DNEW(type)            ((type*) mem_alloc ( sizeof(type) ))
42 #define DMNEW(type,num)       ((type*) mem_alloc ( sizeof(type) * (num) ))
43 #define DMREALLOC(ptr,type,num1,num2)  mem_realloc (ptr, sizeof(type)*(num1),\
44                                                        sizeof(type) * (num2) )
45
46 #define MCOPY(dest,src,type,num)  memcpy (dest,src, sizeof(type)* (num) )
47
48 #ifdef CODEMMAP
49 #define CNEW(type,num)        ((type*) mem_mmap ( sizeof(type) * (num) ))
50 #define CFREE(ptr,num)
51 #else
52 #define CNEW(type,num)        ((type*) mem_alloc ( sizeof(type) * (num) ))
53 #define CFREE(ptr,num)        mem_free (ptr, num)
54 #endif
55
56 void *mem_alloc(int length);
57 void *mem_mmap(int length);
58 void *lit_mem_alloc(int length);
59 void mem_free(void *m, int length);
60 void lit_mem_free(void *m, int length);
61 void *mem_realloc(void *m, int len1, int len2);
62 long int mem_usage();
63
64 void *dump_alloc(int length);
65 void *dump_realloc(void *m, int len1, int len2);
66 long int dump_size();
67 void dump_release(long int size);
68
69 void mem_usagelog(int givewarnings);
70  
71  
72  
73 /* 
74 ---------------------------- Interface description -----------------------
75
76 There are two possible choices for allocating memory:
77
78         1.   explicit allocating / deallocating
79
80                         mem_alloc ..... allocate a memory block 
81                         mem_free ...... free a memory block
82                         mem_realloc ... change size of a memory block (position may change)
83                         mem_usage ..... amount of allocated memory
84
85
86         2.   explicit allocating, automatic deallocating
87         
88                         dump_alloc .... allocate a memory block in the dump area
89                         dump_realloc .. change size of a memory block (position may change)
90                         dump_size ..... marks the current top of dump
91                         dump_release .. free all memory requested after the mark
92                                         
93         
94 There are some useful macros:
95
96         NEW (type) ....... allocate memory for an element of type `type`
97         FREE (ptr,type) .. free memory
98         
99         MNEW (type,num) .. allocate memory for an array
100         MFREE (ptr,type,num) .. free memory
101         
102         MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
103                                          
104 These macros do the same except they operate on the dump area:
105         
106         DNEW,  DMNEW, DMREALLOC   (there is no DFREE)
107
108
109 -------------------------------------------------------------------------------
110
111 Some more macros:
112
113         ALIGN (pos, size) ... make pos divisible by size. always returns an
114                                                   address >= pos.
115                               
116         
117         OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
118                               
119         MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
120         
121
122 */