【发布时间】:2011-08-18 22:27:17
【问题描述】:
我有一个适用于 Windows Mobile 6.x 的 Visual Studio 2008 C++ 项目,我需要比 32MB 进程槽中可用的内存更多的内存。所以,我正在考虑使用内存映射文件。我创建了一个标准的分配器实现,用CreateFileMapping 和MapViewOfFile 替换new/delete。
预期用途是这样的:
struct Foo
{
char a[ 1024 ];
};
int _tmain( int argc, _TCHAR* argv[] )
{
std::vector< boost::shared_ptr< Foo > > v;
for( int i = 0; i < 40000; ++i )
{
v.push_back( boost::allocate_shared< Foo >( MappedFileAllocator< Foo >() ) );
}
return 0;
}
使用std::allocator,在我得到std::bad_alloc 异常之前,我可以在该示例中获得28197 次迭代。使用MappedFileAllocator,在设备完全冻结并且必须重新启动之前,我得到了 32371 次迭代。由于我的设备有 512MB 的 RAM,我希望能够从该循环中获得更多的迭代。
我的MappedFileAllocator 实现是:
template< class T >
class MappedFileAllocator
{
public:
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
pointer address( reference r ) const { return &r; };
const_pointer address( const_reference r ) const { return &r; };
/// convert a MappedFileAllocator<T> to a MappedFileAllocator<U>
template< class U >
struct rebind { typedef MappedFileAllocator< U > other; };
MappedFileAllocator() throw() : mapped_file_( INVALID_HANDLE_VALUE ) { };
template< class U >
explicit MappedFileAllocator( const MappedFileAllocator< U >& other ) throw()
: mapped_file_( INVALID_HANDLE_VALUE )
{
if( other.mapped_file_ != this->mapped_file_ )
{
::DuplicateHandle( GetCurrentProcess(),
other.mapped_file_,
GetCurrentProcess(),
&this->mapped_file_,
0,
FALSE,
DUPLICATE_SAME_ACCESS );
}
};
pointer allocate( size_type n, const void* /*hint*/ = 0 )
{
if( n > max_size() )
throw std::bad_alloc();
if( n > 0 )
{
size_type buf_size = n * sizeof( value_type );
mapped_file_ = ::CreateFileMapping( INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
buf_size,
L"{45E4FA7B-7B1E-4939-8CBB-811276B5D4DE}" );
if( NULL == mapped_file_ )
throw std::bad_alloc();
LPVOID f = ::MapViewOfFile( mapped_file_,
FILE_MAP_READ | FILE_MAP_WRITE,
0,
0,
buf_size );
if( NULL == f )
{
::CloseHandle( mapped_file_ );
mapped_file_ = INVALID_HANDLE_VALUE;
throw std::bad_alloc();
}
return reinterpret_cast< T* >( f );
}
return 0;
};
void deallocate( pointer p, size_type n )
{
if( NULL != p )
{
::FlushViewOfFile( p, n * sizeof( T ) );
::UnmapViewOfFile( p );
}
if( INVALID_HANDLE_VALUE != mapped_file_ )
{
::CloseHandle( mapped_file_ );
mapped_file_ = INVALID_HANDLE_VALUE;
}
};
size_type max_size() const throw()
{
return std::numeric_limits< size_type >::max() / sizeof( T );
};
/// handle to the memory-mapped file
HANDLE mapped_file_;
private:
/// disallow assignment
void operator=( const MappedFileAllocator& );
}; // class MappedFileAllocator
任何人都可以建议我的MappedFileAllocator 实现可能出问题的地方吗?
谢谢, 保罗H
【问题讨论】:
-
检查从 allocate() 返回的每个指针是否在某个边界上对齐;每次您尝试映射文件时,似乎 MapViewOfFile 可能会消耗一个页面。
-
@vividos - 它们在 4 字节边界上与 ARM 对齐。 WM 版本的 MVOF 不需要页面对齐。 msdn.microsoft.com/en-us/library/aa914405.aspx
-
那我不知道是什么问题。接下来我要尝试的是由 VirtualAlloc() 分配的 LMA 中的内存池,而不是使用匿名文件映射。本文档可能会有所帮助:davidfindlay.org/weblog/files/ce_lma.php
-
@vividos - 太美了。我要试一试。谢谢!
标签: c++ memory windows-mobile virtual-memory memory-mapped-files