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