Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / Shared_Malloc / test_multiple_mallocs.cpp
blob582cb33543be8187c4bc71fe53fb8dd3a039453a
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"
8 #include <memory>
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;
17 #else
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";
30 int
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",
39 "request_lock",
40 &request_options),
41 1);
43 std::unique_ptr <ACE_Allocator_Adapter<TEST_MALLOC> > shmem_request (adapter_ptr);
44 ACE_MMAP_Memory_Pool_Options response_options (RESPONSE_BASE_ADDR);
46 TEST_MALLOC *ptr = 0;
47 // Create a non-adapter version of an allocator.
48 ACE_NEW_RETURN (ptr,
49 TEST_MALLOC (ACE_TEXT("response_file"),
50 ACE_TEXT("response_lock"),
51 &response_options),
52 1);
53 std::unique_ptr <TEST_MALLOC> shmem_response (ptr);
54 void *data = 0;
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",
59 data) == 0)
61 ACE_DEBUG ((LM_DEBUG,
62 "%s\n",
63 data));
64 shmem_request->remove ();
67 // This is the first time in, so we allocate the memory and bind it
68 // to the name "foo".
69 else
71 ACE_ALLOCATOR_RETURN (data,
72 shmem_request->malloc (ACE_OS::strlen (request_string) + 1),
73 1);
74 ACE_OS::strcpy ((char *) data,
75 request_string);
77 if (shmem_request->bind ("foo",
78 data) == -1)
79 ACE_ERROR_RETURN ((LM_ERROR,
80 "%p\n",
81 "bind"),
82 1);
84 data = 0;
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",
89 data) == 0)
91 ACE_DEBUG ((LM_DEBUG,
92 "%s\n",
93 data));
94 shmem_response->remove ();
95 ACE_DEBUG ((LM_DEBUG,
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".
101 else
103 ACE_ALLOCATOR_RETURN (data,
104 shmem_response->malloc (ACE_OS::strlen (response_string) + 1),
106 ACE_OS::strcpy ((char *) data,
107 response_string);
109 if (shmem_response->bind ("foo",
110 data) == -1)
111 ACE_ERROR ((LM_ERROR,
112 "%p\n",
113 "bind"));
114 else
115 ACE_DEBUG ((LM_DEBUG,
116 "Run again to see results and release resources.\n"));
119 return 0;