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