]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/ipc/mem/Pointer.h
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / ipc / mem / Pointer.h
index 55cb55b86b63c4e022c6e0bae1a8370ec843a1ac..c9ed70679e3a58c4fcffa1e7e176d9eee35232e7 100644 (file)
@@ -1,4 +1,9 @@
 /*
+ * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
 #ifndef SQUID_IPC_MEM_POINTER_H
@@ -29,10 +34,16 @@ public:
     static Owner *New(const char *const id, const P1 &p1, const P2 &p2, const P3 &p3);
     template <class P1, class P2, class P3, class P4>
     static Owner *New(const char *const id, const P1 &p1, const P2 &p2, const P3 &p3, const P4 &p4);
+    /// attaches to the existing shared memory segment, becoming its owner
+    static Owner *Old(const char *const id);
 
     ~Owner();
 
+    /// Raw access; handy to finalize initiatization, but avoid if possible.
+    Class *object() { return theObject; }
+
 private:
+    explicit Owner(const char *const id);
     Owner(const char *const id, const off_t sharedSize);
 
     // not implemented
@@ -74,7 +85,7 @@ private:
     typedef RefCount< Object<Class> > Base;
 
 public:
-    explicit Pointer(Object<Class> *const anObject = NULL): Base(anObject) {}
+    explicit Pointer(Object<Class> *const anObject = nullptr): Base(anObject) {}
 
     Class *operator ->() const { return Base::operator ->()->theObject; }
     Class &operator *() const { return *Base::operator *().theObject; }
@@ -86,12 +97,20 @@ public:
 
 template <class Class>
 Owner<Class>::Owner(const char *const id, const off_t sharedSize):
-        theSegment(id), theObject(NULL)
+    theSegment(id), theObject(nullptr)
 {
     theSegment.create(sharedSize);
     Must(theSegment.mem());
 }
 
+template <class Class>
+Owner<Class>::Owner(const char *const id):
+    theSegment(id), theObject(nullptr)
+{
+    theSegment.open(true);
+    Must(theSegment.mem());
+}
+
 template <class Class>
 Owner<Class>::~Owner()
 {
@@ -99,6 +118,16 @@ Owner<Class>::~Owner()
         theObject->~Class();
 }
 
+template <class Class>
+Owner<Class> *
+Owner<Class>::Old(const char *const id)
+{
+    auto owner = new Owner(id);
+    owner->theObject = reinterpret_cast<Class*>(owner->theSegment.mem());
+    Must(static_cast<off_t>(owner->theObject->sharedMemorySize()) <= owner->theSegment.size());
+    return owner;
+}
+
 template <class Class>
 Owner<Class> *
 Owner<Class>::New(const char *const id)
@@ -154,10 +183,10 @@ Owner<Class>::New(const char *const id, const P1 &p1, const P2 &p2, const P3 &p3
 template <class Class>
 Object<Class>::Object(const char *const id): theSegment(id)
 {
-    theSegment.open();
+    theSegment.open(false);
     Must(theSegment.mem());
     theObject = reinterpret_cast<Class*>(theSegment.mem());
-    Must(static_cast<off_t>(theObject->sharedMemorySize()) == theSegment.size());
+    Must(static_cast<off_t>(theObject->sharedMemorySize()) <= theSegment.size());
 }
 
 template <class Class>
@@ -176,3 +205,4 @@ Object<Class>::Old(const char *const id)
 } // namespace Ipc
 
 #endif /* SQUID_IPC_MEM_POINTER_H */
+