* src/mm/memory.c (memory_cfree): New function.
[cacao.git] / src / mm / boehm.c
index 249f6b285ddf5e3a2ee528c5bb73e69087692d04..83817d8f32103cf6ae3f29f763cca5d4ffb1029a 100644 (file)
@@ -1,9 +1,9 @@
-/* mm/boehm.c - interface for boehm gc
+/* src/mm/boehm.c - interface for boehm gc
 
-   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
+   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
+   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
+   J. Wenninger, Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
 
    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.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
+   Contact: cacao@cacaojvm.org
 
    Authors: Stefan Ring
 
-   $Id: boehm.c 771 2003-12-13 23:11:08Z stefan $
+   Changes: Christian Thalinger
+
+   $Id: boehm.c 5144 2006-07-17 11:09:21Z twisti $
 
 */
 
 
-#include "main.h"
-#include "boehm.h"
-#include "global.h"
-#include "threads/thread.h"
-#include "asmpart.h"
-#include "toolbox/loging.h"
+#include "config.h"
+#include "vm/types.h"
 
-/* this is temporary workaround */
-#if defined(__X86_64__)
-#define GC_DEBUG
+#if defined(ENABLE_THREADS) && defined(__LINUX__)
+#define GC_LINUX_THREADS
+#endif
+#if defined(ENABLE_THREADS) && defined(__IRIX__)
+#define GC_IRIX_THREADS
 #endif
 
-#include "gc.h"
+#include "boehm-gc/include/gc.h"
+#include "mm/boehm.h"
+#include "mm/memory.h"
 
+#if defined(ENABLE_THREADS)
+# include "threads/native/threads.h"
+#endif
 
-static void *stackcall_twoargs(struct otherstackcall *p)
-{
-       return (*p->p2)(p->p, p->l);
-}
+#include "toolbox/logging.h"
+#include "vm/options.h"
+#include "vm/builtin.h"
+#include "vm/exceptions.h"
+#include "vm/finalizer.h"
+#include "vm/global.h"
+#include "vm/loader.h"
+#include "vm/stringlocal.h"
 
 
-static void *stackcall_malloc(void *p, u4 bytelength)
-{
-       return GC_MALLOC(bytelength);
-}
+/* global variables ***********************************************************/
 
+static bool in_gc_out_of_memory = false;    /* is GC out of memory?           */
 
-static void *stackcall_malloc_atomic(void *p, u4 bytelength)
-{
-       return GC_MALLOC_ATOMIC(bytelength);
-}
 
+/* prototype static functions *************************************************/
 
-static void *stackcall_malloc_uncollectable(void *p, u4 bytelength)
-{
-       return GC_MALLOC_UNCOLLECTABLE(bytelength);
-}
+static void gc_ignore_warnings(char *msg, GC_word arg);
 
 
-static void *stackcall_realloc(void *p, u4 bytelength)
-{
-       return GC_REALLOC(p, bytelength);
-}
+/* gc_init *********************************************************************
 
-static void *stackcall_free(void *p, u4 bytelength)
+   Initializes the boehm garbage collector.
+
+*******************************************************************************/
+
+void gc_init(u4 heapmaxsize, u4 heapstartsize)
 {
-       GC_FREE(p);
-       return NULL;
-}
+       size_t heapcurrentsize;
+
+       GC_INIT();
+
+       /* set the maximal heap size */
+
+       GC_set_max_heap_size(heapmaxsize);
 
+       /* set the initial heap size */
 
-#ifdef USE_THREADS
-#define MAINTHREADCALL(r,m,pp,ll) \
-       if (currentThread == NULL || currentThread == mainThread) { \
-               r = m(pp, ll); \
-       } else { \
-               struct otherstackcall sc; \
-               sc.p2 = m; \
-               sc.p = pp; \
-               sc.l = ll; \
-               r = (*asm_switchstackandcall)(CONTEXT(mainThread).usedStackTop, \
-                               stackcall_twoargs, \
-                               (void**)&(CONTEXT(currentThread).usedStackTop), &sc); \
+       heapcurrentsize = GC_get_heap_size();
+
+       if (heapstartsize > heapcurrentsize) {
+               GC_expand_hp(heapstartsize - heapcurrentsize);
        }
-#else
-#define MAINTHREADCALL(r,m,pp,ll) \
-       { r = m(pp, ll); }
-#endif
 
+       /* define OOM function */
 
-void *heap_alloc_uncollectable(u4 bytelength)
-{
-       void *result;
-       MAINTHREADCALL(result, stackcall_malloc_uncollectable, NULL, bytelength);
-       return result;
+       GC_oom_fn = gc_out_of_memory;
+
+       /* just to be sure (should be set to 1 by JAVA_FINALIZATION macro) */
+
+       GC_java_finalization = 1;
+
+       /* suppress warnings */
+
+       GC_set_warn_proc(gc_ignore_warnings);
+
+       /* install a GC notifier */
+
+       GC_finalize_on_demand = 1;
+       GC_finalizer_notifier = finalizer_notify;
 }
 
 
-void runboehmfinalizer(void *o, void *p)
+static void gc_ignore_warnings(char *msg, GC_word arg)
 {
-       java_objectheader *ob = (java_objectheader *) o;
-       asm_calljavafunction(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
 }
 
 
-void *heap_allocate(u4 bytelength, bool references, methodinfo *finalizer)
+void *heap_alloc_uncollectable(u4 bytelength)
 {
-       void *result;
+       void *p;
 
-       if (references) {
-               MAINTHREADCALL(result, stackcall_malloc, NULL, bytelength);
+       p = GC_MALLOC_UNCOLLECTABLE(bytelength);
 
-       } else {
-               MAINTHREADCALL(result, stackcall_malloc_atomic, NULL, bytelength);
-       }
+       /* clear allocated memory region */
 
-       if (finalizer)
-               GC_REGISTER_FINALIZER(result, runboehmfinalizer, 0, 0, 0);
+       MSET(p, 0, u1, bytelength);
 
-       return (u1*) result;
+       return p;
 }
 
 
-void *heap_reallocate(void *p, u4 bytelength)
+/* heap_allocate ***************************************************************
+
+   Allocates memory on the Java heap.
+
+*******************************************************************************/
+
+void *heap_allocate(u4 bytelength, u4 references, methodinfo *finalizer)
 {
-       void *result;
+       void *p;
+
+       /* We can't use a bool here for references, as it's passed as a
+          bitmask in builtin_new.  Thus we check for != 0. */
+
+       if (references != 0)
+               p = GC_MALLOC(bytelength);
+       else
+               p = GC_MALLOC_ATOMIC(bytelength);
 
-       MAINTHREADCALL(result, stackcall_realloc, p, bytelength);
+       if (p == NULL)
+               return NULL;
 
-       return result;
+       if (finalizer != NULL)
+               GC_REGISTER_FINALIZER(p, finalizer_run, 0, 0, 0);
+
+       /* clear allocated memory region */
+
+       MSET(p, 0, u1, bytelength);
+
+       return p;
 }
 
+
 void heap_free(void *p)
 {
-       void *result;
+       GC_FREE(p);
+}
+
+void gc_call(void)
+{
+       if (opt_verbosegc)
+               dolog("Garbage Collection:  previous/now = %d / %d ",
+                         0, 0);
 
-       MAINTHREADCALL(result, stackcall_free, p, 0);
+       GC_gcollect();
 }
 
 
-void heap_init (u4 size, u4 startsize, void **stackbottom)
+s8 gc_get_heap_size(void)
 {
-       GC_INIT();
+       return GC_get_heap_size();
 }
 
 
-void heap_close()
+s8 gc_get_free_bytes(void)
 {
+       return GC_get_free_bytes();
 }
 
 
-void heap_addreference(void **reflocation)
+s8 gc_get_max_heap_size(void)
 {
+       return GC_get_max_heap_size();
 }
 
 
-void gc_init()
+void gc_invoke_finalizers(void)
 {
+       GC_invoke_finalizers();
 }
 
 
-void gc_call()
+void gc_finalize_all(void)
 {
-       if (collectverbose)
-               dolog("Garbage Collection:  previous/now = %d / %d ",
-                               0, 0);
+       GC_finalize_all();
+}
 
-       GC_gcollect();
+
+/* gc_out_of_memory ************************************************************
+
+   This function is called when boehm detects that it is OOM.
+
+*******************************************************************************/
+
+void *gc_out_of_memory(size_t bytes_requested)
+{
+       /* if this happens, we are REALLY out of memory */
+
+       if (in_gc_out_of_memory) {
+               /* this is all we can do... */
+               exceptions_throw_outofmemory_exit();
+       }
+
+       in_gc_out_of_memory = true;
+
+       /* try to release some memory */
+
+       gc_call();
+
+       /* now instantiate the exception */
+
+       *exceptionptr = new_exception(string_java_lang_OutOfMemoryError);
+
+       in_gc_out_of_memory = false;
+
+       return NULL;
 }