1 /* toolbox/memory.h - macros for memory management
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
29 $Id: memory.h 739 2003-12-13 18:52:21Z stefan $
40 ---------------------------- Interface description -----------------------
42 There are two possible choices for allocating memory:
44 1. explicit allocating / deallocating
46 mem_alloc ..... allocate a memory block
47 mem_free ...... free a memory block
48 mem_realloc ... change size of a memory block (position may change)
49 mem_usage ..... amount of allocated memory
52 2. explicit allocating, automatic deallocating
54 dump_alloc .... allocate a memory block in the dump area
55 dump_realloc .. change size of a memory block (position may change)
56 dump_size ..... marks the current top of dump
57 dump_release .. free all memory requested after the mark
60 There are some useful macros:
62 NEW (type) ....... allocate memory for an element of type `type`
63 FREE (ptr,type) .. free memory
65 MNEW (type,num) .. allocate memory for an array
66 MFREE (ptr,type,num) .. free memory
68 MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
70 These macros do the same except they operate on the dump area:
72 DNEW, DMNEW, DMREALLOC (there is no DFREE)
75 -------------------------------------------------------------------------------
79 ALIGN (pos, size) ... make pos divisible by size. always returns an
83 OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
85 MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
90 /* Uncollectable memory which can contain references */
91 void *heap_alloc_uncollectable(u4 bytelen);
92 void heap_free(void *);
93 #define GCNEW(type,num) heap_alloc_uncollectable(sizeof(type) * (num))
94 #define GCFREE(ptr) heap_free(ptr)
96 #define ALIGN(pos,size) ((((pos) + (size) - 1) / (size)) * (size))
97 #define PADDING(pos,size) (ALIGN((pos),(size)) - (pos))
98 #define OFFSET(s,el) ((int) ((size_t) & (((s*) 0)->el)))
101 #define NEW(type) ((type*) mem_alloc(sizeof(type)))
102 #define FREE(ptr,type) mem_free(ptr, sizeof(type))
104 #define LNEW(type) ((type*) lit_mem_alloc(sizeof(type)))
105 #define LFREE(ptr,type) lit_mem_free(ptr, sizeof(type))
107 #define MNEW(type,num) ((type*) mem_alloc(sizeof(type) * (num)))
108 #define MFREE(ptr,type,num) mem_free(ptr, sizeof(type) * (num))
109 #define MREALLOC(ptr,type,num1,num2) mem_realloc(ptr, sizeof(type) * (num1), \
110 sizeof(type) * (num2))
112 #define DNEW(type) ((type*) dump_alloc(sizeof(type)))
113 #define DMNEW(type,num) ((type*) dump_alloc(sizeof(type) * (num)))
114 #define DMREALLOC(ptr,type,num1,num2) dump_realloc(ptr, sizeof(type) * (num1),\
115 sizeof(type) * (num2))
117 #define MCOPY(dest,src,type,num) memcpy(dest,src, sizeof(type)* (num))
120 #define CNEW(type,num) ((type*) mem_mmap( sizeof(type) * (num)))
121 #define CFREE(ptr,num)
123 #define CNEW(type,num) ((type*) mem_alloc(sizeof(type) * (num)))
124 #define CFREE(ptr,num) mem_free(ptr, num)
128 void *mem_alloc(int length);
129 void *mem_mmap(int length);
130 void *lit_mem_alloc(int length);
131 void mem_free(void *m, int length);
132 void lit_mem_free(void *m, int length);
133 void *mem_realloc(void *m, int len1, int len2);
134 long int mem_usage();
136 void *dump_alloc(int length);
137 void *dump_realloc(void *m, int len1, int len2);
138 long int dump_size();
139 void dump_release(long int size);
141 void mem_usagelog(int givewarnings);
143 #endif /* _MEMORY_H */
147 * These are local overrides for various environment variables in Emacs.
148 * Please do not remove this and leave it at the end of the file, where
149 * Emacs will automagically detect them.
150 * ---------------------------------------------------------------------
153 * indent-tabs-mode: t