Segment.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2020 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);
37  void open(const bool unlinkWhenDone);
38 
39  const String &name() { return theName; }
40  off_t size() { return theSize; }
41  void *mem() { return reserve(0); }
42  void *reserve(size_t chunkSize);
43 
45  static const char *BasePath;
46 
48  static SBuf Name(const SBuf &prefix, const char *suffix);
49 
50 private:
51 
52  // not implemented
53  Segment(const Segment &);
54  Segment &operator =(const Segment &);
55 
56 #if HAVE_SHM
57 
58  bool createFresh(int &err);
59  void attach();
60  void detach();
61  void lock();
62  void unlink();
63  off_t statSize(const char *context) const;
64 
65  static String GenerateName(const char *id);
66 
67  int theFD;
68 
69 #else // HAVE_SHM
70 
71  void checkSupport(const char *const context);
72 
73 #endif // HAVE_SHM
74 
75  const String theName;
76  void *theMem;
77  off_t theSize;
78  off_t theReserved;
79  bool doUnlink;
80 };
81 
85 {
86 public:
87  /* RegisteredRunner API */
88  virtual void useConfig();
89 
90 protected:
92  virtual void create() = 0;
95  virtual void open() {}
96 };
97 
98 } // namespace Mem
99 
100 } // namespace Ipc
101 
102 #endif /* SQUID_IPC_MEM_SEGMENT_H */
103 
Segment & operator=(const Segment &)
Definition: SBuf.h:86
Segment(const char *const id)
Create a shared memory segment.
Definition: Segment.cc:306
void * theMem
pointer to mmapped shared memory segment
Definition: Segment.h:76
Memory Management.
void * reserve(size_t chunkSize)
reserve and return the next chunk
Definition: Segment.cc:38
static const char * BasePath
common path of all segment names in path-based environments
Definition: Segment.h:45
int size
Definition: ModDevPoll.cc:77
off_t theSize
shared memory segment size
Definition: Segment.h:77
virtual void create()=0
called when the runner should create a new memory segment
POSIX shared memory segment.
Definition: Segment.h:23
void checkSupport(const char *const context)
Definition: Segment.cc:363
const String theName
shared memory segment file name
Definition: Segment.h:75
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
static bool Enabled()
Whether shared memory support is available.
Definition: Segment.cc:322
bool doUnlink
whether the segment should be unlinked on destruction
Definition: Segment.h:79
off_t theReserved
the total number of reserve()d bytes
Definition: Segment.h:78
void open(const bool unlinkWhenDone)
Definition: Segment.cc:346
void create(const off_t aSize)
Create a new shared memory segment. Unlinks the segment on destruction.
Definition: Segment.cc:328
virtual void open()
Definition: Segment.h:95
virtual void useConfig()
Definition: Segment.cc:377
Definition: IpcIoFile.h:23

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors