Thread: Partly shared memory

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    166

    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. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    one variable is a pointer/reference and the other is a value.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Quote Originally Posted by m37h0d View Post
    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. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You'd need two copies of the bitmaps and then do a diff of them.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    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)
    Last edited by DrSnuggles; 01-20-2009 at 09:41 PM.

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by DrSnuggles View Post
    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. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    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. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    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. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Quote Originally Posted by cyberfish View Post
    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. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by DrSnuggles View Post
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    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. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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).
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by DrSnuggles View Post
    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. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by King Mir View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shared Memory semaphores?
    By Ironic in forum C Programming
    Replies: 0
    Last Post: 10-31-2008, 07:13 PM
  2. Shared memory woes
    By joshdick in forum C Programming
    Replies: 4
    Last Post: 07-28-2005, 08:59 AM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM
  5. Shared memory in Linux: B-TREE of structures
    By zahid in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:15 PM