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