]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/PagePool.cc
Unlink shared segments used by memory cache, using RunnersRegistry API.
[thirdparty/squid.git] / src / ipc / mem / PagePool.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 54 Interprocess Communication
5 *
6 */
7
8 #include "config.h"
9 #include "base/TextException.h"
10 #include "ipc/mem/Page.h"
11 #include "ipc/mem/PagePool.h"
12
13
14 static String
15 PageIndexId(String id)
16 {
17 id.append("-index");
18 return id;
19 }
20
21
22 // Ipc::Mem::PagePool
23
24 Ipc::Mem::PagePool::PagePool(const String &id, const unsigned int capacity, const size_t pageSize):
25 pageIndex(PageIndexId(id), capacity),
26 shm(id.termedBuf())
27 {
28 shm.create(Shared::MemSize(capacity, pageSize));
29 assert(shm.mem());
30 shared = new (shm.mem()) Shared(capacity, pageSize);
31 }
32
33 Ipc::Mem::PagePool::PagePool(const String &id):
34 pageIndex(PageIndexId(id)), shm(id.termedBuf())
35 {
36 shm.open();
37 shared = reinterpret_cast<Shared *>(shm.mem());
38 assert(shared);
39 }
40
41 void
42 Ipc::Mem::PagePool::Unlink(const String &id)
43 {
44 PageStack::Unlink(PageIndexId(id));
45 Segment::Unlink(id.termedBuf());
46 }
47
48 bool
49 Ipc::Mem::PagePool::get(PageId &page)
50 {
51 if (pageIndex.pop(page.number)) {
52 page.pool = shared->theId;
53 return true;
54 }
55 return false;
56 }
57
58 void
59 Ipc::Mem::PagePool::put(PageId &page)
60 {
61 Must(pageIdIsValid(page));
62 pageIndex.push(page.number);
63 page = PageId();
64 }
65
66 void *
67 Ipc::Mem::PagePool::pagePointer(const PageId &page)
68 {
69 Must(pageIdIsValid(page));
70 return shared->theBuf + shared->thePageSize * (page.number - 1);
71 }
72
73 bool
74 Ipc::Mem::PagePool::pageIdIsValid(const PageId &page) const
75 {
76 return page.pool == shared->theId &&
77 0 < page.number && page.number <= shared->theCapacity;
78 }
79
80
81 // Ipc::Mem::PagePool::Shared
82
83 static unsigned int LastPagePoolId = 0;
84
85 Ipc::Mem::PagePool::Shared::Shared(const unsigned int aCapacity, size_t aPageSize):
86 theId(++LastPagePoolId), theCapacity(aCapacity), thePageSize(aPageSize)
87 {
88 if (LastPagePoolId + 1 == 0)
89 ++LastPagePoolId; // skip zero pool id
90 }
91
92 off_t
93 Ipc::Mem::PagePool::Shared::MemSize(const unsigned int capacity, const size_t pageSize)
94 {
95 return static_cast<off_t>(sizeof(Shared)) +
96 static_cast<off_t>(pageSize) * capacity;
97 }