]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/PagePool.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / ipc / mem / PagePool.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 54 Interprocess Communication
5 *
6 */
7
8 #include "squid.h"
9 #include "base/TextException.h"
10 #include "ipc/mem/Page.h"
11 #include "ipc/mem/PagePool.h"
12
13
14 // Ipc::Mem::PagePool
15
16 Ipc::Mem::PagePool::Owner *
17 Ipc::Mem::PagePool::Init(const char *const id, const unsigned int capacity, const size_t pageSize)
18 {
19 static uint32_t LastPagePoolId = 0;
20 if (++LastPagePoolId == 0)
21 ++LastPagePoolId; // skip zero pool id
22 return shm_new(PageStack)(id, LastPagePoolId, capacity, pageSize);
23 }
24
25 Ipc::Mem::PagePool::PagePool(const char *const id):
26 pageIndex(shm_old(PageStack)(id)),
27 theLevels(reinterpret_cast<Atomic::Word *>(
28 reinterpret_cast<char *>(pageIndex.getRaw()) +
29 pageIndex->stackSize())),
30 theBuf(reinterpret_cast<char *>(theLevels + PageId::maxPurpose))
31 {
32 }
33
34 size_t
35 Ipc::Mem::PagePool::level(const int purpose) const
36 {
37 Must(0 <= purpose && purpose < PageId::maxPurpose);
38 return theLevels[purpose];
39 }
40
41 bool
42 Ipc::Mem::PagePool::get(const PageId::Purpose purpose, PageId &page)
43 {
44 Must(0 <= purpose && purpose < PageId::maxPurpose);
45 if (pageIndex->pop(page)) {
46 page.purpose = purpose;
47 ++theLevels[purpose];
48 return true;
49 }
50 return false;
51 }
52
53 void
54 Ipc::Mem::PagePool::put(PageId &page)
55 {
56 if (!page)
57 return;
58
59 Must(0 <= page.purpose && page.purpose < PageId::maxPurpose);
60 --theLevels[page.purpose];
61 page.purpose = PageId::maxPurpose;
62 return pageIndex->push(page);
63 }
64
65 char *
66 Ipc::Mem::PagePool::pagePointer(const PageId &page)
67 {
68 Must(pageIndex->pageIdIsValid(page));
69 return theBuf + pageSize() * (page.number - 1);
70 }