int main () { //We will create a buffer of 1000 bytes to store a list managed_heap_memory heap_memory(1000); MyList * mylist = heap_memory.construct<MyList>("MyList") (heap_memory.get_segment_manager()); //Obtain handle, that identifies the list in the buffer managed_heap_memory::handle_t list_handle = heap_memory.get_handle_from_address(mylist); //Fill list until there is no more memory in the buffer try{ while(1) { mylist->insert(mylist->begin(), 0); } } catch(const bad_alloc &){ //memory is full } //Let's obtain the size of the list MyList::size_type old_size = mylist->size(); //<- (void)old_size; //-> //To make the list bigger, let's increase the heap buffer //in 1000 bytes more. heap_memory.grow(1000); //If memory has been reallocated, the old pointer is invalid, so //use previously obtained handle to find the new pointer. mylist = static_cast<MyList *> (heap_memory.get_address_from_handle(list_handle)); //Fill list until there is no more memory in the buffer try{ while(1) { mylist->insert(mylist->begin(), 0); } } catch(const bad_alloc &){ //memory is full } //Let's obtain the new size of the list MyList::size_type new_size = mylist->size(); //<- (void)new_size; //-> assert(new_size > old_size); //Destroy list heap_memory.destroy_ptr(mylist); return 0; }
int main () { const char *FileName = "file_mapping"; const std::size_t FileSize = 1000; file_mapping::remove(FileName); try{ std::size_t old_size = 0; managed_mapped_file::handle_t list_handle; { managed_mapped_file mfile_memory(create_only, FileName, FileSize); MyList *mylist = mfile_memory.construct<MyList>("MyList") (mfile_memory.get_segment_manager()); //Obtain handle, that identifies the list in the buffer list_handle = mfile_memory.get_handle_from_address(mylist); //Fill list until there is no more room in the file try{ while(1) { mylist->insert(mylist->begin(), 0); } } catch(const bad_alloc &){ //mapped file is full } //Let's obtain the size of the list old_size = mylist->size(); } //To make the list bigger, let's increase the mapped file //in FileSize bytes more. managed_mapped_file::grow(FileName, FileSize*2); { managed_mapped_file mfile_memory(open_only, FileName); //If mapping address has changed, the old pointer is invalid, //so use previously obtained handle to find the new pointer. MyList *mylist = static_cast<MyList *> (mfile_memory.get_address_from_handle(list_handle)); //Fill list until there is no more room in the file try{ while(1) { mylist->insert(mylist->begin(), 0); } } catch(const bad_alloc &){ //mapped file is full } //Let's obtain the new size of the list std::size_t new_size = mylist->size(); assert(new_size > old_size); //Destroy list mfile_memory.destroy_ptr(mylist); } } catch(...){ file_mapping::remove(FileName); throw; } file_mapping::remove(FileName); return 0; }