ソースを参照

Fixed some interface problems as well as some bugs

devel
Ludovic 'Archivist' Lagouardette 4年前
コミット
4b1aa7da73
4個のファイルの変更51行の追加10行の削除
  1. +2
    -2
      include/gp/algorithm/tmp_manip.hpp
  2. +8
    -6
      include/gp/allocator/arena.hpp
  3. +11
    -2
      include/gp/ring_list.hpp
  4. +30
    -0
      tests/gp_test.cpp

+ 2
- 2
include/gp/algorithm/tmp_manip.hpp ファイルの表示

@ -202,7 +202,7 @@ namespace gp{
typedef std::true_type yes;
typedef std::false_type no;
template<typename U, void* (U::*f)(size_t) const> struct SFINAE{};
template<typename U, void* (U::*f)(size_t)> struct SFINAE{};
template<class C> static yes test(SFINAE<C,&C::allocate>*);
@ -219,7 +219,7 @@ namespace gp{
typedef std::true_type yes;
typedef std::false_type no;
template<typename U, void (U::*f)(void*) const> struct SFINAE{};
template<typename U, bool (U::*f)(void*)> struct SFINAE{};
template<class C> static yes test(SFINAE<C,&C::deallocate>*);

+ 8
- 6
include/gp/allocator/arena.hpp ファイルの表示

@ -7,7 +7,7 @@
namespace gp{
template<typename page_allocator, size_t align = 1>
template<typename page_allocator = int, size_t align = 1>
class arena{
page_allocator allocator;
gp::buffer<char> data;
@ -20,18 +20,20 @@ namespace gp{
,data(gp::buffer<char>(nullptr,nullptr))
{}
template<typename T = typename std::enable_if<gp::has_allocator_interface<page_allocator>::value,int>::type>
arena(size_t sz)
:last(0)
,count(0)
,data(nullptr,nullptr)
{
if(sz!=0)
if constexpr (gp::has_allocator_interface<page_allocator>::value)
{
auto v=allocator.allocate(sz);
if(v!=nullptr)
if(sz!=0)
{
data=gp::buffer<char>(reinterpret_cast<char*>(v),reinterpret_cast<char*>(v)+sz);
auto v=allocator.allocate(sz);
if(v!=nullptr)
{
data=gp::buffer<char>(reinterpret_cast<char*>(v),reinterpret_cast<char*>(v)+sz);
}
}
}
}

+ 11
- 2
include/gp/ring_list.hpp ファイルの表示

@ -95,13 +95,22 @@ namespace gp {
, alloc{_alloc}
{}
ring_list(allocator& _alloc)
: any_node{nullptr}
, sz{0}
, alloc{_alloc}
{}
template<typename V = T, typename ...Args>
bool insert(Args&&... elem) {
allocator& used_allocator = alloc;
void* mem = used_allocator.allocate(sizeof(V));
void* mem;
[[unlikely]] if(
nullptr == (mem = used_allocator.allocate(sizeof(V)))
) return false;
T* p = new(mem) V(elem...);
node* to_insert = nullptr;
if(
na">[[unlikely]] if(
nullptr == (to_insert = reinterpret_cast<node*>(used_allocator.allocate(sizeof(node))))
) return false;
to_insert = new(to_insert) node(p);

+ 30
- 0
tests/gp_test.cpp ファイルの表示

@ -3,6 +3,7 @@
#include "gp/array.hpp"
#include "gp/indexed_array.hpp"
#include "gp/allocator/aggregator.hpp"
#include "gp/allocator/arena.hpp"
#include "gp/allocator/buddy.hpp"
#include "gp/allocator/dummy.hpp"
#include "gp/algorithm/repeat.hpp"
@ -414,6 +415,35 @@ struct aggregator_test : public test_scaffold {
}
auto duration = std::chrono::steady_clock::now() - start;
}
void* a = allocator.allocate(8);
gp_config::assertion(allocator.try_reallocate(a, 16) == false, "could reallocate? was it implemented?");
gp_config::assertion(allocator.deallocate(nullptr) == false, "error, could free an invalid pointer");
allocator.deallocate(a);
{
gp::ring_list<int, gp::aggregator, false> list{allocator};
list.insert(8);
list.insert(16);
list.insert(32);
}
{
gp::array<char, 256> work_array;
gp::arena<> alloc_work(work_array.begin().data, work_array.size());
gp::ring_list<int, gp::arena<>, false> list{alloc_work};
gp_config::assertion(list.insert(8) == true, "could allocate in list with good enough allocator");
gp_config::assertion(list.insert(8) == true, "could allocate in list with good enough allocator");
gp_config::assertion(list.insert(8) == true, "could allocate in list with good enough allocator");
}
{
gp::array<char, sizeof(int)> once_array;
gp::arena<> alloc_once(once_array.begin().data, once_array.size());
gp::ring_list<int, gp::arena<>, false> list{alloc_once};
gp_config::assertion(list.insert(8) == false, "could allocate in list with insufficient allocator");
}
{
gp::arena<> alloc_none(nullptr, 0);
gp::ring_list<int, gp::arena<>, false> list{alloc_none};
gp_config::assertion(list.insert(8) == false, "could allocate in list with fake allocator");
}
return res;
}
};

読み込み中…
キャンセル
保存