Back to index

lightning-sunbird  0.9+nobinonly
Typedefs | Functions
mmapio.h File Reference
#include "prio.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct MmioFileStruct

Functions

PRStatus mmio_FileSeek (MmioFile *file, PRInt32 offset, PRSeekWhence whence)
PRInt32 mmio_FileRead (MmioFile *file, char *dest, PRInt32 count)
PRInt32 mmio_FileWrite (MmioFile *file, const char *src, PRInt32 count)
PRInt32 mmio_FileTell (MmioFile *file)
PRStatus mmio_FileClose (MmioFile *file)
MmioFile * mmio_FileOpen (char *path, PRIntn flags, PRIntn mode)

Typedef Documentation

typedef struct MmioFileStruct

Definition at line 40 of file mmapio.h.


Function Documentation

PRStatus mmio_FileClose ( MmioFile *  file)

Definition at line 170 of file mmapio.c.

{
    if(mmio->addr && mmio->msize) {
        PR_ASSERT(mmio->fileMap);
        PR_MemUnmap(mmio->addr, mmio->msize);
        PR_CloseFileMap(mmio->fileMap);
    }

    PR_Close(mmio->fd);

    memset(mmio, 0, sizeof(*mmio)); /* Catch people who try to keep using it */

    PR_Free(mmio);

    return PR_SUCCESS;
}

Here is the call graph for this function:

MmioFile* mmio_FileOpen ( char *  path,
PRIntn  flags,
PRIntn  mode 
)

Definition at line 187 of file mmapio.c.

{
    PRFileDesc *fd = PR_Open(path, flags, mode);
    PRFileInfo info;
    MmioFile   *mmio;

    if(!fd) {
        return NULL;
    }

    mmio = PR_MALLOC(sizeof(MmioFile));

    if(!mmio || PR_FAILURE==PR_GetOpenFileInfo(fd, &info)) {
        PR_Close(fd);
        return NULL;
    }

    mmio->fd = fd;
    mmio->fileMap = NULL;
    mmio->fsize = info.size;
    mmio->msize = 0;
    mmio->pos   = 0;
    mmio->addr  = NULL;
    mmio->needSeek = PR_FALSE;

    return mmio;
}
PRInt32 mmio_FileRead ( MmioFile *  file,
char *  dest,
PRInt32  count 
)

Definition at line 78 of file mmapio.c.

{
    static PRFileMapProtect prot = PR_PROT_READONLY;
    static PRInt64 fsize_l;

    /* First see if we are going to try and read past the end of the file
     * and shorten count if we are.
    */
    if(mmio->pos+count > mmio->fsize) {
        count = mmio->fsize - mmio->pos;
    }

    if(count<1) {
        return 0;
    }

    /* Check to see if we need to remap for this read */
    if(mmio->pos+count > mmio->msize) {
        if(mmio->addr && mmio->msize) {
            PR_ASSERT(mmio->fileMap);
            PR_MemUnmap(mmio->addr, mmio->msize);
            PR_CloseFileMap(mmio->fileMap);
            mmio->addr  = NULL;
            mmio->msize = 0;
        }

        LL_UI2L(fsize_l, mmio->fsize);
        mmio->fileMap = PR_CreateFileMap(mmio->fd, fsize_l, prot);

        if(!mmio->fileMap) {
            return -1;
        }

        mmio->addr = PR_MemMap(mmio->fileMap, 0, fsize_l);

        if(!mmio->addr) {
            return -1;
        }

        mmio->msize = mmio->fsize;
    }

    memcpy(dest, mmio->addr+mmio->pos, count);

    mmio->pos += count;
    mmio->needSeek = PR_TRUE;

    return count;
}

Here is the call graph for this function:

PRStatus mmio_FileSeek ( MmioFile *  file,
PRInt32  offset,
PRSeekWhence  whence 
)

Definition at line 53 of file mmapio.c.

{
    mmio->needSeek = PR_TRUE;

    switch(whence) {
        case PR_SEEK_SET:
            mmio->pos = offset;
            break;
        case PR_SEEK_END:
            mmio->pos = mmio->fsize + offset;
            break;
        case PR_SEEK_CUR:
            mmio->pos = mmio->pos + offset;
            break;
        default:
            return PR_FAILURE;
    }

    if(mmio->pos<0) {
        mmio->pos = 0;
    }

    return PR_SUCCESS;
}
PRInt32 mmio_FileTell ( MmioFile *  file)

Definition at line 165 of file mmapio.c.

{
    return mmio->pos;
}
PRInt32 mmio_FileWrite ( MmioFile *  file,
const char *  src,
PRInt32  count 
)

Definition at line 128 of file mmapio.c.

{
    PRInt32 wcode;

    if(mmio->needSeek) {
        PR_Seek(mmio->fd, mmio->pos, PR_SEEK_SET);
        mmio->needSeek = PR_FALSE;
    }

    /* If this system does not keep mmap() and write() synchronized, we can
    ** force it to by doing an munmap() when we do a write.  This will
    ** obviously slow things down but fortunatly we do not do that many
    ** writes from within mozilla.  Platforms which need this may want to
    ** use the new USE_BUFFERED_REGISTRY_IO code instead of this code though.
    */
#if MMAP_MISSES_WRITES
    if(mmio->addr && mmio->msize) {
       PR_ASSERT(mmio->fileMap);
       PR_MemUnmap(mmio->addr, mmio->msize);
       PR_CloseFileMap(mmio->fileMap);
       mmio->addr  = NULL;
       mmio->msize = 0;
    }
#endif

    wcode = PR_Write(mmio->fd, src, count);

    if(wcode>0) {
        mmio->pos += wcode;
        if(mmio->pos>mmio->fsize) {
            mmio->fsize=mmio->pos;
        }
    }

    return wcode;
}