2 * Copyright 2016-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/experimental/JemallocNodumpAllocator.h>
19 #include <folly/io/IOBuf.h>
20 #include <folly/memory/Malloc.h>
21 #include <folly/portability/GTest.h>
23 TEST(JemallocNodumpAllocatorTest, Basic) {
24 folly::JemallocNodumpAllocator jna;
26 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
27 if (folly::usingJEMalloc()) {
28 EXPECT_NE(0, jna.getArenaIndex());
30 #endif // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
32 auto ptr = jna.allocate(1024);
33 EXPECT_NE(nullptr, ptr);
37 TEST(JemallocNodumpAllocatorTest, IOBuf) {
38 folly::JemallocNodumpAllocator jna;
40 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
41 if (folly::usingJEMalloc()) {
42 EXPECT_NE(0, jna.getArenaIndex());
44 #endif // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
46 const size_t size{1024};
47 void* ptr = jna.allocate(size);
48 EXPECT_NE(nullptr, ptr);
49 folly::IOBuf ioBuf(folly::IOBuf::TAKE_OWNERSHIP, ptr, size);
50 EXPECT_EQ(size, ioBuf.capacity());
51 EXPECT_EQ(ptr, ioBuf.data());
52 uint8_t* data = ioBuf.writableData();
54 for (auto i = 0u; i < ioBuf.capacity(); ++i) {
57 uint8_t* p = static_cast<uint8_t*> (ptr);
58 for (auto i = 0u; i < size; ++i) {