]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/Segment.h
Migrated RegisteredRunners to a multi-action interface.
[thirdparty/squid.git] / src / ipc / mem / Segment.h
1 /*
2 */
3
4 #ifndef SQUID_IPC_MEM_SEGMENT_H
5 #define SQUID_IPC_MEM_SEGMENT_H
6
7 #include "base/RunnersRegistry.h"
8 #include "SquidString.h"
9
10 namespace Ipc
11 {
12
13 namespace Mem
14 {
15
16 /// POSIX shared memory segment
17 class Segment
18 {
19 public:
20 /// Create a shared memory segment.
21 Segment(const char *const id);
22 ~Segment();
23
24 /// Whether shared memory support is available
25 static bool Enabled();
26
27 /// Create a new shared memory segment. Unlinks the segment on destruction.
28 void create(const off_t aSize);
29 void open(); ///< Open an existing shared memory segment.
30
31 const String &name() { return theName; } ///< shared memory segment name
32 off_t size() { return theSize; } ///< shared memory segment size
33 void *mem() { return reserve(0); } ///< pointer to the next chunk
34 void *reserve(size_t chunkSize); ///< reserve and return the next chunk
35
36 /// common path of all segment names in path-based environments
37 static const char *BasePath;
38
39 private:
40
41 // not implemented
42 Segment(const Segment &);
43 Segment &operator =(const Segment &);
44
45 #if HAVE_SHM
46
47 void attach();
48 void detach();
49 void unlink(); ///< unlink the segment
50 off_t statSize(const char *context) const;
51
52 static String GenerateName(const char *id);
53
54 int theFD; ///< shared memory segment file descriptor
55
56 #else // HAVE_SHM
57
58 void checkSupport(const char *const context);
59
60 #endif // HAVE_SHM
61
62 const String theName; ///< shared memory segment file name
63 void *theMem; ///< pointer to mmapped shared memory segment
64 off_t theSize; ///< shared memory segment size
65 off_t theReserved; ///< the total number of reserve()d bytes
66 bool doUnlink; ///< whether the segment should be unlinked on destruction
67 };
68
69 /// Base class for runners that create and open shared memory segments.
70 /// First may run create() method and then open().
71 class RegisteredRunner: public ::RegisteredRunner
72 {
73 public:
74 /* RegisteredRunner API */
75 virtual void useConfig();
76
77 protected:
78 /// called when the runner should create a new memory segment
79 virtual void create() = 0;
80 /// called when the runner should open a previously created segment,
81 /// not needed if segments are opened in constructor or init methods
82 virtual void open() {}
83 };
84
85 } // namespace Mem
86
87 } // namespace Ipc
88
89 #endif /* SQUID_IPC_MEM_SEGMENT_H */