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"
15 #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1)
16 typedef ACE_PI_Control_Block CONTROL_BLOCK
;
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
;
27 print (Test_Data
*data
)
29 for (Test_Data
*t
= data
; t
!= 0; t
= t
->next_
)
32 "<<<<\ni1_ = %d, i2_ = %d, i3_ = %d\n",
37 "*t->bpl_ = %d, t->long_test_->array_[0] = %d\n>>>>\n",
39 t
->long_test_
->array_
[0]));
44 initialize (TEST_MALLOC
*allocator
)
47 ACE_ALLOCATOR_RETURN (ptr
,
48 allocator
->malloc (sizeof (Test_Data
)),
50 Test_Data
*data1
= new (ptr
) Test_Data
;
57 ACE_ALLOCATOR_RETURN (gap
,
58 allocator
->malloc (sizeof (256)),
61 allocator
->free (gap
);
63 ACE_ALLOCATOR_RETURN (ptr
,
64 allocator
->malloc (sizeof (Test_Data
)),
66 Test_Data
*data2
= new (ptr
) Test_Data
;
77 // Test in shared memory using long (array/pointer)
78 ACE_ALLOCATOR_RETURN (ptr
,
79 allocator
->malloc (sizeof (Long_Test
)),
81 Long_Test
*lt
= new (ptr
) Long_Test
;
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
)),
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);
116 parse_args (int argc
, ACE_TCHAR
*argv
[])
118 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("a:T"));
121 (c
= get_opt ()) != -1;
127 // Override the default base address.
128 base_addr
= reinterpret_cast<void *> (static_cast<intptr_t> (ACE_OS::atoi (get_opt
.opt_arg ())));
131 #if defined (ACE_HAS_TRACE)
132 ACE_Trace::start_tracing ();
133 #endif /* ACE_HAS_TRACE */
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;
149 TEST_MALLOC (ACE_TEXT("test_file"),
150 ACE_TEXT("test_lock"),
153 std::unique_ptr
<TEST_MALLOC
> allocator (ptr
);
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",
161 data
= initialize (allocator
.get ());
163 if (allocator
->bind ("foo",
165 ACE_ERROR_RETURN ((LM_ERROR
,
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.
177 print ((Test_Data
*) data
);
178 allocator
->free (data
);
179 allocator
->remove ();
180 ACE_DEBUG ((LM_DEBUG
,
181 "all resources released\n"));