Skip to content

Commit

Permalink
non-constructing allocator/vector (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan authored Oct 7, 2024
1 parent 42ebd5c commit e133333
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 0 deletions.
48 changes: 48 additions & 0 deletions inc/mkn/kul/alloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cstdint>
#include <cstddef>
#include <cstdlib>
#include <type_traits>

namespace mkn::kul {

Expand Down Expand Up @@ -81,6 +82,53 @@ class AlignedAllocator {
}
};

template <typename T>
class Allocator {
using This = Allocator<T>;

public:
using pointer = T*;
using reference = T&;
using value_type = T;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;

template <typename U>
struct rebind {
using other = Allocator<U>;
};

T* allocate(std::size_t const n) const {
if (n == 0) return nullptr;
return static_cast<T*>(::operator new(n * sizeof(T)));
}

void deallocate(T* const p) noexcept {
if (p) ::operator delete(p);
}
void deallocate(T* const p, std::size_t /*n*/) noexcept { // needed from std::
if (p) ::operator delete(p);
}
bool operator!=(This const& that) const { return !(*this == that); }
bool operator==(This const& /*that*/) const {
return true; // stateless
}
};

template <typename T>
class NonConstructingAllocator : public Allocator<T> {
public:
template <typename U>
struct rebind {
using other = NonConstructingAllocator<U>;
};

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) {}
};

} // namespace mkn::kul

#endif /*_MKN_KUL_ALLOC_HPP_*/
77 changes: 77 additions & 0 deletions inc/mkn/kul/vector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
Copyright (c) 2022, Philip Deegan.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Philip Deegan nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _MKN_KUL_VECTOR_HPP_
#define _MKN_KUL_VECTOR_HPP_

#include "mkn/kul/alloc.hpp"

#include <vector>

namespace mkn::kul {

template <typename T, typename A = void> // A ignored but there for std::vector interop
using NonConstructingVector = std::vector<T, NonConstructingAllocator<T>>;

template <typename T>
std::vector<T, Allocator<T>>& as_super(std::vector<T, NonConstructingAllocator<T>>& v) {
return *reinterpret_cast<std::vector<T, Allocator<T>>*>(&v);
}
template <typename T>
std::vector<T, Allocator<T>> const& as_super(std::vector<T, NonConstructingAllocator<T>> const& v) {
return *reinterpret_cast<std::vector<T, Allocator<T>> const*>(&v);
}

} // namespace mkn::kul

template <typename T, typename A0, typename A1 = void>
bool operator==(std::vector<T, A0> const& v0, mkn::kul::NonConstructingVector<T, A1> const& v1) {
if (v0.size() != v1.size()) return false;
for (std::size_t i = 0; i < v0.size(); i++)
if (v0[i] != v1[i]) return false;
return true;
}

template <typename T, typename A0, typename A1 = void>
bool operator==(mkn::kul::NonConstructingVector<T, A1> const& v0, std::vector<T, A0> const& v1) {
return v1 == v0;
}

template <typename T, typename A0, typename A1 = void>
bool operator!=(std::vector<T, A0> const& v0, mkn::kul::NonConstructingVector<T, A1> const& v1) {
return !(v0 == v1);
}

template <typename T, typename A0, typename A1 = void>
bool operator!=(mkn::kul::NonConstructingVector<T, A1> const& v0, std::vector<T, A0> const& v1) {
return !(v0 == v1);
}

#endif /*_MKN_KUL_VECTOR_HPP_*/
98 changes: 98 additions & 0 deletions test/test/no_construct_alloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@


#include "test_common.hpp"

#include "mkn/kul/dbg.hpp"
#include "mkn/kul/alloc.hpp"
#include "mkn/kul/vector.hpp"

#include <array>
#include <cstddef>
#include <algorithm>
#include <stdexcept>

template <std::size_t size = 8>
struct S {
S() {
for (std::size_t i = 0; i < size; i++) vars[i] = i;
}

bool operator==(S const& that) const {
return std::equal(vars.begin(), vars.end(), that.vars.begin());
}

bool operator!=(S const& that) const { return !(*this == that); }

std::array<std::size_t, size> vars;
};

template <typename V>
auto copy_construct(V const& v) {
KUL_DBG_FUNC_ENTER;
V out{v};
return out;
}

template <typename V>
auto copy_operator_equal(V const& v) {
KUL_DBG_FUNC_ENTER;
V out;
out.reserve(v.capacity());
out = v;
return out;
}

template <typename V>
auto copy_operator_equal_super(V const& v) {
KUL_DBG_FUNC_ENTER;
V out;
out.reserve(v.capacity());
mkn::kul::as_super(out) = mkn::kul::as_super(v);
return out;
}

template <typename V>
auto copy_manual(V const& v) {
KUL_DBG_FUNC_ENTER;
V out;
out.reserve(v.capacity());
out.resize(v.size());
std::copy(v.begin(), v.end(), out.begin());
return out;
}

template <typename V>
auto make_vector(std::size_t const& size) {
KUL_DBG_FUNC_ENTER;
return V(size);
}
template <typename V0, typename V1>
auto make_vector_from(V1 const& v1) {
KUL_DBG_FUNC_ENTER;
V0 v(v1.size());
std::copy(v1.begin(), v1.end(), v.begin());
return v;
}

template <typename T>
void do_compare() {
constexpr static std::size_t N = 2000000;
auto const std_vec = make_vector<std::vector<T>>(N);
auto const std_vec2 = make_vector_from<std::vector<T>>(std_vec);
auto const no_construct_vec = make_vector_from<mkn::kul::NonConstructingVector<T>>(std_vec);
if (std_vec != no_construct_vec) throw std::runtime_error("FAIL");
if (std_vec != std_vec2) throw std::runtime_error("FAIL");
auto const v0 = copy_construct(std_vec);
auto const v1 = copy_construct(no_construct_vec);
auto const v2 = copy_manual(std_vec);
auto const v3 = copy_manual(no_construct_vec);
auto const v4 = copy_operator_equal_super(no_construct_vec);

if (v0 != std_vec) throw std::runtime_error("FAIL 0");
if (v0 == v1) throw std::runtime_error("FAIL 1"); // :(
if (v0 != v2) throw std::runtime_error("FAIL 2");
if (v0 != v3) throw std::runtime_error("FAIL 3");
if (v0 != v4) throw std::runtime_error("FAIL 4");
}

TEST(NoConstructAllocator, copies) { do_compare<S<8>>(); }

0 comments on commit e133333

Please sign in to comment.