A memory file is a block of memory that is perceived as a file by an application. This can be useful if you already have a file image in memory. Memory files let you reduce the number of special-case conditions in your code because, for I/O purposes, you can treat file memory images as if they were disk-based files.

Like I/O buffers, memory files can use memory allocated by the application or by the MMIO Manager. In addition, memory files can be expandable or non-expandable.

Memory is expandable when the system allocates an internal buffer using the MMIO_ALLOCBUF flag of the mmioOpen function. When the MMIO Manager reaches the end of an expandable memory file, it expands the memory file by a predefined increment.

Use the mmioOpen function to open a memory file. Specify NULL for the szFileName parameter and the MMIO_READWRITE flag, as shown:

hmmio = mmioOpen(NULL, &mmioinfo, MMIO_READWRITE);

In addition, set the pmmioinfo parameter to point to an MMIOINFO structure set up as follows:

The following code fragment shows how to open a memory file using a buffer named achMyBuffer.

        /* set mmioinfo structure to 0 */        mmioinfo.fccIOProc= FOURCC_MEM
        mmioinfo.pchBuffer= achMyBuffer
        mmioinfo.cchBuffer= cchMyBuffer
hmmio = mmioOpen("NULL", &mmioinfo, 0);

The following code fragment shows how to open a memory file with 1 byte initially and expand up to 1KB as required.

        /* set mmioinfo structure to 0 */
        mmioinfo.fccIOProc= FOURCC_MEM
        mmioinfo.pchBuffer= NULL
        mmioinfo.cchBuffer= 1
        mmioinfo.aulInfo[0] = 1024;
hmmio = mmioOpen("NULL", &mmioinfo, MMIO_CREATE)

Allocating Memory for Memory Files

There are no restrictions on allocating memory for use as a non-expandable memory file. You can use static memory or stack memory, or you can use locally allocated or globally allocated memory.


[Back] [Next]