1. Partly shared memory

Hi,

Is it possible to have memory that is shared in two variables until a perticular part of the memory changes, in which case only the memory (bytes) that changes get allocated but is only visible to one of the variables?

Example:

You load a bitmap into memory and create two pointers to the colorbytes. The pointers will then share the same memory. Lets say I then change a few bytes in one of the variables... is there a good way to then only have the changed bytes visible to one of the pointers while still sharing the rest of the bytes?

I realize you can't do it with simple pointers like that but perhaps you get the idea.

2. one variable is a pointer/reference and the other is a value.

3. Originally Posted by m37h0d
one variable is a pointer/reference and the other is a value.
Sounds good but how would it be implemented... are you saying it is very simple? If the pointer points to the value then changing the value changes all that the pointer points to as well right?

4. You'd need two copies of the bitmaps and then do a diff of them.

5. Wouldn't something like this work?

Code:
```int numColors = 1000;
COLOR* colors1 = (COLOR*)malloc(4 * numColors);
COLOR** colors2 = new COLOR*[numColors]; //Array of pointers to pointers
for (int i=0;i<numColors;i++)
{
colors2[i] = &colors1[i];
}
//The variables now share the same memory?

//Then later on allocate new memory only for changes like this?
COLOR* b = new COLOR();
colors2[40] = b;```
How much memory does an array of pointers take up?(how many bytes per pointer)

6. Originally Posted by DrSnuggles
Sounds good but how would it be implemented... are you saying it is very simple? If the pointer points to the value then changing the value changes all that the pointer points to as well right?
use one bitmap* (or bitmap&) and one bitmap. it sounds like you're confusing yourself by trying to subvert the concept of pointers.

7. hmm it seems that sizeof a pointer is == sizeof(int) so in my example the array of empty pointers(color2) would take up just as much memory as color1 which holds variables of type COLOR class which is 4 bytes each. So no gain at all by using an array of pointers to pointers it seems.

m37h0d - I don't understand how you mean I should be able to alter part of one variable without also changing the other variable like that, if they start out sharing the same memory. Please explain a bit more.

8. Trying to implement copy-on-write?

How about something like 1 pointer for, 10 or 100 elements? Increasing the block size will decrease the memory consumed by the pointers (and as you have pointed out, using a block size of 1 means no gain at all), but increasing the block size will waste memory when only, say, 1 element of the block is modified (the whole block has to be copied).

9. Originally Posted by cyberfish
Trying to implement copy-on-write?

How about something like 1 pointer for, 10 or 100 elements? Increasing the block size will decrease the memory consumed by the pointers (and as you have pointed out, using a block size of 1 means no gain at all), but increasing the block size will waste memory when only, say, 1 element of the block is modified (the whole block has to be copied).
Ah yes, that is exactly what I'm trying to do. Could be an option to use a larger block size but that will also introduce calculations each time something needs to be copied which is not good. Will consider it though thanks.

10. Originally Posted by DrSnuggles
Ah yes, that is exactly what I'm trying to do. Could be an option to use a larger block size but that will also introduce calculations each time something needs to be copied which is not good. Will consider it though thanks.
So what you want is a tiled raster with the ability to share tiles between rasters. A "copy on write" strategy.

11. Yeah, I guess something like that.

One option would be to store each new copy along with its index in the original array. The memory size of each copy would be double (sizeof(int)+sizeof(COLOR)) but since most of the time only smaller parts of the original array would be copied it might work well, will have to think about it...

12. IIRC, the size of a normal memory block is 1kB, so if you partition your grid into tiles of that size, then you would use memory most efficiently. Using blocks of different sizes will not mean that multiples of 1kB still be allocated by the OS, just not used (although the spare memory may be used by other future calls to new).

13. Originally Posted by DrSnuggles
hmm it seems that sizeof a pointer is == sizeof(int) so in my example the array of empty pointers(color2) would take up just as much memory as color1 which holds variables of type COLOR class which is 4 bytes each. So no gain at all by using an array of pointers to pointers it seems.

m37h0d - I don't understand how you mean I should be able to alter part of one variable without also changing the other variable like that, if they start out sharing the same memory. Please explain a bit more.
i didn't mean that - and you can't AFAIK. i was suggesting something similar to the copy-on-write, but foregoing the optimization of only creating the private copy when a caller attempts to write; i was suggesting just making the copy in the first place.

14. Originally Posted by King Mir
IIRC, the size of a normal memory block is 1kB, so if you partition your grid into tiles of that size, then you would use memory most efficiently. Using blocks of different sizes will not mean that multiples of 1kB still be allocated by the OS, just not used (although the spare memory may be used by other future calls to new).
Normally, memory pages (as for controling "copy-on-write") are 4KB.

For a simple solution, in C++, I would use a class that has a "modifydata" function. The class holds a pointer to the bitmap data, and whenever you call the modifydata member function, it copies the entire bitmap. But you could do it in portions, 4KB or smaller, by keeping a set of pointers to tiles or stripes.

--
Mats