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
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")
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
27 ACE_MMAP_Memory_Pool_Options::NEVER_FIXED
);
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
37 mmap
.init_acquire (1024, rbyte
, ft
);
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
,
50 ACE_PI_Control_Block
>* c_memory_pool
;
53 ShmemMan(bool 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
,
60 ACE_PI_Control_Block
>(MMAP_FILENAME
61 , ACE_TEXT(""), &options
66 c_memory_pool
= new ACE_Malloc_T
<ACE_MMAP_MEMORY_POOL
,
68 ACE_PI_Control_Block
>(MMAP_FILENAME
);
75 c_memory_pool
->release();
79 static ShmemMan
* getInstance(bool no_crash
)
83 c_instance
= new ShmemMan(no_crash
);
90 c_memory_pool
->release();
91 c_memory_pool
->remove();
96 void* getMemoryBlock(const char* block_name
, unsigned int block_size
)
100 ACE_DEBUG((LM_INFO
, ACE_TEXT("errno = %d. Looking for a Shared Memory block named %C\n"),
101 ACE_OS::last_error(),
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."),
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"),
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()));
124 ACE_DEBUG((LM_INFO
, ACE_TEXT("New Shared Memory block was allocated, trying to bind it to the name %C\n"),
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"),
130 ACE_OS::last_error()));
141 ShmemMan
* ShmemMan::c_instance
= 0;
143 const char* block_name
= "block_1";
147 run_main (int argc
, ACE_TCHAR
* argv
[])
149 ACE_START_TEST (ACE_TEXT ("Bug_3911_Regression_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
));