tsan: improve memory allocator a bit


git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@162561 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/sanitizer_common/sanitizer_allocator64.h b/lib/sanitizer_common/sanitizer_allocator64.h
index 68a52a3..2e12afd 100644
--- a/lib/sanitizer_common/sanitizer_allocator64.h
+++ b/lib/sanitizer_common/sanitizer_allocator64.h
@@ -147,11 +147,15 @@
       PopulateFreeList(class_id, region);
     }
     CHECK(!region->free_list.empty());
-    const uptr count = SizeClassMap::MaxCached(class_id);
-    for (uptr i = 0; i < count && !region->free_list.empty(); i++) {
-      AllocatorListNode *node = region->free_list.front();
-      region->free_list.pop_front();
-      free_list->push_front(node);
+    uptr count = SizeClassMap::MaxCached(class_id);
+    if (region->free_list.size() <= count) {
+      free_list->append_front(&region->free_list);
+    } else {
+      for (uptr i = 0; i < count; i++) {
+        AllocatorListNode *node = region->free_list.front();
+        region->free_list.pop_front();
+        free_list->push_front(node);
+      }
     }
     CHECK(!free_list->empty());
   }