aboutsummaryrefslogtreecommitdiff
path: root/odb/shared-ptr
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-08-18 20:02:11 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-08-18 20:02:11 +0200
commit2636e266dc7a048e52b40b668c460c2793e897c4 (patch)
tree9717f573d9733b8cfa09a15ee44ed7768c427d7b /odb/shared-ptr
parentdce5d0658e67ced4d5fa64f98f598b86917927a7 (diff)
Move shared_ptr to the details namespace
Diffstat (limited to 'odb/shared-ptr')
-rw-r--r--odb/shared-ptr/base.cxx77
-rw-r--r--odb/shared-ptr/base.hxx102
-rw-r--r--odb/shared-ptr/base.ixx82
-rw-r--r--odb/shared-ptr/base.txx200
4 files changed, 0 insertions, 461 deletions
diff --git a/odb/shared-ptr/base.cxx b/odb/shared-ptr/base.cxx
deleted file mode 100644
index 197d20b..0000000
--- a/odb/shared-ptr/base.cxx
+++ /dev/null
@@ -1,77 +0,0 @@
-// file : odb/shared-ptr/base.cxx
-// author : Boris Kolpackov <boris@codesynthesis.com>
-// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
-// license : GNU GPL v2; see accompanying LICENSE file
-
-#include <odb/shared-ptr/base.hxx>
-
-using std::size_t;
-
-//
-//
-odb::share shared = odb::share (1);
-odb::share exclusive = odb::share (2);
-
-//
-//
-namespace odb
-{
- const char* not_shared::
- what () const throw ()
- {
- return "object is not shared";
- }
-
- bool shared_base::
- _dec_ref_callback ()
- {
- bool r (--counter_ == 0);
-
- if (r)
- {
- callback_->zero_counter (callback_->arg);
- r = (counter_ == 0);
- }
-
- return r;
- }
-}
-
-//
-//
-void*
-operator new (size_t n, odb::share s) throw (std::bad_alloc)
-{
- 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, odb::share s) throw ()
-{
- // 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/odb/shared-ptr/base.hxx b/odb/shared-ptr/base.hxx
deleted file mode 100644
index 4766aa7..0000000
--- a/odb/shared-ptr/base.hxx
+++ /dev/null
@@ -1,102 +0,0 @@
-// file : odb/shared-ptr/base.hxx
-// author : Boris Kolpackov <boris@codesynthesis.com>
-// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
-// license : GNU GPL v2; see accompanying LICENSE file
-
-#ifndef ODB_SHARED_PTR_BASE_HXX
-#define ODB_SHARED_PTR_BASE_HXX
-
-#include <new>
-#include <cstddef> // std::size_t
-
-#include <odb/exception.hxx>
-
-namespace odb
-{
- struct share
- {
- explicit
- share (char id);
-
- bool
- operator== (share) const;
-
- private:
- char id_;
- };
-}
-
-extern odb::share shared;
-extern odb::share exclusive;
-
-void*
-operator new (std::size_t, odb::share) throw (std::bad_alloc);
-
-void
-operator delete (void*, odb::share) throw ();
-
-namespace odb
-{
- struct not_shared: exception
- {
- virtual const char*
- what () const throw ();
- };
-
- struct shared_base
- {
- shared_base ();
- shared_base (const shared_base&);
- shared_base&
- operator= (const shared_base&);
-
- void
- _inc_ref ();
-
- bool
- _dec_ref ();
-
- std::size_t
- _ref_count () const;
-
- void*
- operator new (std::size_t, share) throw (std::bad_alloc);
-
- void
- operator delete (void*, share) throw ();
-
- void
- operator delete (void*) throw ();
-
- struct refcount_callback
- {
- void* arg;
- void (*zero_counter) (void*);
- };
-
- private:
- bool
- _dec_ref_callback ();
-
- protected:
- std::size_t counter_;
- refcount_callback* callback_;
- };
-
- 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 (const X*);
-}
-
-#include <odb/shared-ptr/base.ixx>
-#include <odb/shared-ptr/base.txx>
-
-#endif // ODB_SHARED_PTR_BASE_HXX
diff --git a/odb/shared-ptr/base.ixx b/odb/shared-ptr/base.ixx
deleted file mode 100644
index 61270a3..0000000
--- a/odb/shared-ptr/base.ixx
+++ /dev/null
@@ -1,82 +0,0 @@
-// file : odb/shared-ptr/base.ixx
-// author : Boris Kolpackov <boris@codesynthesis.com>
-// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
-// license : GNU GPL v2; see accompanying LICENSE file
-
-namespace odb
-{
- // 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), callback_ (0)
- {
- }
-
- inline shared_base::
- shared_base (const shared_base&)
- : counter_ (1), callback_ (0)
- {
- }
-
- inline shared_base& shared_base::
- operator= (const shared_base&)
- {
- return *this;
- }
-
- inline void shared_base::
- _inc_ref ()
- {
- counter_++;
- }
-
- inline bool shared_base::
- _dec_ref ()
- {
- if (callback_ == 0)
- return --counter_ == 0;
- else
- return _dec_ref_callback ();
- }
-
- inline std::size_t shared_base::
- _ref_count () const
- {
- return counter_;
- }
-
- inline void* shared_base::
- operator new (std::size_t n, share) throw (std::bad_alloc)
- {
- return ::operator new (n);
- }
-
- inline void shared_base::
- operator delete (void* p, share) throw ()
- {
- ::operator delete (p);
- }
-
- inline void shared_base::
- operator delete (void* p) throw ()
- {
- ::operator delete (p);
- }
-}
diff --git a/odb/shared-ptr/base.txx b/odb/shared-ptr/base.txx
deleted file mode 100644
index 5cf85f3..0000000
--- a/odb/shared-ptr/base.txx
+++ /dev/null
@@ -1,200 +0,0 @@
-// file : odb/shared-ptr/base.txx
-// author : Boris Kolpackov <boris@codesynthesis.com>
-// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
-// license : GNU GPL v2; see accompanying LICENSE file
-
-#include <odb/meta/answer.hxx>
-#include <odb/meta/polymorphic-p.hxx>
-
-namespace odb
-{
- 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 (const X* 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 (const X* p) : counter_ (p ? bits::counter (p) : 0) {}
- counter_ops (const counter_ops& x) : counter_ (x.counter_) {}
-
- template <typename Z>
- counter_ops (const counter_ops<Z, Z>& x) : counter_ (x.counter_) {}
-
- counter_ops&
- operator= (const counter_ops& x)
- {
- counter_ = x.counter_;
- return *this;
- }
-
- template <typename Z>
- counter_ops&
- operator= (const counter_ops<Z, Z>& x)
- {
- counter_ = x.counter_;
- return *this;
- }
-
- void
- reset (const X* 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 (const X*) const
- {
- return *counter_;
- }
-
- std::size_t* counter_;
- };
-
- template <typename Y>
- struct counter_ops<shared_base, Y>
- {
- counter_ops (const Y*) {}
- counter_ops (const counter_ops&) {}
-
- template <typename Z>
- counter_ops (const counter_ops<shared_base, Z>&) {}
-
- counter_ops&
- operator= (const counter_ops&)
- {
- return *this;
- }
-
- template <typename Z>
- counter_ops&
- operator= (const counter_ops<shared_base, Z>&)
- {
- return *this;
- }
-
- void
- reset (const Y*) {}
-
- 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 (const shared_base* 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 (const X* p)
- {
- bits::counter_ops<typename bits::counter_type<X>::r, X> c (p);
- return c.count (p);
- }
-}