diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2020-12-16 20:29:05 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2021-02-24 16:40:04 +0300 |
commit | 8e761289a2446367267c6c0d9a26e734f0f78306 (patch) | |
tree | fb495d8c18801f271d124ee48731f10df396ca89 /libcutl/shared-ptr | |
parent | 4c8104756b92b9fa16b3a725e8a6aa620dfd606e (diff) |
Get rid of legacy build systems and rename cutl/ to libcutl/
Diffstat (limited to 'libcutl/shared-ptr')
-rw-r--r-- | libcutl/shared-ptr/base.cxx | 61 | ||||
-rw-r--r-- | libcutl/shared-ptr/base.hxx | 91 | ||||
-rw-r--r-- | libcutl/shared-ptr/base.ixx | 77 | ||||
-rw-r--r-- | libcutl/shared-ptr/base.txx | 198 |
4 files changed, 427 insertions, 0 deletions
diff --git a/libcutl/shared-ptr/base.cxx b/libcutl/shared-ptr/base.cxx new file mode 100644 index 0000000..a3bc774 --- /dev/null +++ b/libcutl/shared-ptr/base.cxx @@ -0,0 +1,61 @@ +// file : libcutl/shared-ptr/base.cxx +// license : MIT; see accompanying LICENSE file + +#include <libcutl/shared-ptr/base.hxx> + +using std::size_t; + +// +// +cutl::share shared = cutl::share (1); +cutl::share exclusive = cutl::share (2); + +// +// +namespace cutl +{ + char const* not_shared:: + what () const noexcept + { + return "object is not shared"; + } +} + +// +// +void* +operator new (size_t n, cutl::share s) +{ + if (s == shared) + { + // Here we need to make sure we don't break the alignment of the + // returned block. For that we need to know the maximum alignment + // of this platform. Twice the pointer size is a good guess for + // most platforms. + // + size_t* p = static_cast<size_t*> (operator new (n + 2 * sizeof (size_t))); + *p++ = 1; // Initial count. + *p++ = 0xDEADBEEF; // Signature. + return p; + } + else + return operator new (n); + +} + +void +operator delete (void* p, cutl::share s) noexcept +{ + // This version of operator delete is only called when the c-tor + // fails. In this case there is no object and we can just free the + // memory. + // + if (s == shared) + { + size_t* sp = static_cast<size_t*> (p); + sp -= 2; + operator delete (sp); + } + else + operator delete (p); +} diff --git a/libcutl/shared-ptr/base.hxx b/libcutl/shared-ptr/base.hxx new file mode 100644 index 0000000..f49a532 --- /dev/null +++ b/libcutl/shared-ptr/base.hxx @@ -0,0 +1,91 @@ +// file : libcutl/shared-ptr/base.hxx +// license : MIT; see accompanying LICENSE file + +#ifndef LIBCUTL_SHARED_PTR_BASE_HXX +#define LIBCUTL_SHARED_PTR_BASE_HXX + +#include <new> +#include <cstddef> // std::size_t + +#include <libcutl/exception.hxx> + +#include <libcutl/export.hxx> + +namespace cutl +{ + struct share + { + explicit + share (char id); + + bool + operator== (share) const; + + private: + char id_; + }; +} + +extern LIBCUTL_EXPORT cutl::share shared; +extern LIBCUTL_EXPORT cutl::share exclusive; + +LIBCUTL_EXPORT void* +operator new (std::size_t, cutl::share); + +LIBCUTL_EXPORT void +operator delete (void*, cutl::share) noexcept; + +namespace cutl +{ + struct LIBCUTL_EXPORT not_shared: exception + { + virtual char const* + what () const noexcept; + }; + + struct LIBCUTL_EXPORT shared_base + { + shared_base (); + shared_base (shared_base const&); + shared_base& + operator= (shared_base const&); + + void + _inc_ref (); + + bool + _dec_ref (); + + std::size_t + _ref_count () const; + + void* + operator new (std::size_t, share); + + void + operator delete (void*, share) noexcept; + + void + operator delete (void*) noexcept; + + protected: + std::size_t counter_; + }; + + template <typename X> + inline X* + inc_ref (X*); + + template <typename X> + inline void + dec_ref (X*); + + template <typename X> + inline std::size_t + ref_count (X const*); +} + +#include <libcutl/shared-ptr/base.ixx> +#include <libcutl/shared-ptr/base.txx> + +#endif // LIBCUTL_SHARED_PTR_BASE_HXX diff --git a/libcutl/shared-ptr/base.ixx b/libcutl/shared-ptr/base.ixx new file mode 100644 index 0000000..d4fe1ea --- /dev/null +++ b/libcutl/shared-ptr/base.ixx @@ -0,0 +1,77 @@ +// file : libcutl/shared-ptr/base.ixx +// license : MIT; see accompanying LICENSE file + +namespace cutl +{ + // share + // + + inline share:: + share (char id) + : id_ (id) + { + } + + inline bool share:: + operator== (share x) const + { + return id_ == x.id_; + } + + // shared_base + // + + inline shared_base:: + shared_base () + : counter_ (1) + { + } + + inline shared_base:: + shared_base (shared_base const&) + : counter_ (1) + { + } + + inline shared_base& shared_base:: + operator= (shared_base const&) + { + return *this; + } + + inline void shared_base:: + _inc_ref () + { + counter_++; + } + + inline bool shared_base:: + _dec_ref () + { + return --counter_ == 0; + } + + inline std::size_t shared_base:: + _ref_count () const + { + return counter_; + } + + inline void* shared_base:: + operator new (std::size_t n, share) + { + return ::operator new (n); + } + + inline void shared_base:: + operator delete (void* p, share) noexcept + { + ::operator delete (p); + } + + inline void shared_base:: + operator delete (void* p) noexcept + { + ::operator delete (p); + } +} diff --git a/libcutl/shared-ptr/base.txx b/libcutl/shared-ptr/base.txx new file mode 100644 index 0000000..6bd3ed8 --- /dev/null +++ b/libcutl/shared-ptr/base.txx @@ -0,0 +1,198 @@ +// file : libcutl/shared-ptr/base.txx +// license : MIT; see accompanying LICENSE file + +#include <libcutl/meta/answer.hxx> +#include <libcutl/meta/polymorphic-p.hxx> + +namespace cutl +{ + namespace bits + { + // Support for locating the counter in the memory block. + // + template <typename X, bool poly = meta::polymorphic_p<X>::r> + struct locator; + + template <typename X> + struct locator<X, false> + { + static std::size_t* + counter (X* x) + { + std::size_t* p (reinterpret_cast<std::size_t*> (x)); + + if (*(--p) != 0xDEADBEEF) + throw not_shared (); + + return --p; + } + }; + + template <typename X> + struct locator<X, true> + { + static std::size_t* + counter (X* x) + { + std::size_t* p ( + static_cast<std::size_t*> ( + dynamic_cast<void*> (x))); + + if (*(--p) != 0xDEADBEEF) + throw not_shared (); + + return --p; + } + }; + + template <typename X> + std::size_t* + counter (X const* p) + { + return bits::locator<X>::counter (const_cast<X*> (p)); + } + + // Counter type and operations. + // + meta::no test (...); + meta::yes test (shared_base*); + + template <typename X, + std::size_t A = sizeof (bits::test (reinterpret_cast<X*> (0)))> + struct counter_type; + + template <typename X> + struct counter_type<X, sizeof (meta::no)> + { + typedef X r; + }; + + template <typename X> + struct counter_type<X, sizeof (meta::yes)> + { + typedef shared_base r; + }; + + template <typename X, typename Y> + struct counter_ops; + + template <typename X> + struct counter_ops<X, X> + { + counter_ops (X const* p) : counter_ (p ? bits::counter (p) : 0) {} + counter_ops (counter_ops const& x) : counter_ (x.counter_) {} + + template <typename Z> + counter_ops (counter_ops<Z, Z> const& x) : counter_ (x.counter_) {} + + counter_ops& + operator= (counter_ops const& x) + { + counter_ = x.counter_; + return *this; + } + + template <typename Z> + counter_ops& + operator= (counter_ops<Z, Z> const& x) + { + counter_ = x.counter_; + return *this; + } + + void + reset (X const* p) + { + counter_ = p ? bits::counter (p) : 0; + } + + void + inc (X*) + { + (*counter_)++; + } + + void + dec (X* p) + { + if (--(*counter_) == 0) + { + p->~X (); + operator delete (counter_); // Counter is the top of the memory block. + } + } + + std::size_t + count (X const*) const + { + return *counter_; + } + + std::size_t* counter_; + }; + + template <typename Y> + struct counter_ops<shared_base, Y> + { + counter_ops (Y const*) {} + counter_ops (counter_ops const&) {} + + template <typename Z> + counter_ops (counter_ops<shared_base, Z> const&) {} + + counter_ops& + operator= (counter_ops const&) + { + return *this; + } + + template <typename Z> + counter_ops& + operator= (counter_ops<shared_base, Z> const&) + { + return *this; + } + + void + reset (Y const*) {} + + void + inc (shared_base* p) {p->_inc_ref ();} + + void + dec (Y* p) + { + if (static_cast<shared_base*> (p)->_dec_ref ()) + delete p; + } + + std::size_t + count (shared_base const* p) const {return p->_ref_count ();} + }; + } + + template <typename X> + inline X* + inc_ref (X* p) + { + bits::counter_ops<typename bits::counter_type<X>::r, X> c (p); + c.inc (p); + return p; + } + + template <typename X> + inline void + dec_ref (X* p) + { + bits::counter_ops<typename bits::counter_type<X>::r, X> c (p); + c.dec (p); + } + + template <typename X> + inline std::size_t + ref_count (X const* p) + { + bits::counter_ops<typename bits::counter_type<X>::r, X> c (p); + return c.count (p); + } +} |