In PL/1, in addition to regular pointer variables, there were also "area" variables and "offset" variables. The idea being that it didn't matter where an area started, because all the offset variables were relative to the start address.
You could do the same thing in C, declaring e.g. unsigned char *shared_start; off_t offset; and assigning the return from shmat() to shared_start. You could then use shared_start[offset] or *(shared_start+offset) to access any particular byte, cast (shared_start+offset) as needed to access other sorts of objects, etc. You would never store pointers, only values of offset. That way, it matter what valid address shmat() returned, references to something within the same shared memory segment via an offset would always be correct. Alternatively, you could do most of what you're doing now, but convert pointers to offsets before storing them within shared_memory segment, i.e. #define ptr_to_offset(base,ptr) ((char *) (ptr) - (char *) (base)) and never store anything but the result of that anywhere another process might see it. A nice thing about that is that you could have linked lists, b-trees, and other tricky data structures (assuming some sort of serialization if in shared memory) in an area of memory (even from malloc() if it wasn't shared), and then, to save state for later, write the whole area out to disk and be able to reload it later, possibly at a different address, and have it all work. Although to really make that sort of thing work well, I'd like to see a library of functions that would allocate _within_ an area (which might have come from shmat(), mmap(), malloc(), or whatever), using either no locking, thread-safe locking, or even multi-process safe locking (for shared memory). It should also have functions to provide safe get/set for any given object or data structure. And it should probably never give back pointers at all, preferring offsets within the area (although civilized people would treat those as opaque to guarantee they weren't bypassing the locking). I don't suppose someone has already written a reasonably portable version of such a library (using POSIX thread and/or IPC mechanisms)? :-) This message posted from opensolaris.org _______________________________________________ perf-discuss mailing list [email protected]
