
Originally Posted by
cpradio
So for your first issue, it sounds like you need to implement a locking technique, which is typical for any shared system. Just like you need to implement locking when dealing with files, locking with shared memory would be a requirement to stop the clashing. I'd likely follow the pattern used by 'apt' on debian based systems. Create a file/indicator when the shared memory is being accessed, and remove it when done. You would check the existence of said indicator/file, if it doesn't exist, create it, access your memory, remove the indicator/file. If the indicator/file exists, wait, check again, wait (and so on) until the existence isn't found, create the indicator/file, access the memory, remove the indicator/file.
As for your static size, I think you have the right idea there, to dynamically increase it as necessary. Be sure to increase it more than its immediate necessity is (to help performance). Example: Let's say you have 100 bytes to start with and you are now at 98 bytes used. The next 4 bytes come in, meaning you need 102 bytes, but only have 100 currently reserved. Setup a new 130 bytes, transfer over the existing 98 and add the new 4 bytes so you have 102 out of 130 bytes used (I've used a 30% rule in the past, increasing the bytes by 30 percent on each growth, with the thought, as the data gets larger and larger the expectation is the data being sent is increasing larger and larger too). From what I recall from my C++ dynamic memory allocation courses, grabbing and allocating memory can be expensive if done often, it is best to give room for growth (but not too much where you are taking more than what you will actually need -- making you look resource intensive).
Lastly, I'd allow yourself the ability to pass in the initial size. There may be times you know your original dataset is going to be 300 bytes, why reserve 100 just to increase it immediately to 300 or 400 when you write to it?
Bookmarks