#include #include #include #include namespace bip = boost::interprocess; typedef bip::managed_shared_memory::segment_manager segment_manager_t; typedef bip::allocator long_allocator; typedef bip::vector long_vector; int main(int argc, char ** argv) { struct shm_remove { shm_remove() { bip::shared_memory_object::remove("ShmTest"); } ~shm_remove() { bip::shared_memory_object::remove("ShmTest"); } } remover; size_t szLength = 280000000; size_t szRequired = szLength * sizeof(long); size_t szRequested = (size_t) (szRequired * 1.05); bip::managed_shared_memory segment(bip::create_only, "ShmTest", szRequested); std::cout << "Length: " << szLength << "\n" << "sizeof(long): " << sizeof(long) << "\n" << "Required: " << szRequired << "\n" << "Requested: " << szRequested << "\n" << "Allocated: " << segment.get_size() << "\n" << "Overhead: " << segment.get_size() - segment.get_free_memory() << "\n" << "Free: " << segment.get_free_memory() << "\n\n"; long_allocator alloc(segment.get_segment_manager()); long_vector vector(alloc); if (argc > 1) { std::cout << "Reserving Length of " << szLength << "\n"; vector.reserve(szLength); std::cout << "Vector Capacity: " << vector.capacity() << "\tFree: " << segment.get_free_memory() << "\n\n"; } for (size_t i = 0; i < szLength; i++) { if ((i % (szLength / 100)) == 0) { std::cout << i << ": " <<"\tVector Capacity: " << vector.capacity() << "\tFree: " << segment.get_free_memory() << "\n"; } vector.push_back(i); } std::cout << "end: " <<"\tVector Capacity: " << vector.capacity() << "\tFree: " << segment.get_free_memory() << "\n"; return 0; }