c++ - 内存映射文件 std::allocator 实现卡住 WM6 设备

标签 c++ memory windows-mobile virtual-memory memory-mapped-files

我有一个适用于 Windows Mobile 6.x 的 Visual Studio 2008 C++ 项目,我需要比 32MB 进程槽中可用的内存更多的内存。所以,我正在考虑使用内存映射文件。我创建了一个标准分配器实现,用 CreateFileMapping 替换 new/delete和 MapViewOfFile .

预期用途是这样的:

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

最佳答案

检查 boost::interprocess 是否支持 Windows Mobile。它们具有创建内存映射文件和使用内存分配您想要的任何数据的功能:

http://www.boost.org/doc/libs/1_47_0/doc/html/interprocess/sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file

关于c++ - 内存映射文件 std::allocator 实现卡住 WM6 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5890594/

相关文章:

c++ - 我想知道为什么用 vector 可以成功而用数组却不行?

c++ - 普通旧数据和 `std::memcpy` 对齐问题

c++ - TCHAR 的二维数组

c++ - 复制构造函数和动态分配的数组

c - printf() var-arg 引用如何与堆栈内存布局交互?

c - 在 C 中打破或合并数组上的循环?

windows-mobile - OpenNETCF 1.4 与 OpenNETCF 2.3

c++ - 转换运算符重载 - 函数歧义

c# - 保存单例实例的最佳位置

c# - 在 Windows Mobile 6 中绘制图表