Done?
[cacao.git] / toolbox / memory.h
index c4122f96e8d4883eb441199530774b62dd0b4ce9..bfa9bc79f73601ea76995dca1be6a05bb67bf242 100644 (file)
-/************************* toolbox/memory.h ************************************
+/* toolbox/memory.h - macros for memory management
 
-       Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
+   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
+   R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
+   M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
+   P. Tomsich, J. Wenninger
 
-       See file COPYRIGHT for information on usage and disclaimer of warranties
+   This file is part of CACAO.
 
-       Headerfiles und Makros f"ur die Speicherverwaltung.
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2, or (at
+   your option) any later version.
 
-       Authors: Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
 
-       Last Change: 1996/10/03
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+   02111-1307, USA.
 
-*******************************************************************************/
+   Contact: cacao@complang.tuwien.ac.at
 
-#define CODEMMAP
+   Authors: Reinhard Grafl
 
-#define ALIGN(pos,size)       ( ( ((pos)+(size)-1) / (size))*(size) )
-#define PADDING(pos,size)     ( ALIGN((pos),(size)) - (pos) )
-#define OFFSET(s,el)          ( (int) &( ((s*)0) -> el ) )
+   $Id: memory.h 739 2003-12-13 18:52:21Z stefan $
 
+*/
 
-#define NEW(type)             ((type*) mem_alloc ( sizeof(type) ))
-#define FREE(ptr,type)        mem_free (ptr, sizeof(type) )
-
-#define LNEW(type)             ((type*) lit_mem_alloc ( sizeof(type) ))
-#define LFREE(ptr,type)        lit_mem_free (ptr, sizeof(type) )
-
-#define MNEW(type,num)        ((type*) mem_alloc ( sizeof(type) * (num) ))
-#define MFREE(ptr,type,num)   mem_free (ptr, sizeof(type) * (num) )
-#define MREALLOC(ptr,type,num1,num2) mem_realloc (ptr, sizeof(type) * (num1), \
-                                                       sizeof(type) * (num2) )
-
-#define DNEW(type)            ((type*) dump_alloc ( sizeof(type) ))
-#define DMNEW(type,num)       ((type*) dump_alloc ( sizeof(type) * (num) ))
-#define DMREALLOC(ptr,type,num1,num2)  dump_realloc (ptr, sizeof(type)*(num1),\
-                                                       sizeof(type) * (num2) )
 
-#define MCOPY(dest,src,type,num)  memcpy (dest,src, sizeof(type)* (num) )
+#ifndef _MEMORY_H
+#define _MEMORY_H
 
-#ifdef CODEMMAP
-#define CNEW(type,num)        ((type*) mem_mmap ( sizeof(type) * (num) ))
-#define CFREE(ptr,num)
-#else
-#define CNEW(type,num)        ((type*) mem_alloc ( sizeof(type) * (num) ))
-#define CFREE(ptr,num)        mem_free (ptr, num)
-#endif
+#include "types.h"
 
-void *mem_alloc(int length);
-void *mem_mmap(int length);
-void *lit_mem_alloc(int length);
-void mem_free(void *m, int length);
-void lit_mem_free(void *m, int length);
-void *mem_realloc(void *m, int len1, int len2);
-long int mem_usage();
-
-void *dump_alloc(int length);
-void *dump_realloc(void *m, int len1, int len2);
-long int dump_size();
-void dump_release(long int size);
-
-void mem_usagelog(int givewarnings);
 /* 
----------------------------- Schnittstellenbeschreibung -----------------------
+---------------------------- Interface description -----------------------
 
-Der Speicherverwalter hat zwei m"ogliche Arten Speicher zu reservieren
-und freizugeben:
+There are two possible choices for allocating memory:
 
-       1.   explizites Anfordern / Freigeben
+       1.   explicit allocating / deallocating
 
-                       mem_alloc ..... Anfordern eines Speicherblocks 
-                       mem_free ...... Freigeben eines Speicherblocks
-                       mem_realloc ... Vergr"o"sern eines Speicherblocks (wobei 
-                                       der Inhalt eventuell an eine neue Position kommt)
-                       mem_usage ..... Menge des bereits belegten Speichers
+                       mem_alloc ..... allocate a memory block 
+                       mem_free ...... free a memory block
+                       mem_realloc ... change size of a memory block (position may change)
+                       mem_usage ..... amount of allocated memory
 
 
-       2.   explizites Anfordern und automatisches Freigeben
+       2.   explicit allocating, automatic deallocating
        
-                       dump_alloc .... Anfordern eines Speicherblocks vom
-                                       (wie ich es nenne) DUMP-Speicher
-                       dump_realloc .. Vergr"o"sern eines Speicherblocks
-                       dump_size ..... Merkt sich eine Freigabemarke am Dump
-                       dump_release .. Gibt allen Speicher, der nach der Marke angelegt 
-                                       worden ist, wieder frei.
+                       dump_alloc .... allocate a memory block in the dump area
+                       dump_realloc .. change size of a memory block (position may change)
+                       dump_size ..... marks the current top of dump
+                       dump_release .. free all memory requested after the mark
                                        
        
-Es gibt f"ur diese Funktionen ein paar praktische Makros:
+There are some useful macros:
 
-       NEW (type) ....... legt Speicher f"ur ein Element des Typs `type` an.
-       FREE (ptr,type) .. gibt Speicher zur"uck
+       NEW (type) ....... allocate memory for an element of type `type`
+       FREE (ptr,type) .. free memory
        
-       MNEW (type,num) .. legt Speicher f"ur ein ganzes Array an
-       MFREE (ptr,type,num) .. gibt den Speicher wieder her
+       MNEW (type,num) .. allocate memory for an array
+       MFREE (ptr,type,num) .. free memory
        
-       MREALLOC (ptr,type,num1,num2) .. vergr"o"sert den Speicher f"ur das Array
-                                        auf die Gr"o"se num2
+       MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
                                         
-Die meisten der Makros gibt es auch f"ur den DUMP-Speicher, na"mlich mit
-gleichem Namen, nur mit vorangestelltem 'D', also:     
+These macros do the same except they operate on the dump area:
        
-       DNEW,  DMNEW, DMREALLOC   (DFREE gibt es nat"urlich keines)
+       DNEW,  DMNEW, DMREALLOC   (there is no DFREE)
 
 
 -------------------------------------------------------------------------------
 
-Die restlichen Makros:
+Some more macros:
 
-       ALIGN (pos, size) ... Rundet den Wert von 'pos' auf die n"achste durch
-                             'size' teilbare Zahl auf.
+       ALIGN (pos, size) ... make pos divisible by size. always returns an
+                                                 address >= pos.
                              
        
-       OFFSET (s,el) ....... Berechnet den Offset (in Bytes) des Elementes 'el'   
-                             in der Struktur 's'.
+       OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
                              
-       MCOPY (dest,src,type,num) ... Kopiert 'num' Elemente vom Typ 'type'.
+       MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
        
 
 */
+
+/* Uncollectable memory which can contain references */
+void *heap_alloc_uncollectable(u4 bytelen);
+void heap_free(void *);
+#define GCNEW(type,num)       heap_alloc_uncollectable(sizeof(type) * (num))
+#define GCFREE(ptr)           heap_free(ptr)
+
+#define ALIGN(pos,size)       ((((pos) + (size) - 1) / (size)) * (size))
+#define PADDING(pos,size)     (ALIGN((pos),(size)) - (pos))
+#define OFFSET(s,el)          ((int) ((size_t) & (((s*) 0)->el)))
+
+
+#define NEW(type)             ((type*) mem_alloc(sizeof(type)))
+#define FREE(ptr,type)        mem_free(ptr, sizeof(type))
+
+#define LNEW(type)            ((type*) lit_mem_alloc(sizeof(type)))
+#define LFREE(ptr,type)       lit_mem_free(ptr, sizeof(type))
+
+#define MNEW(type,num)        ((type*) mem_alloc(sizeof(type) * (num)))
+#define MFREE(ptr,type,num)   mem_free(ptr, sizeof(type) * (num))
+#define MREALLOC(ptr,type,num1,num2) mem_realloc(ptr, sizeof(type) * (num1), \
+                                                      sizeof(type) * (num2))
+
+#define DNEW(type)            ((type*) dump_alloc(sizeof(type)))
+#define DMNEW(type,num)       ((type*) dump_alloc(sizeof(type) * (num)))
+#define DMREALLOC(ptr,type,num1,num2)  dump_realloc(ptr, sizeof(type) * (num1),\
+                                                         sizeof(type) * (num2))
+
+#define MCOPY(dest,src,type,num)  memcpy(dest,src, sizeof(type)* (num))
+
+#ifdef USE_CODEMMAP
+#define CNEW(type,num)        ((type*) mem_mmap( sizeof(type) * (num)))
+#define CFREE(ptr,num)
+#else
+#define CNEW(type,num)        ((type*) mem_alloc(sizeof(type) * (num)))
+#define CFREE(ptr,num)        mem_free(ptr, num)
+#endif
+
+
+void *mem_alloc(int length);
+void *mem_mmap(int length);
+void *lit_mem_alloc(int length);
+void mem_free(void *m, int length);
+void lit_mem_free(void *m, int length);
+void *mem_realloc(void *m, int len1, int len2);
+long int mem_usage();
+
+void *dump_alloc(int length);
+void *dump_realloc(void *m, int len1, int len2);
+long int dump_size();
+void dump_release(long int size);
+
+void mem_usagelog(int givewarnings);
+#endif /* _MEMORY_H */
+
+
+/*
+ * These are local overrides for various environment variables in Emacs.
+ * Please do not remove this and leave it at the end of the file, where
+ * Emacs will automagically detect them.
+ * ---------------------------------------------------------------------
+ * Local variables:
+ * mode: c
+ * indent-tabs-mode: t
+ * c-basic-offset: 4
+ * tab-width: 4
+ * End:
+ */