]> granicus.if.org Git - gc/commitdiff
Simplify loops of collect_a_little/stopped_mark invoking mark_some
authorIvan Maidanski <ivmai@mail.ru>
Tue, 16 Apr 2019 06:29:42 +0000 (09:29 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Tue, 16 Apr 2019 06:31:14 +0000 (09:31 +0300)
(code refactoring)

* alloc.c (GC_collect_a_little_inner, GC_stopped_mark): If
GC_mark_some() returns true then just break to quit the loop; move the
logic that is needed to funish the collection outside the loop.
* alloc.c (GC_stopped_mark): Change i local variable type from unsigned
to int.

alloc.c

diff --git a/alloc.c b/alloc.c
index a298b459c7d44c5f1f0816bd330d66ea34cf964d..3e005cd96ae0df9dea1390cf510486ef78291e19 100644 (file)
--- a/alloc.c
+++ b/alloc.c
@@ -658,31 +658,34 @@ GC_INNER void GC_collect_a_little_inner(int n)
         int max_deficit = GC_rate * n;
 
         for (i = GC_deficit; i < max_deficit; i++) {
-            if (GC_mark_some((ptr_t)0)) {
-                /* Need to finish a collection */
-#               ifdef SAVE_CALL_CHAIN
-                    GC_save_callers(GC_last_stack);
-#               endif
-#               ifdef PARALLEL_MARK
-                    if (GC_parallel)
-                      GC_wait_for_reclaim();
-#               endif
-                if (GC_n_attempts < max_prior_attempts
-                    && GC_time_limit != GC_TIME_UNLIMITED) {
-#                 ifndef NO_CLOCK
+            if (GC_mark_some(NULL))
+                break;
+        }
+
+        if (i < max_deficit) {
+            /* Need to finish a collection.     */
+#           ifdef SAVE_CALL_CHAIN
+                GC_save_callers(GC_last_stack);
+#           endif
+#           ifdef PARALLEL_MARK
+                if (GC_parallel)
+                    GC_wait_for_reclaim();
+#           endif
+            if (GC_n_attempts < max_prior_attempts
+                && GC_time_limit != GC_TIME_UNLIMITED) {
+#               ifndef NO_CLOCK
                     GET_TIME(GC_start_time);
-#                 endif
-                  if (!GC_stopped_mark(GC_timeout_stop_func)) {
-                    GC_n_attempts++;
-                    break;
-                  }
+#               endif
+                if (GC_stopped_mark(GC_timeout_stop_func)) {
+                    GC_finish_collection();
                 } else {
-                  /* TODO: If possible, GC_default_stop_func should be  */
-                  /* used here.                                         */
-                  (void)GC_stopped_mark(GC_never_stop_func);
+                    GC_n_attempts++;
                 }
+            } else {
+                /* TODO: If possible, GC_default_stop_func should be    */
+                /* used here.                                           */
+                (void)GC_stopped_mark(GC_never_stop_func);
                 GC_finish_collection();
-                break;
             }
         }
         if (GC_deficit > 0) {
@@ -741,7 +744,7 @@ GC_API int GC_CALL GC_collect_a_little(void)
  */
 STATIC GC_bool GC_stopped_mark(GC_stop_func stop_func)
 {
-    unsigned i;
+    int i;
 #   ifndef NO_CLOCK
       CLOCK_TYPE start_time = CLOCK_TYPE_INITIALIZER;
 #   endif
@@ -794,31 +797,35 @@ STATIC GC_bool GC_stopped_mark(GC_stop_func stop_func)
             GC_noop6(0,0,0,0,0,0);
 
         GC_initiate_gc();
-        for (i = 0;;i++) {
-          if ((*stop_func)()) {
-            GC_COND_LOG_PRINTF("Abandoned stopped marking after"
-                               " %u iterations\n", i);
-            GC_deficit = i;     /* Give the mutator a chance.   */
-#           ifdef THREAD_LOCAL_ALLOC
-              GC_world_stopped = FALSE;
-#           endif
+        for (i = 0; !(*stop_func)(); i++) {
+          if (GC_mark_some(GC_approx_sp())) {
+            i = -1;
+            break;
+          }
+        }
 
-#           ifdef THREADS
-              if (GC_on_collection_event)
-                GC_on_collection_event(GC_EVENT_PRE_START_WORLD);
-#           endif
+        if (i >= 0) {
+          GC_COND_LOG_PRINTF("Abandoned stopped marking after"
+                             " %d iterations\n", i);
+          GC_deficit = i;       /* Give the mutator a chance.   */
+#         ifdef THREAD_LOCAL_ALLOC
+            GC_world_stopped = FALSE;
+#         endif
 
-            START_WORLD();
+#         ifdef THREADS
+            if (GC_on_collection_event)
+              GC_on_collection_event(GC_EVENT_PRE_START_WORLD);
+#         endif
 
-#           ifdef THREADS
-              if (GC_on_collection_event)
-                GC_on_collection_event(GC_EVENT_POST_START_WORLD);
-#           endif
+          START_WORLD();
 
-            /* TODO: Notify GC_EVENT_MARK_ABANDON */
-            return(FALSE);
-          }
-          if (GC_mark_some(GC_approx_sp())) break;
+#         ifdef THREADS
+            if (GC_on_collection_event)
+              GC_on_collection_event(GC_EVENT_POST_START_WORLD);
+#         endif
+
+          /* TODO: Notify GC_EVENT_MARK_ABANDON */
+          return FALSE;
         }
 
     GC_gc_no++;