This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Properly handle index wraparound when updating memusage data


catomic_exchange_and_add returns the _old_ value, so idx could never
equal buffer_cnt and the latter was never actually reset.

Andreas.

2009-10-29  Andreas Schwab  <schwab@redhat.com>

	* malloc/memusage.c (update_data): Fix index wraparound handling
	so that buffer_cnt is actually reset.

diff --git a/malloc/memusage.c b/malloc/memusage.c
index fcd58dc..382261c 100644
--- a/malloc/memusage.c
+++ b/malloc/memusage.c
@@ -163,15 +163,16 @@ update_data (struct header *result, size_t len, size_t old_len)
   if (fd != -1)
     {
       uatomic32_t idx = catomic_exchange_and_add (&buffer_cnt, 1);
-      if (idx >= 2 * buffer_size)
+      if (idx + 1 >= 2 * buffer_size)
 	{
 	  /* We try to reset the counter to the correct range.  If
 	     this fails because of another thread increasing the
 	     counter it does not matter since that thread will take
 	     care of the correction.  */
-	  uatomic32_t reset = idx % (2 * buffer_size);
-	  catomic_compare_and_exchange_val_acq (&buffer_cnt, reset, idx);
-	  idx = reset;
+	  uatomic32_t reset = (idx + 1) % (2 * buffer_size);
+	  catomic_compare_and_exchange_val_acq (&buffer_cnt, reset, idx + 1);
+	  if (idx >= 2 * buffer_size)
+	    idx = reset - 1;
 	}
       assert (idx < 2 * DEFAULT_BUFFER_SIZE);
 
-- 
1.6.5.1


-- 
Andreas Schwab, schwab@redhat.com
GPG Key fingerprint = D4E8 DBE3 3813 BB5D FA84  5EC7 45C6 250E 6F00 984E
"And now for something completely different."


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]