Bläddra i källkod

Bunch of fixes and progress on cbor

cbor
Ludovic 'Archivist' Lagouardette 4 år sedan
förälder
incheckning
61741f6506
10 ändrade filer med 134 tillägg och 73 borttagningar
  1. +1
    -1
      Makefile
  2. +25
    -3
      include/gp/algorithm/move.hpp
  3. +16
    -6
      include/gp/enveloppe/cbor.hpp
  4. +27
    -28
      include/gp/function.hpp
  5. +6
    -0
      include/gp/optional.hpp
  6. +46
    -18
      include/gp/variant.hpp
  7. +6
    -6
      include/gp/vector.hpp
  8. +3
    -3
      tests.cpp
  9. +2
    -2
      tests/cbor_test.cpp
  10. +2
    -6
      tests/math.cpp

+ 1
- 1
Makefile Visa fil

@ -1,6 +1,6 @@
CXX= clang++-10
CXXFLAGS= --std=c++20 -O0 -g -pthread -DGP_TESTS -DFUZZ_STRENGTH=500 -DNO_BENCH=0 -pedantic \
-frtti -fprofile-instr-generate -fcoverage-mapping -Wno-unknown-attributes \
-fprofile-instr-generate -fcoverage-mapping -Wno-unknown-attributes \
-fsanitize=address -fno-omit-frame-pointer
all: tests

+ 25
- 3
include/gp/algorithm/move.hpp Visa fil

@ -28,9 +28,31 @@ namespace gp{
T& rhs
)
{
auto tmp = lhs;
lhs = rhs;
rhs = tmp;
auto tmp = gp::move(lhs);
lhs = gp::move(rhs);
rhs = gp::move(tmp);
}
template<typename T>
constexpr void swap(
T&& lhs,
T& rhs
)
{
auto tmp = gp::move(lhs);
lhs = gp::move(rhs);
rhs = gp::move(tmp);
}
template<typename T>
constexpr void swap(
T& lhs,
T&& rhs
)
{
auto tmp = gp::move(lhs);
lhs = gp::move(rhs);
rhs = gp::move(tmp);
}
template<typename T>

+ 16
- 6
include/gp/enveloppe/cbor.hpp Visa fil

@ -76,13 +76,13 @@ namespace gp {
template<typename T>
using cbor_composite = gp::fixed_variant<
cbor_number,
n">gp::buffer<std::byte>,
kt">bool,
n">gp::vector<T>,
c1">//gp::buffer<std::byte>,
c1">//bool,
c1">//gp::vector<T>,
gp::vector<gp::pair<T, T>>,
n">gp::nullopt_t,
undefined_tp">,
n">cbor_floating_point
c1">//gp::nullopt_t,
undefined_tc1">//,
c1">//cbor_floating_point
>;
class cbor_value {
@ -101,6 +101,16 @@ namespace gp {
, alloc(alloc_v)
{}
cbor_value(cbor_value& oth)
: contents(oth.contents)
, alloc(oth.alloc)
{}
cbor_value(cbor_value&& oth)
: contents(gp::move(oth.contents))
, alloc(gp::move(oth.alloc))
{}
cbor_value& operator=(cbor_value& value) {
contents = value.contents;
alloc = value.alloc;

+ 27
- 28
include/gp/function.hpp Visa fil

@ -74,43 +74,34 @@ namespace gp{
, condestructor(reinterpret_cast<condestruct_fn_t>(condestruct<func>))
, data_ptr(
sizeof(func) <= sizeof(data_ptr)
? mi">0
? k">nullptr
: details::ensure(
[&](){return alloc.has_value();},
[&](){return (char*)(alloc.value().get().allocate(sizeof(func)));}
)
)
, data_size(
sizeof(func) <= sizeof(data_ptr)
? 0
: sizeof(func)
sizeof(func)
)
{
gp_config::assertion(!(alloc.has_value() && data_ptr == nullptr), "allocator failed in function");
if(data_ptr != nullptr) this->condestructor(data_size < sizeof(data_ptr) ? (char*)&data_ptr : data_ptr, reinterpret_cast<char*>(&f));
gp_config::assertion(n">data_size <= sizeof(func) || !(alloc.has_value() && data_ptr == nullptr), "allocator failed in function");
this->condestructor(data_size <= sizeof(data_ptr) ? (char*)&data_ptr : data_ptr, reinterpret_cast<char*>(&f));
}
template<typename func>
function(func f, nullopt_t alloc_v = nullopt)
function(func f, nullopt_t alloc_v)
: alloc(alloc_v)
, invokator(reinterpret_cast<invoke_fn_t>(invoke<func>))
, condestructor(reinterpret_cast<condestruct_fn_t>(condestruct<func>))
, data_ptr(
sizeof(func) <= sizeof(data_ptr)
? 0
: details::ensure(
[&](){return alloc.has_value();},
[&](){return (char*)(alloc.value().get().allocate(sizeof(func)));}
)
)
, data_ptr()
, data_size(
sizeof(func) <= sizeof(data_ptr)
? 0
: sizeof(func)
details::ensure(
[&](){return sizeof(func) <= sizeof(data_ptr);},
[&](){return sizeof(func);}
)
)
{
gp_config::assertion(!(alloc.has_value() && data_ptr == nullptr), "allocator failed in function");
if(data_ptr != nullptr) this->condestructor(data_size < sizeof(data_ptr) ? (char*)&data_ptr : data_ptr, reinterpret_cast<char*>(&f));
this->condestructor((char*)&data_ptr, reinterpret_cast<char*>(&f));
}
function(function const& rhs)
@ -127,25 +118,29 @@ namespace gp{
)
, data_size(rhs.data_size)
{
gp_config::assertion(!(alloc.has_value() && data_ptr == nullptr), "allocator failed in function");
gp_config::assertion(n">data_size <= sizeof(data_ptr) || !(alloc.has_value() && data_ptr == nullptr), "allocator failed in function");
if(
data_ptr != nullptr
and rhs.data_ptr != nullptr
) this->condestructor(data_size <= sizeof(data_ptr) ? (char*)&data_ptr : data_ptr, rhs.data_ptr);
data_size != 0
and rhs.data_size != 0
) this->condestructor(
data_size <= sizeof(data_ptr) ? (char*)&data_ptr : data_ptr,
data_size <= sizeof(data_ptr) ? (char*)&(rhs.data_ptr) : rhs.data_ptr
);
}
function(function&& rhs)
: alloc(rhs.alloc)
, invokator(rhs.invokator)
, condestructor(rhs.condestructor)
, data_ptr(data_size <= sizeof(data_ptr) ? mi">0 : rhs.data_ptr)
, data_ptr(rhs.data_size <= sizeof(data_ptr) ? k">nullptr : rhs.data_ptr)
, data_size(rhs.data_size)
{
if(data_size <= sizeof(data_ptr) && data_size != 0 && data_ptr != nullptr && alloc.has_value()) {
if(data_size != 0 && data_size <= sizeof(data_ptr)) {
condestructor((char*)&data_ptr, (char*)&rhs.data_ptr);
condestructor(nullptr, (char*)&rhs.data_ptr);
}
rhs.data_ptr = nullptr;
rhs.data_size = 0;
}
~function(){
@ -179,7 +174,7 @@ namespace gp{
alloc = rhs.alloc;
invokator = rhs.invokator;
condestructor = rhs.condestructor;
condestructor(nullptr, data_ptr);
k">if(data_size) condestructor(nullptr, data_ptr);
alloc.value().get().deallocate(data_ptr);
data_ptr = rhs.data_size <= sizeof(data_ptr)
? 0
@ -198,7 +193,11 @@ namespace gp{
}
ret operator()(args&&... argv) {
return invokator(data_ptr, gp::forward<args>(argv)...);
return invokator(data_size <= sizeof(data_ptr) ? (fn_ptr)&data_ptr : data_ptr, gp::forward<args>(argv)...);
}
bool ready() {
return invokator != nullptr;
}
};

+ 6
- 0
include/gp/optional.hpp Visa fil

@ -39,6 +39,12 @@ namespace gp{
new(buffer) T(gp::move(value));
}
~optional() {
if(ready) {
((T*)buffer)->~T();
}
}
optional& operator=(nullopt_t) {
if(ready) {
((T*)buffer)->~T();

+ 46
- 18
include/gp/variant.hpp Visa fil

@ -2,6 +2,7 @@
#include "gp_config.hpp"
#include <gp/algorithm/move.hpp>
#include <gp/algorithm/tmp_manip.hpp>
#include "gp/allocator/dummy.hpp"
#include "gp/exception.hpp"
@ -18,19 +19,26 @@ namespace gp{
class fixed_variant final {
std::size_t index = std::numeric_limits<std::size_t>::max();
char buffer[max_size<T...>()];
gp::function<void(void*, void*)> cpytor = [](void*, void*){};
gp::function<void(void*, void*)> mvtor = [](void*, void*){};
gp::function<void(void*)> dtor = [](void*){};
gp::function<void(void*, void*)> cpytor = {[](void*, void*){}, nullopt};
gp::function<void(void*, void*)> mvtor = {[](void*, void*){}, nullopt};
gp::function<void(void*)> dtor = {[](void*){}, nullopt};
static_assert(all_of_fixed_size<T...>::value, "not fixed");
void buffswap(fixed_variant& oth) {
char tmp[max_size<T...>()];
mvtor(tmp, buffer);
oth.mvtor(buffer, oth.buffer);
mvtor(oth.buffer, tmp);
}
public:
template<typename U>//, typename std::enable_if<list_contains_class<U,T...>::value,int>::type>
constexpr fixed_variant(U& value)
: index{r_index_of<U, T...>::value}
{
new(buffer) U(value);
dtor = gp::function<void(void*)>([](void* thing){((U*)thing)->~U();});
cpytor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(*(U*)src);});
mvtor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(gp::move(*(U*)src));});
dtor = gp::function<void(void*)>([](void* thing){((U*)thing)->~U();}, nullopt);
cpytor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(*(U*)src);}, nullopt);
mvtor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(gp::move(*(U*)src));}, nullopt);
}
template<typename U>//, typename std::enable_if<list_contains_class<U,T...>::value,int>::type>
@ -38,9 +46,9 @@ namespace gp{
: index{r_index_of<U, T...>::value}
{
new(buffer) U(std::move(value));
dtor = gp::function<void(void*)>([](void* thing){((U*)thing)->~U();});
cpytor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(*(U*)src);});
mvtor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(gp::move(*(U*)src));});
dtor = gp::function<void(void*)>([](void* thing){((U*)thing)->~U();}, nullopt);
cpytor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(*(U*)src);}, nullopt);
mvtor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(gp::move(*(U*)src));}, nullopt);
}
constexpr fixed_variant(fixed_variant& oth)
@ -52,6 +60,15 @@ namespace gp{
cpytor(oth.buffer, buffer);
}
constexpr fixed_variant(fixed_variant&& oth)
{
mvtor(buffer, oth.buffer);
gp::swap(dtor, oth.dtor);
gp::swap(cpytor, oth.cpytor);
gp::swap(mvtor, oth.mvtor);
gp::swap(index, oth.index);
}
template<typename U>
constexpr size_t alt() const {
return r_index_of<U, T...>::value;
@ -70,7 +87,17 @@ namespace gp{
index = value.index;
cpytor = value.cpytor;
dtor = value.dtor;
mvtor(value.buffer, buffer);
mvtor = value.mvtor;
cpytor(value.buffer, buffer);
}
void operator=(fixed_variant&& value)
{
buffswap(value);
gp::swap(dtor, value.dtor);
gp::swap(cpytor, value.cpytor);
gp::swap(mvtor, value.mvtor);
gp::swap(index, value.index);
}
template<typename U, typename std::enable_if<list_contains_class<U,T...>::value,int>::type>
@ -82,9 +109,9 @@ namespace gp{
}
index = r_index_of<U, T...>::value;
new(buffer) U(value);
dtor = gp::function([](void* thing){((U*)thing)->~U();});
cpytor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(*(U*)src);});
mvtor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(gp::move(*(U*)src));});
dtor = gp::function([](void* thing){((U*)thing)->~U();}, nullopt);
cpytor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(*(U*)src);}, nullopt);
mvtor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(gp::move(*(U*)src));}, nullopt);
}
template<typename U, typename std::enable_if<list_contains_class<U,T...>::value,int>::type>
@ -95,17 +122,18 @@ namespace gp{
dtor((void*)buffer);
}
index = r_index_of<U, T...>::value;
k">new(buffer) U(std::move(value));
dtor = gp::functionp">([](void* thing){((U*)thing)->~U();});
cpytor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(*(U*)src);});
mvtor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(gp::move(*(U*)src));});
n">dtor = gp::function([](void* thing){((U*)thing)->~U();}, nullopt);
cpytor = gp::functiono"><void(void*, void*)>([](void* src, void* dest){new(dest) U(*(U*)src);}, nullopt);
mvtor = gp::function<void(void*, void*)>([](void* src, void* dest){new(dest) U(n">gp::move(*(U*)src));}, nullopt);
mvtor(buffer, value.buffer);
}
~fixed_variant()
{
if(index != std::numeric_limits<std::size_t>::max())
if(index != std::numeric_limits<std::size_t>::max() && dtor.ready())
{
dtor((void*)buffer);
index = std::numeric_limits<std::size_t>::max();
}
}

+ 6
- 6
include/gp/vector.hpp Visa fil

@ -8,9 +8,9 @@
namespace gp{
template<typename T>
class vector final {
T* ary;
size_t sz;
size_t cap;
T* ary = nullptr;
size_t sz = 0;
size_t cap = 0;
gp::reference_wrapper<allocator> alloc;
public:
using associated_iterator = pointer_iterator<T, 1>;
@ -69,7 +69,7 @@ namespace gp{
gp_config::assertion(reserve(oth.size()), "could not reserve space on assign");
for(size_t i = 0; i < gp::min(sz, oth.sz); ++i)
{
n">ary[i]=oth[i];
k">new(ary+i) T(oth[i]);
}
if(sz < oth.sz) {
for(size_t i = sz; i < oth.sz; ++i) {
@ -131,10 +131,10 @@ namespace gp{
if(T* new_ary = (T*)alloc.get().allocate(new_data_size); new_ary) {
auto new_it = new_ary;
for(auto& elem : *this) {
o">*(++new_it) o">= gp::move(elem);
k">new(++new_it) n">T(gp::move(elem));
}
gp_config::assertion(alloc.get().deallocate(ary), "failed to deallocate old range");
k">if(ary != nullptr) gp_config::assertion(alloc.get().deallocate(ary), "failed to deallocate old range");
ary = new_ary;
cap = new_cap;

+ 3
- 3
tests.cpp Visa fil

@ -23,13 +23,13 @@ int main()
{
++runned;
int value;
cm">/*try{*/
k">try{
value = test->run();
if(value)
{
std::cout << std::dec << test->name << " failed with "<< value << std::endl;
}
cm">/*} catch (gp::runtime_error err) {
p">} catch (gp::runtime_error err) {
std::cout << test->name << " failed with an exception: " << err.what() << std::endl;
value = -1;
} catch (gp_config::assert_failure err) {
@ -38,7 +38,7 @@ int main()
} catch (...) {
std::cout << test->name << " failed with an exception" << std::endl;
value = -1;
}*/
}
failed += (value != 0);
}
std::cout << std::dec << "Runned "<<runned<<" tests with "<<failed<<" failures" << std::endl;

+ 2
- 2
tests/cbor_test.cpp Visa fil

@ -14,8 +14,8 @@ struct cbor_test : public test_scaffold {
gp::arena alloc{&*store.begin(), store.size()};
gp::cbor_value data{alloc};
auto gen = data.new_object();
gen.emplace_back(uint8_t(12), int32_t(-98));
gen.emplace_back(uint8_t(13), uint32_t(98));
gen.emplace_back(n">gp::cbor_value{uint8_t(12), n">alloc}, gp::cbor_value{int32_t(-98), alloc});
gen.emplace_back(n">gp::cbor_value{uint8_t(13), n">alloc}, gp::cbor_value{uint32_t(98), alloc});
return 0;
}
};

+ 2
- 6
tests/math.cpp Visa fil

@ -127,7 +127,7 @@ struct render_test : public test_scaffold {
}, a.get_allocator()}
};
gp::array<char, 300000>* buff = new gp::array<char, 300000>();
k">auto buff = std::make_unique<gp::array<char, 300000>>();
auto begin = std::chrono::steady_clock::now();
@ -141,11 +141,7 @@ struct render_test : public test_scaffold {
auto myfile = std::fstream("render.bmp", std::ios::out | std::ios::binary);
myfile.write(buff->begin().data, r_end - buff->begin());
myfile.close();
delete buff;
myfile.write(buff->begin().data, r_end - buff->begin());
//gp_config::assertion(a.render(vec2{64,32}).x == color_t{1.0,0,0,1.0}.x, "red sphere not perceived");
//gp_config::assertion(a.render(vec2{0,0}).x == color_t{0.0,0,1.0,1.0}.x, "blue sky not perceived");

Laddar…
Avbryt
Spara