Make x.0.10 publicly available
[ACE_TAO.git] / ACE / tests / Dynamic_Test.cpp
blob77c000b2c469643b55d2f189e9bfb1433bc79288
2 //=============================================================================
3 /**
4 * @file Dynamic_Test.cpp
6 * This tests the ACE_Dynamic class
8 * @author Johnny Willemsen <jwillemsen@remedy.nl>
9 */
10 //=============================================================================
13 #include "test_config.h"
14 #include "ace/OS_NS_string.h"
15 #include "ace/Dynamic.h"
16 #include "ace/OS_Memory.h"
19 class A
21 public:
22 A ();
24 void *operator new (size_t n);
26 void *operator new (size_t n, const std::nothrow_t&) throw();
27 void operator delete (void *p, const std::nothrow_t&) throw ();
28 void * operator new (size_t n, void *p);
30 void operator delete (void *);
32 void operator delete (void *, void *);
34 /// Have we been dynamically created?
35 bool dynamic_;
38 void*
39 A::operator new (size_t n)
41 ACE_Dynamic *const dynamic_instance = ACE_Dynamic::instance ();
43 if (dynamic_instance == 0)
45 // If this ACE_TEST_ASSERT fails, it may be due to running of out TSS
46 // keys. Try using ACE_HAS_TSS_EMULATION, or increasing
47 // ACE_DEFAULT_THREAD_KEYS if already using TSS emulation.
48 ACE_TEST_ASSERT (dynamic_instance != 0);
50 ACE_throw_bad_alloc;
52 else
54 // Allocate the memory and store it (usually in thread-specific
55 // storage, depending on config flags).
56 dynamic_instance->set ();
58 return ::new char[n];
62 void*
63 A::operator new (size_t n, const std::nothrow_t&) throw()
65 ACE_Dynamic *const dynamic_instance = ACE_Dynamic::instance ();
67 if (dynamic_instance == 0)
69 // If this ACE_TEST_ASSERT fails, it may be due to running of out TSS
70 // keys. Try using ACE_HAS_TSS_EMULATION, or increasing
71 // ACE_DEFAULT_THREAD_KEYS if already using TSS emulation.
72 ACE_TEST_ASSERT (dynamic_instance != 0);
74 return 0;
76 else
78 // Allocate the memory and store it (usually in thread-specific
79 // storage, depending on config flags).
80 dynamic_instance->set ();
82 return ::new(std::nothrow) char[n];
86 void
87 A::operator delete (void *p, const std::nothrow_t&) throw()
89 ::delete [] static_cast <char *> (p);
92 void
93 A::operator delete (void *obj)
95 ::delete [] static_cast <char *> (obj);
98 A::A()
100 this->dynamic_ = ACE_Dynamic::instance ()->is_dynamic ();
102 if (this->dynamic_)
103 // Make sure to reset the flag.
104 ACE_Dynamic::instance ()->reset ();
108 run_main (int, ACE_TCHAR *[])
110 ACE_START_TEST (ACE_TEXT ("Dynamic_Test"));
111 A from_stack;
112 A* heap = 0;
113 ACE_NEW_RETURN (heap, A, 1);
114 if (from_stack.dynamic_)
116 ACE_ERROR_RETURN ((LM_ERROR,
117 ACE_TEXT ("dynamic_ is true for an object on the stack\n")),
121 if (!heap->dynamic_)
123 ACE_ERROR_RETURN ((LM_ERROR,
124 ACE_TEXT ("dynamic_ is false for an object from the heap\n")),
128 delete heap;
129 ACE_END_TEST;
130 return 0;