OmniSciDB  a667adc9c8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ArenaAllocator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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 #pragma once
18 
19 #include "DataMgr/DataMgr.h"
20 #include "Shared/checked_alloc.h"
21 
22 template <class T>
23 class SysAllocator {
24  public:
25  using Self = SysAllocator;
26  using value_type = T;
27 
28  constexpr SysAllocator() = default;
29 
30  constexpr SysAllocator(SysAllocator const&) = default;
31 
32  template <class U>
33  constexpr SysAllocator(const SysAllocator<U>&) noexcept {}
34 
35  [[nodiscard]] T* allocate(size_t count) {
36  return reinterpret_cast<T*>(checked_malloc(count));
37  }
38 
39  void deallocate(T* p, size_t /* count */) { free(p); }
40 
41  friend bool operator==(Self const&, Self const&) noexcept { return true; }
42  friend bool operator!=(Self const&, Self const&) noexcept { return false; }
43 };
44 
45 #ifdef HAVE_FOLLY
46 
47 #include <folly/Memory.h>
48 #include <folly/memory/Arena.h>
49 
50 using AllocatorType = char;
51 
52 constexpr size_t kArenaBlockOverhead =
53  folly::Arena< ::SysAllocator<AllocatorType> >::kBlockOverhead;
54 
59 class Arena : public folly::Arena< ::SysAllocator<AllocatorType> > {
60  public:
61  explicit Arena(size_t min_block_size = static_cast<size_t>(1UL << 32) + kBlockOverhead,
62  size_t size_limit = kNoSizeLimit,
63  size_t max_align = kDefaultMaxAlign)
64  : folly::Arena<SysAllocator<AllocatorType> >({},
65  min_block_size,
66  size_limit,
67  max_align) {}
68 
69  void* allocateAndZero(const size_t size) {
70  auto ret = allocate(size);
71  std::memset(ret, 0, size);
72  return ret;
73  }
74 };
75 
76 template <>
77 struct folly::ArenaAllocatorTraits< ::SysAllocator<AllocatorType> > {
78  static size_t goodSize(const ::SysAllocator<AllocatorType>& /* alloc */, size_t size) {
79  return folly::goodMallocSize(size);
80  }
81 };
82 
83 #else
84 
85 constexpr size_t kArenaBlockOverhead = 0;
86 
92 class Arena {
93  public:
94  explicit Arena(size_t min_block_size = 1UL << 32, size_t size_limit = 0) {}
95 
96  ~Arena() {
97  for (auto ptr : allocations_) {
98  allocator_.deallocate(ptr, 0);
99  }
100  }
101 
102  void* allocate(size_t num_bytes) {
103  auto ret = allocator_.allocate(num_bytes);
104  allocations_.push_back(ret);
105  return ret;
106  }
107 
108  void* allocateAndZero(const size_t size) {
109  auto ret = allocate(size);
110  std::memset(ret, 0, size);
111  return ret;
112  }
113 
114  private:
116  std::vector<void*> allocations_;
117 };
118 
119 #endif
Arena(size_t min_block_size=1UL<< 32, size_t size_limit=0)
constexpr size_t kArenaBlockOverhead
friend bool operator!=(Self const &, Self const &) noexcept
std::vector< void * > allocations_
constexpr SysAllocator()=default
T * allocate(size_t count)
void deallocate(T *p, size_t)
void * checked_malloc(const size_t size)
Definition: checked_alloc.h:45
int count
SysAllocator< void > allocator_
constexpr SysAllocator(const SysAllocator< U > &) noexcept
void * allocateAndZero(const size_t size)
void * allocate(size_t num_bytes)
friend bool operator==(Self const &, Self const &) noexcept