1 /************************* toolbox/memory.h ************************************
3 Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
5 See file COPYRIGHT for information on usage and disclaimer of warranties
7 Macros for memory management
9 Authors: Reinhard Grafl EMAIL: cacao@complang.tuwien.ac.at
11 Last Change: 1996/10/03
13 *******************************************************************************/
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))
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 ) ) )
26 #define NEW(type) ((type*) mem_alloc ( sizeof(type) ))
27 #define FREE(ptr,type) mem_free (ptr, sizeof(type) )
29 #define LNEW(type) ((type*) lit_mem_alloc ( sizeof(type) ))
30 #define LFREE(ptr,type) lit_mem_free (ptr, sizeof(type) )
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) )
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) )
42 #define MCOPY(dest,src,type,num) memcpy (dest,src, sizeof(type)* (num) )
45 #define CNEW(type,num) ((type*) mem_mmap ( sizeof(type) * (num) ))
46 #define CFREE(ptr,num)
48 #define CNEW(type,num) ((type*) mem_alloc ( sizeof(type) * (num) ))
49 #define CFREE(ptr,num) mem_free (ptr, num)
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);
60 void *dump_alloc(int length);
61 void *dump_realloc(void *m, int len1, int len2);
63 void dump_release(long int size);
65 void mem_usagelog(int givewarnings);
70 ---------------------------- Interface description -----------------------
72 There are two possible choices for allocating memory:
74 1. explicit allocating / deallocating
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
82 2. explicit allocating, automatic deallocating
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
90 There are some useful macros:
92 NEW (type) ....... allocate memory for an element of type `type`
93 FREE (ptr,type) .. free memory
95 MNEW (type,num) .. allocate memory for an array
96 MFREE (ptr,type,num) .. free memory
98 MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
100 These macros do the same except they operate on the dump area:
102 DNEW, DMNEW, DMREALLOC (there is no DFREE)
105 -------------------------------------------------------------------------------
109 ALIGN (pos, size) ... make pos divisible by size. always returns an
113 OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
115 MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.