Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Bug_3911_Regression_Test.cpp
blob55a98040cd0a64f0eb6dc4d16d486ebd0a9f0f82
1 /**
2 * @file Bug_3911_Regression_Test.cpp
4 * Reproduces the problem reported in bug 3911
5 * http://bugzilla.dre.vanderbilt.edu/show_bug.cgi?id=3911
6 */
8 #include "test_config.h"
10 #include <ace/Malloc_T.h>
11 #include <ace/Memory_Pool.h>
12 #include <ace/Process_Mutex.h>
13 #include <ace/Null_Mutex.h>
14 #include <ace/PI_Malloc.h>
16 #define MMAP_FILENAME ACE_TEXT ("shared_memory")
18 static void
19 init_test ()
21 // Cleanup the MMAP file so we won't trip over the leftover mmap
22 // file from the previous crash.
23 ACE_MMAP_Memory_Pool_Options options (ACE_DEFAULT_BASE_ADDR);
24 //FUZZ: disable check_for_lack_ACE_OS
25 ACE_MMAP_Memory_Pool mmap (MMAP_FILENAME, &options);
26 //FUZZ: enable check_for_lack_ACE_OS
28 size_t rbyte = 0;
29 int ft = 0;
30 mmap.init_acquire (1024, rbyte, ft);
31 mmap.release ();
34 class ShmemMan
36 private:
37 // Used to implement the singleton pattern:
38 static ShmemMan* c_instance;
40 // The memory pool managing all the shared memory:
41 ACE_Malloc_T<ACE_MMAP_MEMORY_POOL,
42 ACE_Null_Mutex,
43 ACE_PI_Control_Block>* c_memory_pool;
45 protected:
46 ShmemMan(bool no_crash)
48 if (no_crash)
50 ACE_MMAP_Memory_Pool_Options options (ACE_DEFAULT_BASE_ADDR);
51 c_memory_pool = new ACE_Malloc_T<ACE_MMAP_MEMORY_POOL,
52 ACE_Null_Mutex,
53 ACE_PI_Control_Block>(MMAP_FILENAME
54 , ACE_TEXT(""), &options
57 else
59 c_memory_pool = new ACE_Malloc_T<ACE_MMAP_MEMORY_POOL,
60 ACE_Null_Mutex,
61 ACE_PI_Control_Block>(MMAP_FILENAME);
65 public:
66 ~ShmemMan()
68 c_memory_pool->release();
69 delete c_memory_pool;
72 static ShmemMan* getInstance(bool no_crash)
74 if (c_instance == 0)
76 c_instance = new ShmemMan(no_crash);
78 return c_instance;
81 int clean()
83 c_memory_pool->release();
84 c_memory_pool->remove();
86 return 0;
89 void* getMemoryBlock(const char* block_name, unsigned int block_size)
91 void* shared = 0;
93 ACE_DEBUG((LM_INFO, ACE_TEXT("errno = %d. Looking for a Shared Memory block named %C\n"),
94 ACE_OS::last_error(),
95 block_name));
97 if (c_memory_pool->find(block_name, shared) == 0)
99 // An existing block was found, so take that:
100 ACE_DEBUG((LM_INFO, ACE_TEXT("Shared Memory block %C was found."),
101 block_name));
103 else
105 // Allocate the memory and bind it to a name:
106 ACE_DEBUG((LM_INFO, ACE_TEXT("Shared Memory block %C was not found. errno = %d. Trying to allocate new block\n"),
107 block_name,
108 ACE_OS::last_error()));
109 shared = c_memory_pool->malloc(block_size);
110 // reinterpret_cast due to some compilers warning against ordering pointers with integers
111 if (ptrdiff_t (shared) < 0)
113 ACE_DEBUG((LM_INFO, ACE_TEXT("New Shared Memory block could not be allocated. errno = %d.\n"),
114 ACE_OS::last_error()));
115 return (void*)(-1);
117 ACE_DEBUG((LM_INFO, ACE_TEXT("New Shared Memory block was allocated, trying to bind it to the name %C\n"),
118 block_name));
119 if (c_memory_pool->bind(block_name, shared) < 0)
121 ACE_DEBUG((LM_INFO, ACE_TEXT("New Shared Memory block could not be bound to the name %C. errno = %d.\n"),
122 block_name,
123 ACE_OS::last_error()));
125 return (void*)(-1);
129 return shared;
133 ShmemMan* ShmemMan::c_instance = 0;
135 const char* block_name = "block_1";
137 // Main function.
139 run_main (int argc, ACE_TCHAR * argv[])
141 ACE_START_TEST (ACE_TEXT ("Bug_3911_Regression_Test"));
143 init_test ();
145 bool no_crash = (argc>1 && argv[1][0]=='1');
146 ShmemMan* smm = ShmemMan::getInstance (no_crash);
148 void* buf = smm->getMemoryBlock (block_name, 10 * 4096);
150 ACE_DEBUG((LM_INFO, ACE_TEXT("allocated shmem block at %@\n"), buf));
152 smm->clean ();
154 ACE_END_TEST;
155 return 0;