OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FastAllocator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2023 HEAVY.AI, 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 
23 #pragma once
24 
25 #include "Shared/SimpleAllocator.h"
26 
27 #include <algorithm>
28 #include <exception>
29 #include <mutex>
30 #include <sstream>
31 
32 namespace heavyai {
33 namespace allocator {
34 namespace detail {
35 
36 inline std::runtime_error outOfMemoryError(size_t n, size_t remaining, size_t capacity) {
37  std::ostringstream oss;
38  oss << "allocate(" << n << ") called but only " << remaining << " out of " << capacity
39  << " available.";
40  return std::runtime_error(oss.str());
41 }
42 
43 // FastAllocator accepts a pre-allocated buffer of given capacity and
44 // allocates sequential chunks, tracking the size_ starting at size_=0.
45 // If size_ exceeds capacity_ then throw an exception.
46 // There is no deallocate() function, nor is there a destructor.
47 template <typename T>
49  public:
50  FastAllocator() : buffer_(nullptr), capacity_(0), size_(0) {}
51  FastAllocator(T* buffer, size_t capacity)
52  : buffer_(buffer), capacity_(capacity), size_(0) {}
53  FastAllocator(FastAllocator const&) = delete;
54  FastAllocator(FastAllocator&&) = delete;
55  FastAllocator& operator=(FastAllocator const&) = delete;
57  buffer_ = rhs.buffer_;
58  capacity_ = rhs.capacity_;
59  size_ = rhs.size_;
60  rhs.reset();
61  return *this;
62  }
63 
64  // Allocate n>0 elements of type T. Caller responsible for proper data alignment.
65  T* allocate(size_t const n) {
66  CHECK(n);
67  std::lock_guard<std::mutex> lock_guard(mutex_);
68  if (n <= available()) {
69  T* const ptr = buffer_ + size_;
70  size_ += n;
71  return ptr;
72  }
74  }
75 
76  size_t available() const { return capacity_ - size_; } // number of available elements
77  size_t capacity() const { return capacity_; } // number of reserved elements
78 
79  protected:
80  void reset() {
81  buffer_ = nullptr;
82  capacity_ = 0u;
83  size_ = 0u;
84  }
85 
86  T* buffer_; // Pointer to reserved buffer.
87  size_t capacity_; // Number of elements of type T reserved.
88  size_t size_; // Number of elements of type T allocated.
89  mutable std::mutex mutex_;
90 };
91 
92 } // namespace detail
93 } // namespace allocator
94 } // namespace heavyai
95 
std::lock_guard< T > lock_guard
FastAllocator & operator=(FastAllocator &&rhs)
Definition: FastAllocator.h:56
#define CHECK(condition)
Definition: Logger.h:291
constexpr double n
Definition: Utm.h:38
FastAllocator & operator=(FastAllocator const &)=delete
std::runtime_error outOfMemoryError(size_t n, size_t remaining, size_t capacity)
Definition: FastAllocator.h:36
FastAllocator(T *buffer, size_t capacity)
Definition: FastAllocator.h:51