1 // Test the capability of <ACE_Malloc> to handle multiple mallocs
2 // rooted at different base addresses.
4 #include "ace/OS_NS_string.h"
5 #include "ace/Malloc_T.h"
6 #include "ace/MMAP_Memory_Pool.h"
7 #include "ace/Process_Mutex.h"
10 typedef ACE_Malloc
<ACE_MMAP_MEMORY_POOL
, ACE_Process_Mutex
> TEST_MALLOC
;
12 #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1)
13 // The Address for the shared memory mapped files defaults to wherever
14 // the OS wants to map it.
15 const void *REQUEST_BASE_ADDR
= 0;
16 const void *RESPONSE_BASE_ADDR
= 0;
18 // Default address for shared memory mapped files and SYSV shared
19 // memory (defaults to 64 M).
20 const void *REQUEST_BASE_ADDR
= ((void *) (64 * 1024 * 1024));
22 // Default address for shared memory mapped files and SYSV shared
23 // memory (defaults to 64 M).
24 const void *RESPONSE_BASE_ADDR
= ((void *) (128 * 1024 * 1024));
25 #endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */
27 static const char *request_string
= "hello from request repository";
28 static const char *response_string
= "hello from response repository";
31 ACE_TMAIN (int, ACE_TCHAR
*[])
33 ACE_MMAP_Memory_Pool_Options
request_options (REQUEST_BASE_ADDR
);
35 // Create an adapter version of an allocator.
36 ACE_Allocator_Adapter
<TEST_MALLOC
> *adapter_ptr
= 0;
37 ACE_NEW_RETURN (adapter_ptr
,
38 ACE_Allocator_Adapter
<TEST_MALLOC
> ("request_file",
43 std::unique_ptr
<ACE_Allocator_Adapter
<TEST_MALLOC
> > shmem_request (adapter_ptr
);
44 ACE_MMAP_Memory_Pool_Options
response_options (RESPONSE_BASE_ADDR
);
47 // Create a non-adapter version of an allocator.
49 TEST_MALLOC (ACE_TEXT("response_file"),
50 ACE_TEXT("response_lock"),
53 std::unique_ptr
<TEST_MALLOC
> shmem_response (ptr
);
56 // If we find "foo" then we're running the "second" time, so we must
57 // release the resources.
58 if (shmem_request
->find ("foo",
64 shmem_request
->remove ();
67 // This is the first time in, so we allocate the memory and bind it
71 ACE_ALLOCATOR_RETURN (data
,
72 shmem_request
->malloc (ACE_OS::strlen (request_string
) + 1),
74 ACE_OS::strcpy ((char *) data
,
77 if (shmem_request
->bind ("foo",
79 ACE_ERROR_RETURN ((LM_ERROR
,
86 // If we find "foo" then we're running the "second" time, so we must
87 // release the resources.
88 if (shmem_response
->find ("foo",
94 shmem_response
->remove ();
96 "all shared memory resources have been released\n"));
99 // This is the first time in, so we allocate the memory and bind it
100 // to the name "foo".
103 ACE_ALLOCATOR_RETURN (data
,
104 shmem_response
->malloc (ACE_OS::strlen (response_string
) + 1),
106 ACE_OS::strcpy ((char *) data
,
109 if (shmem_response
->bind ("foo",
111 ACE_ERROR ((LM_ERROR
,
115 ACE_DEBUG ((LM_DEBUG
,
116 "Run again to see results and release resources.\n"));