]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/Segment.h
3567c00a118f1e97869f9e0449e7c4e4911eed49
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_IPC_MEM_SEGMENT_H
10 #define SQUID_IPC_MEM_SEGMENT_H
12 #include "base/RunnersRegistry.h"
13 #include "sbuf/forward.h"
14 #include "SquidString.h"
22 /// POSIX shared memory segment
26 /// Create a shared memory segment.
27 Segment(const char *const id
);
30 /// Whether shared memory support is available
31 static bool Enabled();
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.
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
42 /// common path of all segment names in path-based environments
43 static const char *BasePath
;
45 /// concatenates parts of a name to form a complete name (or its prefix)
46 static SBuf
Name(const SBuf
&prefix
, const char *suffix
);
51 Segment(const Segment
&);
52 Segment
&operator =(const Segment
&);
56 bool createFresh(int &err
);
60 void unlink(); ///< unlink the segment
61 off_t
statSize(const char *context
) const;
63 static String
GenerateName(const char *id
);
65 int theFD
; ///< shared memory segment file descriptor
69 void checkSupport(const char *const context
);
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
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
85 /* RegisteredRunner API */
86 virtual void useConfig();
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() {}
100 #endif /* SQUID_IPC_MEM_SEGMENT_H */