1 ////////////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2001 by Andrei Alexandrescu
4 // This code accompanies the book:
5 // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
6 // Patterns Applied". Copyright (c) 2001. Addison-Wesley.
7 // Permission to use, copy, modify, distribute and sell this software for any
8 // purpose is hereby granted without fee, provided that the above copyright
9 // notice appear in all copies and that both that copyright notice and this
10 // permission notice appear in supporting documentation.
11 // The author or Addison-Wesley Longman make no representations about the
12 // suitability of this software for any purpose. It is provided "as is"
13 // without express or implied warranty.
14 ////////////////////////////////////////////////////////////////////////////////
16 // $Id: Singleton.cpp 756 2006-10-17 20:05:42Z syntheticpp $
19 #include <loki/Singleton.h>
22 #ifdef LOKI_ENABLE_NEW_SETLONGLIVITY_HELPER_DATA_IMPL
23 Loki::Private::TrackerArray *Loki::Private::pTrackerArray = 0;
25 Loki::Private::TrackerArray Loki::Private::pTrackerArray = 0;
26 unsigned int Loki::Private::elements = 0;
29 ////////////////////////////////////////////////////////////////////////////////
31 // Ensures proper destruction of objects with longevity
32 ////////////////////////////////////////////////////////////////////////////////
34 #ifdef LOKI_ENABLE_NEW_SETLONGLIVITY_HELPER_DATA_IMPL
36 void LOKI_C_CALLING_CONVENTION_QUALIFIER Loki::Private::AtExitFn()
38 assert(pTrackerArray!=0 && !pTrackerArray->empty());
40 // Pick the element at the top of the stack
41 LifetimeTracker *pTop = pTrackerArray->back();
43 // Remove that object off the stack _before_ deleting pTop
44 pTrackerArray->pop_back();
46 // Destroy the element
49 // Destroy stack when it's empty _after_ deleting pTop
50 if(pTrackerArray->empty())
59 void LOKI_C_CALLING_CONVENTION_QUALIFIER Loki::Private::AtExitFn()
61 assert(elements > 0 && pTrackerArray != 0);
62 // Pick the element at the top of the stack
63 LifetimeTracker *pTop = pTrackerArray[elements - 1];
64 // Remove that object off the stack
65 // Don't check errors - realloc with less memory
67 pTrackerArray = static_cast<TrackerArray>(std::realloc(
68 pTrackerArray, sizeof(*pTrackerArray) * --elements));
69 // Destroy the element