fnlz_mlc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "private/gc_priv.h"
  15. #ifdef ENABLE_DISCLAIM
  16. #include "gc_disclaim.h"
  17. #include "gc_inline.h" /* for GC_malloc_kind */
  18. STATIC int GC_finalized_kind = 0;
  19. #if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
  20. /* The first bit is already used for a debug purpose. */
  21. # define FINALIZER_CLOSURE_FLAG 0x2
  22. #else
  23. # define FINALIZER_CLOSURE_FLAG 0x1
  24. #endif
  25. STATIC int GC_CALLBACK GC_finalized_disclaim(void *obj)
  26. {
  27. word fc_word = *(word *)obj;
  28. if ((fc_word & FINALIZER_CLOSURE_FLAG) != 0) {
  29. /* The disclaim function may be passed fragments from the */
  30. /* free-list, on which it should not run finalization. */
  31. /* To recognize this case, we use the fact that the first word */
  32. /* on such fragments is always multiple of 4 (a link to the next */
  33. /* fragment, or NULL). If it is desirable to have a finalizer */
  34. /* which does not use the first word for storing finalization */
  35. /* info, GC_reclaim_with_finalization must be extended to clear */
  36. /* fragments so that the assumption holds for the selected word. */
  37. const struct GC_finalizer_closure *fc
  38. = (struct GC_finalizer_closure *)(fc_word
  39. & ~(word)FINALIZER_CLOSURE_FLAG);
  40. (*fc->proc)((word *)obj + 1, fc->cd);
  41. }
  42. return 0;
  43. }
  44. GC_API void GC_CALL GC_init_finalized_malloc(void)
  45. {
  46. DCL_LOCK_STATE;
  47. GC_init(); /* In case it's not already done. */
  48. LOCK();
  49. if (GC_finalized_kind != 0) {
  50. UNLOCK();
  51. return;
  52. }
  53. /* The finalizer closure is placed in the first word in order to */
  54. /* use the lower bits to distinguish live objects from objects on */
  55. /* the free list. The downside of this is that we need one-word */
  56. /* offset interior pointers, and that GC_base does not return the */
  57. /* start of the user region. */
  58. GC_register_displacement_inner(sizeof(word));
  59. GC_finalized_kind = GC_new_kind_inner(GC_new_free_list_inner(),
  60. GC_DS_LENGTH, TRUE, TRUE);
  61. GC_ASSERT(GC_finalized_kind != 0);
  62. GC_register_disclaim_proc(GC_finalized_kind, GC_finalized_disclaim, TRUE);
  63. UNLOCK();
  64. }
  65. GC_API void GC_CALL GC_register_disclaim_proc(int kind, GC_disclaim_proc proc,
  66. int mark_unconditionally)
  67. {
  68. GC_ASSERT((unsigned)kind < MAXOBJKINDS);
  69. GC_obj_kinds[kind].ok_disclaim_proc = proc;
  70. GC_obj_kinds[kind].ok_mark_unconditionally = (GC_bool)mark_unconditionally;
  71. }
  72. GC_API GC_ATTR_MALLOC void * GC_CALL GC_finalized_malloc(size_t lb,
  73. const struct GC_finalizer_closure *fclos)
  74. {
  75. word *op;
  76. GC_ASSERT(GC_finalized_kind != 0);
  77. op = (word *)GC_malloc_kind(SIZET_SAT_ADD(lb, sizeof(word)),
  78. GC_finalized_kind);
  79. if (EXPECT(NULL == op, FALSE))
  80. return NULL;
  81. *op = (word)fclos | FINALIZER_CLOSURE_FLAG;
  82. return op + 1;
  83. }
  84. #endif /* ENABLE_DISCLAIM */