TestJemalloc.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/mozalloc.h"
  6. #include "mozmemory.h"
  7. #include "gtest/gtest.h"
  8. static inline void
  9. TestOne(size_t size)
  10. {
  11. size_t req = size;
  12. size_t adv = malloc_good_size(req);
  13. char* p = (char*)malloc(req);
  14. size_t usable = moz_malloc_usable_size(p);
  15. // NB: Using EXPECT here so that we still free the memory on failure.
  16. EXPECT_EQ(adv, usable) <<
  17. "malloc_good_size(" << req << ") --> " << adv << "; "
  18. "malloc_usable_size(" << req << ") --> " << usable;
  19. free(p);
  20. }
  21. static inline void
  22. TestThree(size_t size)
  23. {
  24. ASSERT_NO_FATAL_FAILURE(TestOne(size - 1));
  25. ASSERT_NO_FATAL_FAILURE(TestOne(size));
  26. ASSERT_NO_FATAL_FAILURE(TestOne(size + 1));
  27. }
  28. TEST(Jemalloc, UsableSizeInAdvance)
  29. {
  30. #define K * 1024
  31. #define M * 1024 * 1024
  32. /*
  33. * Test every size up to a certain point, then (N-1, N, N+1) triplets for a
  34. * various sizes beyond that.
  35. */
  36. for (size_t n = 0; n < 16 K; n++)
  37. ASSERT_NO_FATAL_FAILURE(TestOne(n));
  38. for (size_t n = 16 K; n < 1 M; n += 4 K)
  39. ASSERT_NO_FATAL_FAILURE(TestThree(n));
  40. for (size_t n = 1 M; n < 8 M; n += 128 K)
  41. ASSERT_NO_FATAL_FAILURE(TestThree(n));
  42. }