]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/Segment.cc
4 * DEBUG: section 54 Interprocess Communication
10 #include "ipc/mem/Segment.h"
16 #include <sys/types.h>
19 Ipc::Mem::Segment::Segment(const char *const id
):
20 theName(GenerateName(id
)), theFD(-1), theMem(NULL
),
21 theSize(0), theReserved(0)
25 Ipc::Mem::Segment::~Segment() {
28 if (close(theFD
) != 0)
29 debugs(54, 5, HERE
<< "close: " << xstrerror());
34 Ipc::Mem::Segment::create(const int aSize
)
39 theFD
= shm_open(theName
.termedBuf(), O_CREAT
| O_RDWR
| O_TRUNC
,
42 debugs(54, 5, HERE
<< "shm_open: " << xstrerror());
43 fatal("Ipc::Mem::Segment::create failed to shm_open");
46 if (ftruncate(theFD
, aSize
)) {
47 debugs(54, 5, HERE
<< "ftruncate: " << xstrerror());
48 fatal("Ipc::Mem::Segment::create failed to ftruncate");
58 Ipc::Mem::Segment::open()
62 theFD
= shm_open(theName
.termedBuf(), O_RDWR
, 0);
64 debugs(54, 5, HERE
<< "shm_open: " << xstrerror());
65 String s
= "Ipc::Mem::Segment::open failed to shm_open";
72 memset(&s
, 0, sizeof(s
));
73 if (fstat(theFD
, &s
)) {
74 debugs(54, 5, HERE
<< "fstat: " << xstrerror());
75 String s
= "Ipc::Mem::Segment::open failed to fstat";
86 /// Map the shared memory segment to the process memory space.
88 Ipc::Mem::Segment::attach()
94 mmap(NULL
, theSize
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, theFD
, 0);
95 if (p
== MAP_FAILED
) {
96 debugs(54, 5, HERE
<< "mmap: " << xstrerror());
97 fatal("Ipc::Mem::Segment::attach failed to mmap");
102 /// Unmap the shared memory segment from the process memory space.
104 Ipc::Mem::Segment::detach()
109 if (munmap(theMem
, theSize
)) {
110 debugs(54, 5, HERE
<< "munmap: " << xstrerror());
111 fatal("Ipc::Mem::Segment::detach failed to munmap");
117 Ipc::Mem::Segment::reserve(size_t chunkSize
)
119 assert(chunkSize
<= theSize
);
120 assert(theReserved
<= theSize
- chunkSize
);
121 theReserved
+= chunkSize
;
122 return reinterpret_cast<char*>(mem()) + theReserved
;
125 /// Generate name for shared memory segment. Replaces all slashes with dots.
127 Ipc::Mem::Segment::GenerateName(const char *id
)
129 String
name("/squid-");
130 for (const char *slash
= strchr(id
, '/'); slash
; slash
= strchr(id
, '/')) {
132 name
.append(id
, slash
- id
);