From: Alex Rousskov Date: Sat, 9 Apr 2011 04:20:21 +0000 (-0600) Subject: Added reserve() method to allow nested classes or similar related users of X-Git-Tag: take06~47 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=937d0d3f1a1ba2a07def3bf46418d46146feaa4d;p=thirdparty%2Fsquid.git Added reserve() method to allow nested classes or similar related users of the same segment to safely bite off pieces of the same shared segment. Still need to convert the callers. The reserve() method is useful for single-users as well because it allows to check that a segment has enough bytes allocated for its single user. Changed theSize type from int to "size of any single object in RAM" size_t. --- diff --git a/src/ipc/mem/Segment.cc b/src/ipc/mem/Segment.cc index d677505f69..3de370bf71 100644 --- a/src/ipc/mem/Segment.cc +++ b/src/ipc/mem/Segment.cc @@ -17,7 +17,8 @@ #include Ipc::Mem::Segment::Segment(const char *const id): - theName(GenerateName(id)), theFD(-1), theSize(-1), theMem(NULL) + theName(GenerateName(id)), theFD(-1), theMem(NULL), + theSize(0), theReserved(0) { } @@ -48,6 +49,7 @@ Ipc::Mem::Segment::create(const int aSize) } theSize = aSize; + theReserved = 0; attach(); } @@ -86,7 +88,6 @@ void Ipc::Mem::Segment::attach() { assert(theFD >= 0); - assert(theSize >= 0); assert(!theMem); void *const p = @@ -112,6 +113,15 @@ Ipc::Mem::Segment::detach() theMem = 0; } +void * +Ipc::Mem::Segment::reserve(size_t chunkSize) +{ + assert(chunkSize <= theSize); + assert(theReserved <= theSize - chunkSize); + theReserved += chunkSize; + return reinterpret_cast(mem()) + theReserved; +} + /// Generate name for shared memory segment. Replaces all slashes with dots. String Ipc::Mem::Segment::GenerateName(const char *id) diff --git a/src/ipc/mem/Segment.h b/src/ipc/mem/Segment.h index dc8475d60c..807b24fa38 100644 --- a/src/ipc/mem/Segment.h +++ b/src/ipc/mem/Segment.h @@ -27,6 +27,8 @@ public: const String &name() { return theName; } ///< shared memory segment name int size() { return theSize; } ///< shared memory segment size void *mem() { return theMem; } ///< pointer to mmapped shared memory segment + void *reserve(size_t chunkSize); ///< reserve and return the next chunk + // TODO: convert most mem() calls to reserve() private: void attach(); @@ -36,8 +38,9 @@ private: const String theName; ///< shared memory segment file name int theFD; ///< shared memory segment file descriptor - int theSize; ///< shared memory segment size void *theMem; ///< pointer to mmapped shared memory segment + size_t theSize; ///< shared memory segment size + size_t theReserved; ///< the total number of reserve()d bytes }; } // namespace Mem