proxygen
JemallocHugePageAllocator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018-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 
17 // http://www.canonware.com/download/jemalloc/jemalloc-latest/doc/jemalloc.html
18 
19 #pragma once
20 
21 #include <folly/CPortability.h>
22 #include <folly/memory/Malloc.h>
26 
27 #include <cstddef>
28 #include <cstdint>
29 
30 namespace folly {
31 
58  public:
59  static bool init(int nr_pages);
60 
61  static void* allocate(size_t size) {
62  // If uninitialized, flags_ will be 0 and the mallocx behavior
63  // will match that of a regular malloc
64  return hugePagesSupported ? mallocx(size, flags_) : malloc(size);
65  }
66 
67  static void* reallocate(void* p, size_t size) {
68  return hugePagesSupported ? rallocx(p, size, flags_) : realloc(p, size);
69  }
70 
71  static void deallocate(void* p, size_t = 0) {
73  }
74 
75  static bool initialized() {
76  return flags_ != 0;
77  }
78 
79  static size_t freeSpace();
80  static bool addressInArena(void* address);
81 
82  private:
83  static int flags_;
84  static bool hugePagesSupported;
85 };
86 
87 // STL compatible huge page allocator, for use with STL-style containers
88 template <typename T>
90  private:
92 
93  public:
94  using value_type = T;
95 
97 
98  template <typename U>
100 
101  T* allocate(std::size_t n) {
102  return static_cast<T*>(JemallocHugePageAllocator::allocate(sizeof(T) * n));
103  }
104  void deallocate(T* p, std::size_t n) {
106  }
107 
108  friend bool operator==(Self const&, Self const&) noexcept {
109  return true;
110  }
111  friend bool operator!=(Self const&, Self const&) noexcept {
112  return false;
113  }
114 };
115 
116 } // namespace folly
CxxHugePageAllocator(CxxHugePageAllocator< U > const &)
void(* dallocx)(void *, int)
Definition: MallocImpl.cpp:39
void deallocate(T *p, std::size_t n)
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
static void deallocate(void *p, size_t=0)
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
friend bool operator==(Self const &, Self const &) noexcept
void free()
void *(* mallocx)(size_t, int)
Definition: MallocImpl.cpp:35
static void * reallocate(void *p, size_t size)
void *(* rallocx)(void *, size_t, int)
Definition: MallocImpl.cpp:36
friend bool operator!=(Self const &, Self const &) noexcept