proxygen
JemallocNodumpAllocatorTest.cpp
Go to the documentation of this file.
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 
18 
19 #include <folly/io/IOBuf.h>
20 #include <folly/memory/Malloc.h>
22 
23 TEST(JemallocNodumpAllocatorTest, Basic) {
25 
26 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
27  if (folly::usingJEMalloc()) {
28  EXPECT_NE(0, jna.getArenaIndex());
29  }
30 #endif // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
31 
32  auto ptr = jna.allocate(1024);
33  EXPECT_NE(nullptr, ptr);
34  jna.deallocate(ptr);
35 }
36 
37 TEST(JemallocNodumpAllocatorTest, IOBuf) {
39 
40 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
41  if (folly::usingJEMalloc()) {
42  EXPECT_NE(0, jna.getArenaIndex());
43  }
44 #endif // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
45 
46  const size_t size{1024};
47  void* ptr = jna.allocate(size);
48  EXPECT_NE(nullptr, ptr);
50  EXPECT_EQ(size, ioBuf.capacity());
51  EXPECT_EQ(ptr, ioBuf.data());
52  uint8_t* data = ioBuf.writableData();
53  EXPECT_EQ(ptr, data);
54  for (auto i = 0u; i < ioBuf.capacity(); ++i) {
55  data[i] = 'A';
56  }
57  uint8_t* p = static_cast<uint8_t*>(ptr);
58  for (auto i = 0u; i < size; ++i) {
59  EXPECT_EQ('A', p[i]);
60  }
61 }
void * ptr
bool usingJEMalloc() noexcept
Definition: Malloc.h:147
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST(JemallocNodumpAllocatorTest, Basic)
const uint8_t * data() const
Definition: IOBuf.h:499
std::size_t capacity() const
Definition: IOBuf.h:593
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
uint8_t * writableData()
Definition: IOBuf.h:509
static void deallocate(void *p, void *userData)
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43