/* * memmappedfile.h * * Created on: May 27, 2013 * Author: gregor */ #ifndef GLTB_MEMMAPPEDFILE_H_ #define GLTB_MEMMAPPEDFILE_H_ #include #include #if defined WIN32 #include #endif #include "GLTB/referencedobject.h" #include "GLTB/refptr.h" #include "GLTB/weakptr.h" namespace gltb { class MemMappedFile; class Mapping : public ReferencedObject { friend class MemMappedFile; public: ~Mapping(); void *getBase() const { return base; }; long long getSize() const { return size; }; bool isValid() const { return valid; }; void syncToDisk(bool synchronous = true) { syncToDisk(base, size, synchronous); }; void syncToDisk(void *base, unsigned long long size, bool synchronous = true); void remove(); private: Mapping(WeakPtr memMappedFile, void *realBase, void *base, unsigned long long realSize, unsigned long long size); void *realBase; void *base; unsigned long long realSize; unsigned long long size; bool valid; WeakPtr memMappedFile; }; class MemMappedFile : public ReferencedObject { public: struct Mode { Mode(bool read, bool write, bool exec) : readable(read), writeable(write), executable(exec) {}; bool readable; bool writeable; bool executable; }; MemMappedFile(std::string filename, bool read, bool write, bool exec); MemMappedFile(std::string filename, Mode mode); MemMappedFile(); ~MemMappedFile(); RefPtr mapRegion(unsigned long long offset, unsigned long long size); bool isOpen() const { return opened; }; void close(); void _removeMapping(RefPtr mapping); #if defined WIN32 HANDLE _getFileHandle() { return file; }; #endif private: std::string filename; Mode mode; bool opened; std::vector > mappings; #if defined LINUX || defined ANDROID int fileID; #elif defined WIN32 HANDLE file; HANDLE fileMapping; #endif void init(); }; } #endif /* GLTB_MEMMAPPEDFILE_H_ */