Doxygen changes
[ACE_TAO.git] / ACE / tests / Bug_3911_Regression_Test.cpp
blob39657681b563a244d3f0a38312be509c9caedc56
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 #if defined (ACE_HAS_WINCE) || defined (ACE_OPENVMS)
24 // WinCE cannot do fixed base, ever.
25 ACE_MMAP_Memory_Pool_Options options
26 (0,
27 ACE_MMAP_Memory_Pool_Options::NEVER_FIXED);
28 #else
29 ACE_MMAP_Memory_Pool_Options options (ACE_DEFAULT_BASE_ADDR);
30 #endif /* ACE_HAS_WINCE */
31 //FUZZ: disable check_for_lack_ACE_OS
32 ACE_MMAP_Memory_Pool mmap (MMAP_FILENAME, &options);
33 //FUZZ: enable check_for_lack_ACE_OS
35 size_t rbyte = 0;
36 int ft = 0;
37 mmap.init_acquire (1024, rbyte, ft);
38 mmap.release ();
41 class ShmemMan
43 private:
44 // Used to implement the singleton pattern:
45 static ShmemMan* c_instance;
47 // The memory pool managing all the shared memory:
48 ACE_Malloc_T<ACE_MMAP_MEMORY_POOL,
49 ACE_Null_Mutex,
50 ACE_PI_Control_Block>* c_memory_pool;
52 protected:
53 ShmemMan(bool no_crash)
55 if (no_crash)
57 ACE_MMAP_Memory_Pool_Options options (ACE_DEFAULT_BASE_ADDR);
58 c_memory_pool = new ACE_Malloc_T<ACE_MMAP_MEMORY_POOL,
59 ACE_Null_Mutex,
60 ACE_PI_Control_Block>(MMAP_FILENAME
61 , ACE_TEXT(""), &options
64 else
66 c_memory_pool = new ACE_Malloc_T<ACE_MMAP_MEMORY_POOL,
67 ACE_Null_Mutex,
68 ACE_PI_Control_Block>(MMAP_FILENAME);
72 public:
73 ~ShmemMan()
75 c_memory_pool->release();
76 delete c_memory_pool;
79 static ShmemMan* getInstance(bool no_crash)
81 if (c_instance == 0)
83 c_instance = new ShmemMan(no_crash);
85 return c_instance;
88 int clean()
90 c_memory_pool->release();
91 c_memory_pool->remove();
93 return 0;
96 void* getMemoryBlock(const char* block_name, unsigned int block_size)
98 void* shared = 0;
100 ACE_DEBUG((LM_INFO, ACE_TEXT("errno = %d. Looking for a Shared Memory block named %C\n"),
101 ACE_OS::last_error(),
102 block_name));
104 if (c_memory_pool->find(block_name, shared) == 0)
106 // An existing block was found, so take that:
107 ACE_DEBUG((LM_INFO, ACE_TEXT("Shared Memory block %C was found."),
108 block_name));
110 else
112 // Allocate the memory and bind it to a name:
113 ACE_DEBUG((LM_INFO, ACE_TEXT("Shared Memory block %C was not found. errno = %d. Trying to allocate new block\n"),
114 block_name,
115 ACE_OS::last_error()));
116 shared = c_memory_pool->malloc(block_size);
117 // reinterpret_cast due to some compilers warning against ordering pointers with integers
118 if (ptrdiff_t (shared) < 0)
120 ACE_DEBUG((LM_INFO, ACE_TEXT("New Shared Memory block could not be allocated. errno = %d.\n"),
121 ACE_OS::last_error()));
122 return (void*)(-1);
124 ACE_DEBUG((LM_INFO, ACE_TEXT("New Shared Memory block was allocated, trying to bind it to the name %C\n"),
125 block_name));
126 if (c_memory_pool->bind(block_name, shared) < 0)
128 ACE_DEBUG((LM_INFO, ACE_TEXT("New Shared Memory block could not be bound to the name %C. errno = %d.\n"),
129 block_name,
130 ACE_OS::last_error()));
132 return (void*)(-1);
136 return shared;
141 ShmemMan* ShmemMan::c_instance = 0;
143 const char* block_name = "block_1";
145 // Main function.
147 run_main (int argc, ACE_TCHAR * argv[])
149 ACE_START_TEST (ACE_TEXT ("Bug_3911_Regression_Test"));
151 init_test ();
153 bool no_crash = (argc>1 && argv[1][0]=='1');
154 ShmemMan* smm = ShmemMan::getInstance (no_crash);
156 void* buf = smm->getMemoryBlock (block_name, 10 * 4096);
158 ACE_DEBUG((LM_INFO, ACE_TEXT("allocated shmem block at %@\n"), buf));
160 smm->clean ();
162 ACE_END_TEST;
163 return 0;