Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Shared_Memory / PI_Malloc.cpp
blobe9da843f997f1c0abaf748c54cf2b0c0cd5252d9
1 #include "ace/OS_NS_stdio.h"
2 #include "ace/OS_NS_string.h"
4 // Listing 1 code/ch17
5 #include "ace/MMAP_Memory_Pool.h"
6 #include "ace/Malloc_T.h"
7 #include "ace/Null_Mutex.h"
8 #include "ace/PI_Malloc.h"
10 typedef ACE_Malloc_T <ACE_MMAP_MEMORY_POOL,
11 ACE_Null_Mutex,
12 ACE_PI_Control_Block>
13 ALLOCATOR;
14 typedef ACE_Malloc_LIFO_Iterator_T<ACE_MMAP_MEMORY_POOL,
15 ACE_Null_Mutex,
16 ACE_PI_Control_Block>
17 MALLOC_LIFO_ITERATOR;
19 ALLOCATOR *g_allocator;
20 // Listing 1
21 // Listing 2 code/ch17
22 class Record
24 public:
25 Record (int id1, int id2, char *name)
26 : id1_(id1), id2_(id2)
28 size_t len = ACE_OS::strlen (name) + 1;
29 char *buf =
30 reinterpret_cast<char *> (g_allocator->malloc (len));
31 ACE_OS::strcpy (buf, name);
32 name_ = buf;
35 ~Record() { g_allocator->free (name_.addr ()); }
37 char *name () { return name_; }
38 int id1 () { return id1_; }
39 int id2 () { return id2_; }
41 private:
42 int id1_;
43 int id2_;
44 ACE_Based_Pointer_Basic<char> name_;
46 // Listing 2
48 void showRecords ()
50 ACE_DEBUG ((LM_DEBUG,
51 ACE_TEXT ("The following records were found:\n")));
54 MALLOC_LIFO_ITERATOR iter (*g_allocator);
56 for (void *temp = 0; iter.next (temp) != 0; iter.advance ())
58 Record *record =
59 reinterpret_cast<Record *> (temp);
60 ACE_DEBUG ((LM_DEBUG,
61 ACE_TEXT ("Record name: %C|id1:%d|id2:%d\n"),
62 record->name(), record->id1(), record->id2()));
67 int addRecords ()
69 char buf[32];
71 for (int i = 0; i < 10; i++)
73 ACE_OS::sprintf (buf, "%s:%d", "Record", i);
75 void *memory = g_allocator->malloc (sizeof (Record));
76 if (memory == 0)
77 ACE_ERROR_RETURN ((LM_ERROR,
78 ACE_TEXT ("%p\n"),
79 ACE_TEXT ("Unable to malloc")),
80 -1);
82 // Allocate and place record
83 Record* newRecord = new (memory) Record (i, i+1, buf);
84 if (g_allocator->bind (buf, newRecord) == -1)
85 ACE_ERROR_RETURN ((LM_ERROR,
86 ACE_TEXT ("%p\n"),
87 ACE_TEXT ("bind failed")),
88 -1);
91 return 0;
94 // Listing 3 code/ch17
95 // Backing file where the data is kept.
96 #define BACKING_STORE ACE_TEXT("backing2.store")
98 int ACE_TMAIN (int argc, ACE_TCHAR *[])
100 if (argc > 1)
102 ACE_MMAP_Memory_Pool_Options options
103 (ACE_DEFAULT_BASE_ADDR,
104 ACE_MMAP_Memory_Pool_Options::ALWAYS_FIXED);
105 ACE_NEW_RETURN (g_allocator,
106 ALLOCATOR (BACKING_STORE,
107 BACKING_STORE,
108 &options),
109 -1);
110 ACE_DEBUG ((LM_DEBUG,
111 ACE_TEXT ("Mapped to base address %@\n"),
112 g_allocator->base_addr ()));
114 showRecords ();
116 else
118 ACE_MMAP_Memory_Pool_Options options
119 (0, ACE_MMAP_Memory_Pool_Options::NEVER_FIXED);
120 ACE_NEW_RETURN (g_allocator,
121 ALLOCATOR (BACKING_STORE,
122 BACKING_STORE,
123 &options),
124 -1);
126 ACE_DEBUG ((LM_DEBUG,
127 ACE_TEXT ("Mapped to base address %@\n"),
128 g_allocator->base_addr ()));
130 addRecords();
133 g_allocator->sync ();
134 delete g_allocator;
135 return 0;
137 // Listing 3