Segment.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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 
23 class Segment
24 {
25 public:
27  Segment(const char *const id);
28  ~Segment();
29 
31  static bool Enabled();
32 
34  void create(const off_t aSize);
35  void open();
36 
37  const String &name() { return theName; }
38  off_t size() { return theSize; }
39  void *mem() { return reserve(0); }
40  void *reserve(size_t chunkSize);
41 
43  static const char *BasePath;
44 
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();
61  off_t statSize(const char *context) const;
62 
63  static String GenerateName(const char *id);
64 
65  int theFD;
66 
67 #else // HAVE_SHM
68 
69  void checkSupport(const char *const context);
70 
71 #endif // HAVE_SHM
72 
73  const String theName;
74  void *theMem;
75  off_t theSize;
76  off_t theReserved;
77  bool doUnlink;
78 };
79 
83 {
84 public:
85  /* RegisteredRunner API */
86  virtual void useConfig();
87 
88 protected:
90  virtual void create() = 0;
93  virtual void open() {}
94 };
95 
96 } // namespace Mem
97 
98 } // namespace Ipc
99 
100 #endif /* SQUID_IPC_MEM_SEGMENT_H */
101 
Segment(const char *const id)
Create a shared memory segment.
Definition: Segment.cc:305
void * mem()
pointer to the next chunk
Definition: Segment.h:39
Definition: SBuf.h:86
static SBuf Name(const SBuf &prefix, const char *suffix)
concatenates parts of a name to form a complete name (or its prefix)
Definition: Segment.cc:52
Segment & operator=(const Segment &)
Definition: IpcIoFile.h:23
POSIX shared memory segment.
Definition: Segment.h:23
void open()
Open an existing shared memory segment.
Definition: Segment.cc:345
static bool Enabled()
Whether shared memory support is available.
Definition: Segment.cc:321
virtual void open()
Definition: Segment.h:93
static const char * BasePath
common path of all segment names in path-based environments
Definition: Segment.h:43
void create(const off_t aSize)
Create a new shared memory segment. Unlinks the segment on destruction.
Definition: Segment.cc:327
const String theName
shared memory segment file name
Definition: Segment.h:73
bool doUnlink
whether the segment should be unlinked on destruction
Definition: Segment.h:77
void * reserve(size_t chunkSize)
reserve and return the next chunk
Definition: Segment.cc:38
Memory Management.
void * theMem
pointer to mmapped shared memory segment
Definition: Segment.h:74
void checkSupport(const char *const context)
Definition: Segment.cc:362
off_t theReserved
the total number of reserve()d bytes
Definition: Segment.h:76
off_t size()
shared memory segment size
Definition: Segment.h:38
const String & name()
shared memory segment name
Definition: Segment.h:37
off_t theSize
shared memory segment size
Definition: Segment.h:75

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors