aboutsummaryrefslogtreecommitdiff
path: root/odb/details/shared-ptr/base.cxx
blob: 590d8449890c5db52c651284fbee4f81741dbed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// file      : odb/details/shared-ptr/base.cxx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <odb/details/shared-ptr/base.hxx>

using std::size_t;

namespace odb
{
  namespace details
  {
    share shared = share (1);
    share exclusive = share (2);

    const char* not_shared::
    what () const throw ()
    {
      return "object is not shared";
    }

    bool shared_base::
    _dec_ref_callback ()
    {
      bool r (--counter_ == 0);

      if (r)
        r = callback_->zero_counter (callback_->arg);

      return r;
    }
  }
}

void*
operator new (size_t n, odb::details::share s) throw (std::bad_alloc)
{
  if (s == odb::details::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::details::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 == odb::details::shared)
  {
    size_t* sp = static_cast<size_t*> (p);
    sp -= 2;
    operator delete (sp);
  }
  else
    operator delete (p);
}