Blame fnlz_mlc.c

Packit d28291
/*
Packit d28291
 * Copyright (c) 2011 by Hewlett-Packard Company.  All rights reserved.
Packit d28291
 *
Packit d28291
 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
Packit d28291
 * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
Packit d28291
 *
Packit d28291
 * Permission is hereby granted to use or copy this program
Packit d28291
 * for any purpose,  provided the above notices are retained on all copies.
Packit d28291
 * Permission to modify the code and to distribute modified code is granted,
Packit d28291
 * provided the above notices are retained, and a notice that the code was
Packit d28291
 * modified is included with the above copyright notice.
Packit d28291
 *
Packit d28291
 */
Packit d28291
Packit d28291
#include "private/gc_priv.h"
Packit d28291
Packit d28291
#ifdef ENABLE_DISCLAIM
Packit d28291
Packit d28291
#include "gc_disclaim.h"
Packit d28291
#include "gc_inline.h" /* for GC_malloc_kind */
Packit d28291
Packit d28291
STATIC int GC_finalized_kind = 0;
Packit d28291
Packit d28291
#if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
Packit d28291
  /* The first bit is already used for a debug purpose. */
Packit d28291
# define FINALIZER_CLOSURE_FLAG 0x2
Packit d28291
#else
Packit d28291
# define FINALIZER_CLOSURE_FLAG 0x1
Packit d28291
#endif
Packit d28291
Packit d28291
STATIC int GC_CALLBACK GC_finalized_disclaim(void *obj)
Packit d28291
{
Packit d28291
    word fc_word = *(word *)obj;
Packit d28291
Packit d28291
    if ((fc_word & FINALIZER_CLOSURE_FLAG) != 0) {
Packit d28291
       /* The disclaim function may be passed fragments from the        */
Packit d28291
       /* free-list, on which it should not run finalization.           */
Packit d28291
       /* To recognize this case, we use the fact that the first word   */
Packit d28291
       /* on such fragments is always multiple of 4 (a link to the next */
Packit d28291
       /* fragment, or NULL).  If it is desirable to have a finalizer   */
Packit d28291
       /* which does not use the first word for storing finalization    */
Packit d28291
       /* info, GC_reclaim_with_finalization must be extended to clear  */
Packit d28291
       /* fragments so that the assumption holds for the selected word. */
Packit d28291
        const struct GC_finalizer_closure *fc
Packit d28291
                        = (void *)(fc_word & ~(word)FINALIZER_CLOSURE_FLAG);
Packit d28291
        (*fc->proc)((word *)obj + 1, fc->cd);
Packit d28291
    }
Packit d28291
    return 0;
Packit d28291
}
Packit d28291
Packit d28291
static GC_bool done_init = FALSE;
Packit d28291
Packit d28291
GC_API void GC_CALL GC_init_finalized_malloc(void)
Packit d28291
{
Packit d28291
    DCL_LOCK_STATE;
Packit d28291
Packit d28291
    GC_init();  /* In case it's not already done.       */
Packit d28291
    LOCK();
Packit d28291
    if (done_init) {
Packit d28291
        UNLOCK();
Packit d28291
        return;
Packit d28291
    }
Packit d28291
    done_init = TRUE;
Packit d28291
Packit d28291
    /* The finalizer closure is placed in the first word in order to    */
Packit d28291
    /* use the lower bits to distinguish live objects from objects on   */
Packit d28291
    /* the free list.  The downside of this is that we need one-word    */
Packit d28291
    /* offset interior pointers, and that GC_base does not return the   */
Packit d28291
    /* start of the user region.                                        */
Packit d28291
    GC_register_displacement_inner(sizeof(word));
Packit d28291
Packit d28291
    GC_finalized_kind = GC_new_kind_inner(GC_new_free_list_inner(),
Packit d28291
                                          GC_DS_LENGTH, TRUE, TRUE);
Packit d28291
    GC_register_disclaim_proc(GC_finalized_kind, GC_finalized_disclaim, TRUE);
Packit d28291
    UNLOCK();
Packit d28291
}
Packit d28291
Packit d28291
GC_API void GC_CALL GC_register_disclaim_proc(int kind, GC_disclaim_proc proc,
Packit d28291
                                              int mark_unconditionally)
Packit d28291
{
Packit d28291
    GC_ASSERT((unsigned)kind < MAXOBJKINDS);
Packit d28291
    GC_obj_kinds[kind].ok_disclaim_proc = proc;
Packit d28291
    GC_obj_kinds[kind].ok_mark_unconditionally = (GC_bool)mark_unconditionally;
Packit d28291
}
Packit d28291
Packit d28291
GC_API GC_ATTR_MALLOC void * GC_CALL GC_finalized_malloc(size_t lb,
Packit d28291
                                const struct GC_finalizer_closure *fclos)
Packit d28291
{
Packit d28291
    word *op;
Packit d28291
Packit d28291
    GC_ASSERT(done_init);
Packit d28291
    op = GC_malloc_kind(SIZET_SAT_ADD(lb, sizeof(word)), GC_finalized_kind);
Packit d28291
    if (EXPECT(NULL == op, FALSE))
Packit d28291
        return NULL;
Packit d28291
    *op = (word)fclos | FINALIZER_CLOSURE_FLAG;
Packit d28291
    return op + 1;
Packit d28291
}
Packit d28291
Packit d28291
#endif /* ENABLE_DISCLAIM */