diff --git a/octa/memory.h b/octa/memory.h index 80a7db1..f9b7357 100644 --- a/octa/memory.h +++ b/octa/memory.h @@ -6,6 +6,8 @@ #ifndef OCTA_MEMORY_H #define OCTA_MEMORY_H +#include + #include "octa/new.h" #include "octa/utility.h" #include "octa/type_traits.h" @@ -533,6 +535,101 @@ namespace octa { template typename __OctaBoxIf::__OctaBoxKnownSize make_box(A &&...args) = delete; + + /* allocator */ + + template struct Allocator; + + template<> struct Allocator { + typedef void ValType; + typedef void *PtrType; + typedef const void *ConstPtrType; + + template using Rebind = Allocator; + }; + + template<> struct Allocator { + typedef const void ValType; + typedef const void *PtrType; + typedef const void *ConstPtrType; + + template using Rebind = Allocator; + }; + + template struct Allocator { + typedef size_t SizeType; + typedef ptrdiff_t DiffType; + typedef T ValType; + typedef T &RefType; + typedef const T &ConstRefType; + typedef T *PtrType; + typedef const T *ConstPtrType; + + template using Rebind = Allocator; + + PtrType address(RefType v) const noexcept { + return address_of(v); + }; + ConstPtrType address(ConstRefType v) const noexcept { + return address_of(v); + }; + + SizeType max_size() const noexcept { return SizeType(~0) / sizeof(T); } + + PtrType allocate(SizeType n, Allocator::ConstPtrType = nullptr) noexcept { + return (PtrType) ::new uchar[n * sizeof(T)]; + } + + void deallocate(PtrType p, SizeType) { ::delete[] (uchar *) p; } + + template + void construct(U *p, A &&...args) { + ::new((void *)p) U(forward(args)...); + } + + void destroy(PtrType p) { p->~T(); } + }; + + template struct Allocator { + typedef size_t SizeType; + typedef ptrdiff_t DiffType; + typedef const T ValType; + typedef const T &RefType; + typedef const T &ConstRefType; + typedef const T *PtrType; + typedef const T *ConstPtrType; + + template using Rebind = Allocator; + + ConstPtrType address(ConstRefType v) const noexcept { + return address_of(v); + }; + + SizeType max_size() const noexcept { return SizeType(~0) / sizeof(T); } + + PtrType allocate(SizeType n, Allocator::ConstPtrType = nullptr) noexcept { + return (PtrType) ::new uchar[n * sizeof(T)]; + } + + void deallocate(PtrType p, SizeType) { ::delete[] (uchar *) p; } + + template + void construct(U *p, A &&...args) { + ::new((void *)p) U(forward(args)...); + } + + void destroy(PtrType p) { p->~T(); } + }; + + template + bool operator==(const Allocator &, const Allocator &) noexcept { + return true; + } + + template + bool operator!=(const Allocator &, const Allocator &) noexcept { + return false; + } } #endif \ No newline at end of file