////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2025-2025. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/interprocess for documentation. // ////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include "get_process_id_name.hpp" using namespace boost::interprocess; template struct filename_traits; template <> struct filename_traits { static const char* get() { return filename.c_str(); } static std::string filename; }; std::string filename_traits::filename = get_filename(); #ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES template <> struct filename_traits { static const wchar_t* get() { return filename.c_str(); } static std::wstring filename; }; std::wstring filename_traits::filename = get_wfilename(); #endif //#ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES template int test_managed_heap_memory() { const int MemSize = 65536*10; //STL compatible allocator object for heap typedef allocator allocator_int_t; //A vector that uses that allocator typedef boost::container::vector MyVect; { const int max = 100; void *array[std::size_t(max)]; //Named allocate capable shared memory allocator managed_heap_memory mheap(MemSize); std::size_t i; //Let's allocate some memory for(i = 0; i < max; ++i){ array[std::ptrdiff_t(i)] = mheap.allocate(i+1u); } //Deallocate allocated memory for(i = 0; i < max; ++i){ mheap.deallocate(array[std::ptrdiff_t(i)]); } //Construct the STL-like allocator with the segment manager const allocator_int_t myallocator (mheap.get_segment_manager()); //Construct vector MyVect *mheap_vect = mheap.construct ("MyVector") (myallocator); //Test that vector can be found via name if(mheap_vect != mheap.find("MyVector").first) return -1; //Destroy and check it is not present mheap.destroy ("MyVector"); mheap_vect = 0; if(0 != mheap.find("MyVector").first) return -1; //Construct a vector in the heap mheap_vect = mheap.construct ("MyVector") (myallocator); managed_heap_memory::size_type old_free_memory = mheap.get_free_memory(); //Now grow the file mheap.grow(MemSize); //Check vector is still there mheap_vect = mheap.find("MyVector").first; if(!mheap_vect) return -1; if(mheap.get_size() != (MemSize*2)) return -1; if(mheap.get_free_memory() <= old_free_memory) return -1; //Destroy and check it is not present mheap.destroy_ptr(mheap_vect); mheap_vect = 0; if(0 != mheap.find("MyVector").first) return -1; { //Now test move semantics managed_heap_memory original(1024u); managed_heap_memory move_ctor(boost::move(original)); managed_heap_memory move_assign; move_assign = boost::move(move_ctor); move_assign.swap(original); } } return 0; } int main () { int r; r = test_managed_heap_memory(); if(r) return r; #ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES r = test_managed_heap_memory(); if(r) return r; #endif return 0; }