allocator_unittest.cc 15.8 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4 5 6 7 8
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>   // for min()

9
#include "butil/atomicops.h"
gejun's avatar
gejun committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#include <gtest/gtest.h>

// Number of bits in a size_t.
static const int kSizeBits = 8 * sizeof(size_t);
// The maximum size of a size_t.
static const size_t kMaxSize = ~static_cast<size_t>(0);
// Maximum positive size of a size_t if it were signed.
static const size_t kMaxSignedSize = ((size_t(1) << (kSizeBits-1)) - 1);
// An allocation size which is not too big to be reasonable.
static const size_t kNotTooBig = 100000;
// An allocation size which is just too big.
static const size_t kTooBig = ~static_cast<size_t>(0);

namespace {

using std::min;

// Fill a buffer of the specified size with a predetermined pattern
static void Fill(unsigned char* buffer, int n) {
  for (int i = 0; i < n; i++) {
    buffer[i] = (i & 0xff);
  }
}

// Check that the specified buffer has the predetermined pattern
// generated by Fill()
static bool Valid(unsigned char* buffer, int n) {
  for (int i = 0; i < n; i++) {
    if (buffer[i] != (i & 0xff)) {
      return false;
    }
  }
  return true;
}

// Check that a buffer is completely zeroed.
static bool ALLOW_UNUSED IsZeroed(unsigned char* buffer, int n) {
  for (int i = 0; i < n; i++) {
    if (buffer[i] != 0) {
      return false;
    }
  }
  return true;
}

// Check alignment
static void CheckAlignment(void* p, int align) {
  EXPECT_EQ(0, reinterpret_cast<uintptr_t>(p) & (align-1));
}

// Return the next interesting size/delta to check.  Returns -1 if no more.
static int NextSize(int size) {
  if (size < 100)
    return size+1;

  if (size < 100000) {
    // Find next power of two
    int power = 1;
    while (power < size)
      power <<= 1;

    // Yield (power-1, power, power+1)
    if (size < power-1)
      return power-1;

    if (size == power-1)
      return power;

    assert(size == power);
    return power+1;
  } else {
    return -1;
  }
}

template <class AtomicType>
static void TestAtomicIncrement() {
  // For now, we just test single threaded execution

  // use a guard value to make sure the NoBarrier_AtomicIncrement doesn't go
  // outside the expected address bounds.  This is in particular to
  // test that some future change to the asm code doesn't cause the
  // 32-bit NoBarrier_AtomicIncrement to do the wrong thing on 64-bit machines.
  struct {
    AtomicType prev_word;
    AtomicType count;
    AtomicType next_word;
  } s;

  AtomicType prev_word_value, next_word_value;
  memset(&prev_word_value, 0xFF, sizeof(AtomicType));
  memset(&next_word_value, 0xEE, sizeof(AtomicType));

  s.prev_word = prev_word_value;
  s.count = 0;
  s.next_word = next_word_value;

107
  EXPECT_EQ(butil::subtle::NoBarrier_AtomicIncrement(&s.count, 1), 1);
gejun's avatar
gejun committed
108 109 110 111
  EXPECT_EQ(s.count, 1);
  EXPECT_EQ(s.prev_word, prev_word_value);
  EXPECT_EQ(s.next_word, next_word_value);

112
  EXPECT_EQ(butil::subtle::NoBarrier_AtomicIncrement(&s.count, 2), 3);
gejun's avatar
gejun committed
113 114 115 116
  EXPECT_EQ(s.count, 3);
  EXPECT_EQ(s.prev_word, prev_word_value);
  EXPECT_EQ(s.next_word, next_word_value);

117
  EXPECT_EQ(butil::subtle::NoBarrier_AtomicIncrement(&s.count, 3), 6);
gejun's avatar
gejun committed
118 119 120 121
  EXPECT_EQ(s.count, 6);
  EXPECT_EQ(s.prev_word, prev_word_value);
  EXPECT_EQ(s.next_word, next_word_value);

122
  EXPECT_EQ(butil::subtle::NoBarrier_AtomicIncrement(&s.count, -3), 3);
gejun's avatar
gejun committed
123 124 125 126
  EXPECT_EQ(s.count, 3);
  EXPECT_EQ(s.prev_word, prev_word_value);
  EXPECT_EQ(s.next_word, next_word_value);

127
  EXPECT_EQ(butil::subtle::NoBarrier_AtomicIncrement(&s.count, -2), 1);
gejun's avatar
gejun committed
128 129 130 131
  EXPECT_EQ(s.count, 1);
  EXPECT_EQ(s.prev_word, prev_word_value);
  EXPECT_EQ(s.next_word, next_word_value);

132
  EXPECT_EQ(butil::subtle::NoBarrier_AtomicIncrement(&s.count, -1), 0);
gejun's avatar
gejun committed
133 134 135 136
  EXPECT_EQ(s.count, 0);
  EXPECT_EQ(s.prev_word, prev_word_value);
  EXPECT_EQ(s.next_word, next_word_value);

137
  EXPECT_EQ(butil::subtle::NoBarrier_AtomicIncrement(&s.count, -1), -1);
gejun's avatar
gejun committed
138 139 140 141
  EXPECT_EQ(s.count, -1);
  EXPECT_EQ(s.prev_word, prev_word_value);
  EXPECT_EQ(s.next_word, next_word_value);

142
  EXPECT_EQ(butil::subtle::NoBarrier_AtomicIncrement(&s.count, -4), -5);
gejun's avatar
gejun committed
143 144 145 146
  EXPECT_EQ(s.count, -5);
  EXPECT_EQ(s.prev_word, prev_word_value);
  EXPECT_EQ(s.next_word, next_word_value);

147
  EXPECT_EQ(butil::subtle::NoBarrier_AtomicIncrement(&s.count, 5), 0);
gejun's avatar
gejun committed
148 149 150 151 152 153 154 155 156 157 158 159
  EXPECT_EQ(s.count, 0);
  EXPECT_EQ(s.prev_word, prev_word_value);
  EXPECT_EQ(s.next_word, next_word_value);
}


#define NUM_BITS(T) (sizeof(T) * 8)


template <class AtomicType>
static void TestCompareAndSwap() {
  AtomicType value = 0;
160
  AtomicType prev = butil::subtle::NoBarrier_CompareAndSwap(&value, 0, 1);
gejun's avatar
gejun committed
161 162 163 164 165 166 167 168
  EXPECT_EQ(1, value);
  EXPECT_EQ(0, prev);

  // Use test value that has non-zero bits in both halves, more for testing
  // 64-bit implementation on 32-bit platforms.
  const AtomicType k_test_val = (static_cast<uint64_t>(1) <<
                                 (NUM_BITS(AtomicType) - 2)) + 11;
  value = k_test_val;
169
  prev = butil::subtle::NoBarrier_CompareAndSwap(&value, 0, 5);
gejun's avatar
gejun committed
170 171 172 173
  EXPECT_EQ(k_test_val, value);
  EXPECT_EQ(k_test_val, prev);

  value = k_test_val;
174
  prev = butil::subtle::NoBarrier_CompareAndSwap(&value, k_test_val, 5);
gejun's avatar
gejun committed
175 176 177 178 179 180 181 182
  EXPECT_EQ(5, value);
  EXPECT_EQ(k_test_val, prev);
}


template <class AtomicType>
static void TestAtomicExchange() {
  AtomicType value = 0;
183
  AtomicType new_value = butil::subtle::NoBarrier_AtomicExchange(&value, 1);
gejun's avatar
gejun committed
184 185 186 187 188 189 190 191
  EXPECT_EQ(1, value);
  EXPECT_EQ(0, new_value);

  // Use test value that has non-zero bits in both halves, more for testing
  // 64-bit implementation on 32-bit platforms.
  const AtomicType k_test_val = (static_cast<uint64_t>(1) <<
                                 (NUM_BITS(AtomicType) - 2)) + 11;
  value = k_test_val;
192
  new_value = butil::subtle::NoBarrier_AtomicExchange(&value, k_test_val);
gejun's avatar
gejun committed
193 194 195 196
  EXPECT_EQ(k_test_val, value);
  EXPECT_EQ(k_test_val, new_value);

  value = k_test_val;
197
  new_value = butil::subtle::NoBarrier_AtomicExchange(&value, 5);
gejun's avatar
gejun committed
198 199 200 201 202 203 204 205 206 207 208
  EXPECT_EQ(5, value);
  EXPECT_EQ(k_test_val, new_value);
}


template <class AtomicType>
static void TestAtomicIncrementBounds() {
  // Test increment at the half-width boundary of the atomic type.
  // It is primarily for testing at the 32-bit boundary for 64-bit atomic type.
  AtomicType test_val = static_cast<uint64_t>(1) << (NUM_BITS(AtomicType) / 2);
  AtomicType value = test_val - 1;
209
  AtomicType new_value = butil::subtle::NoBarrier_AtomicIncrement(&value, 1);
gejun's avatar
gejun committed
210 211 212
  EXPECT_EQ(test_val, value);
  EXPECT_EQ(value, new_value);

213
  butil::subtle::NoBarrier_AtomicIncrement(&value, -1);
gejun's avatar
gejun committed
214 215 216 217 218 219 220 221 222 223 224 225
  EXPECT_EQ(test_val - 1, value);
}

// This is a simple sanity check that values are correct. Not testing
// atomicity
template <class AtomicType>
static void TestStore() {
  const AtomicType kVal1 = static_cast<AtomicType>(0xa5a5a5a5a5a5a5a5LL);
  const AtomicType kVal2 = static_cast<AtomicType>(-1);

  AtomicType value;

226
  butil::subtle::NoBarrier_Store(&value, kVal1);
gejun's avatar
gejun committed
227
  EXPECT_EQ(kVal1, value);
228
  butil::subtle::NoBarrier_Store(&value, kVal2);
gejun's avatar
gejun committed
229 230
  EXPECT_EQ(kVal2, value);

231
  butil::subtle::Acquire_Store(&value, kVal1);
gejun's avatar
gejun committed
232
  EXPECT_EQ(kVal1, value);
233
  butil::subtle::Acquire_Store(&value, kVal2);
gejun's avatar
gejun committed
234 235
  EXPECT_EQ(kVal2, value);

236
  butil::subtle::Release_Store(&value, kVal1);
gejun's avatar
gejun committed
237
  EXPECT_EQ(kVal1, value);
238
  butil::subtle::Release_Store(&value, kVal2);
gejun's avatar
gejun committed
239 240 241 242 243 244 245 246 247 248 249 250 251
  EXPECT_EQ(kVal2, value);
}

// This is a simple sanity check that values are correct. Not testing
// atomicity
template <class AtomicType>
static void TestLoad() {
  const AtomicType kVal1 = static_cast<AtomicType>(0xa5a5a5a5a5a5a5a5LL);
  const AtomicType kVal2 = static_cast<AtomicType>(-1);

  AtomicType value;

  value = kVal1;
252
  EXPECT_EQ(kVal1, butil::subtle::NoBarrier_Load(&value));
gejun's avatar
gejun committed
253
  value = kVal2;
254
  EXPECT_EQ(kVal2, butil::subtle::NoBarrier_Load(&value));
gejun's avatar
gejun committed
255 256

  value = kVal1;
257
  EXPECT_EQ(kVal1, butil::subtle::Acquire_Load(&value));
gejun's avatar
gejun committed
258
  value = kVal2;
259
  EXPECT_EQ(kVal2, butil::subtle::Acquire_Load(&value));
gejun's avatar
gejun committed
260 261

  value = kVal1;
262
  EXPECT_EQ(kVal1, butil::subtle::Release_Load(&value));
gejun's avatar
gejun committed
263
  value = kVal2;
264
  EXPECT_EQ(kVal2, butil::subtle::Release_Load(&value));
gejun's avatar
gejun committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
}

template <class AtomicType>
static void TestAtomicOps() {
  TestCompareAndSwap<AtomicType>();
  TestAtomicExchange<AtomicType>();
  TestAtomicIncrementBounds<AtomicType>();
  TestStore<AtomicType>();
  TestLoad<AtomicType>();
}

static void TestCalloc(size_t n, size_t s, bool ok) {
  char* p = reinterpret_cast<char*>(calloc(n, s));
  if (!ok) {
    EXPECT_EQ(NULL, p) << "calloc(n, s) should not succeed";
  } else {
    EXPECT_NE(reinterpret_cast<void*>(NULL), p) <<
        "calloc(n, s) should succeed";
    for (size_t i = 0; i < n*s; i++) {
      EXPECT_EQ('\0', p[i]);
    }
    free(p);
  }
}


// A global test counter for number of times the NewHandler is called.
static int news_handled = 0;
static void TestNewHandler() {
  ++news_handled;
  throw std::bad_alloc();
}

// Because we compile without exceptions, we expect these will not throw.
static void TestOneNewWithoutExceptions(void* (*func)(size_t),
                                        bool should_throw) {
  // success test
  try {
    void* ptr = (*func)(kNotTooBig);
    EXPECT_NE(reinterpret_cast<void*>(NULL), ptr) <<
        "allocation should not have failed.";
  } catch(...) {
    EXPECT_EQ(0, 1) << "allocation threw unexpected exception.";
  }

  // failure test
  try {
    void* rv = (*func)(kTooBig);
    EXPECT_EQ(NULL, rv);
    EXPECT_FALSE(should_throw) << "allocation should have thrown.";
  } catch(...) {
    EXPECT_TRUE(should_throw) << "allocation threw unexpected exception.";
  }
}

static void TestNothrowNew(void* (*func)(size_t)) {
  news_handled = 0;

  // test without new_handler:
  std::new_handler saved_handler = std::set_new_handler(0);
  TestOneNewWithoutExceptions(func, false);

  // test with new_handler:
  std::set_new_handler(TestNewHandler);
  TestOneNewWithoutExceptions(func, true);
  EXPECT_EQ(news_handled, 1) << "nothrow new_handler was not called.";
  std::set_new_handler(saved_handler);
}

}  // namespace

//-----------------------------------------------------------------------------

TEST(Atomics, AtomicIncrementWord) {
339
    TestAtomicIncrement<butil::subtle::AtomicWord>();
gejun's avatar
gejun committed
340 341 342
}

TEST(Atomics, AtomicIncrement32) {
343
    TestAtomicIncrement<butil::subtle::Atomic32>();
gejun's avatar
gejun committed
344 345 346
}

TEST(Atomics, AtomicOpsWord) {
347
    TestAtomicIncrement<butil::subtle::AtomicWord>();
gejun's avatar
gejun committed
348 349 350
}

TEST(Atomics, AtomicOps32) {
351
    TestAtomicIncrement<butil::subtle::Atomic32>();
gejun's avatar
gejun committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
}

TEST(Allocators, Malloc) {
  // Try allocating data with a bunch of alignments and sizes
  for (int size = 1; size < 1048576; size *= 2) {
    unsigned char* ptr = reinterpret_cast<unsigned char*>(malloc(size));
    CheckAlignment(ptr, 2);  // Should be 2 byte aligned
    Fill(ptr, size);
    EXPECT_TRUE(Valid(ptr, size));
    free(ptr);
  }
}

TEST(Allocators, Calloc) {
  TestCalloc(0, 0, true);
  TestCalloc(0, 1, true);
  TestCalloc(1, 1, true);
  TestCalloc(1<<10, 0, true);
  TestCalloc(1<<20, 0, true);
  TestCalloc(0, 1<<10, true);
  TestCalloc(0, 1<<20, true);
  TestCalloc(1<<20, 2, true);
  TestCalloc(2, 1<<20, true);
  TestCalloc(1000, 1000, true);

  // Not work in glib 2.12 (Red Hat 4.4.6-3, Linux 2.6.32)
  // TestCalloc(kMaxSize, 2, false);
  // TestCalloc(2, kMaxSize, false);
  // TestCalloc(kMaxSize, kMaxSize, false);

  // TestCalloc(kMaxSignedSize, 3, false);
  // TestCalloc(3, kMaxSignedSize, false);
  // TestCalloc(kMaxSignedSize, kMaxSignedSize, false);
}

TEST(Allocators, New) {
  TestNothrowNew(&::operator new);
  TestNothrowNew(&::operator new[]);
}

// This makes sure that reallocing a small number of bytes in either
// direction doesn't cause us to allocate new memory.
TEST(Allocators, Realloc1) {
  int start_sizes[] = { 100, 1000, 10000, 100000 };
  int deltas[] = { 1, -2, 4, -8, 16, -32, 64, -128 };

  for (size_t s = 0; s < sizeof(start_sizes)/sizeof(*start_sizes); ++s) {
    void* p = malloc(start_sizes[s]);
    ASSERT_TRUE(p);
    // The larger the start-size, the larger the non-reallocing delta.
    for (size_t d = 0; d < s*2; ++d) {
      void* new_p = realloc(p, start_sizes[s] + deltas[d]);
      ASSERT_EQ(p, new_p);  // realloc should not allocate new memory
    }
    // Test again, but this time reallocing smaller first.
    for (size_t d = 0; d < s*2; ++d) {
      void* new_p = realloc(p, start_sizes[s] - deltas[d]);
      ASSERT_EQ(p, new_p);  // realloc should not allocate new memory
    }
    free(p);
  }
}

TEST(Allocators, Realloc2) {
  for (int src_size = 0; src_size >= 0; src_size = NextSize(src_size)) {
    for (int dst_size = 0; dst_size >= 0; dst_size = NextSize(dst_size)) {
      unsigned char* src = reinterpret_cast<unsigned char*>(malloc(src_size));
      Fill(src, src_size);
      unsigned char* dst =
          reinterpret_cast<unsigned char*>(realloc(src, dst_size));
      EXPECT_TRUE(Valid(dst, min(src_size, dst_size)));
      Fill(dst, dst_size);
      EXPECT_TRUE(Valid(dst, dst_size));
      if (dst != NULL) free(dst);
    }
  }

  // Now make sure realloc works correctly even when we overflow the
  // packed cache, so some entries are evicted from the cache.
  // The cache has 2^12 entries, keyed by page number.
  const int kNumEntries = 1 << 14;
  int** p = reinterpret_cast<int**>(malloc(sizeof(*p) * kNumEntries));
  int sum = 0;
  for (int i = 0; i < kNumEntries; i++) {
    // no page size is likely to be bigger than 8192?
    p[i] = reinterpret_cast<int*>(malloc(8192));
    p[i][1000] = i;              // use memory deep in the heart of p
  }
  for (int i = 0; i < kNumEntries; i++) {
    p[i] = reinterpret_cast<int*>(realloc(p[i], 9000));
  }
  for (int i = 0; i < kNumEntries; i++) {
    sum += p[i][1000];
    free(p[i]);
  }
  EXPECT_EQ(kNumEntries/2 * (kNumEntries - 1), sum);  // assume kNE is even
  free(p);
}

TEST(Allocators, ReallocZero) {
  // Test that realloc to zero does not return NULL.
  for (int size = 0; size >= 0; size = NextSize(size)) {
    char* ptr = reinterpret_cast<char*>(malloc(size));
    EXPECT_NE(static_cast<char*>(NULL), ptr);
    ptr = reinterpret_cast<char*>(realloc(ptr, 0));
    EXPECT_NE(static_cast<char*>(NULL), ptr);
    if (ptr)
      free(ptr);
  }
}

#ifdef WIN32
// Test recalloc
TEST(Allocators, Recalloc) {
  for (int src_size = 0; src_size >= 0; src_size = NextSize(src_size)) {
    for (int dst_size = 0; dst_size >= 0; dst_size = NextSize(dst_size)) {
      unsigned char* src =
          reinterpret_cast<unsigned char*>(_recalloc(NULL, 1, src_size));
      EXPECT_TRUE(IsZeroed(src, src_size));
      Fill(src, src_size);
      unsigned char* dst =
          reinterpret_cast<unsigned char*>(_recalloc(src, 1, dst_size));
      EXPECT_TRUE(Valid(dst, min(src_size, dst_size)));
      Fill(dst, dst_size);
      EXPECT_TRUE(Valid(dst, dst_size));
      if (dst != NULL)
        free(dst);
    }
  }
}

// Test windows specific _aligned_malloc() and _aligned_free() methods.
TEST(Allocators, AlignedMalloc) {
  // Try allocating data with a bunch of alignments and sizes
  static const int kTestAlignments[] = {8, 16, 256, 4096, 8192, 16384};
  for (int size = 1; size > 0; size = NextSize(size)) {
    for (int i = 0; i < ARRAYSIZE(kTestAlignments); ++i) {
      unsigned char* ptr = static_cast<unsigned char*>(
          _aligned_malloc(size, kTestAlignments[i]));
      CheckAlignment(ptr, kTestAlignments[i]);
      Fill(ptr, size);
      EXPECT_TRUE(Valid(ptr, size));

      // Make a second allocation of the same size and alignment to prevent
      // allocators from passing this test by accident.  Per jar, tcmalloc
      // provides allocations for new (never before seen) sizes out of a thread
      // local heap of a given "size class."  Each time the test requests a new
      // size, it will usually get the first element of a span, which is a
      // 4K aligned allocation.
      unsigned char* ptr2 = static_cast<unsigned char*>(
          _aligned_malloc(size, kTestAlignments[i]));
      CheckAlignment(ptr2, kTestAlignments[i]);
      Fill(ptr2, size);
      EXPECT_TRUE(Valid(ptr2, size));

      // Should never happen, but sanity check just in case.
      ASSERT_NE(ptr, ptr2);
      _aligned_free(ptr);
      _aligned_free(ptr2);
    }
  }
}

#endif