]> git.ipfire.org Git - thirdparty/squid.git/blame - src/stmem.cc
Simplify appending SBuf to String (#2108)
[thirdparty/squid.git] / src / stmem.cc
CommitLineData
30a4f2a8 1/*
1f7b830e 2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
26ac0430 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.
019dd986 7 */
ed43818f 8
bbc27441
AJ
9/* DEBUG: section 19 Store Memory Primitives */
10
582c2af2
FC
11#include "squid.h"
12#include "Generic.h"
a0c227a9 13#include "HttpReply.h"
528b2c61 14#include "mem_node.h"
15#include "MemObject.h"
582c2af2 16#include "stmem.h"
528b2c61 17
12d74f96 18/*
19 * NodeGet() is called to get the data buffer to pass to storeIOWrite().
20 * By setting the write_pending flag here we are assuming that there
21 * will be no other users of NodeGet(). The storeIOWrite() callback
22 * is memNodeWriteComplete(), which, for whatever reason, lives in
23 * mem_node.cc.
24 */
d06925a4 25char *
26mem_hdr::NodeGet(mem_node * aNode)
27{
12d74f96 28 assert(!aNode->write_pending);
3dd52a0b 29 aNode->write_pending = true;
d06925a4 30 return aNode->data;
31}
32
47f6e231 33int64_t
528b2c61 34mem_hdr::lowestOffset () const
35{
42a503bd 36 const SplayNode<mem_node *> *theStart = nodes.start();
37
38 if (theStart)
39 return theStart->data->nodeBuffer.offset;
62e76326 40
528b2c61 41 return 0;
42}
43
47f6e231 44int64_t
528b2c61 45mem_hdr::endOffset () const
46{
47f6e231 47 int64_t result = 0;
b8bad68c 48 const SplayNode<mem_node *> *theEnd = nodes.finish();
62e76326 49
42a503bd 50 if (theEnd)
51 result = theEnd->data->dataRange().end;
62e76326 52
528b2c61 53 assert (result == inmem_hi);
62e76326 54
528b2c61 55 return result;
56}
090089c4 57
d89d1fb6 58void
528b2c61 59mem_hdr::freeContent()
60{
552ec743 61 nodes.destroy();
528b2c61 62 inmem_hi = 0;
bf95c10a 63 debugs(19, 9, this << " hi: " << inmem_hi);
528b2c61 64}
65
12d74f96 66bool
42a503bd 67mem_hdr::unlink(mem_node *aNode)
528b2c61 68{
12d74f96 69 if (aNode->write_pending) {
d816f28d 70 debugs(0, DBG_CRITICAL, "ERROR: cannot unlink mem_node " << aNode << " while write_pending");
12d74f96 71 return false;
72 }
d06925a4 73
97754f5a 74 debugs(19, 8, this << " removing " << aNode);
12d74f96 75 nodes.remove (aNode, NodeCompare);
76 delete aNode;
77 return true;
090089c4 78}
79
47f6e231 80int64_t
81mem_hdr::freeDataUpto(int64_t target_offset)
528b2c61 82{
97754f5a 83 debugs(19, 8, this << " up to " << target_offset);
528b2c61 84 /* keep the last one to avoid change to other part of code */
810635e3 85 SplayNode<mem_node*> const * theStart;
42a503bd 86
12d74f96 87 while ((theStart = nodes.start())) {
88 if (theStart == nodes.finish())
89 break;
90
47f6e231 91 if (theStart->data->end() > target_offset )
12d74f96 92 break;
93
94 if (!unlink(theStart->data))
95 break;
42a503bd 96 }
62e76326 97
528b2c61 98 return lowestOffset ();
99}
100
528b2c61 101size_t
47f6e231 102mem_hdr::writeAvailable(mem_node *aNode, int64_t location, size_t amount, char const *source)
528b2c61 103{
104 /* if we attempt to overwrite existing data or leave a gap within a node */
ed013b6c 105 assert (location == aNode->nodeBuffer.offset + (int64_t)aNode->nodeBuffer.length);
528b2c61 106 /* And we are not at the end of the node */
107 assert (aNode->canAccept (location));
62e76326 108
528b2c61 109 /* these two can go I think */
ed013b6c 110 assert (location - aNode->nodeBuffer.offset == (int64_t)aNode->nodeBuffer.length);
d85c3078 111 size_t copyLen = min(amount, aNode->space());
528b2c61 112
41d00cd3 113 memcpy(aNode->nodeBuffer.data + aNode->nodeBuffer.length, source, copyLen);
528b2c61 114
bf95c10a 115 debugs(19, 9, this << " hi: " << inmem_hi);
47f6e231 116 if (inmem_hi <= location)
62e76326 117 inmem_hi = location + copyLen;
118
528b2c61 119 /* Adjust the ptr and len according to what was deposited in the page */
120 aNode->nodeBuffer.length += copyLen;
62e76326 121
bf95c10a 122 debugs(19, 9, this << " hi: " << inmem_hi);
123 debugs(19, 9, this << " hi: " << endOffset());
528b2c61 124 return copyLen;
125}
126
127void
128mem_hdr::appendNode (mem_node *aNode)
129{
42a503bd 130 nodes.insert (aNode, NodeCompare);
090089c4 131}
132
528b2c61 133/* returns a mem_node that contains location..
134 * If no node contains the start, it returns NULL.
135 */
136mem_node *
47f6e231 137mem_hdr::getBlockContainingLocation (int64_t location) const
528b2c61 138{
a5c1e941 139 // Optimize: do not create a whole mem_node just to store location
42a503bd 140 mem_node target (location);
141 target.nodeBuffer.length = 1;
142 mem_node *const *result = nodes.find (&target, NodeCompare);
62e76326 143
42a503bd 144 if (result)
145 return *result;
62e76326 146
aee3523a 147 return nullptr;
528b2c61 148}
149
150size_t
47f6e231 151mem_hdr::copyAvailable(mem_node *aNode, int64_t location, size_t amount, char *target) const
528b2c61 152{
47f6e231 153 if (aNode->nodeBuffer.offset > location)
62e76326 154 return 0;
155
47f6e231 156 assert (aNode->nodeBuffer.offset <= location);
62e76326 157
528b2c61 158 assert (aNode->end() > location);
62e76326 159
528b2c61 160 size_t copyOffset = location - aNode->nodeBuffer.offset;
62e76326 161
d85c3078 162 size_t copyLen = min(amount, aNode->nodeBuffer.length - copyOffset);
528b2c61 163
41d00cd3 164 memcpy(target, aNode->nodeBuffer.data + copyOffset, copyLen);
62e76326 165
528b2c61 166 return copyLen;
167}
168
b8bad68c 169void
170mem_hdr::debugDump() const
171{
db7778f1 172 debugs (19, 0, "mem_hdr::debugDump: lowest offset: " << lowestOffset() << " highest offset + 1: " << endOffset() << ".");
b8bad68c 173 std::ostringstream result;
174 PointerPrinter<mem_node *> foo(result, " - ");
97754f5a 175 getNodes().visit(foo);
db7778f1 176 debugs (19, 0, "mem_hdr::debugDump: Current available data is: " << result.str() << ".");
b8bad68c 177}
178
9837567d 179/* XXX: how do we deal with sparse results -
528b2c61 180 * where we have (say)
26ac0430 181 * 0-500 and 1000-1500, but are asked for
528b2c61 182 * 0-2000
183 * Partial answer:
184 * we supply 0-500 and stop.
185 */
02be0294 186ssize_t
90703668 187mem_hdr::copy(StoreIOBuffer const &target) const
090089c4 188{
528b2c61 189
47f6e231 190 assert(target.range().end > target.range().start);
e2851fe7 191 debugs(19, 6, "memCopy: " << this << " " << target.range());
62e76326 192
b8bad68c 193 /* we shouldn't ever ask for absent offsets */
194
42a503bd 195 if (nodes.size() == 0) {
e0236918 196 debugs(19, DBG_IMPORTANT, "mem_hdr::copy: No data to read");
b8bad68c 197 debugDump();
42a503bd 198 assert (0);
62e76326 199 return 0;
42a503bd 200 }
62e76326 201
528b2c61 202 /* RC: the next assert is nearly useless */
90703668 203 assert(target.length > 0);
528b2c61 204
090089c4 205 /* Seek our way into store */
47f6e231 206 mem_node *p = getBlockContainingLocation(target.offset);
62e76326 207
528b2c61 208 if (!p) {
d816f28d 209 debugs(19, DBG_IMPORTANT, "ERROR: memCopy: could not find start of " << target.range() <<
e4049756 210 " in memory.");
b8bad68c 211 debugDump();
24fe24be 212 fatal_dump("Squid has attempted to read data from memory that is not present. This is an indication of of (pre-3.0) code that hasn't been updated to deal with sparse objects in memory. Squid should coredump.allowing to review the cause. Immediately preceding this message is a dump of the available data in the format [start,end). The [ means from the value, the ) means up to the value. I.e. [1,5) means that there are 4 bytes of data, at offsets 1,2,3,4.\n");
62e76326 213 return 0;
090089c4 214 }
62e76326 215
90703668 216 size_t bytes_to_go = target.length;
217 char *ptr_to_buf = target.data;
47f6e231 218 int64_t location = target.offset;
62e76326 219
2f8abb64 220 /* Start copying beginning with this block until
090089c4 221 * we're satiated */
528b2c61 222
090089c4 223 while (p && bytes_to_go > 0) {
62e76326 224 size_t bytes_to_copy = copyAvailable (p,
225 location, bytes_to_go, ptr_to_buf);
226
227 /* hit a sparse patch */
228
229 if (bytes_to_copy == 0)
90703668 230 return target.length - bytes_to_go;
62e76326 231
232 location += bytes_to_copy;
233
234 ptr_to_buf += bytes_to_copy;
235
236 bytes_to_go -= bytes_to_copy;
237
42a503bd 238 p = getBlockContainingLocation(location);
090089c4 239 }
62e76326 240
90703668 241 return target.length - bytes_to_go;
090089c4 242}
528b2c61 243
244bool
47f6e231 245mem_hdr::hasContigousContentRange(Range<int64_t> const & range) const
528b2c61 246{
47f6e231 247 int64_t currentStart = range.start;
62e76326 248
528b2c61 249 while (mem_node *curr = getBlockContainingLocation(currentStart)) {
62e76326 250 currentStart = curr->end();
251
4c50505b 252 if (currentStart >= range.end)
62e76326 253 return true;
528b2c61 254 }
62e76326 255
2f8abb64 256 return !range.size(); // empty range is contiguous
528b2c61 257}
258
259bool
260mem_hdr::unionNotEmpty(StoreIOBuffer const &candidate)
261{
528b2c61 262 assert (candidate.offset >= 0);
42a503bd 263 mem_node target(candidate.offset);
264 target.nodeBuffer.length = candidate.length;
265 return nodes.find (&target, NodeCompare);
528b2c61 266}
267
268mem_node *
47f6e231 269mem_hdr::nodeToRecieve(int64_t offset)
528b2c61 270{
271 /* case 1: Nothing in memory */
62e76326 272
42a503bd 273 if (!nodes.size()) {
62e76326 274 appendNode (new mem_node(offset));
42a503bd 275 return nodes.start()->data;
528b2c61 276 }
277
aee3523a 278 mem_node *candidate = nullptr;
528b2c61 279 /* case 2: location fits within an extant node */
62e76326 280
42a503bd 281 if (offset > 0) {
282 mem_node search (offset - 1);
283 search.nodeBuffer.length = 1;
284 mem_node *const *leadup = nodes.find (&search, NodeCompare);
285
286 if (leadup)
287 candidate = *leadup;
528b2c61 288 }
289
42a503bd 290 if (candidate && candidate->canAccept(offset))
62e76326 291 return candidate;
528b2c61 292
293 /* candidate can't accept, so we need a new node */
294 candidate = new mem_node(offset);
62e76326 295
528b2c61 296 appendNode (candidate);
62e76326 297
528b2c61 298 /* simpler to write than a indented if */
299 return candidate;
300}
301
528b2c61 302bool
303mem_hdr::write (StoreIOBuffer const &writeBuffer)
304{
e2851fe7 305 debugs(19, 6, "mem_hdr::write: " << this << " " << writeBuffer.range() << " object end " << endOffset());
528b2c61 306
307 if (unionNotEmpty(writeBuffer)) {
fa84c01d 308 debugs(19, DBG_CRITICAL, "mem_hdr::write: writeBuffer: " << writeBuffer.range());
db7778f1 309 debugDump();
fbdf945d 310 fatal_dump("Attempt to overwrite already in-memory data. Preceding this there should be a mem_hdr::write output that lists the attempted write, and the currently present data. Please get a 'backtrace full' from this error - using the generated core, and file a bug report with the squid developers including the last 10 lines of cache.log and the backtrace.\n");
62e76326 311 return false;
528b2c61 312 }
313
314 assert (writeBuffer.offset >= 0);
315
316 mem_node *target;
47f6e231 317 int64_t currentOffset = writeBuffer.offset;
528b2c61 318 char *currentSource = writeBuffer.data;
319 size_t len = writeBuffer.length;
62e76326 320
528b2c61 321 while (len && (target = nodeToRecieve(currentOffset))) {
62e76326 322 size_t wrote = writeAvailable(target, currentOffset, len, currentSource);
323 assert (wrote);
324 len -= wrote;
325 currentOffset += wrote;
326 currentSource += wrote;
528b2c61 327 }
328
329 return true;
330}
4c50505b 331
42a503bd 332mem_hdr::mem_hdr() : inmem_hi(0)
e2851fe7 333{
bf95c10a 334 debugs(19, 9, this << " hi: " << inmem_hi);
e2851fe7 335}
4c50505b 336
337mem_hdr::~mem_hdr()
338{
339 freeContent();
340}
42a503bd 341
342/* splay of mem nodes:
343 * conditions:
344 * a = b if a.intersection(b).size > 0;
345 * a < b if a < b
346 */
347int
348mem_hdr::NodeCompare(mem_node * const &left, mem_node * const &right)
349{
350 // possibly Range can help us at some point.
351
352 if (left->dataRange().intersection(right->dataRange()).size() > 0)
353 return 0;
354
355 return *left < *right ? -1 : 1;
356}
357
358void
359mem_hdr::dump() const
360{
e0236918
FC
361 debugs(20, DBG_IMPORTANT, "mem_hdr: " << (void *)this << " nodes.start() " << nodes.start());
362 debugs(20, DBG_IMPORTANT, "mem_hdr: " << (void *)this << " nodes.finish() " << nodes.finish());
42a503bd 363}
364
365size_t
366mem_hdr::size() const
367{
368 return nodes.size();
369}
370
b8bad68c 371const Splay<mem_node *> &
372mem_hdr::getNodes() const
373{
374 return nodes;
375}
f53969cc 376