1 #include "ace/Containers.h"
2 #include "ace/Malloc_T.h"
3 #include "ace/Synch.h" // Needed for the lock.
4 #include "DataElement.h"
9 // Illustrate all the differnet
10 // types of stacks provided by ACE.
14 // Illustrate the use of an unbounded stack.
15 int runUnboundedStack (ACE_Allocator
* allocator
);
18 // Listing 1 code/ch05
19 int StackExample::run ()
21 ACE_TRACE ("StackExample::run");
23 ACE_Allocator
*allocator
= 0;
24 size_t block_size
= sizeof(ACE_Node
<DataElement
>);
27 ACE_Dynamic_Cached_Allocator
<ACE_Null_Mutex
>
28 (100 + 1, block_size
),
31 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n# of live objects %d\n"),
32 DataElement::numOfActiveObjects ()));
34 ACE_TEST_ASSERT (this->runUnboundedStack (allocator
) != -1);
36 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n# of live objects %d\n"),
37 DataElement::numOfActiveObjects ()));
43 // Listing 2 code/ch05
44 int StackExample::runUnboundedStack (ACE_Allocator
* allocator
)
46 ACE_TRACE ("StackExample::runUnboundedStack");
48 // Pass in an allocator during construction.
49 ACE_Unbounded_Stack
<DataElement
> ustack (allocator
);
51 for (int m
= 0; m
< 100; m
++)
54 int result
= ustack
.push (elem
);
57 ((LM_ERROR
, ACE_TEXT ("%p\n"),
58 ACE_TEXT ("Push Next Element")),
62 void* furtherMemory
= 0;
63 furtherMemory
= allocator
->malloc
64 (sizeof(ACE_Node
<DataElement
>));
65 ACE_TEST_ASSERT (furtherMemory
== 0);
68 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%p\n"),
69 ACE_TEXT ("No memory..")));
71 // Free up some memory in the allocator.
73 for (int n
= 0; n
< 10; n
++)
79 allocator
->malloc (sizeof (ACE_Node
<DataElement
>));
80 ACE_TEST_ASSERT (furtherMemory
!= 0);
86 int ACE_TMAIN (int, ACE_TCHAR
*[])