OmniSciDB  2e3a973ef4
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) { return checked_malloc(count); }
36 
37  void deallocate(T* p, size_t /* count */) { free(p); }
38 
39  friend bool operator==(Self const&, Self const&) noexcept { return true; }
40  friend bool operator!=(Self const&, Self const&) noexcept { return false; }
41 };
42 
43 #ifdef HAVE_FOLLY
44 
45 #include <folly/Memory.h>
46 #include <folly/memory/Arena.h>
47 
48 constexpr size_t kArenaBlockOverhead = folly::Arena<::SysAllocator<void>>::kBlockOverhead;
49 
54 class Arena : public folly::Arena<::SysAllocator<void>> {
55  public:
56  explicit Arena(size_t min_block_size = static_cast<size_t>(1UL << 32) + kBlockOverhead,
57  size_t size_limit = kNoSizeLimit,
58  size_t max_align = kDefaultMaxAlign)
59  : folly::Arena<SysAllocator<void>>({}, min_block_size, size_limit, max_align) {}
60 
61  void* allocateAndZero(const size_t size) {
62  auto ret = allocate(size);
63  std::memset(ret, 0, size);
64  return ret;
65  }
66 };
67 
68 template <>
69 struct folly::ArenaAllocatorTraits<::SysAllocator<void>> {
70  static size_t goodSize(const ::SysAllocator<void>& /* alloc */, size_t size) {
71  return folly::goodMallocSize(size);
72  }
73 };
74 
75 #else
76 
77 constexpr size_t kArenaBlockOverhead = 0;
78 
84 class Arena {
85  public:
86  explicit Arena(size_t min_block_size = 1UL << 32, size_t size_limit = 0) {}
87 
88  ~Arena() {
89  for (auto ptr : allocations_) {
90  allocator_.deallocate(ptr, 0);
91  }
92  }
93 
94  void* allocate(size_t num_bytes) {
95  auto ret = allocator_.allocate(num_bytes);
96  allocations_.push_back(ret);
97  return ret;
98  }
99 
100  void* allocateAndZero(const size_t size) {
101  auto ret = allocate(size);
102  std::memset(ret, 0, size);
103  return ret;
104  }
105 
106  private:
108  std::vector<void*> allocations_;
109 };
110 
111 #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:44
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