1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
//=============================================================================
/**
* @file Based_Pointer_Test.cpp
*
* $Id: Based_Pointer_Test.cpp 93638 2011-03-24 13:16:05Z johnnyw $
*
* This test check the Based_Pointer and Based_Pointer_repository classes.
*
*
* @author Steve Williams <steve@telxio>
*/
//=============================================================================
#include "test_config.h"
#include "ace/DLL.h"
#include "ace/ACE.h"
#ifdef ACE_HAS_POSITION_INDEPENDENT_POINTERS
#include "ace/Based_Pointer_Repository.h"
#endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS */
#include "ace/Malloc_T.h"
#include "ace/MMAP_Memory_Pool.h"
#include "ace/PI_Malloc.h"
#include "ace/Null_Mutex.h"
#include "ace/Based_Pointer_T.h"
#include "ace/SString.h"
#include "ace/OS_NS_unistd.h"
class Foo
{
private:
Foo(const Foo &)
{
}
public:
friend class ace_dewarn_gplusplus;
~Foo ()
{
}
};
class Void_Pointer : public ACE_Based_Pointer<void>
{
};
#ifdef ACE_HAS_POSITION_INDEPENDENT_POINTERS
#if defined (ACE_LD_DECORATOR_STR)
# define OBJ_SUFFIX ACE_LD_DECORATOR_STR ACE_DLL_SUFFIX
#else
# define OBJ_SUFFIX ACE_DLL_SUFFIX
#endif /* ACE_LD_DECORATOR_STR */
#if defined (ACE_WIN32) || defined (ACE_OPENVMS)
# define OBJ_PREFIX ACE_DLL_PREFIX
#else
# define OBJ_PREFIX ACE_TEXT("./") ACE_DLL_PREFIX
#endif /* ACE_WIN32 */
// Declare the type of the DLL symbol:
typedef void *(*Get_Bp_Repository_Inst)(void);
// Declare an allocator based MMAP_Memory_Pool
typedef ACE_Malloc_T< ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex, ACE_PI_Control_Block
> MMAP_Allocator;
// Check that the ACE_Based_Pointer_Repository can be accessed
// from a Windows DLL
// (see http://deuce.doc.wustl.edu/bugzilla/show_bug.cgi?id=1991)
int singleton_test (void)
{
void* baddr1 = ACE_BASED_POINTER_REPOSITORY::instance();
void* baddr2 = ACE_BASED_POINTER_REPOSITORY::instance();
if (baddr1 != baddr2)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("ACE_Based_Pointer_Repository is not a singleton\n")),
-1);
}
// Protection against this test being run on platforms not supporting Dlls.
#if defined(ACE_HAS_DYNAMIC_LINKING)
ACE_TString dll_file;
const char *subdir_env = ACE_OS::getenv ("ACE_EXE_SUB_DIR");
if (subdir_env)
{
dll_file = ACE_TEXT_CHAR_TO_TCHAR (subdir_env);
dll_file += ACE_DIRECTORY_SEPARATOR_STR;
}
dll_file += OBJ_PREFIX ACE_TEXT ("Based_Pointer_Test_Lib") OBJ_SUFFIX;
// If DLL causes multiple instances of singleton
// then the ACE_Cleanup object registered
// with the ACE_Object_manager will no longer be valid,
// at exit time if the library is unloaded. Override
// the default close on destruct.
ACE_DLL dll;
int retval = dll.open (dll_file.c_str (),
ACE_DEFAULT_SHLIB_MODE,
0);
if (retval != 0)
{
ACE_TCHAR *dll_error = dll.error ();
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Error in DLL Open: %s\n"),
dll_error ? dll_error : ACE_TEXT ("unknown error")),
-1);
}
#if defined (ACE_OPENVMS)
// with OPENVMS symbol names > 31 cause us trouble with dlsym()
void* foo = dll.symbol (ACE_TEXT ("get_based_pointer_repo_inst"));
#else
void* foo = dll.symbol (ACE_TEXT ("get_based_pointer_repository_instance"));
#endif
// Cast the void* to function* with a long as intermediate.
ptrdiff_t tmp = reinterpret_cast<ptrdiff_t> (foo);
Get_Bp_Repository_Inst get_bp_repository_inst =
reinterpret_cast<Get_Bp_Repository_Inst> (tmp);
if (get_bp_repository_inst == 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
dll.error ()),
-1);
void* baddr_dll = get_bp_repository_inst ();
dll.close ();
if (baddr_dll != baddr1)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("ACE_Based_Pointer_Repository is not a ")
ACE_TEXT ("singleton in DLL <%@> <%@>\n"),
baddr_dll,
baddr1),
-1);
}
#endif /* ACE_HAS_DYNAMIC_LINKING */
return 0;
}
// Check that MMAP memory blocks are correctly mapped
// into the Based_Pointer_Repository
int
mmap_map_test(void)
{
MMAP_Allocator* alloc = 0;
ACE_OS::unlink("foo");
{
// The 'options' are only here to quiet MSVC 6. It can be removed
// when MSVC 6 support is removed.
MMAP_Allocator::MEMORY_POOL_OPTIONS *options = 0;
ACE_NEW_RETURN
(alloc,
MMAP_Allocator (ACE_TEXT ("foo"), ACE_TEXT ("foo"), options),
-1);
void* addr = alloc->base_addr();
if(addr == 0)
{
ACE_ERROR((LM_ERROR,
ACE_TEXT ("Unable to get base to MMAP Memory Pool\n")));
alloc->remove();
delete alloc;
return -1;
}
// Check a base address mapping was added to the Repository
// when the pool was created
void* ba = 0;
if(ACE_BASED_POINTER_REPOSITORY::instance()->find(addr, ba) == -1)
{
ACE_ERROR((LM_ERROR, ACE_TEXT ("Unable to access repository\n")));
alloc->remove();
delete alloc;
return -1;
}
alloc->remove();
delete alloc;
if(ba != addr)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("MMAP pool mapping not present\n")),
-1);
}
// Check Mapping is removed when object is deleted
if (ACE_BASED_POINTER_REPOSITORY::instance()->find(addr, ba) == -1)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Unable to access repository\n")),
-1);
}
if(ba != 0)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("MMAP pool mapping not removed\n")),
-1);
}
}
return 0;
}
// Check that persistent MMAP memory blocks are correctly remapped
// into the Based_Pointer_Repository
// (i.e. maps based on backing stores that are already
// present in the filesystem)
// (see http://deuce.doc.wustl.edu/bugzilla/show_bug.cgi?id=2216)
int
mmap_persistent_map_test(void)
{
MMAP_Allocator* alloc = 0;
// The 'options' are only here to quiet MSVC 6. It can be removed
// when MSVC 6 support is removed.
MMAP_Allocator::MEMORY_POOL_OPTIONS *options = 0;
ACE_OS::unlink("foo");
{
ACE_NEW_RETURN
(alloc,
MMAP_Allocator (ACE_TEXT ("foo"), ACE_TEXT ("foo"), options),
-1);
alloc->sync();
// Delete Malloc and the memory pool, but do not remove
// the backing store
alloc->memory_pool().release(0);
delete alloc;
}
//
// Recreate segment with existing backing store
//
ACE_NEW_RETURN
(alloc,
MMAP_Allocator (ACE_TEXT ("foo"), ACE_TEXT("foo"), options),
-1);
void* addr = alloc->base_addr();
if(addr == 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Unable to get base to persistent MMAP Memory Pool\n")));
alloc->remove();
delete alloc;
return -1;
}
void* ba = 0;
if(ACE_BASED_POINTER_REPOSITORY::instance()->find(addr, ba) == -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Unable to find base address after map of persistent segment\n")));
alloc->remove();
delete alloc;
return -1;
}
if(ba == 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Persistent MMAP Memory Pool not mapped\n")));
alloc->remove();
delete alloc;
return -1;
}
alloc->remove();
delete alloc;
return 0;
}
// Check that MMAP memory blocks are correctly remapped
// into the Based_Pointer_Repository
// (i.e. when a segment is resized it may move its base address
// because the OS cannot fit the new segment size at the same
// base address, in this case the Repository must be updated)
// (see http://deuce.doc.wustl.edu/bugzilla/show_bug.cgi?id=2218)
int
mmap_remap_test(void)
{
// Use a Position Independent memory segment
// because this one is going to move
MMAP_Allocator *alloc[ 3 ]= {0, 0, 0};
void *pool_base[ 3 ]= {0, 0, 0};
// Make sure the Pool options are set to allow
// the segment to move
ACE_MMAP_Memory_Pool_Options data_opts(
0,
ACE_MMAP_Memory_Pool_Options::NEVER_FIXED );
int i;
for (i= 0; i<3; ++i)
{
ACE_TCHAR store[ MAXPATHLEN + 1 ];
ACE_OS::sprintf( store, ACE_TEXT("foo%d"), i );
ACE_OS::unlink( store );
ACE_NEW_RETURN (alloc[ i ],
MMAP_Allocator (store, store, &data_opts),
-1);
pool_base[ i ]= alloc[ i ]->base_addr();
}
// sort pools into base address order
for (i= 0; i<2; ++i)
{
if (pool_base[ i ] < pool_base[ i+1 ])
{
void *tmp1= pool_base[ i ];
MMAP_Allocator *tmp2= alloc[ i ];
pool_base[ i ]= pool_base[ i+1 ];
alloc[ i ]= alloc[ i+1 ];
pool_base[ i+1 ]= tmp1;
alloc[ i+1 ]= tmp2;
i= -1;
}
}
// alloc[1] is now bounded, whether memory grows up or
// down, it will hit either alloc[0] or alloc[2] and have
// to be remapped.
//
// Calculate maximum space between base addresses
size_t size= (char *) pool_base[ 0 ] - (char *) pool_base[ 1 ];
size_t tmpsize= (char *) pool_base[ 1 ] - (char *) pool_base[ 2 ];
size= (size < tmpsize) ? tmpsize : size;
// force pool to move
++size;
(void)alloc[ 1 ]->malloc(size);
void *nba= alloc[ 1 ]->base_addr();
if (pool_base[ 1 ] == nba)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("MMAP Pool did not move base address as expected\n")));
for (i= 0; i<3; ++i)
{
alloc[ i ]->remove();
delete alloc[ i ];
}
return -1;
}
void *ba= 0;
if (ACE_BASED_POINTER_REPOSITORY::instance()->find(nba, ba) == -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Unable to find base address after remap of segment\n")));
for (i= 0; i<3; ++i)
{
alloc[ i ]->remove();
delete alloc[ i ];
}
return -1;
}
if (ba != nba)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("New base address not mapped after MMAP remap\n")));
for (i= 0; i<3; ++i)
{
alloc[ i ]->remove();
delete alloc[ i ];
}
return -1;
}
// Check old base address has been removed
// from the repository
if (ACE_BASED_POINTER_REPOSITORY::instance()->find( pool_base[ 1 ], ba ) == -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Unable to find base address after remap of segment\n")));
for (i= 0; i<3; ++i)
{
alloc[ i ]->remove();
delete alloc[ i ];
}
return -1;
}
if (ba == pool_base[ 1 ])
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Old base address not removed after MMAP remap\n")));
for (i= 0; i<3; ++i)
{
alloc[ i ]->remove();
delete alloc[ i ];
}
return -1;
}
for (i= 0; i<3; ++i)
{
alloc[ i ]->remove();
delete alloc[ i ];
}
return 0;
}
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Based_Pointer_Test"));
int retval = 0;
retval += singleton_test ();
retval += mmap_map_test();
retval += mmap_persistent_map_test();
retval += mmap_remap_test();
ACE_Based_Pointer_Basic<Foo> Foo_Ptr;
ACE_UNUSED_ARG (Foo_Ptr);
ACE_END_TEST;
return retval == 0 ? 0 : 1;
}
#else /* ! ACE_HAS_POSITION_INDEPENDENT_POINTERS */
// Nothing to test !
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Based_Pointer_Test"));
ACE_Based_Pointer_Basic<Foo> Foo_Ptr;
ACE_UNUSED_ARG (Foo_Ptr);
ACE_END_TEST;
return 0;
}
#endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS */
|