Workaround 'label handle_thr_start is not used' cppcheck style warning
[platform/upstream/libgc.git] / finalize.c
index c73febe..c8708b9 100644 (file)
@@ -124,7 +124,9 @@ STATIC void GC_grow_table(struct hash_chain_entry ***table,
     GC_ASSERT(I_HOLD_LOCK());
     /* Avoid growing the table in case of at least 25% of entries can   */
     /* be deleted by enforcing a collection.  Ignored for small tables. */
-    if (log_old_size >= GC_ON_GROW_LOG_SIZE_MIN) {
+    /* In incremental mode we skip this optimization, as we want to     */
+    /* avoid triggering a full GC whenever possible.                    */
+    if (log_old_size >= GC_ON_GROW_LOG_SIZE_MIN && !GC_incremental) {
       IF_CANCEL(int cancel_state;)
 
       DISABLE_CANCEL(cancel_state);
@@ -236,11 +238,11 @@ STATIC int GC_register_disappearing_link_inner(
     new_dl -> dl_hidden_obj = GC_HIDE_POINTER(obj);
     new_dl -> dl_hidden_link = GC_HIDE_POINTER(link);
     dl_set_next(new_dl, dl_hashtbl -> head[index]);
+    GC_dirty(new_dl);
     dl_hashtbl -> head[index] = new_dl;
     dl_hashtbl -> entries++;
     GC_dirty(dl_hashtbl->head + index);
     UNLOCK();
-    GC_dirty(new_dl);
     return GC_SUCCESS;
 }
 
@@ -634,10 +636,27 @@ GC_API GC_await_finalize_proc GC_CALL GC_get_await_finalize_proc(void)
 /* overflow is handled by the caller, and is not a disaster.            */
 STATIC void GC_normal_finalize_mark_proc(ptr_t p)
 {
+# if defined(_MSC_VER) && defined(I386)
     hdr * hhdr = HDR(p);
+    /* This is a manually inlined variant of GC_push_obj().  Otherwise  */
+    /* some optimizer bug is tickled in VC for X86 (v19, at least).     */
+#   define mark_stack_top GC_mark_stack_top
+    mse * mark_stack_limit = GC_mark_stack + GC_mark_stack_size;
+    word descr = hhdr -> hb_descr;
 
-    PUSH_OBJ(p, hhdr, GC_mark_stack_top,
-             &(GC_mark_stack[GC_mark_stack_size]));
+    if (descr != 0) {
+      mark_stack_top++;
+      if ((word)mark_stack_top >= (word)mark_stack_limit) {
+        mark_stack_top = GC_signal_mark_stack_overflow(mark_stack_top);
+      }
+      mark_stack_top -> mse_start = p;
+      mark_stack_top -> mse_descr.w = descr;
+    }
+#   undef mark_stack_top
+# else
+    GC_mark_stack_top = GC_push_obj(p, HDR(p), GC_mark_stack_top,
+                                    GC_mark_stack + GC_mark_stack_size);
+# endif
 }
 
 /* This only pays very partial attention to the mark descriptor.        */
@@ -756,10 +775,8 @@ STATIC void GC_register_finalizer_inner(void * obj,
             GC_dirty(GC_fnlz_roots.fo_head + index);
           UNLOCK();
 #         ifndef DBG_HDRS_ALL
-            if (EXPECT(new_fo != 0, FALSE)) {
               /* Free unused new_fo returned by GC_oom_fn() */
               GC_free((void *)new_fo);
-            }
 #         endif
           return;
         }
@@ -814,11 +831,11 @@ STATIC void GC_register_finalizer_inner(void * obj,
     new_fo -> fo_object_size = hhdr -> hb_sz;
     new_fo -> fo_mark_proc = mp;
     fo_set_next(new_fo, GC_fnlz_roots.fo_head[index]);
+    GC_dirty(new_fo);
     GC_fo_entries++;
     GC_fnlz_roots.fo_head[index] = new_fo;
     GC_dirty(GC_fnlz_roots.fo_head + index);
     UNLOCK();
-    GC_dirty(new_fo);
 }
 
 GC_API void GC_CALL GC_register_finalizer(void * obj,
@@ -940,74 +957,53 @@ GC_API void GC_CALL GC_register_finalizer_unreachable(void * obj,
   }
 #endif /* THREADS */
 
-#define ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr_dl, prev_dl) \
-  { \
-    size_t i; \
-    size_t dl_size = dl_hashtbl->log_size == -1 ? 0 : \
-                                (size_t)1 << dl_hashtbl->log_size; \
-    GC_bool needs_barrier = FALSE; \
-    GC_ASSERT(I_HOLD_LOCK()); \
-    for (i = 0; i < dl_size; i++) { \
-      struct disappearing_link *prev_dl = NULL; \
-      curr_dl = dl_hashtbl -> head[i]; \
-      while (curr_dl) {
-
-#define ITERATE_DL_HASHTBL_END(curr_dl, prev_dl) \
-        prev_dl = curr_dl; \
-        curr_dl = dl_next(curr_dl); \
-      } \
-    } \
-    if (needs_barrier) \
-      GC_dirty(dl_hashtbl -> head); /* entire object */ \
-  }
-
-#define DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr_dl, prev_dl, next_dl) \
-  { \
-    next_dl = dl_next(curr_dl); \
-    if (NULL == prev_dl) { \
-        dl_hashtbl -> head[i] = next_dl; \
-        needs_barrier = TRUE; \
-    } else { \
-        dl_set_next(prev_dl, next_dl); \
-        GC_dirty(prev_dl); \
-    } \
-    GC_clear_mark_bit(curr_dl); \
-    dl_hashtbl -> entries--; \
-    curr_dl = next_dl; \
-    continue; \
-  }
-
 GC_INLINE void GC_make_disappearing_links_disappear(
-                                struct dl_hashtbl_s* dl_hashtbl)
+                                        struct dl_hashtbl_s* dl_hashtbl,
+                                        GC_bool is_remove_dangling)
 {
-    struct disappearing_link *curr, *next;
+  size_t i;
+  size_t dl_size = dl_hashtbl->log_size == -1 ? 0
+                        : (size_t)1 << dl_hashtbl->log_size;
+  GC_bool needs_barrier = FALSE;
+
+  GC_ASSERT(I_HOLD_LOCK());
+  for (i = 0; i < dl_size; i++) {
+    struct disappearing_link *curr_dl, *next_dl;
+    struct disappearing_link *prev_dl = NULL;
 
-    ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr, prev)
-        ptr_t real_ptr = (ptr_t)GC_REVEAL_POINTER(curr->dl_hidden_obj);
-        ptr_t real_link = (ptr_t)GC_REVEAL_POINTER(curr->dl_hidden_link);
+    for (curr_dl = dl_hashtbl->head[i]; curr_dl != NULL; curr_dl = next_dl) {
+      next_dl = dl_next(curr_dl);
+      if (is_remove_dangling) {
+        ptr_t real_link = (ptr_t)GC_base(GC_REVEAL_POINTER(
+                                                curr_dl->dl_hidden_link));
 
-        if (!GC_is_marked(real_ptr)) {
-            *(word *)real_link = 0;
-            GC_clear_mark_bit(curr);
-            DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr, prev, next);
+        if (NULL == real_link || EXPECT(GC_is_marked(real_link), TRUE)) {
+          prev_dl = curr_dl;
+          continue;
         }
-    ITERATE_DL_HASHTBL_END(curr, prev)
-}
-
-GC_INLINE void GC_remove_dangling_disappearing_links(
-                                struct dl_hashtbl_s* dl_hashtbl)
-{
-    struct disappearing_link *curr, *next;
-
-    ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr, prev)
-        ptr_t real_link =
-                (ptr_t)GC_base(GC_REVEAL_POINTER(curr->dl_hidden_link));
-
-        if (NULL != real_link && !GC_is_marked(real_link)) {
-            GC_clear_mark_bit(curr);
-            DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr, prev, next);
+      } else {
+        if (EXPECT(GC_is_marked((ptr_t)GC_REVEAL_POINTER(
+                                        curr_dl->dl_hidden_obj)), TRUE)) {
+          prev_dl = curr_dl;
+          continue;
         }
-    ITERATE_DL_HASHTBL_END(curr, prev)
+        *(ptr_t *)GC_REVEAL_POINTER(curr_dl->dl_hidden_link) = NULL;
+      }
+
+      /* Delete curr_dl entry from dl_hashtbl.  */
+      if (NULL == prev_dl) {
+        dl_hashtbl -> head[i] = next_dl;
+        needs_barrier = TRUE;
+      } else {
+        dl_set_next(prev_dl, next_dl);
+        GC_dirty(prev_dl);
+      }
+      GC_clear_mark_bit(curr_dl);
+      dl_hashtbl -> entries--;
+    }
+  }
+  if (needs_barrier)
+    GC_dirty(dl_hashtbl -> head); /* entire object */
 }
 
 /* Called with held lock (but the world is running).                    */
@@ -1034,7 +1030,7 @@ GC_INNER void GC_finalize(void)
 #   ifndef GC_TOGGLE_REFS_NOT_NEEDED
       GC_mark_togglerefs();
 #   endif
-    GC_make_disappearing_links_disappear(&GC_dl_hashtbl);
+    GC_make_disappearing_links_disappear(&GC_dl_hashtbl, FALSE);
 
   /* Mark all objects reachable via chains of 1 or more pointers        */
   /* from finalizable objects.                                          */
@@ -1069,7 +1065,11 @@ GC_INNER void GC_finalize(void)
               next_fo = fo_next(curr_fo);
               if (NULL == prev_fo) {
                 GC_fnlz_roots.fo_head[i] = next_fo;
-                needs_barrier = TRUE;
+                if (GC_object_finalized_proc) {
+                  GC_dirty(GC_fnlz_roots.fo_head + i);
+                } else {
+                  needs_barrier = TRUE;
+                }
               } else {
                 fo_set_next(prev_fo, next_fo);
                 GC_dirty(prev_fo);
@@ -1158,13 +1158,15 @@ GC_INNER void GC_finalize(void)
   if (needs_barrier)
     GC_dirty(GC_fnlz_roots.fo_head); /* entire object */
 
-  GC_remove_dangling_disappearing_links(&GC_dl_hashtbl);
+  /* Remove dangling disappearing links. */
+  GC_make_disappearing_links_disappear(&GC_dl_hashtbl, TRUE);
+
 # ifndef GC_TOGGLE_REFS_NOT_NEEDED
     GC_clear_togglerefs();
 # endif
 # ifndef GC_LONG_REFS_NOT_NEEDED
-    GC_make_disappearing_links_disappear(&GC_ll_hashtbl);
-    GC_remove_dangling_disappearing_links(&GC_ll_hashtbl);
+    GC_make_disappearing_links_disappear(&GC_ll_hashtbl, FALSE);
+    GC_make_disappearing_links_disappear(&GC_ll_hashtbl, TRUE);
 # endif
 
   if (GC_fail_count) {
@@ -1343,7 +1345,7 @@ GC_INNER void GC_notify_or_invoke_finalizers(void)
 #       ifdef KEEP_BACK_PTRS
           long i;
           /* Stops when GC_gc_no wraps; that's OK.      */
-          last_back_trace_gc_no = (word)(-1);  /* disable others. */
+          last_back_trace_gc_no = GC_WORD_MAX;  /* disable others. */
           for (i = 0; i < GC_backtraces; ++i) {
               /* FIXME: This tolerates concurrent heap mutation,        */
               /* which may cause occasional mysterious results.         */
@@ -1383,8 +1385,8 @@ GC_INNER void GC_notify_or_invoke_finalizers(void)
 
     /* These variables require synchronization to avoid data races.     */
     if (last_finalizer_notification != GC_gc_no) {
-        last_finalizer_notification = GC_gc_no;
         notifier_fn = GC_finalizer_notifier;
+        last_finalizer_notification = GC_gc_no;
     }
     UNLOCK();
     if (notifier_fn != 0)