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