Memory Files

In the Unix environment, everything is a file. For example, you can open the terminal as a file, which I covered in a previous Lesson. But what about memory? Can you open memory as a file? And why would you want to?

The traditional paradigm is to allocate memory. Operators and functions manipulate memory, but you don’t read from and write to memory as you would a file. I think it would be convenient to do so. If it’s not convenient, then it’s a fun exercise to try, which can also help you understand a bit more about how files work in the computer.

As with writing to a file, you need a handle to write to a memory-file. For a file, the FILE handle is a pointer to a structure that contains file information. A memory-file handle works similarly, containing details about the memory chunk pretending to be a file. It’s not long-term storage, nor would the memory-file be available to other programs unless the memory handle were implemented at the operating system level.

Oh, but I prattle.

Here is my notion for a memory handle structure used to manage a chunk of memory like a file:

struct mem_file {
	void *address;
	char *name;
	int size;
	int offset;
	char *timestamp;
};

Most important is the memory chunk’s address, pointer member address. Then comes a name for the memory chunk.

The memory chunk’s size is contained in member size. This value represents the number of bytes stored in the memory-file, not its allocated memory size.

The offset member is used when reading data from the memory-file. It works like the file position indicator in the fseek() function.

Finally, a timestamp string is stored in the mem_file structure.

As with file storage, a constant defines this size of memory-file allocated chunks:

#define DEFAULT_SIZE 2048

When considering this value, understand that different file systems use different allocation sizes. For example, a file system may automatically allocate 4K of storage (4096 bytes) when the file is created. In the olden days, DOS allocated file storage in 512 byte chunks, which was the size of a floppy disk sector. (Sectors are a subdivision of a track on the floppy disk, like slices in a pie.)

I chose the value 2048 (2K) as a typical memory chunk. More storage is allocated if necessary as the memory-file grows. The size member of the mem_file structure is monitored and when it crosses the DEFAULT_SIZE value, more memory is allocated for the memory-file. This operation is done internally, just as it would be for a file stored on mass media.

To use the mem-file handle, a memory-file must be opened or created. Unlike a media file, memory files are opened for reading and writing simultaneously. And like a media file, memory files can be closed, which frees the memory.

Unlike media files, a memory-file doesn’t remain in storage after it’s closed. I could code something like that, but it requires more overhead in the form a memory-file file system, similar to the old “mem drive” utility in DOS. For now, I just want to explore the memory-file concept on a simple level.

In next week’s Lesson, I present the mem_open() and mem_close() functions.

2 thoughts on “Memory Files

  1. “[…] but you don’t read from and write to memory as you would a file. I think it would be convenient to do so.”

    Interesting idea. Probably every programmer finds himself in a situation once in a while, where it would really be convenient to be able to treat memory as a FILE.

    Looking forward to next weeks blog entry, to see where you’re going with this!

  2. It turned out to be more interesting than I thought. But I don’t know if it would ever be practical, as you really need a memory file system to retain the data after the memory-file is closed.

Leave a Reply