Results 1 to 1 of 1

Thread: Memory-mapped files

  1. #1
    Programmer Bulat Ziganshin's Avatar
    Join Date
    Mar 2007
    Location
    Uzbekistan
    Posts
    4,497
    Thanks
    733
    Thanked 659 Times in 354 Posts

    Memory-mapped files

    Simple class from SREP sources, now with only WinAPI support:

    #ifdef FREEARC_WIN

    #include <io.h>
    #include <windows.h>


    struct MMAP_FILE
    {
    bool initialized;
    FILE *file;
    char* iomode;
    Offset filesize;
    HANDLE mmapHandle;
    char *mmapPtr;


    MMAP_FILE (bool use_mmap, FILE *_file, char* _iomode, Offset _filesize) : initialized(!use_mmap), file(_file), iomode(_iomode), filesize(_filesize), mmapHandle(NULL), mmapPtr(NULL) {}


    // Initialize only on first use
    void initialize()
    {
    if (initialized) return;
    initialized = true;


    HANDLE osfhandle = (HANDLE) _get_osfhandle(fileno(file));
    DWORD access = (strequ(iomode,"r")? PAGE_READONLY : PAGE_READWRITE);
    mmapHandle = CreateFileMapping (osfhandle, NULL, access, 0, 0, NULL);


    if (mmapHandle)
    {
    DWORD access = (strequ(iomode,"r")? FILE_MAP_READ :
    (strequ(iomode,"w")? FILE_MAP_WRITE :
    FILE_MAP_ALL_ACCESS));
    mmapPtr = (char*) MapViewOfFile (mmapHandle, access, 0, 0, 0);
    }
    }


    // File was successully memory-mapped
    bool mmapped()
    {
    initialize();
    return (mmapHandle && mmapPtr);
    }


    // Read len bytes at offset pos; return number of bytes read
    // Data are read either into internal buffer or into provided buf[]; this address placed into *ptr
    int read (char **ptr, void *buf, Offset pos, int len, FILE *f = NULL)
    {
    if (mmapped()) { // File was successully memory-mapped, return memory address corresponding to pos
    *ptr = mmapPtr+pos;
    return pos>filesize? 0 : mymin (filesize-pos, len);
    } else { // Memory-mapping isn't active, perform I/O operation into user-supplied buffer
    *ptr = (char*) buf;
    file_seek(f?f:file,pos);
    return file_read(f?f:file,buf,len);
    }
    }


    ~MMAP_FILE()
    {
    if (!initialized) return;
    UnmapViewOfFile(mmapPtr);
    CloseHandle(mmapHandle);
    }
    };




    #else //!FREEARC_WIN




    struct MMAP_FILE
    {
    FILE *file;


    MMAP_FILE (bool, FILE *_file, char*, Offset) : file(_file) {}


    bool mmapped() {return false;} // File ISN'T memory-mapped


    // Read len bytes at offset pos; return number of bytes read
    // Data are read either into internal buffer or into provided buf[]; this address placed into *ptr
    int read (char **ptr, void *buf, Offset pos, int len, FILE *f = NULL)
    {
    *ptr = (char*) buf;
    file_seek(f?f:file,pos);
    return file_read(f?f:file,buf,len);
    }
    };


    #endif
    Last edited by Bulat Ziganshin; 24th March 2012 at 01:27.

Similar Threads

  1. convert swf files to avi files
    By Jabilo in forum The Off-Topic Lounge
    Replies: 13
    Last Post: 26th October 2016, 11:39
  2. SandyBridge-E memory controller
    By Bulat Ziganshin in forum The Off-Topic Lounge
    Replies: 2
    Last Post: 14th January 2012, 00:03
  3. Memory Limit?
    By Earl Colby Pottinger in forum Data Compression
    Replies: 28
    Last Post: 11th April 2010, 03:59
  4. IBM Active Memory Expansion
    By m^2 in forum Data Compression
    Replies: 0
    Last Post: 17th February 2010, 01:15
  5. 2G+ memory blocks
    By Shelwien in forum Data Compression
    Replies: 0
    Last Post: 6th March 2009, 03:13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •