]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ipc/mem/Segment.h
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / ipc / mem / Segment.h
CommitLineData
f5eef98c 1/*
77b1029d 2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
bbc27441
AJ
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.
f5eef98c
DK
7 */
8
d8a82533
AR
9#ifndef SQUID_IPC_MEM_SEGMENT_H
10#define SQUID_IPC_MEM_SEGMENT_H
f5eef98c 11
f5480a9e 12#include "base/RunnersRegistry.h"
92e8f3ad 13#include "sbuf/forward.h"
f5eef98c
DK
14#include "SquidString.h"
15
9199139f
AR
16namespace Ipc
17{
d8a82533 18
9199139f
AR
19namespace Mem
20{
d8a82533 21
f5eef98c 22/// POSIX shared memory segment
9199139f
AR
23class Segment
24{
f5eef98c 25public:
f1eaa254 26 /// Create a shared memory segment.
d8a82533
AR
27 Segment(const char *const id);
28 ~Segment();
f5eef98c 29
c975f532
DK
30 /// Whether shared memory support is available
31 static bool Enabled();
32
15953570 33 /// Create a new shared memory segment. Unlinks the segment on destruction.
e08b0f77 34 void create(const off_t aSize);
f5eef98c
DK
35 void open(); ///< Open an existing shared memory segment.
36
37 const String &name() { return theName; } ///< shared memory segment name
e08b0f77 38 off_t size() { return theSize; } ///< shared memory segment size
2d0647fb 39 void *mem() { return reserve(0); } ///< pointer to the next chunk
937d0d3f 40 void *reserve(size_t chunkSize); ///< reserve and return the next chunk
f5eef98c 41
ef8de464
AR
42 /// common path of all segment names in path-based environments
43 static const char *BasePath;
c011f9bc 44
1860fbac
AR
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
f5eef98c 48private:
be9cb908
DK
49
50 // not implemented
51 Segment(const Segment &);
52 Segment &operator =(const Segment &);
53
54#if HAVE_SHM
55
45a6b2c8 56 bool createFresh(int &err);
f5eef98c
DK
57 void attach();
58 void detach();
c756d517 59 void lock();
68353d5a 60 void unlink(); ///< unlink the segment
e08b0f77 61 off_t statSize(const char *context) const;
f5eef98c 62
9a51593d 63 static String GenerateName(const char *id);
f5eef98c 64
be9cb908
DK
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
68353d5a 72
f5eef98c 73 const String theName; ///< shared memory segment file name
f5eef98c 74 void *theMem; ///< pointer to mmapped shared memory segment
e08b0f77
AR
75 off_t theSize; ///< shared memory segment size
76 off_t theReserved; ///< the total number of reserve()d bytes
68353d5a 77 bool doUnlink; ///< whether the segment should be unlinked on destruction
f5eef98c
DK
78};
79
f5480a9e
DK
80/// Base class for runners that create and open shared memory segments.
81/// First may run create() method and then open().
82class RegisteredRunner: public ::RegisteredRunner
83{
84public:
85 /* RegisteredRunner API */
21b7990f 86 virtual void useConfig();
f5480a9e
DK
87
88protected:
89 /// called when the runner should create a new memory segment
21b7990f 90 virtual void create() = 0;
f5480a9e
DK
91 /// called when the runner should open a previously created segment,
92 /// not needed if segments are opened in constructor or init methods
21b7990f 93 virtual void open() {}
f5480a9e
DK
94};
95
d8a82533
AR
96} // namespace Mem
97
98} // namespace Ipc
99
100#endif /* SQUID_IPC_MEM_SEGMENT_H */
f53969cc 101