]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/PagePool.cc
3dd4dbd84d40255a4f72d808e34970ccc39f9580
[thirdparty/squid.git] / src / ipc / mem / PagePool.cc
1 /*
2 * Copyright (C) 1996-2025 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 shmId, const Ipc::Mem::PoolId stackId, const unsigned int capacity, const size_t pageSize)
20 {
21 PageStack::Config config;
22 config.poolId = stackId;
23 config.pageSize = pageSize; // the pages are stored in Ipc::Mem::Pages
24 config.capacity = capacity;
25 config.createFull = true; // all pages are initially available
26 return shm_new(PageStack)(shmId, config);
27 }
28
29 Ipc::Mem::PagePool::PagePool(const char *const id):
30 pageIndex(shm_old(PageStack)(id)),
31 theLevels(reinterpret_cast<Levels_t *>(
32 reinterpret_cast<char *>(pageIndex.getRaw()) +
33 pageIndex->stackSize() + pageIndex->levelsPaddingSize())),
34 theBuf(reinterpret_cast<char *>(theLevels + PageId::maxPurpose))
35 {
36 }
37
38 size_t
39 Ipc::Mem::PagePool::level(const int purpose) const
40 {
41 Must(0 <= purpose && purpose < PageId::maxPurpose);
42 return theLevels[purpose];
43 }
44
45 bool
46 Ipc::Mem::PagePool::get(const PageId::Purpose purpose, PageId &page)
47 {
48 Must(0 <= purpose && purpose < PageId::maxPurpose);
49 if (pageIndex->pop(page)) {
50 page.purpose = purpose;
51 ++theLevels[purpose];
52 return true;
53 }
54 return false;
55 }
56
57 void
58 Ipc::Mem::PagePool::put(PageId &page)
59 {
60 if (!page)
61 return;
62
63 Must(0 <= page.purpose && page.purpose < PageId::maxPurpose);
64 --theLevels[page.purpose];
65 page.purpose = PageId::maxPurpose;
66 return pageIndex->push(page);
67 }
68
69 char *
70 Ipc::Mem::PagePool::pagePointer(const PageId &page)
71 {
72 Must(pageIndex->pageIdIsValid(page));
73 return theBuf + pageSize() * (page.number - 1);
74 }
75