Reviewers: Vyacheslav Egorov,

Message:
Vyacheslav, can you take a look at this please?  It's a trivial change.
Previously, when the gc_count is using a signed int type, the (gc_count_ <
last_gc_count + kGCsBetweenCleanup) expression will not behave as we would
expect it to when the value of gc_count_ is near the point of overflow.

For example, consider when the value of gc_count_ is 2147483646 (0x7ffffffe),
and last_gc_count is 2147483646 (0x7ffffffe):

Previously with signed numbers:
LHS: 2147483646
RHS: 2147483646 (0x7ffffffe) + 4 => -2147483646 (0x80000002)
LHS < RHS ==> 2147483646 < -2147483646 ==> false

With this patch using unsigned numbers:

LHS: 0x7ffffffe - 0x7ffffffe =>0
RHS: 4
LHS < RHS ==> 0 < 4 ==> true

But in this case, gc_count_ and last_gc_count both have the same value.  We
would expect the difference to be less than kGCsBetweenCleanup, but it turns out
to not be in the signed int case.


Description:
Heap::gc_count_, last_gc_count, and kGCsBetweenCleanup should be unsigned
in order to not be vulnerable to overflow issues.


Please review this at http://codereview.chromium.org/5966001/

SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/

Affected files:
  M     src/heap.h
  M     src/heap.cc


Index: src/heap.cc
===================================================================
--- src/heap.cc (revision 6057)
+++ src/heap.cc (working copy)
@@ -133,7 +133,7 @@

 int Heap::mc_count_ = 0;
 int Heap::ms_count_ = 0;
-int Heap::gc_count_ = 0;
+unsigned int Heap::gc_count_ = 0;

 GCTracer* Heap::tracer_ = NULL;

@@ -3758,9 +3758,9 @@
   static const int kIdlesBeforeMarkSweep = 7;
   static const int kIdlesBeforeMarkCompact = 8;
   static const int kMaxIdleCount = kIdlesBeforeMarkCompact + 1;
-  static const int kGCsBetweenCleanup = 4;
+  static const unsigned int kGCsBetweenCleanup = 4;
   static int number_idle_notifications = 0;
-  static int last_gc_count = gc_count_;
+  static unsigned int last_gc_count = gc_count_;

   bool uncommit = true;
   bool finished = false;
@@ -3769,7 +3769,7 @@
   // GCs have taken place. This allows another round of cleanup based
   // on idle notifications if enough work has been carried out to
   // provoke a number of garbage collections.
-  if (gc_count_ < last_gc_count + kGCsBetweenCleanup) {
+  if (gc_count_ - last_gc_count < kGCsBetweenCleanup) {
     number_idle_notifications =
         Min(number_idle_notifications + 1, kMaxIdleCount);
   } else {
Index: src/heap.h
===================================================================
--- src/heap.h  (revision 6057)
+++ src/heap.h  (working copy)
@@ -1141,7 +1141,7 @@

   static int mc_count_;  // how many mark-compact collections happened
   static int ms_count_;  // how many mark-sweep collections happened
-  static int gc_count_;  // how many gc happened
+  static unsigned int gc_count_;  // how many gc happened

   // Total length of the strings we failed to flatten since the last GC.
   static int unflattened_strings_length_;


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to