summaryrefslogtreecommitdiff
path: root/examples/APG/Containers/Allocator.cpp
blob: 9f9fe92d2ccf9c7fc8d713bd4fbd98981ee424d6 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// $Id$

#include "ace/Containers.h"
#include "ace/Malloc_T.h"
#include "ace/Synch.h" // Needed for the lock.
#include "DataElement.h"

class StackExample
{
public:
  // Illustrate all the differnet
  // types of stacks provided by ACE.
  int run (void);

private:
  // Illustrate the use of an unbounded stack.
  int runUnboundedStack (ACE_Allocator* allocator);
};

// Listing 1 code/ch05
int StackExample::run (void)
{
  ACE_TRACE (ACE_TEXT ("StackUser::run"));

  ACE_Allocator *allocator = 0;
  size_t block_size = sizeof(ACE_Node<DataElement>);
  ACE_NEW_RETURN
    (allocator,
     ACE_Dynamic_Cached_Allocator<ACE_Null_Mutex>
       (100 + 1, block_size),
     -1);

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n# of live objects %d\n"),
              DataElement::numOfActiveObjects ()));

  ACE_ASSERT (this->runUnboundedStack (allocator) != -1);

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n# of live objects %d\n"),
              DataElement::numOfActiveObjects ()));

  delete allocator;
  return 0;
}
// Listing 1
// Listing 2 code/ch05
int StackExample::runUnboundedStack (ACE_Allocator* allocator)
{
  ACE_TRACE (ACE_TEXT ("StackExample::runUnboundedStack"));

  // Pass in an allocator during construction.
  ACE_Unbounded_Stack<DataElement> ustack (allocator);

  for (int m = 0; m < 100; m++)
    {
      DataElement elem (m);
      int result = ustack.push (elem);
      if (result == -1)
        ACE_ERROR_RETURN
          ((LM_ERROR, ACE_TEXT ("%p\n"),
            ACE_TEXT ("Push Next Element")),
           -1);
    }

  void* furtherMemory = 0;
  furtherMemory = allocator->malloc
    (sizeof(ACE_Node<DataElement>));
  ACE_ASSERT (furtherMemory == 0);

  // No memory left..
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%p\n"),
              ACE_TEXT ("No memory..")));

  // Free up some memory in the allocator.
  DataElement e;
  for (int n = 0; n < 10; n++)
    {
      ustack.pop (e);
    }

  furtherMemory =
    allocator->malloc (sizeof (ACE_Node<DataElement>));
  ACE_ASSERT (furtherMemory != 0);

  return 0;
}
// Listing 2

int ACE_TMAIN (int, ACE_TCHAR *[])
{
  StackExample se;
  return se.run ();
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Dynamic_Cached_Allocator<ACE_Null_Mutex>;
template class ACE_Unbounded_Stack<DataElement>;
template class ACE_Node<DataElement>;
template class ACE_Cached_Mem_Pool_Node<char>;
template class ACE_Free_List<ACE_Cached_Mem_Pool_Node<char> >;
template class ACE_Locked_Free_List<ACE_Cached_Mem_Pool_Node<char>,ACE_Null_Mutex>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Dynamic_Cached_Allocator<ACE_Null_Mutex>
#pragma instantiate ACE_Unbounded_Stack<DataElement>
#pragma instantiate ACE_Node<DataElement>
#pragma instantiate ACE_Cached_Mem_Pool_Node<char>
#pragma instantiate ACE_Free_List<ACE_Cached_Mem_Pool_Node<char> >
#pragma instantiate ACE_Locked_Free_List<ACE_Cached_Mem_Pool_Node<char>,ACE_Null_Mutex>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION*/