ACE+TAO-7_0_8
[ACE_TAO.git] / ACE / tests / Dynamic_Test.cpp
blob958ac6fa10ef5d021e664f95373f3d7250c5c3df
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"
20 class A
22 public:
23 A ();
25 void *operator new (size_t n);
27 void *operator new (size_t n, const std::nothrow_t&) throw();
28 void operator delete (void *p, const std::nothrow_t&) throw ();
29 void * operator new (size_t n, void *p);
31 void operator delete (void *);
33 void operator delete (void *, void *);
35 /// Have we been dynamically created?
36 bool dynamic_;
39 void*
40 A::operator new (size_t n)
42 ACE_Dynamic *const dynamic_instance = ACE_Dynamic::instance ();
44 if (dynamic_instance == 0)
46 // If this ACE_TEST_ASSERT fails, it may be due to running of out TSS
47 // keys. Try using ACE_HAS_TSS_EMULATION, or increasing
48 // ACE_DEFAULT_THREAD_KEYS if already using TSS emulation.
49 ACE_TEST_ASSERT (dynamic_instance != 0);
51 ACE_throw_bad_alloc;
53 else
55 // Allocate the memory and store it (usually in thread-specific
56 // storage, depending on config flags).
57 dynamic_instance->set ();
59 return ::new char[n];
63 void*
64 A::operator new (size_t n, const std::nothrow_t&) throw()
66 ACE_Dynamic *const dynamic_instance = ACE_Dynamic::instance ();
68 if (dynamic_instance == 0)
70 // If this ACE_TEST_ASSERT fails, it may be due to running of out TSS
71 // keys. Try using ACE_HAS_TSS_EMULATION, or increasing
72 // ACE_DEFAULT_THREAD_KEYS if already using TSS emulation.
73 ACE_TEST_ASSERT (dynamic_instance != 0);
75 return 0;
77 else
79 // Allocate the memory and store it (usually in thread-specific
80 // storage, depending on config flags).
81 dynamic_instance->set ();
83 return ::new(std::nothrow) char[n];
87 void
88 A::operator delete (void *p, const std::nothrow_t&) throw()
90 ::delete [] static_cast <char *> (p);
93 void
94 A::operator delete (void *obj)
96 ::delete [] static_cast <char *> (obj);
99 A::A()
101 this->dynamic_ = ACE_Dynamic::instance ()->is_dynamic ();
103 if (this->dynamic_)
104 // Make sure to reset the flag.
105 ACE_Dynamic::instance ()->reset ();
109 run_main (int, ACE_TCHAR *[])
111 ACE_START_TEST (ACE_TEXT ("Dynamic_Test"));
112 A from_stack;
113 A* heap = 0;
114 ACE_NEW_RETURN (heap, A, 1);
115 if (from_stack.dynamic_)
117 ACE_ERROR_RETURN ((LM_ERROR,
118 ACE_TEXT ("dynamic_ is true for an object on the stack\n")),
122 if (!heap->dynamic_)
124 ACE_ERROR_RETURN ((LM_ERROR,
125 ACE_TEXT ("dynamic_ is false for an object from the heap\n")),
129 delete heap;
130 ACE_END_TEST;
131 return 0;