]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ipc/mem/Pages.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / ipc / mem / Pages.cc
CommitLineData
3e0ddf16 1/*
5b74111a 2 * Copyright (C) 1996-2018 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"
a4555399 12#include "base/RunnersRegistry.h"
602d9612 13#include "base/TextException.h"
56f8aa50 14#include "ipc/mem/PagePool.h"
3e0ddf16 15#include "ipc/mem/Pages.h"
5bed43d6 16#include "tools.h"
3e0ddf16 17
56f8aa50 18// Uses a single PagePool instance, for now.
3e0ddf16 19// Eventually, we may have pools dedicated to memory caching, disk I/O, etc.
56f8aa50
DK
20
21// TODO: make pool id more unique so it does not conflict with other Squids?
a4555399 22static const char *PagePoolId = "squid-page-pool";
56f8aa50 23static Ipc::Mem::PagePool *ThePagePool = 0;
ea2cdeb6 24static int TheLimits[Ipc::Mem::PageId::maxPurpose];
56f8aa50 25
72261456 26// TODO: make configurable to avoid waste when mem-cached objects are small/big
d1ad4017 27size_t
9199139f
AR
28Ipc::Mem::PageSize()
29{
72261456 30 return 32*1024;
56f8aa50
DK
31}
32
56f8aa50 33bool
551f8a18 34Ipc::Mem::GetPage(const PageId::Purpose purpose, PageId &page)
56f8aa50 35{
551f8a18 36 return ThePagePool && PagesAvailable(purpose) > 0 ?
9199139f 37 ThePagePool->get(purpose, page) : false;
56f8aa50
DK
38}
39
40void
41Ipc::Mem::PutPage(PageId &page)
42{
43 Must(ThePagePool);
44 ThePagePool->put(page);
45}
46
8ed94021 47char *
56f8aa50
DK
48Ipc::Mem::PagePointer(const PageId &page)
49{
50 Must(ThePagePool);
51 return ThePagePool->pagePointer(page);
52}
72261456
AR
53
54size_t
e0bdae60
DK
55Ipc::Mem::PageLimit()
56{
551f8a18
DK
57 size_t limit = 0;
58 for (int i = 0; i < PageId::maxPurpose; ++i)
59 limit += PageLimit(i);
60 return limit;
e0bdae60
DK
61}
62
63size_t
551f8a18 64Ipc::Mem::PageLimit(const int purpose)
72261456 65{
ea2cdeb6
DK
66 Must(0 <= purpose && purpose <= PageId::maxPurpose);
67 return TheLimits[purpose];
68}
69
70// note: adjust this if we start recording needs during reconfigure
71void
72Ipc::Mem::NotePageNeed(const int purpose, const int count)
73{
74 Must(0 <= purpose && purpose <= PageId::maxPurpose);
75 Must(count >= 0);
76 TheLimits[purpose] += count;
e0bdae60
DK
77}
78
79size_t
80Ipc::Mem::PageLevel()
81{
551f8a18 82 return ThePagePool ? ThePagePool->level() : 0;
72261456
AR
83}
84
b940ff87 85size_t
551f8a18 86Ipc::Mem::PageLevel(const int purpose)
b940ff87 87{
551f8a18 88 return ThePagePool ? ThePagePool->level(purpose) : 0;
b940ff87 89}
a4555399
AR
90
91/// initializes shared memory pages
4404f1c5 92class SharedMemPagesRr: public Ipc::Mem::RegisteredRunner
a4555399
AR
93{
94public:
95 /* RegisteredRunner API */
68353d5a 96 SharedMemPagesRr(): owner(NULL) {}
21b7990f
AR
97 virtual void useConfig();
98 virtual void create();
99 virtual void open();
c011f9bc 100 virtual ~SharedMemPagesRr();
68353d5a
DK
101
102private:
103 Ipc::Mem::PagePool::Owner *owner;
a4555399
AR
104};
105
21b7990f 106RunnerRegistrationEntry(SharedMemPagesRr);
a4555399 107
4404f1c5 108void
21b7990f 109SharedMemPagesRr::useConfig()
a4555399 110{
ea2cdeb6 111 if (Ipc::Mem::PageLimit() <= 0)
3f2ac0f7 112 return;
3f2ac0f7 113
21b7990f 114 Ipc::Mem::RegisteredRunner::useConfig();
4404f1c5 115}
68353d5a 116
4404f1c5 117void
21b7990f 118SharedMemPagesRr::create()
4404f1c5
DK
119{
120 Must(!owner);
121 owner = Ipc::Mem::PagePool::Init(PagePoolId, Ipc::Mem::PageLimit(),
122 Ipc::Mem::PageSize());
123}
124
125void
21b7990f 126SharedMemPagesRr::open()
4404f1c5 127{
68353d5a
DK
128 Must(!ThePagePool);
129 ThePagePool = new Ipc::Mem::PagePool(PagePoolId);
a4555399 130}
c011f9bc
DK
131
132SharedMemPagesRr::~SharedMemPagesRr()
133{
134 delete ThePagePool;
282ee641 135 ThePagePool = NULL;
68353d5a 136 delete owner;
c011f9bc 137}
f53969cc 138