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