Compare commits

..

11 Commits

Author SHA1 Message Date
Avril 130f21f57e
Removed rust version from master
2 years ago
Avril 671fcd3968
Merge branch 'rust': Wrong branch, whoops.
2 years ago
Avril 0d082f5b6f
Added `cow_create_fd(fd, size)`: Create a `cow_t*` over an already existing file descriptor
2 years ago
Avril 6f21491298
typed OO bullshit
3 years ago
Avril 2d4ebc3142
error type
3 years ago
Avril 7b623e0fdf
rust bindings: start
3 years ago
Avril 7e4e3f9867
kill me
3 years ago
Avril 646482c555
scrapped
3 years ago
Avril 2c88d329b9
typed cow testing n stuff
3 years ago
Avril 382efd5d43
started TypedCow <cow/typed.hpp>
3 years ago
Avril ffe01128c4
version bump
3 years ago

2
.gitignore vendored

@ -4,3 +4,5 @@ cow-*
obj/
*~
vgcore.*
target/
!cowslice/*.a

@ -5,7 +5,7 @@ PROJECT=cow
AUTHOR=Avril (Flanchan) <flanchan@cumallover.me>
VERSION_MAJOR=0
VERSION_MINOR=2.0r1
VERSION_MINOR=3.0
VERSION=$(VERSION_MAJOR).$(VERSION_MINOR)
ifeq ($(PREFIX),)

@ -35,6 +35,13 @@ typedef struct cow_mapped_slice cow_t;
/// Writes to this instance pointer (`cow_ptr()`) are written to the allocated memory.
/// Writes to any cloned instances do not propagate to this instance.
cow_t* cow_create(size_t size);
/// Create a new copy-on-write area of `size` bytes from file `fd`.
/// Writes to this instance pointer (`cow_ptr()`) are written to the allocated memory.
/// Writes to any cloned instances do not propagate to this instance.
///
/// The resulting object does not own `fd`, but does own a duplicate of it.
cow_t* cow_create_fd(int fd, size_t size);
/// Free a cow area. This should be called on all clones before the parent of those clones is freed.
void cow_free(cow_t* restrict cow);
/// Create a clone of this instance. Any writes to the returned pointer will not be propagated to the input one.

@ -21,12 +21,15 @@ struct Cow : public _cow_util::Span<unsigned char> {
struct Fake;
Cow() = delete;
Cow(int fd, size_t size);
explicit Cow(size_t size);
Cow(Cow&& m);
virtual ~Cow();
virtual Fake clone() const;
protected:
inline void* area() override {
auto raw = get_raw();
return raw ? cow_ptr(raw) : nullptr;
@ -35,6 +38,7 @@ struct Cow : public _cow_util::Span<unsigned char> {
auto raw = get_raw();
return raw ? cow_ptr_of(const void, raw) : nullptr;
}
public:
/// Get the size of the mapped area.
///
@ -59,6 +63,8 @@ struct Cow : public _cow_util::Span<unsigned char> {
protected:
Cow(const Cow& c);
explicit inline Cow(std::shared_ptr<_inner>&& super) : super(std::move(super)){}
const std::shared_ptr<_inner> super;
virtual cow_t* get_raw() const;
@ -84,9 +90,3 @@ struct Cow::Fake : public Cow {
cow_t* const fake;
};
//TODO
template<typename T>
struct TypedCow : private Cow, public _cow_util::Span<T> {
struct Fake : private Cow::Fake, public _cow_util::Span<T>{};
};

@ -5,9 +5,10 @@
#include <utility>
#include <cow.hpp>
#include "slice.hpp"
namespace _cow_util {
struct Area {
struct Area : public Span<unsigned char> {
Area() = delete;
explicit Area(size_t sz);
@ -30,7 +31,13 @@ namespace _cow_util {
inline cow_t* raw() const { return _area->raw(); }
inline size_t size() const override { return _area->size(); }
~Area();
protected:
inline void* area() override { return _area->ptr(); }
inline const void* area() const override { return _area->ptr(); }
private:
const std::unique_ptr<Cow> _area;
};

@ -4,17 +4,19 @@ namespace _cow_util {
/// A type that spans a sized region of memory
template<typename T>
struct Span {
protected:
virtual const void* area() const =0;
virtual void* area() = 0;
public:
virtual size_t size() const = 0;
inline T* ptr() { return (T*)area(); }
inline const T* ptr() const { return (const T*)area(); }
inline T* ptr() { return reinterpret_cast<T*>(area()); }
inline const T* ptr() const { return reinterpret_cast<const T*>(area()); }
inline size_t size_bytes() const { return size() * sizeof(T); }
inline unsigned char* as_bytes() { return (unsigned char*)area(); }
inline const unsigned char* as_bytes() const { return (const unsigned char*)area(); }
inline unsigned char* as_bytes() { return area_as<unsigned char>(); }
inline const unsigned char* as_bytes() const { return area_as<unsigned char>(); }
inline T& operator[](size_t index) {
if(index >= size()) throw "index out of range";
@ -38,9 +40,9 @@ namespace _cow_util {
inline operator T*() { return &(*this)[0]; }
template<typename U>
inline U* area_as() requires(sizeof(T) % sizeof(U) == 0) { return (U*)area(); }
inline U* area_as() requires(sizeof(T) % sizeof(U) == 0) { return reinterpret_cast<U*>(area()); }
template<typename U>
inline const U* area_as() const requires(sizeof(T) % sizeof(U) == 0) { return (U*)area(); }
inline const U* area_as() const requires(sizeof(T) % sizeof(U) == 0) { return reinterpret_cast<const U*>(area()); }
template<typename U>
size_t size_as() const requires(sizeof(T) % sizeof(U) == 0) { return size_bytes() / sizeof(U); }
@ -89,23 +91,24 @@ namespace _cow_util {
inline const Slice slice_wrap(ssize_t len) const { return slice_abs((size_t)wrap_len(len)); }
template<typename U>
inline Span<U>::Slice reinterpret() { return typename Span<U>::Slice((U*)area(), size_bytes() / sizeof(U)); }
inline Span<U>::Slice reinterpret() { return typename Span<U>::Slice(area_as<U>(), size_bytes() / sizeof(U)); }
template<typename U>
inline Span<const U>::Slice reinterpret() const { return typename Span<const U>::Slice((const U*)area(), size_bytes() / sizeof(U)); }
inline Span<const U>::Slice reinterpret() const { return typename Span<const U>::Slice(area_as<U>(), size_bytes() / sizeof(U)); }
};
/// A slice of memory with a backing pointer and size.
template<typename T>
struct Span<T>::Slice : public Span<T> {
inline Slice(T* ptr, size_t sz) : _area((void*)ptr), _size(sz){}
inline Slice(T* ptr, size_t sz) : _area(reinterpret_cast<void*>(ptr)), _size(sz){}
inline Slice(const Span<T>& slice) : _area(const_cast<void*>(slice.area())), _size(slice.size()){}
inline Slice(const Slice& copy) = default;
inline Slice(Slice&& copy) : _area(copy._area), _size(copy._size){ *const_cast<size_t*>(&copy._size) = 0; }
Slice() = delete;
inline size_t size() const override { return _size; }
protected:
inline const void* area() const override { return _area; }
inline void* area() override { return _area; }
inline size_t size() const override { return _size; }
private:
void* const _area;

@ -0,0 +1,121 @@
#pragma once
#include <cow.hpp>
#include <utility>
template<typename T>
struct TypedCow : public _cow_util::Span<T> {
struct Fake;
template<typename... Args>
inline TypedCow(size_t sz, Args&&... args) : real(Cow(sz * sizeof(T))) { init_copy( T(std::forward<Args>(args)...) ); }
inline TypedCow(size_t sz, const T& copy) : real(Cow(sz * sizeof(T))) { init_copy(copy); }
inline TypedCow(size_t sz) : TypedCow(sz, T()){}
inline virtual ~TypedCow() { uninit(); }
inline virtual Fake clone() const { return Fake(*this); }
inline virtual cow_t* raw() const { return real.raw(); }
inline size_t size() const { return real.size() / sizeof(T); }
protected:
//inline explicit TypedCow(const TypedCow<T>& unsafeCopy) : real(Cow(unsafeCopy.real)){}
inline virtual void* area() override { return reinterpret_cast<void*>( real.ptr() ); }
inline virtual const void* area() const override { return reinterpret_cast<const void*>( real.ptr() ); }
inline void init_copy(const T& copy_from) {
T* ptr = this->ptr();
for(size_t i=0;i<size();i++)
new ((void*)(ptr+i)) T(copy_from);
}
// UNSAFE: Explicitly calls destructors of each element in this instance.
inline void uninit() {
T* ptr = this->ptr();
if(!ptr) return;
for(size_t i=0;i<size();i++)
(ptr+i)->~T();
}
private:
Cow real;
};
template<typename T>
struct TypedCow<T>::Fake : public TypedCow<T> {
//XXX: How THE FUCK do we initialise base's `real` here?????
Fake() = delete;
inline Fake(const Fake& copy) : fake(cow_clone(copy.fake)){}
inline Fake(Fake&& move) : fake(move.fake) { *const_cast<cow_t**>(&move.fake) = nullptr; }
inline Fake(const TypedCow<T>& clone) : fake(cow_clone(clone.raw())){}
inline cow_t* raw() const override { return fake; }
inline Fake clone() const override { return Fake(*this); }
inline ~Fake(){}
inline size_t size() const override { return fake ? cow_size(fake) : 0; }
protected:
inline void* area() override { return fake ? cow_ptr(fake) : nullptr; }
inline const void* area() const override { return fake ? cow_ptr_of(const void, fake) : nullptr; }
private:
cow_t* const fake;
};
#if 0
struct Fake;
friend class Fake;
TypedCow() = delete;
inline TypedCow(TypedCow<T>&& move) : Cow(std::move(move.super)) { /* Moves the shared_ptr. No need to move the elements. */ }
// protected: copy
inline TypedCow(size_t sz) : Cow(sz * sizeof(T)) { init_copy(T()); }
inline TypedCow(size_t sz, const T& copy_from) : TypedCow(sz) { init_copy(copy_from); }
//template<typename... Args>
//inline TypedCow(size_t sz, Args&&... args) : TypedCow(sz) { init_copy( T(std::forward<Args>(args)...) ); }
virtual inline ~TypedCow() { uninit(); }
inline Cow::Fake clone() const override { return Cow::clone(); }
inline Fake clone() const { return Fake(Cow::clone()); }
inline size_t size() const override { return Cow::size() / sizeof(T); }
protected:
inline void* area() override { return Cow::area(); }
inline const void* area() const override { return Cow::area(); }
//// Should only be used for creating Fakes. Copies the refcounted pointer.
//inline TypedCow(const TypedCow<T>& copy) : Cow(copy.super) {}
// UNSAFE: Placement-new's copys of `copy_from` into `0..size()` of this instance.
inline void init_copy(const T& copy_from) {
T* ptr = _cow_util::Span<T>::ptr();
for(size_t i=0;i<size();i++)
new ((void*)(ptr+i)) T(copy_from);
}
// UNSAFE: Explicitly calls destructors of each element in this instance.
inline void uninit() {
T* ptr = _cow_util::Span<T>::ptr();
for(size_t i=0;i<size();i++)
(ptr+i)->~T();
}
};
template<typename T>
struct TypedCow<T>::Fake : private Cow::Fake, public _cow_util::Span<T> {
Fake() = delete;
explicit inline Fake(Cow::Fake&& untyped) : Cow::Fake(untyped){}
inline Fake(const Fake& copy) : Fake(copy.Cow::Fake::clone()){}
inline Fake(Fake&& move) : Cow::Fake(std::move(move)) {}
inline ~Fake(){}
inline size_t size() const override { return Cow::Fake::size(); }
protected:
inline const void* area() const override { return Cow::Fake::area(); }
inline void* area() override { return Cow::Fake::area(); }
};
#endif

@ -76,13 +76,13 @@ size_t cow_size(const cow_t* cow)
return cow->size;
}
inline internal cow_t _cow_create_unboxed(size_t size)
inline internal cow_t _cow_create_unboxed(int _fd, size_t size)
{
cow_t ret;
ret.size = size;
if( (ret.poisoned =
((ret.fd = shm_fd(size)) == -1))
((ret.fd = _fd < 0 ? shm_fd(size) : _fd) == -1))
) {
ret.origin = NULL;
return ret;
@ -96,7 +96,14 @@ inline internal cow_t _cow_create_unboxed(size_t size)
}
cow_t* cow_create(size_t size)
{
return box_value(_cow_create_unboxed(size));
return box_value(_cow_create_unboxed(-1, size));
}
cow_t* cow_create_fd(int fd, size_t size)
{
fd = dup(fd);
if(__builtin_expect(fd < 0, false)) return NULL;
return box_value(_cow_create_unboxed(fd, size));
}
inline internal void _cow_free_unboxed(const cow_t* cow)

@ -15,6 +15,7 @@ struct Cow::_inner {
~_inner();
_inner(size_t sz);
_inner(int fd, size_t sz);
_inner(cow_t* ptr);
_inner(const _inner& copy) = delete;
@ -27,10 +28,12 @@ Cow::_inner::~_inner() {
_cow_free_unboxed(ptr());
cow.poisoned=true;
}
Cow::_inner::_inner(size_t sz) : cow(_cow_create_unboxed(sz)){
Cow::_inner::_inner(int fd, size_t sz) : cow(_cow_create_unboxed(fd, sz)){
if(UNLIKELY(cow.poisoned)) throw CowException(cow_err());
}
Cow::_inner::_inner(size_t sz) : _inner(-1, sz) {}
Cow::_inner::_inner(cow_t* ptr) : cow(*ptr)
{
free(ptr);
@ -38,6 +41,7 @@ Cow::_inner::_inner(cow_t* ptr) : cow(*ptr)
}
Cow::Cow(size_t size) : super(std::make_shared<_inner>(size)){}
Cow::Cow(int fd, size_t size) : super(std::make_shared<_inner>(fd, size)){}
Cow::Cow(cow_t* raw) : super(std::make_shared<_inner>(raw)){}
Cow::Cow(Cow&& m) : super(std::move(*const_cast<std::shared_ptr<_inner>*>(&m.super))){}

@ -44,7 +44,7 @@ _Static_assert
(offsetof(cow_t, size) == sizeof(void*), "`cow_t.size` should have an offset equal to `sizeof(void*)` or cow_size_unsafe() becomes UB.");
#endif
cow_t _cow_create_unboxed(size_t size) internal;
cow_t _cow_create_unboxed(int rfd, size_t size) internal;
void _cow_free_unboxed(const cow_t* cow) internal;
#ifdef __cplusplus

@ -8,6 +8,7 @@
#include <vector>
#include <cow/area.hpp>
#include <cow/typed.hpp>
using namespace _cow_util;
@ -146,7 +147,7 @@ namespace Tiling {
template<typename T = unsigned char>
void print_slice(Slice<T> memory)
{
printf("slice: { %p, %lu (%lu bytes) }\n", memory.area(), memory.size(), memory.size_bytes());
printf("slice: { %p, %lu (%lu bytes) }\n", memory.ptr(), memory.size(), memory.size_bytes());
}
@ -168,6 +169,12 @@ void moving_cow(Cow moved)
read_fake(moved);
}
void typed_cow()
{
TypedCow<int> tc(1024);
}
int main()
{
Cow _area(4000);
@ -203,7 +210,7 @@ int main()
Cow::Fake clone = real;
printf("Fake size: %lu\n", clone.size());
printf("Fake ptr: %p\n", clone.area());
printf("Fake ptr: %p\n", clone.ptr());
read_fake(clone);
write_fake(clone, "hello fake!");
@ -216,6 +223,7 @@ int main()
printf("First byte of: fake = %x\n", clone[0]);
read_fake(clone); //clone still functions because of refcount on origin.
typed_cow();
printf("Last error: %d, %s\n", cow_err(), *cow_err_msg(cow_err()));
try {
Cow should_fail(SIZE_MAX);

Loading…
Cancel
Save