Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » Getting a pointer to hard drive memory location?

Credits go to Arthur Kalliokoski for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
Getting a pointer to hard drive memory location?
Thomas Fjellstrom
Member #476
June 2000
avatar

Arthur's, without a doubt. What Thomas and I were talking about would be more useful for creating filesystems, inspecting disks, and things of that nature.

Um, mmap gets you a virtual memory address range for a file on disk.

If you want to work under the level of the FS, you need to actually open a handle to the disk, with I assume CreateFile.

On linux the equivalent would be open (NOT fopen).

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

relpatseht
Member #5,034
September 2004
avatar

Sorry, it's been a while since I looked up mmap documentation.

Dustin Dettmer
Member #3,935
October 2003
avatar

mmap's primary purpose is to save the memory copies that come about with typical file read and write methods. It moves the workload into the kernel, which is in a better position to do the work in an optimized fashion. It also can make certain kinds of operations easier for huge files.

But it's much more complex to setup (and requires care to destroy properly) that I wouldn't recommend it for the OP. Just the number of parameters could be considered daunting.

void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset);

verthex
Member #11,340
September 2009
avatar

I don't plan on having too many hard disk look ups. I just needed it to save data in a formatted way.

Thomas Fjellstrom
Member #476
June 2000
avatar

So yeah, just use the standard file functions. Either stdio, or C++s iostream stuff.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

verthex
Member #11,340
September 2009
avatar

So yeah, just use the standard file functions. Either stdio, or C++s iostream stuff.

Nope that would be too difficult... just imagine the row major order calculation for an array and thats how I want the formatting to exist. Basically I want to save my world in a 3d coordinate system, sort of like a 2d bitmap. The problem with that is that STL file routines are messy with lookup and replacing operations.

relpatseht
Member #5,034
September 2004
avatar

You do realize the functions Arthur pointed out are stdio, and iostream has all the same functionality, correct?

verthex
Member #11,340
September 2009
avatar

You do realize the functions Arthur pointed out are stdio, and iostream has all the same functionality, correct?

No, had no idea, the operator he mentioned didn't seem related to it at all.

relpatseht
Member #5,034
September 2004
avatar

fseek and ftell fstream equivalents are seekg and tellg respectively. Both sets work about the same. Regardless, seeking around in files isn't the fastest thing in the world. Normally, you're better off just using multiple files.
Try decomposing your world into chunks and then just loading those instead of having large files you seek in.

verthex
Member #11,340
September 2009
avatar

seek [en.cppreference.com] and ftell [en.cppreference.com] fstream equivalents are seekg [en.cppreference.com] and tellg [en.cppreference.com] respectively. Both sets work about the same. Regardless, seeking around in files isn't the fastest thing in the world. Normally, you're better off just using multiple files.
Try decomposing your world into chunks and then just loading those instead of having large files you seek in.

I guess I could use ftell, but wouldn't using pointers make more sense, since you have addresses in memory that are contiguous and then incrementing or doing some other operation to find the offset would land you somewhere in the file?

relpatseht
Member #5,034
September 2004
avatar

If you want to use pointers, this is not the correct api. I mean, you could wrap it in a class in c++ such that it looked like a pointer, but that is beside the point.

Because of the way hard drives' drivers are optimized for contiguous reads and writes, not jumping all over the place to write tiny bits of data as you would in memory. This, however, is probably irrelevant as there is probably a better way to do what you are trying to do than trying to use a large file on the hard drive as a big block of contiguous memory.

If you would, state exactly what you're trying to do. I or someone else will be able to tell you the best way to go about it.

Based on what you've said thus far, I'm currently thinking you want a large 3D world which you're only loading pieces of. If this is the case, it is certainly a better idea to break your world up into chunks, with each chunk being an individual file. You would then load entire files whenever you needed to load different parts of the world. This would perform much better.

verthex
Member #11,340
September 2009
avatar

If you would, state exactly what you're trying to do.

Quote:

This, however, is probably irrelevant as there is probably a better way to do what you are trying to do than trying to use a large file on the hard drive as a big block of contiguous memory.

Well its more of a one time deal saving the world in a file, I'm sure there are better ways but the ghetto method works wonders too.

relpatseht
Member #5,034
September 2004
avatar

What do you learn if you aren't doing things in new and better ways?

Thomas Fjellstrom
Member #476
June 2000
avatar

There is no way to get a "pointer" to a file on disk, other than something like mmap.

Storing/Reading fixed length record files in stdio is dead simple. I'm surprised you think its "too difficult". Each record is RECORD_LEN bytes in size, say you want the 1023'd record, just do: fseek(fh, 1023 * RECORD_LEN, SEEK_SET); fread(ptr, RECCORD_LEN, 1, fh); (but with more error checking).

It really isn't hard working with files, especially with fixed length records. It's more dynamic formats thats hard.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

verthex
Member #11,340
September 2009
avatar

Storing/Reading fixed length record files in stdio is dead simple. I'm surprised you think its "too difficult". Each record is RECORD_LEN bytes in size, say you want the 1023'd record, just do: fseek(fh, 1023 * RECORD_LEN, SEEK_SET); fread(ptr, RECCORD_LEN, 1, fh); (but with more error checking).

I'll try that, thanks Thomas.

 1   2 


Go to: