Pointer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_IPC_MEM_POINTER_H
10 #define SQUID_IPC_MEM_POINTER_H
11 
12 #include "base/RefCount.h"
13 #include "base/TextException.h"
14 #include "ipc/mem/Segment.h"
15 
16 namespace Ipc
17 {
18 
19 namespace Mem
20 {
21 
24 template <class Class>
25 class Owner
26 {
27 public:
28  static Owner *New(const char *const id);
29  template <class P1>
30  static Owner *New(const char *const id, const P1 &p1);
31  template <class P1, class P2>
32  static Owner *New(const char *const id, const P1 &p1, const P2 &p2);
33  template <class P1, class P2, class P3>
34  static Owner *New(const char *const id, const P1 &p1, const P2 &p2, const P3 &p3);
35  template <class P1, class P2, class P3, class P4>
36  static Owner *New(const char *const id, const P1 &p1, const P2 &p2, const P3 &p3, const P4 &p4);
37 
38  ~Owner();
39 
41  Class *object() { return theObject; }
42 
43 private:
44  Owner(const char *const id, const off_t sharedSize);
45 
46  // not implemented
47  Owner(const Owner &);
48  Owner &operator =(const Owner &);
49 
51  Class *theObject;
52 };
53 
54 template <class Class> class Pointer;
55 
57 template <class Class>
58 class Object: public RefCountable
59 {
60 public:
61  static Pointer<Class> Old(const char *const id);
62 
63 private:
64  explicit Object(const char *const id);
65 
66  // not implemented
67  Object(const Object &);
68  Object &operator =(const Object &);
69 
71  Class *theObject;
72 
73  friend class Pointer<Class>;
74 };
75 
78 template <class Class>
79 class Pointer: public RefCount< Object<Class> >
80 {
81 private:
83 
84 public:
85  explicit Pointer(Object<Class> *const anObject = NULL): Base(anObject) {}
86 
87  Class *operator ->() const { return Base::operator ->()->theObject; }
88  Class &operator *() const { return *Base::operator *().theObject; }
89  const Class *getRaw() const { return Base::getRaw()->theObject; }
90  Class *getRaw() { return Base::getRaw()->theObject; }
91 };
92 
93 // Owner implementation
94 
95 template <class Class>
96 Owner<Class>::Owner(const char *const id, const off_t sharedSize):
97  theSegment(id), theObject(NULL)
98 {
99  theSegment.create(sharedSize);
100  Must(theSegment.mem());
101 }
102 
103 template <class Class>
105 {
106  if (theObject)
107  theObject->~Class();
108 }
109 
110 template <class Class>
111 Owner<Class> *
112 Owner<Class>::New(const char *const id)
113 {
114  const off_t sharedSize = Class::SharedMemorySize();
115  Owner *const owner = new Owner(id, sharedSize);
116  owner->theObject = new (owner->theSegment.reserve(sharedSize)) Class;
117  return owner;
118 }
119 
120 template <class Class> template <class P1>
121 Owner<Class> *
122 Owner<Class>::New(const char *const id, const P1 &p1)
123 {
124  const off_t sharedSize = Class::SharedMemorySize(p1);
125  Owner *const owner = new Owner(id, sharedSize);
126  owner->theObject = new (owner->theSegment.reserve(sharedSize)) Class(p1);
127  return owner;
128 }
129 
130 template <class Class> template <class P1, class P2>
131 Owner<Class> *
132 Owner<Class>::New(const char *const id, const P1 &p1, const P2 &p2)
133 {
134  const off_t sharedSize = Class::SharedMemorySize(p1, p2);
135  Owner *const owner = new Owner(id, sharedSize);
136  owner->theObject = new (owner->theSegment.reserve(sharedSize)) Class(p1, p2);
137  return owner;
138 }
139 
140 template <class Class> template <class P1, class P2, class P3>
141 Owner<Class> *
142 Owner<Class>::New(const char *const id, const P1 &p1, const P2 &p2, const P3 &p3)
143 {
144  const off_t sharedSize = Class::SharedMemorySize(p1, p2, p3);
145  Owner *const owner = new Owner(id, sharedSize);
146  owner->theObject = new (owner->theSegment.reserve(sharedSize)) Class(p1, p2, p3);
147  return owner;
148 }
149 
150 template <class Class> template <class P1, class P2, class P3, class P4>
151 Owner<Class> *
152 Owner<Class>::New(const char *const id, const P1 &p1, const P2 &p2, const P3 &p3, const P4 &p4)
153 {
154  const off_t sharedSize = Class::SharedMemorySize(p1, p2, p3, p4);
155  Owner *const owner = new Owner(id, sharedSize);
156  owner->theObject = new (owner->theSegment.reserve(sharedSize)) Class(p1, p2, p3, p4);
157  return owner;
158 }
159 
160 // Object implementation
161 
162 template <class Class>
163 Object<Class>::Object(const char *const id): theSegment(id)
164 {
165  theSegment.open();
166  Must(theSegment.mem());
167  theObject = reinterpret_cast<Class*>(theSegment.mem());
168  Must(static_cast<off_t>(theObject->sharedMemorySize()) <= theSegment.size());
169 }
170 
171 template <class Class>
173 Object<Class>::Old(const char *const id)
174 {
175  return Pointer<Class>(new Object(id));
176 }
177 
178 // convenience macros for creating shared objects
179 #define shm_new(Class) Ipc::Mem::Owner<Class>::New
180 #define shm_old(Class) Ipc::Mem::Object<Class>::Old
181 
182 } // namespace Mem
183 
184 } // namespace Ipc
185 
186 #endif /* SQUID_IPC_MEM_POINTER_H */
187 
Object< Class > * operator->() const
Definition: RefCount.h:67
Class & operator*() const
Definition: Pointer.h:88
Class * operator->() const
Definition: Pointer.h:87
void * mem()
pointer to the next chunk
Definition: Segment.h:39
Segment theSegment
shared memory segment that holds the object
Definition: Pointer.h:50
Class * theObject
shared object
Definition: Pointer.h:51
Object & operator=(const Object &)
static Owner * New(const char *const id)
Definition: Pointer.h:112
Class * theObject
shared object
Definition: Pointer.h:71
POSIX shared memory segment.
Definition: Segment.h:23
void open()
Open an existing shared memory segment.
Definition: Segment.cc:345
Object< Class > & operator*() const
Definition: RefCount.h:69
const Class * getRaw() const
Definition: Pointer.h:89
Class * getRaw()
Definition: Pointer.h:90
Owner(const char *const id, const off_t sharedSize)
Definition: Pointer.h:96
RefCount< Object< Class > > Base
Definition: Pointer.h:82
Object(const char *const id)
Definition: Pointer.h:163
void create(const off_t aSize)
Create a new shared memory segment. Unlinks the segment on destruction.
Definition: Segment.cc:327
#define Must(cond)
Definition: TextException.h:89
void * reserve(size_t chunkSize)
reserve and return the next chunk
Definition: Segment.cc:38
Owner & operator=(const Owner &)
Pointer(Object< Class > *const anObject=NULL)
Definition: Pointer.h:85
Segment theSegment
shared memory segment that holds the object
Definition: Pointer.h:70
Class * object()
Raw access; handy to finalize initiatization, but avoid if possible.
Definition: Pointer.h:41
off_t size()
shared memory segment size
Definition: Segment.h:38
Object< Class > * getRaw() const
Definition: RefCount.h:74
#define NULL
Definition: types.h:166
static Pointer< Class > Old(const char *const id)
Definition: Pointer.h:173
attaches to a shared memory segment with Class object owned by Owner
Definition: Pointer.h:58

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors