2013-04-03 54 views
5

首先我將概述帶源代碼的域。Boost進程間分配器 - 管理文件大小

namespace bip=boost::interprocess; 

typedef bip::allocator<int, bip::managed_mapped_file::segment_manager> allocator; 
typedef bip::vector<int, allocator> vector; 

bip::managed_mapped_file m_file(open_or_create, "./file", constant_value);  
bip::allocator alloc(m_file.get_segment_manager()); 
bip::vector *vec = m_file.find_or_construct<vector>("vector")(alloc); 

我不關心底層文件的最終大小,但我不能預見這個值。是否有任何提升機制,它將處理調整底層文件的大小?或者我必須抓住bip :: bad_alloc並關心這個由我自己?

回答

6

閱讀文檔中的this section

你有靜態成員函數grow(),可能是你所需要的:

bip::managed_mapped_file::grow("./file", extra_bytes); 

但是你必須要確保沒有人正在使用該文件,這就是爲什麼他們稱之爲離線成長。這可能是不可能的,這取決於問題。

+0

哦。很簡單。我必須承認,在POSIX mmap上直接解決類似的挑戰,並使用Boost IOstreams後,我得到了補償。請參閱例如此答案[如何在C++中交換另一行](http://stackoverflow.com/a/17374711/85371)。我想知道他們是如何實現它的便攜性。 – sehe