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