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