Skip to content

Commit

Permalink
non constructing allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Jul 11, 2024
1 parent 72aa34b commit c5e6bf0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
34 changes: 26 additions & 8 deletions inc/mkn/gpu/alloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef _MKN_GPU_ALLOC_HPP_
#define _MKN_GPU_ALLOC_HPP_

template <typename T, std::int32_t alignment = 32>
class ManagedAllocator {
using This = ManagedAllocator<T, alignment>;
template <typename T, std::int32_t alignment>
class MknGPUAllocator {
using This = MknGPUAllocator<T, alignment>;

public:
using pointer = T*;
Expand All @@ -42,11 +42,6 @@ class ManagedAllocator {
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;

template <typename U>
struct rebind {
using other = ManagedAllocator<U, alignment>;
};

T* allocate(std::size_t const n) const {
if (n == 0) return nullptr;

Expand All @@ -70,6 +65,29 @@ class ManagedAllocator {
}
};

template <typename T, std::int32_t alignment = 32>
class NoConstructAllocator : public MknGPUAllocator<T, alignment> {
public:
template <typename U>
struct rebind {
using other = NoConstructAllocator<U, alignment>;
};

template <typename U, typename... Args>
void construct(U* /*ptr*/, Args&&... /*args*/) {} // nothing
template <typename U>
void construct(U* /*ptr*/) noexcept(std::is_nothrow_default_constructible<U>::value) {}
};

template <typename T, std::int32_t alignment = 32>
class ManagedAllocator : public MknGPUAllocator<T, alignment> {
public:
template <typename U>
struct rebind {
using other = ManagedAllocator<U, alignment>;
};
};

template <typename T, typename Size>
void copy(T* const dst, T const* const src, Size size) {
assert(dst and src);
Expand Down
35 changes: 35 additions & 0 deletions test/any/construct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#include "mkn/gpu.hpp"

static constexpr uint32_t NUM = 10;

template <typename T>
using ManagedVector = std::vector<T, mkn::gpu::ManagedAllocator<T>>;

template <typename T>
using ManagedMemory = std::vector<T, mkn::gpu::NoConstructAllocator<T>>;

std::size_t alloced = 0;
struct S {
S() { ++alloced; }
};

std::uint32_t test_does_construct_on_resize() {
alloced = 0;
ManagedVector<S> mem{NUM};
mem.resize(NUM + NUM);
return alloced != NUM + NUM;
}

std::uint32_t test_does_not_construct_on_resize() {
alloced = 0;
ManagedMemory<S> mem{NUM};
mem.resize(NUM + NUM);
return alloced != 0;
}

int main() {
KOUT(NON) << __FILE__;

return test_does_construct_on_resize() + test_does_not_construct_on_resize();
}

0 comments on commit c5e6bf0

Please sign in to comment.