From 8e761289a2446367267c6c0d9a26e734f0f78306 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 16 Dec 2020 20:29:05 +0300 Subject: Get rid of legacy build systems and rename cutl/ to libcutl/ --- libcutl/shared-ptr/base.cxx | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 libcutl/shared-ptr/base.cxx (limited to 'libcutl/shared-ptr/base.cxx') 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 + +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 (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 (p); + sp -= 2; + operator delete (sp); + } + else + operator delete (p); +} -- cgit v1.1