]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Added reserve() method to allow nested classes or similar related users of
authorAlex Rousskov <rousskov@measurement-factory.com>
Sat, 9 Apr 2011 04:20:21 +0000 (22:20 -0600)
committerAlex Rousskov <rousskov@measurement-factory.com>
Sat, 9 Apr 2011 04:20:21 +0000 (22:20 -0600)
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.

src/ipc/mem/Segment.cc
src/ipc/mem/Segment.h

index d677505f695e361647c2479d457cfd28bb43f54d..3de370bf71121bf9494ca8c0f335e7b3693d4ef5 100644 (file)
@@ -17,7 +17,8 @@
 #include <unistd.h>
 
 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<char*>(mem()) + theReserved;
+}
+
 /// Generate name for shared memory segment. Replaces all slashes with dots.
 String
 Ipc::Mem::Segment::GenerateName(const char *id)
index dc8475d60c61a65bd58bb3d35f52d43bbf15ed38..807b24fa381d864961e30c54ccb987e862687698 100644 (file)
@@ -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