JemallocNodumpAllocatorTest.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/experimental/JemallocNodumpAllocator.h>
  17. #include <folly/io/IOBuf.h>
  18. #include <folly/memory/Malloc.h>
  19. #include <folly/portability/GTest.h>
  20. TEST(JemallocNodumpAllocatorTest, Basic) {
  21. folly::JemallocNodumpAllocator jna;
  22. #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
  23. if (folly::usingJEMalloc()) {
  24. EXPECT_NE(0, jna.getArenaIndex());
  25. }
  26. #endif // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
  27. auto ptr = jna.allocate(1024);
  28. EXPECT_NE(nullptr, ptr);
  29. jna.deallocate(ptr);
  30. }
  31. TEST(JemallocNodumpAllocatorTest, IOBuf) {
  32. folly::JemallocNodumpAllocator jna;
  33. #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
  34. if (folly::usingJEMalloc()) {
  35. EXPECT_NE(0, jna.getArenaIndex());
  36. }
  37. #endif // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
  38. const size_t size{1024};
  39. void* ptr = jna.allocate(size);
  40. EXPECT_NE(nullptr, ptr);
  41. folly::IOBuf ioBuf(folly::IOBuf::TAKE_OWNERSHIP, ptr, size);
  42. EXPECT_EQ(size, ioBuf.capacity());
  43. EXPECT_EQ(ptr, ioBuf.data());
  44. uint8_t* data = ioBuf.writableData();
  45. EXPECT_EQ(ptr, data);
  46. for (auto i = 0u; i < ioBuf.capacity(); ++i) {
  47. data[i] = 'A';
  48. }
  49. uint8_t* p = static_cast<uint8_t*>(ptr);
  50. for (auto i = 0u; i < size; ++i) {
  51. EXPECT_EQ('A', p[i]);
  52. }
  53. }