C++ wrapper start

cpp
Avril 3 years ago
parent 52c80b1b08
commit 4b7180ed5d
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,10 +1,14 @@
#ifndef _COW_H
#define _COW_H
#ifdef __cplusplus
#define restrict __restrict__
#endif
#include <stdlib.h>
// Copy-on-write mapped memory.
typedef struct cow cow_t, *cow;
typedef struct cow cow_t;
/// Create a new copy-on-write area of `size` bytes.
/// Writes to this instance pointer (`cow_ptr()`) are written to the allocated memory.
@ -29,4 +33,8 @@ size_t cow_size(const cow_t* cow);
//XXX: Too unsafe and not useful enough to warrant a function/macro.
//#define cow_from_ptr(p) (cow_t*)(&(p))
#ifdef __cplusplus
#undef restrict
#endif
#endif /* _COW_H */

@ -0,0 +1,38 @@
#pragma once
#include "cow.h"
#include <memory>
struct Cow {
Cow() = delete;
Cow(size_t size);
Cow(Cow&& m);
virtual ~Cow();
static Cow from_raw(cow_t* owned);
struct Fake;
private:
struct _inner;
Cow(cow_t* raw);
protected:
const std::shared_ptr<_inner> super;
Cow(const Cow& c);
};
struct Cow::Fake : Cow {
Fake() = delete;
Fake(const Fake& c);
Fake(Fake&& m);
~Fake();
static Fake from_parent(const Cow& parent);
private:
Fake(const Cow& parent);
cow_t* fake;
};

@ -0,0 +1,39 @@
#include <cow.hpp>
#include <utility>
struct Cow::_inner {
cow_t* ptr;
~_inner();
_inner(size_t sz);
_inner(cow_t* ptr);
_inner(const _inner& copy) = delete;
_inner(_inner&& move) = delete;
_inner() = delete;
};
Cow::_inner::~_inner() {
if(ptr) {
cow_free(ptr);
ptr = nullptr;
}
}
Cow::_inner::_inner(size_t sz) : ptr(cow_create(sz)){}
Cow::_inner::_inner(cow_t* ptr) : ptr(ptr){}
Cow::Cow(size_t size) : super(std::make_shared<_inner>(size)){}
Cow::Cow(cow_t* raw) : super(std::make_shared<_inner>(raw)){}
Cow::~Cow(){}
Cow::Cow(Cow&& m) : super(std::move(*const_cast<std::shared_ptr<_inner>*>(&super))){}
Cow Cow::from_raw(cow_t* owned) { if(cow_is_fake(owned)) throw "Trying to create real from fake raw"; else return Cow(owned); }
Cow::Fake::Fake(const Cow& parent) : Cow(parent), fake(cow_clone(parent.super->ptr)){}
Cow::Fake::~Fake() { if(fake) { cow_free(fake); *const_cast<cow_t**>(&fake) = nullptr; } }
Cow::Fake Cow::Fake::from_parent(const Cow& parent) { return Fake(parent); }
Cow::Fake::Fake(Fake&& move) : Cow(std::move(move)), fake(move.fake)
{
*const_cast<cow_t**>(&fake) = nullptr;
}
Loading…
Cancel
Save