| 1 |
/* |
| 2 |
* Copyright (c) 2011 by Hewlett-Packard Company. All rights reserved. |
| 3 |
* |
| 4 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 5 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 6 |
* |
| 7 |
* Permission is hereby granted to use or copy this program |
| 8 |
* for any purpose, provided the above notices are retained on all copies. |
| 9 |
* Permission to modify the code and to distribute modified code is granted, |
| 10 |
* provided the above notices are retained, and a notice that the code was |
| 11 |
* modified is included with the above copyright notice. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "private/gc_priv.h" |
| 16 |
|
| 17 |
#ifdef ENABLE_DISCLAIM |
| 18 |
|
| 19 |
#include "gc_disclaim.h" |
| 20 |
#include "gc_inline.h" /* for GC_malloc_kind */ |
| 21 |
#include "private/dbg_mlc.h" /* for oh type */ |
| 22 |
|
| 23 |
#if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH) |
| 24 |
/* The first bit is already used for a debug purpose. */ |
| 25 |
# define FINALIZER_CLOSURE_FLAG 0x2 |
| 26 |
#else |
| 27 |
# define FINALIZER_CLOSURE_FLAG 0x1 |
| 28 |
#endif |
| 29 |
|
| 30 |
STATIC int GC_CALLBACK GC_finalized_disclaim(void *obj) |
| 31 |
{ |
| 32 |
# ifdef AO_HAVE_load |
| 33 |
word fc_word = (word)AO_load((volatile AO_t *)obj); |
| 34 |
# else |
| 35 |
word fc_word = *(word *)obj; |
| 36 |
# endif |
| 37 |
|
| 38 |
if ((fc_word & FINALIZER_CLOSURE_FLAG) != 0) { |
| 39 |
/* The disclaim function may be passed fragments from the */ |
| 40 |
/* free-list, on which it should not run finalization. */ |
| 41 |
/* To recognize this case, we use the fact that the first word */ |
| 42 |
/* on such fragments is always multiple of 4 (a link to the next */ |
| 43 |
/* fragment, or NULL). If it is desirable to have a finalizer */ |
| 44 |
/* which does not use the first word for storing finalization */ |
| 45 |
/* info, GC_disclaim_and_reclaim() must be extended to clear */ |
| 46 |
/* fragments so that the assumption holds for the selected word. */ |
| 47 |
const struct GC_finalizer_closure *fc |
| 48 |
= (struct GC_finalizer_closure *)(fc_word |
| 49 |
& ~(word)FINALIZER_CLOSURE_FLAG); |
| 50 |
GC_ASSERT(!GC_find_leak); |
| 51 |
(*fc->proc)((word *)obj + 1, fc->cd); |
| 52 |
} |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
GC_API void GC_CALL GC_init_finalized_malloc(void) |
| 57 |
{ |
| 58 |
DCL_LOCK_STATE; |
| 59 |
|
| 60 |
GC_init(); /* In case it's not already done. */ |
| 61 |
LOCK(); |
| 62 |
if (GC_finalized_kind != 0) { |
| 63 |
UNLOCK(); |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
/* The finalizer closure is placed in the first word in order to */ |
| 68 |
/* use the lower bits to distinguish live objects from objects on */ |
| 69 |
/* the free list. The downside of this is that we need one-word */ |
| 70 |
/* offset interior pointers, and that GC_base does not return the */ |
| 71 |
/* start of the user region. */ |
| 72 |
GC_register_displacement_inner(sizeof(word)); |
| 73 |
|
| 74 |
/* And, the pointer to the finalizer closure object itself is */ |
| 75 |
/* displaced due to baking in this indicator. */ |
| 76 |
GC_register_displacement_inner(FINALIZER_CLOSURE_FLAG); |
| 77 |
GC_register_displacement_inner(sizeof(oh) + FINALIZER_CLOSURE_FLAG); |
| 78 |
|
| 79 |
GC_finalized_kind = GC_new_kind_inner(GC_new_free_list_inner(), |
| 80 |
GC_DS_LENGTH, TRUE, TRUE); |
| 81 |
GC_ASSERT(GC_finalized_kind != 0); |
| 82 |
GC_register_disclaim_proc(GC_finalized_kind, GC_finalized_disclaim, TRUE); |
| 83 |
UNLOCK(); |
| 84 |
} |
| 85 |
|
| 86 |
GC_API void GC_CALL GC_register_disclaim_proc(int kind, GC_disclaim_proc proc, |
| 87 |
int mark_unconditionally) |
| 88 |
{ |
| 89 |
GC_ASSERT((unsigned)kind < MAXOBJKINDS); |
| 90 |
if (!EXPECT(GC_find_leak, FALSE)) { |
| 91 |
GC_obj_kinds[kind].ok_disclaim_proc = proc; |
| 92 |
GC_obj_kinds[kind].ok_mark_unconditionally = |
| 93 |
(GC_bool)mark_unconditionally; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
GC_API GC_ATTR_MALLOC void * GC_CALL GC_finalized_malloc(size_t lb, |
| 98 |
const struct GC_finalizer_closure *fclos) |
| 99 |
{ |
| 100 |
void *op; |
| 101 |
|
| 102 |
GC_ASSERT(GC_finalized_kind != 0); |
| 103 |
GC_ASSERT(NONNULL_ARG_NOT_NULL(fclos)); |
| 104 |
GC_ASSERT(((word)fclos & FINALIZER_CLOSURE_FLAG) == 0); |
| 105 |
op = GC_malloc_kind(SIZET_SAT_ADD(lb, sizeof(word)), GC_finalized_kind); |
| 106 |
if (EXPECT(NULL == op, FALSE)) |
| 107 |
return NULL; |
| 108 |
# ifdef AO_HAVE_store |
| 109 |
AO_store((volatile AO_t *)op, (AO_t)fclos | FINALIZER_CLOSURE_FLAG); |
| 110 |
# else |
| 111 |
*(word *)op = (word)fclos | FINALIZER_CLOSURE_FLAG; |
| 112 |
# endif |
| 113 |
GC_dirty(op); |
| 114 |
REACHABLE_AFTER_DIRTY(fclos); |
| 115 |
return (word *)op + 1; |
| 116 |
} |
| 117 |
|
| 118 |
#endif /* ENABLE_DISCLAIM */ |