Skip to main content
GameDev.net gamedev.net
🔒 Locked

data structures on disk

Started by wolverine Oct 10, 2001 at 6:42 AM 7 replies 650+ views
Original Post
wolverine
wolverine
Does anyone knows where i can find info (preferably online) about memory management on disk? For example, having an vector (or linked list or something) in main memory, save it to disk and reload it to main memory exactly as i saved it. Thanks!
spillsome
spillsome
search for "Serialisation"
BlueNexus
BlueNexus
Here are a few API functions to look up.

CreateFileMapping
MapViewOfFile
UnmapViewOfFile
FlushViewOfFile

What they do is take an open file and copy the contents to memory. You get back a pointer to that section in memory. From there you just cast your pointer to whatever data structure you wrote the file with. When you flush the data, it will write the contents that are in memory to the file. MSDN will have all the details.

I have an example if you need one.
Good luck!
-BlueNexus
bluenexus@hotmail.com
-BlueNexus--This is my sig. There are many like it, but this one is mine.
wolverine
wolverine
Sorry!
I forgot to mention, im doing it in C. No JAVA.

Thanks anyway for your help guys.
BeerNutts
BeerNutts
If you''re trying to do this to save your data to HD, and quit the program your running (I don''t believe those API calls will be valid if you shut down your computer and start the program up at a later time), a good idea is to save the pointer to each node in the list or vector, write th data stored in the node in the following bytes, write some know node seperator, then write the next node''s address to disk.

This way, when you load it back up, you can have unique identifiers for each node, and where each node''s next pointer is suppose to point to. I''ve done the exact same thing when I have to save the state of my AI''s "Brain" (a large network of nodes).
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
Grib
Grib
FileViews will also not work as expected on a linked list where each node is created with new(other than placement new). It is a good and powerfull technique, with the primary advantage that you can constantly expand the contiguous memory pointed to by the file view (also known as virtual memory). This memory can also be shared by more than one program.

Your best bet with this technique is to use fixed length records that contain no pointers, Make the record length a multiple of 2K, and if you really need to link one record to another, use an index from the start of the records, just like a simple array.

Rec *records = new(static_cast < Rec* > pointer_to_fileview) Rec[numRec];

I have never used this with anything but POD with trivial constructors, but it should work.
wolverine
wolverine
Sorry BlueNexus, i didnt realize you were talking about C. By the names of the functions, i (wrongly) assumed you were talking about JAVA. Sorry.

BeerNuts ->
You''ve got the idea. Basicly, it''s a database program and when i quit the program, i should save what''s in main memory to HD, and when i start the program it''s suppose to "load" what was previously saved on HD. Kind of like Serialisable in JAVA. After exiting the program, i could shut down the computer or do other stuff.
But that''s not all. I have to deal with a lot of info. Some things wich i have at a given time in main memory, can be "un-needed" at another given time. When this happens, i should write it HD and load something else i need. Basicly, all the data is in disk and main memory will act like a cache for things that are currently being accessed. This means it will spend a great deal of time writting and saving things to disk.
I already have some ideas on writting/loading nodes and stuff like that to disk, but i would like to check that. Unfortunately, i can''t find any book or internet resources on that.

Thanks again.

P.S. your A.I.''s "Brain" seems interesting.......
BlueNexus
BlueNexus
Grib is correct in stating that the FileViews wouldn''t work real well with linked lists. They work great for any type of flat memory layouts. It shouldn''t be real hard to come up with an indexed array to simulate a linked list. And when you close the FileView, it dumps everythign back into the file so you can recall it. Even after you restart your machine. It will probably be loaded into a different section of memory, but you will still have the data arranged as you left it.

I''ll post an example when I get home from work tonight.

-BlueNexus
-BlueNexus--This is my sig. There are many like it, but this one is mine.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.