-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[util] Changed my mind on allocator, switch to a polymorphic strategy
fix: Change MemoryResource to be polymorphic add: Allocator for adapting to std::allocator interface fix: massively reduce dependencies.
- Loading branch information
Showing
7 changed files
with
139 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "Allocator.hpp" | ||
#include "memory.hpp" | ||
|
||
namespace nw { | ||
namespace detail { | ||
|
||
MemoryResourceInternal::MemoryResourceInternal(MemoryResource* resource) | ||
: resource_{resource} | ||
{ | ||
} | ||
|
||
void* MemoryResourceInternal::allocate(size_t bytes, size_t alignment) | ||
{ | ||
return resource_->allocate(bytes, alignment); | ||
} | ||
|
||
void MemoryResourceInternal::deallocate(void* p, size_t bytes, size_t alignment) | ||
{ | ||
resource_->deallocate(p, bytes, alignment); | ||
} | ||
|
||
} // namespace detail | ||
} // namespace nw |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#pragma once | ||
|
||
#include <stddef.h> | ||
|
||
namespace nw { | ||
|
||
struct MemoryResource; | ||
|
||
namespace detail { | ||
// Hide MemoryResource impl details | ||
struct MemoryResourceInternal { | ||
MemoryResourceInternal(MemoryResource* resoure); | ||
void* allocate(size_t nbytes, size_t alignment = alignof(max_align_t)); | ||
void deallocate(void* p, size_t bytes, size_t alignment = alignof(max_align_t)); | ||
MemoryResource* resource_; | ||
}; | ||
|
||
} | ||
|
||
/// Allocator adapter for MemoryResource, so this is a polymorphic allocator. | ||
template <typename T> | ||
class Allocator { | ||
public: | ||
using value_type = T; | ||
|
||
Allocator(MemoryResource* resource) | ||
: resource_(resource) | ||
{ | ||
} | ||
|
||
template <typename U> | ||
Allocator(const Allocator<U>& other) noexcept | ||
: resource_(other.resource_) | ||
{ | ||
} | ||
|
||
T* allocate(size_t n) | ||
{ | ||
if (n == 0) { return nullptr; } | ||
return static_cast<T*>(resource_.allocate(n * sizeof(T), alignof(T))); | ||
} | ||
|
||
void deallocate(T* ptr, size_t size) noexcept | ||
{ | ||
resource_.deallocate(ptr, size); | ||
} | ||
|
||
template <typename U> | ||
struct rebind { | ||
using other = Allocator<U>; | ||
}; | ||
|
||
// private: | ||
detail::MemoryResourceInternal resource_; | ||
}; | ||
|
||
template <typename T, typename U> | ||
bool operator==(const Allocator<T>& a, const Allocator<U>& b) noexcept | ||
{ | ||
return &a.resource_.resource_ == &b.resource_.resource_; | ||
} | ||
|
||
template <typename T, typename U> | ||
bool operator!=(const Allocator<T>& a, const Allocator<U>& b) noexcept | ||
{ | ||
return !(a == b); | ||
} | ||
} // namespace nw |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.