aboutsummaryrefslogtreecommitdiff
path: root/libcutl/shared-ptr/base.cxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2020-12-16 20:29:05 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2021-02-24 16:40:04 +0300
commit8e761289a2446367267c6c0d9a26e734f0f78306 (patch)
treefb495d8c18801f271d124ee48731f10df396ca89 /libcutl/shared-ptr/base.cxx
parent4c8104756b92b9fa16b3a725e8a6aa620dfd606e (diff)
Get rid of legacy build systems and rename cutl/ to libcutl/
Diffstat (limited to 'libcutl/shared-ptr/base.cxx')
-rw-r--r--libcutl/shared-ptr/base.cxx61
1 files changed, 61 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);
+}