Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
allocator.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #ifndef ARROW_ALLOCATOR_H
19 #define ARROW_ALLOCATOR_H
20 
21 #include <cstddef>
22 #include <memory>
23 #include <utility>
24 
25 #include "arrow/memory_pool.h"
26 #include "arrow/status.h"
27 #include "arrow/util/macros.h"
28 
29 namespace arrow {
30 
31 template <class T>
33  public:
34  using value_type = T;
35  using pointer = T*;
36  using const_pointer = const T*;
37  using reference = T&;
38  using const_reference = const T&;
39  using size_type = std::size_t;
40  using difference_type = std::ptrdiff_t;
41 
42  template <class U>
43  struct rebind {
45  };
46 
47  stl_allocator() noexcept : pool_(default_memory_pool()) {}
48  explicit stl_allocator(MemoryPool* pool) noexcept : pool_(pool) {}
49 
50  template <class U>
51  stl_allocator(const stl_allocator<U>& rhs) noexcept : pool_(rhs.pool_) {}
52 
53  ~stl_allocator() { pool_ = NULLPTR; }
54 
55  pointer address(reference r) const noexcept { return std::addressof(r); }
56 
57  const_pointer address(const_reference r) const noexcept { return std::addressof(r); }
58 
59  pointer allocate(size_type n, const void* /*hint*/ = NULLPTR) {
60  uint8_t* data;
61  Status s = pool_->Allocate(n * sizeof(T), &data);
62  if (!s.ok()) throw std::bad_alloc();
63  return reinterpret_cast<pointer>(data);
64  }
65 
67  pool_->Free(reinterpret_cast<uint8_t*>(p), n * sizeof(T));
68  }
69 
70  size_type size_max() const noexcept { return size_type(-1) / sizeof(T); }
71 
72  template <class U, class... Args>
73  void construct(U* p, Args&&... args) {
74  new (reinterpret_cast<void*>(p)) U(std::forward<Args>(args)...);
75  }
76 
77  template <class U>
78  void destroy(U* p) {
79  p->~U();
80  }
81 
82  MemoryPool* pool() const noexcept { return pool_; }
83 
84  private:
85  MemoryPool* pool_;
86 };
87 
88 template <class T1, class T2>
89 bool operator==(const stl_allocator<T1>& lhs, const stl_allocator<T2>& rhs) noexcept {
90  return lhs.pool() == rhs.pool();
91 }
92 
93 template <class T1, class T2>
94 bool operator!=(const stl_allocator<T1>& lhs, const stl_allocator<T2>& rhs) noexcept {
95  return !(lhs == rhs);
96 }
97 
98 } // namespace arrow
99 
100 #endif // ARROW_ALLOCATOR_H
void destroy(U *p)
Definition: allocator.h:78
const T * const_pointer
Definition: allocator.h:36
const_pointer address(const_reference r) const noexcept
Definition: allocator.h:57
T value_type
Definition: allocator.h:34
#define NULLPTR
Definition: macros.h:69
T * pointer
Definition: allocator.h:35
std::size_t size_type
Definition: allocator.h:39
Definition: status.h:106
bool ok() const
Definition: status.h:167
stl_allocator(const stl_allocator< U > &rhs) noexcept
Definition: allocator.h:51
MemoryPool * default_memory_pool()
bool operator!=(const stl_allocator< T1 > &lhs, const stl_allocator< T2 > &rhs) noexcept
Definition: allocator.h:94
Definition: allocator.h:43
pointer allocate(size_type n, const void *=NULLPTR)
Definition: allocator.h:59
size_type size_max() const noexcept
Definition: allocator.h:70
Top-level namespace for Apache Arrow C++ API.
Definition: allocator.h:29
MemoryPool * pool() const noexcept
Definition: allocator.h:82
Definition: allocator.h:32
~stl_allocator()
Definition: allocator.h:53
T & reference
Definition: allocator.h:37
const T & const_reference
Definition: allocator.h:38
void deallocate(pointer p, size_type n)
Definition: allocator.h:66
bool operator==(const stl_allocator< T1 > &lhs, const stl_allocator< T2 > &rhs) noexcept
Definition: allocator.h:89
std::ptrdiff_t difference_type
Definition: allocator.h:40
pointer address(reference r) const noexcept
Definition: allocator.h:55
Base class for memory allocation.
Definition: memory_pool.h:34
stl_allocator(MemoryPool *pool) noexcept
Definition: allocator.h:48
void construct(U *p, Args &&... args)
Definition: allocator.h:73
stl_allocator() noexcept
Definition: allocator.h:47