#include #include #include #include #include //#include //#include using namespace boost::intrusive; namespace ip= boost::interprocess; class channel_t : public list_base_hook< void_pointer< ip::offset_ptr > > { public: char xxx[4]; int ch_order; int ch_id; channel_t(int _ch_order, int _ch_id) : ch_order(_ch_order), ch_id(_ch_id) {} channel_t(const channel_t& other) : ch_order(other.ch_order), ch_id(other.ch_id) { printf("copy constructor 0x%x -> 0x%x \n", (long long)&other, (long long) this); } channel_t() {} void set (unsigned int a, unsigned int b) { ch_order=a; ch_id=b; xxx[0]='A'; xxx[1]='B'; xxx[2]='C'; xxx[3]='C'; } }; typedef ip::allocator shm_allocator_t; typedef ip::list chlist_t; int main_create (int argc, char **argv) { try{ //Shared memory front-end that is able to construct objects //associated with a c-string. Erase previous shared memory with the name //to be used and create the memory segment at the specified address and initialize resources ip::managed_mapped_file segment(ip::create_only,"/tmp/MyMappedFile", 4096*4); //Alias an STL compatible allocator of ints that allocates ints from the managed //shared memory segment. This allocator will allow to place containers //in managed shared memory segments //Initialize shared memory STL-compatible allocator shm_allocator_t shm_alloc (segment.get_segment_manager()); typedef ip::vector shm_vector_t; unsigned int i; //Construct a list in shared memory chlist_t *list = segment.construct("ChannelInfo")(shm_alloc); for(int i = 0; i < 100; ++i) { channel_t c(i, i+100); list->push_front(c); } chlist_t::iterator it=list->begin(); for(;it!=list->end();++it) { channel_t d= *it; printf("ch_order=%d ch_id=%d\n", d.ch_order, d.ch_id); } } catch(...){ throw; } return 0; } int main_test (int argc, char **argv) { try{ //Shared memory front-end that is able to construct objects //associated with a c-string. Erase previous shared memory with the name //to be used and create the memory segment at the specified address and initialize resources ip::managed_mapped_file segment(ip::open_only,"/tmp/MyMappedFile"); //Alias an STL compatible allocator of ints that allocates ints from the managed //shared memory segment. This allocator will allow to place containers //in managed shared memory segments //Initialize shared memory STL-compatible allocator shm_allocator_t shm_alloc (segment.get_segment_manager()); //Find the vector using the c-string name chlist_t *list = segment.find("ChannelInfo").first; printf("list=0x%x\n", (long long)list); unsigned int i; chlist_t::iterator it=list->begin(); for(;it!=list->end();++it) { channel_t d= *it; printf("ch_order=%d ch_id=%d\n", d.ch_order, d.ch_id); } channel_t& d= *list->begin(); } catch(...){ throw; } return 0; } int main(int argc, char**argv) { switch(atoi(argv[1])) { case 1: return main_create(argc,argv); break; case 2: return main_test(argc,argv); break; } }