move Arena, ThreadCachedArena, and Malloc to memory/
[folly.git] / folly / experimental / test / JemallocNodumpAllocatorTest.cpp
1 /*
2  * Copyright 2017 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
17 #include <folly/experimental/JemallocNodumpAllocator.h>
18
19 #include <folly/io/IOBuf.h>
20 #include <folly/memory/Malloc.h>
21 #include <folly/portability/GTest.h>
22
23 TEST(JemallocNodumpAllocatorTest, Basic) {
24   folly::JemallocNodumpAllocator jna;
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) {
38   folly::JemallocNodumpAllocator jna;
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);
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();
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 }