Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Shared_Malloc / test_position_independent_malloc.cpp
blob75e125b12d398d62238efb2be4fdfc0c9a2f41fe
1 // Test the capability of the "position-independent" <ACE_Malloc> to
2 // handle a single malloc that can be rooted at different base
3 // addresses each time it's used. The actual backing store used by
4 // <ACE_Malloc> is located in a memory-mapped file.
6 #include "test_position_independent_malloc.h"
7 #include "ace/PI_Malloc.h"
8 #include "ace/Based_Pointer_T.h"
9 #include "ace/Get_Opt.h"
10 #include "ace/Process_Mutex.h"
11 #include "ace/Malloc_T.h"
12 #include "ace/MMAP_Memory_Pool.h"
13 #include <memory>
15 #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1)
16 typedef ACE_PI_Control_Block CONTROL_BLOCK;
17 #else
18 typedef ACE_Control_Block CONTROL_BLOCK;
19 #endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */
21 typedef ACE_Malloc_T <ACE_MMAP_MEMORY_POOL, ACE_Process_Mutex, CONTROL_BLOCK> TEST_MALLOC;
23 // Default address for memory-mapped files.
24 static void *base_addr = ACE_DEFAULT_BASE_ADDR;
26 static void
27 print (Test_Data *data)
29 for (Test_Data *t = data; t != 0; t = t->next_)
31 ACE_DEBUG ((LM_DEBUG,
32 "<<<<\ni1_ = %d, i2_ = %d, i3_ = %d\n",
33 t->i1_,
34 t->i2_,
35 t->i3_));
36 ACE_DEBUG ((LM_DEBUG,
37 "*t->bpl_ = %d, t->long_test_->array_[0] = %d\n>>>>\n",
38 *t->long_test_->bpl_,
39 t->long_test_->array_[0]));
43 static void *
44 initialize (TEST_MALLOC *allocator)
46 void *ptr;
47 ACE_ALLOCATOR_RETURN (ptr,
48 allocator->malloc (sizeof (Test_Data)),
49 0);
50 Test_Data *data1 = new (ptr) Test_Data;
52 data1->i1_ = 111;
53 data1->i2_ = 222;
54 data1->i3_ = 333;
56 void *gap = 0;
57 ACE_ALLOCATOR_RETURN (gap,
58 allocator->malloc (sizeof (256)),
59 0);
61 allocator->free (gap);
63 ACE_ALLOCATOR_RETURN (ptr,
64 allocator->malloc (sizeof (Test_Data)),
65 0);
66 Test_Data *data2 = new (ptr) Test_Data;
68 data1->next_ = 0;
69 data1->i1_ = 111;
70 data1->i2_ = 222;
71 data1->i3_ = 333;
72 data2->next_ = data1;
73 data2->i1_ = -111;
74 data2->i2_ = -222;
75 data2->i3_ = -333;
77 // Test in shared memory using long (array/pointer)
78 ACE_ALLOCATOR_RETURN (ptr,
79 allocator->malloc (sizeof (Long_Test)),
80 0);
81 Long_Test *lt = new (ptr) Long_Test;
83 lt->array_[0] = 1000;
84 lt->array_[1] = 1001;
85 lt->array_[2] = 1002;
86 lt->array_[3] = 1003;
87 lt->array_[4] = 1004;
88 lt->bpl_ = lt->array_;
90 data1->long_test_= lt;
92 ACE_ASSERT (*lt->bpl_ == 1000);
93 ACE_ASSERT (lt->bpl_[3] == 1003);
95 ACE_ALLOCATOR_RETURN (ptr,
96 allocator->malloc (sizeof (Long_Test)),
97 0);
98 lt = new (ptr) Long_Test;
100 lt->array_[0] = 2000;
101 lt->array_[1] = 2001;
102 lt->array_[2] = 2002;
103 lt->array_[3] = 2003;
104 lt->array_[4] = 2004;
105 lt->bpl_ = lt->array_;
107 data2->long_test_= lt;
109 ACE_ASSERT (*lt->bpl_ == 2000);
110 ACE_ASSERT (lt->bpl_[4] == 2004);
112 return data2;
115 static void
116 parse_args (int argc, ACE_TCHAR *argv[])
118 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("a:T"));
120 for (int c;
121 (c = get_opt ()) != -1;
124 switch (c)
126 case 'a':
127 // Override the default base address.
128 base_addr = reinterpret_cast<void *> (static_cast<intptr_t> (ACE_OS::atoi (get_opt.opt_arg ())));
129 break;
130 case 'T':
131 #if defined (ACE_HAS_TRACE)
132 ACE_Trace::start_tracing ();
133 #endif /* ACE_HAS_TRACE */
134 break;
140 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
142 parse_args (argc, argv);
144 ACE_MMAP_Memory_Pool_Options options (base_addr);
146 // Create an allocator.
147 TEST_MALLOC *ptr = 0;
148 ACE_NEW_RETURN (ptr,
149 TEST_MALLOC (ACE_TEXT("test_file"),
150 ACE_TEXT("test_lock"),
151 &options),
153 std::unique_ptr <TEST_MALLOC> allocator (ptr);
154 void *data = 0;
156 // This is the first time in, so we allocate the memory and bind it
157 // to the name "foo".
158 if (allocator->find ("foo",
159 data) == -1)
161 data = initialize (allocator.get ());
163 if (allocator->bind ("foo",
164 data) == -1)
165 ACE_ERROR_RETURN ((LM_ERROR,
166 "%p\n",
167 "bind"),
170 ACE_DEBUG ((LM_DEBUG,
171 "Run again to see results and release resources.\n"));
173 // If we find "foo" then we're running the "second" time, so we must
174 // release the resources.
175 else
177 print ((Test_Data *) data);
178 allocator->free (data);
179 allocator->remove ();
180 ACE_DEBUG ((LM_DEBUG,
181 "all resources released\n"));
184 return 0;