views:

132

answers:

2

I've been playing around with embedding resources into my c++ program. In order to do this I hexdump the data to a simple array, i.e.

unsigned char image_png[] ={
    0x0a, 0x0b, 0x0c, 0x0d, ...
};

Some of these resources are not used after loading (i.e. they get converted to something else and then the original data is just bulk... though a small bit of bulk for the ease of distribution might be worth it).

I'm curious if there's a way to embed the resource into the program, so that I don't have to worry about the binary being able to find all it's most important resources, but then free it up after it's done being used so that the runtime memory footprint takes less of a hit.

Is this possible? If it is possible, is it a stupid thing to try to do? For instance, maybe the os will keep the entire program image in memory anyway (I'm not sure exactly how that works).

edit: To answer comments, I'm working on Linux (Ubuntu 10.04), but if there are cross-platform solutions I would love to hear them

A: 

One way I've seen being used in several applications is concatenating the data in the end of the executable, then appending the size of the data itself.

You can then fopen the executable file, go to the end of the stream and read the size of the data, then go back that size and read the resources.

Note that the resources will be exactly as you put them, so organization might be at risk.

I also can't tell if this is a best practice, but it seems to work.

luiscubal
That's a clever solution. I could tar the resources together and then use a tar library to find all the files inside the archive. If I understand correctly the process will be something like: $SIZE = stat -s%o "executable"; cat resources.tar >> executable; printf "%20i" $SIZE >> executable;
cheshirekow
Just be prepared for every antivirus under the sun to start complaining when checksums don't match up. ;)
Billy ONeal
Good point. On a side note, a large program that consumes a huge chunk of system resources, slows down (and second guesses) everything you do, and sporatically prevents normal operation of a system... sounds like a virus to me. My favorite anti-virus software: rdiff-backup.
cheshirekow
Ok, so maybe not a great solution, but +1 for a clever hack.
cheshirekow
+4  A: 

As Tomaka17 says, you don't really have to worry about it - if you never touch that resource, it will never be faulted in, and it won't consume physical memory. When you load a DLL/so/whatever, it really only maps the file into memory; trying to access that file is what results in actually reading the file, piece by piece.

Paul Betts
so I will definately use the resource *once*, at the beginning of the program. i.e. load a png file, turn it into a native bitmap to display on the GUI. After that, the png isn't needed anymore.
cheshirekow
This is not strictly true -- because in all likelihood that data is going to be on the same memory page as some other items that you *are* using.
Billy ONeal
@Billy Granted, but images are pretty big, and pages are only 4k. It's far better than the naive view of the entire DLL being loaded verbatim
Paul Betts
@Paul: Touché :)
Billy ONeal
@cheshirekow The PNG data will eventually be reclaimed by the OS if your code never touches it. The read-only pages can be discarded because they are never written to, and will be paged in on demand again if needed. The only cost to your program is some virtual address space.
karunski
So, really the only reason you'd worry about this is if you were trying to reduce startup time (a legitimate concern). If your app starts up fast enough, I wouldn't worry.
Paul Betts
@karunski, @paul betts, Thanks. That completely answers my question. So it's not something to worry about unless I run into issues of it loading slowly.
cheshirekow