void main() { test_shared_ptr(); test01(); test02(); test03(); test04(); #ifdef NTL_CXX_RV test05(); #endif test06(); test07(); test08(); test09(); test10(); test11(); test12(); test14(); test15(); test16(); test17(); test18(); test19(); test20(); test21(); test22(); test23(); test24(); test25(); #ifdef _CPPUNWIND test26(); #endif // TODO: "libstdc++-v3\testsuite\20_util\shared_ptr\creation" и далее }
/* * a few things to note about this function; the debug messages are not operating in the limited memory space * which would be nice, but we would have to override the allocation tendencies of not only std::string * but also std::ostringstream and its like, which would be rather a tedious and error prone process. * * any who. In this function we allocate a 1GB chunk of memory which we have exclusive control over and * we use the Free List Allocation method to divvy it up. we then allocate a multimap of pair<int,double>'s * multimap is good for testing for two reasons, it has horrible times in deallocation with the FreeLitAllocator * and it deallocates ALL of its memory when multimap<>::clear() is called. which the unordered_map variants * do not since they keep their hash tables until their destructors are called. */ void memoryStuff( evq::EventQueue * eventQueue ) { // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("Creating DefaultAllocator.") ); // wrapper for ::operator new() and ::operator delete() alloc::DefaultAllocator defaultAllocator( 0, nullptr ); // pointer to our memory pool void * _mem_pool; // size of our memory pool: 1GB std::size_t _mem_size = 1024*1024*1024; // size of our memory pool: 0.5MB //std::size_t _mem_size = 1024*1024*0.5f; // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("Allocating Memory Pool of size " + to_string(_mem_size) + "bytes.") ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("Allocating FreeListAllocator, for Memory Pool.") ); // allocate a free list allocator and allocate the memory pool and pass it to the free list allocator. alloc::SafeFreeListAllocator * fla = new( (alloc::SafeFreeListAllocator*)defaultAllocator ) alloc::SafeFreeListAllocator( _mem_size, _mem_pool = defaultAllocator.allocateBlock(_mem_size,0) ); { alloc::ptr::weak_ptr<int> test_weak_ptr_outer; alloc::ptr::shared_ptr<int> test_shared_ptr_outer; { //alloc::ptr::shared_ptr<int> test_shared_ptr( new( (int*)*fla ) int(1), fla ); alloc::ptr::shared_ptr<int> test_shared_ptr( alloc::ptr::allocate_shared<int>(fla, 1) ); std::cout << *test_shared_ptr << std::endl; test_weak_ptr_outer = test_shared_ptr; test_shared_ptr_outer = test_shared_ptr; } if( auto t = test_weak_ptr_outer.lock() ) { std::cout << *t << std::endl; } alloc::ptr::unique_ptr<int> test_unique_ptr_outer; { alloc::ptr::unique_ptr<int> test_unique_ptr( new( (int*)*fla ) int(1000), fla ); test_unique_ptr_outer.swap( test_unique_ptr ); { alloc::ptr::unique_ptr<int[]> test_array_unique_ptr( new( fla->allocateArray<int>( 100 ) ) int[100], fla ); for( std::size_t i = 0; i < 100; ++i ) { test_array_unique_ptr[i] = i; } } } std::cout << *test_unique_ptr_outer << std::endl; } { //alloc::LockAllocator< alloc::LockAllocator< alloc::DefaultAllocator > > llalt(); // FIXME(dean): this line should not compile //llalt.printDebugInfo(); } //std::ostringstream stream; // we don't have control over this memory std::basic_ostringstream<char> stream( std::basic_string<char>( alloc::stl_adapter<char>( fla ) ), std::ios_base::out ); { // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("Allocating IntMap from FreeListAllocator.") ); // allocate a std::multimap<int,double> with our custom allocator adapters alloc::stl::map< int, double > * intmap = new( (alloc::stl::map< int, double >*)*fla ) alloc::stl::map< int, double >( fla ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("") ); { // make 10000 pairs! for( std::size_t i = 0; i < 100000; ++i ) { // insert a pair uint32_t key = Rand::Int( 0u, ~0u ); double value = sqrt( double(key) ); // here multimap will allocate space for a node that contains a pair<int,double> in it. //intmap->insert( std::pair<int,double>(key,value) ); (*intmap)[key] = value; // print progress if( (i) % 1000 == 0 ) { // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("Inserted " + to_string(i) + " pairs into IntMap on FreeListAllocator. Last pair: " + to_string(key) + "," + to_string(value) + ".") ); } } } // clear the stringstream buffer stream.str( std::basic_string< char >( alloc::stl_adapter<char>(fla) ) ); stream.clear(); // put allocator debug info into stringstream buffer fla->printDebugInfo( stream ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent( std::string( stream.str().c_str() ) ) ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent("Deallocating all memory associated with IntMap in FreeListAllocator.") ); // deallocate and destruct the multimap<int,double> fla->deallocate( intmap ); } // clear the stringstream buffer stream.str( std::basic_string< char >( alloc::stl_adapter<char>(fla) ) ); stream.clear(); // put allocator debug info into stringstream buffer fla->printDebugInfo( stream ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent( std::string( stream.str().c_str() ) ) ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent( "cleaning up ... fla" ) ); // destruct the FreeListAllocator defaultAllocator.deallocate( fla ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent( "cleaning up ... _mem_pool" ) ); // deallocate the memory block defaultAllocator.deallocateBlock( _mem_pool ); // clear the stringstream buffer //stream.str( std::basic_string< char >( alloc::stl_adapter<char>(fla) ) ); //stream.clear(); // put allocator debug info into stringstream buffer defaultAllocator.printDebugInfo( stream ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent( std::string( stream.str().c_str() ) ) ); // we don't use the memory allocator with the events because we have yet to properly setup custom allocator deletion for events eventQueue->queueEvent( new DebugListener::MessageEvent( "complete." ) ); }