SuperDex Physics C++ API
Loading...
Searching...
No Matches
stl_allocator.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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
20
21#include <cstddef>
22
23namespace superdex {
24
25// An STL-compatible allocator template using a polymorphic superdex::Allocator pointer.
26template <typename T>
28 public:
29 using value_type = T;
30
31 StlAllocator() = default;
32
33 // This constructor is not "explicit" so that you can pass a pointer to a superdex::Allocator to the
34 // STL container's constructor (not a pointer to a superdex::StlAllocator wrapper).
35 StlAllocator(Allocator* alloc) : _allocator(alloc) {}
36
37 template <typename U>
38 StlAllocator(StlAllocator<U> const& other) : _allocator(other.get_allocator()) {}
39
40 T* allocate(std::size_t n) {
41 return static_cast<T*>(_allocator->allocate(n * sizeof(T), alignof(T)));
42 }
43
44 void deallocate(T* ptr, std::size_t n) {
45 _allocator->deallocate(ptr, n * sizeof(T), alignof(T));
46 }
47
49 return _allocator;
50 }
51
52 friend bool operator==(StlAllocator const& a, StlAllocator const& b) {
53 return a._allocator->is_equal(*b._allocator);
54 }
55
56 friend bool operator!=(StlAllocator const& a, StlAllocator const& b) {
57 return !(a == b);
58 }
59
60 private:
61 Allocator* _allocator = GetDefaultAllocator();
62};
63
64} // namespace superdex
Virtual interface class for memory allocation and deallocation.
Definition allocator.h:47
bool is_equal(Allocator const &other) const noexcept
Return true if memory allocated by this Allocator instance can be deallocated by the other Allocator ...
Allocator * get_allocator() const
StlAllocator(Allocator *alloc)
friend bool operator!=(StlAllocator const &a, StlAllocator const &b)
friend bool operator==(StlAllocator const &a, StlAllocator const &b)
void deallocate(T *ptr, std::size_t n)
StlAllocator(StlAllocator< U > const &other)
T * allocate(std::size_t n)
Allocator * GetDefaultAllocator()
Get an instance of the DefaultAllocator class.