]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ipc/mem/PagePool.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / ipc / mem / PagePool.cc
CommitLineData
3e0ddf16 1/*
bbc27441 2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3e0ddf16 3 *
bbc27441
AJ
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.
3e0ddf16
AR
7 */
8
bbc27441
AJ
9/* DEBUG: section 54 Interprocess Communication */
10
f7f3304a 11#include "squid.h"
56f8aa50
DK
12#include "base/TextException.h"
13#include "ipc/mem/Page.h"
3e0ddf16
AR
14#include "ipc/mem/PagePool.h"
15
56f8aa50
DK
16// Ipc::Mem::PagePool
17
68353d5a
DK
18Ipc::Mem::PagePool::Owner *
19Ipc::Mem::PagePool::Init(const char *const id, const unsigned int capacity, const size_t pageSize)
56f8aa50 20{
68353d5a
DK
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);
56f8aa50
DK
25}
26
68353d5a 27Ipc::Mem::PagePool::PagePool(const char *const id):
f53969cc
SM
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))
56f8aa50 33{
551f8a18
DK
34}
35
36size_t
37Ipc::Mem::PagePool::level(const int purpose) const
38{
39 Must(0 <= purpose && purpose < PageId::maxPurpose);
40 return theLevels[purpose];
41}
42
43bool
44Ipc::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
55void
56Ipc::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);
56f8aa50
DK
65}
66
8ed94021 67char *
56f8aa50
DK
68Ipc::Mem::PagePool::pagePointer(const PageId &page)
69{
68353d5a
DK
70 Must(pageIndex->pageIdIsValid(page));
71 return theBuf + pageSize() * (page.number - 1);
39c0d994 72}
f53969cc 73