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 1637 2004-12-01 10:33:25Z twisti $
42 ---------------------------- Interface description -----------------------
44 There are two possible choices for allocating memory:
46 1. explicit allocating / deallocating
48 mem_alloc ..... allocate a memory block
49 mem_free ...... free a memory block
50 mem_realloc ... change size of a memory block (position may change)
51 mem_usage ..... amount of allocated memory
54 2. explicit allocating, automatic deallocating
56 dump_alloc .... allocate a memory block in the dump area
57 dump_realloc .. change size of a memory block (position may change)
58 dump_size ..... marks the current top of dump
59 dump_release .. free all memory requested after the mark
62 There are some useful macros:
64 NEW (type) ....... allocate memory for an element of type `type`
65 FREE (ptr,type) .. free memory
67 MNEW (type,num) .. allocate memory for an array
68 MFREE (ptr,type,num) .. free memory
70 MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
72 These macros do the same except they operate on the dump area:
74 DNEW, DMNEW, DMREALLOC (there is no DFREE)
77 -------------------------------------------------------------------------------
81 ALIGN (pos, size) ... make pos divisible by size. always returns an
85 OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
87 MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
93 #define DUMPBLOCKSIZE 2 << 13 /* 2 * 8192 bytes */
97 /* dumpblock *******************************************************************
101 *******************************************************************************/
103 typedef struct dumpblock dumpblock;
112 /* dumpinfo ********************************************************************
116 *******************************************************************************/
118 typedef struct dumpinfo dumpinfo;
121 dumpblock *currentdumpblock;
122 s4 allocateddumpsize;
127 /* Uncollectable memory which can contain references */
129 #define GCNEW(type,num) ((type *) heap_alloc_uncollectable(sizeof(type) * (num)))
130 #define GCFREE(ptr) heap_free((ptr))
132 #define ALIGN(pos,size) ((((pos) + (size) - 1) / (size)) * (size))
133 #define PADDING(pos,size) (ALIGN((pos),(size)) - (pos))
134 #define OFFSET(s,el) ((int) ((size_t) & (((s*) 0)->el)))
137 #define NEW(type) ((type *) mem_alloc(sizeof(type)))
138 #define FREE(ptr,type) mem_free((ptr), sizeof(type))
140 #define MNEW(type,num) ((type *) mem_alloc(sizeof(type) * (num)))
141 #define MFREE(ptr,type,num) mem_free((ptr), sizeof(type) * (num))
142 #define MREALLOC(ptr,type,num1,num2) mem_realloc((ptr), sizeof(type) * (num1), \
143 sizeof(type) * (num2))
145 #define DNEW(type) ((type *) dump_alloc(sizeof(type)))
146 #define DMNEW(type,num) ((type *) dump_alloc(sizeof(type) * (num)))
147 #define DMREALLOC(ptr,type,num1,num2) dump_realloc((ptr), sizeof(type) * (num1), \
148 sizeof(type) * (num2))
150 #define MCOPY(dest,src,type,num) memcpy((dest), (src), sizeof(type) * (num))
151 #define MSET(ptr,byte,type,num) memset((ptr), (byte), sizeof(type) * (num))
153 #if defined(USE_CODEMMAP)
154 #define CNEW(type,num) ((type *) mem_mmap(sizeof(type) * (num)))
155 #define CFREE(ptr,num) /* nothing */
157 #define CNEW(type,num) ((type *) mem_alloc(sizeof(type) * (num)))
158 #define CFREE(ptr,num) mem_free((ptr), (num))
162 /* function prototypes ********************************************************/
164 void *mem_mmap(s4 size);
165 void *mem_alloc(s4 size);
166 void mem_free(void *m, s4 size);
167 void *mem_realloc(void *src, s4 len1, s4 len2);
169 void *dump_alloc(s4 size);
170 void *dump_realloc(void *src, s4 len1, s4 len2);
172 void dump_release(s4 size);
174 #endif /* _MEMORY_H */
178 * These are local overrides for various environment variables in Emacs.
179 * Please do not remove this and leave it at the end of the file, where
180 * Emacs will automagically detect them.
181 * ---------------------------------------------------------------------
184 * indent-tabs-mode: t