]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/stmem.cc
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 19 Store Memory Primitives */
13 #include "HttpReply.h"
15 #include "MemObject.h"
16 #include "profiler/Profiler.h"
20 * NodeGet() is called to get the data buffer to pass to storeIOWrite().
21 * By setting the write_pending flag here we are assuming that there
22 * will be no other users of NodeGet(). The storeIOWrite() callback
23 * is memNodeWriteComplete(), which, for whatever reason, lives in
27 mem_hdr :: NodeGet ( mem_node
* aNode
)
29 assert (! aNode
-> write_pending
);
30 aNode
-> write_pending
= true ;
35 mem_hdr :: lowestOffset () const
37 const SplayNode
< mem_node
*> * theStart
= nodes
. start ();
40 return theStart
-> data
-> nodeBuffer
. offset
;
46 mem_hdr :: endOffset () const
49 const SplayNode
< mem_node
*> * theEnd
= nodes
. finish ();
52 result
= theEnd
-> data
-> dataRange (). end
;
54 assert ( result
== inmem_hi
);
60 mem_hdr :: freeContent ()
64 debugs ( 19 , 9 , HERE
<< this << " hi: " << inmem_hi
);
68 mem_hdr :: unlink ( mem_node
* aNode
)
70 if ( aNode
-> write_pending
) {
71 debugs ( 0 , DBG_CRITICAL
, "cannot unlink mem_node " << aNode
<< " while write_pending" );
75 debugs ( 19 , 8 , this << " removing " << aNode
);
76 nodes
. remove ( aNode
, NodeCompare
);
82 mem_hdr :: freeDataUpto ( int64_t target_offset
)
84 debugs ( 19 , 8 , this << " up to " << target_offset
);
85 /* keep the last one to avoid change to other part of code */
86 SplayNode
< mem_node
*> const * theStart
;
88 while (( theStart
= nodes
. start ())) {
89 if ( theStart
== nodes
. finish ())
92 if ( theStart
-> data
-> end () > target_offset
)
95 if (! unlink ( theStart
-> data
))
99 assert ( lowestOffset () <= target_offset
);
101 return lowestOffset ();
105 mem_hdr :: appendToNode ( mem_node
* aNode
, const char * data
, int maxLength
)
107 size_t result
= writeAvailable ( aNode
, aNode
-> nodeBuffer
. offset
+ aNode
-> nodeBuffer
. length
, maxLength
, data
);
112 mem_hdr :: writeAvailable ( mem_node
* aNode
, int64_t location
, size_t amount
, char const * source
)
114 /* if we attempt to overwrite existing data or leave a gap within a node */
115 assert ( location
== aNode
-> nodeBuffer
. offset
+ ( int64_t ) aNode
-> nodeBuffer
. length
);
116 /* And we are not at the end of the node */
117 assert ( aNode
-> canAccept ( location
));
119 /* these two can go I think */
120 assert ( location
- aNode
-> nodeBuffer
. offset
== ( int64_t ) aNode
-> nodeBuffer
. length
);
121 size_t copyLen
= min ( amount
, aNode
-> space ());
123 memcpy ( aNode
-> nodeBuffer
. data
+ aNode
-> nodeBuffer
. length
, source
, copyLen
);
125 debugs ( 19 , 9 , HERE
<< this << " hi: " << inmem_hi
);
126 if ( inmem_hi
<= location
)
127 inmem_hi
= location
+ copyLen
;
129 /* Adjust the ptr and len according to what was deposited in the page */
130 aNode
-> nodeBuffer
. length
+= copyLen
;
132 debugs ( 19 , 9 , HERE
<< this << " hi: " << inmem_hi
);
133 debugs ( 19 , 9 , HERE
<< this << " hi: " << endOffset ());
138 mem_hdr :: appendNode ( mem_node
* aNode
)
140 nodes
. insert ( aNode
, NodeCompare
);
144 mem_hdr :: makeAppendSpace ()
147 appendNode ( new mem_node ( 0 ));
151 if (! nodes
. finish ()-> data
-> space ())
152 appendNode ( new mem_node ( endOffset ()));
154 assert ( nodes
. finish ()-> data
-> space ());
158 mem_hdr :: internalAppend ( const char * data
, int len
)
160 debugs ( 19 , 6 , "memInternalAppend: " << this << " len " << len
);
164 int copied
= appendToNode ( nodes
. finish ()-> data
, data
, len
);
172 /* returns a mem_node that contains location..
173 * If no node contains the start, it returns NULL.
176 mem_hdr :: getBlockContainingLocation ( int64_t location
) const
178 // Optimize: do not create a whole mem_node just to store location
179 mem_node
target ( location
);
180 target
. nodeBuffer
. length
= 1 ;
181 mem_node
* const * result
= nodes
. find (& target
, NodeCompare
);
190 mem_hdr :: copyAvailable ( mem_node
* aNode
, int64_t location
, size_t amount
, char * target
) const
192 if ( aNode
-> nodeBuffer
. offset
> location
)
195 assert ( aNode
-> nodeBuffer
. offset
<= location
);
197 assert ( aNode
-> end () > location
);
199 size_t copyOffset
= location
- aNode
-> nodeBuffer
. offset
;
201 size_t copyLen
= min ( amount
, aNode
-> nodeBuffer
. length
- copyOffset
);
203 memcpy ( target
, aNode
-> nodeBuffer
. data
+ copyOffset
, copyLen
);
209 mem_hdr :: debugDump () const
211 debugs ( 19 , 0 , "mem_hdr::debugDump: lowest offset: " << lowestOffset () << " highest offset + 1: " << endOffset () << "." );
212 std :: ostringstream result
;
213 PointerPrinter
< mem_node
*> foo ( result
, " - " );
214 getNodes (). visit ( foo
);
215 debugs ( 19 , 0 , "mem_hdr::debugDump: Current available data is: " << result
. str () << "." );
218 /* XXX: how do we deal with sparse results -
219 * where we have (say)
220 * 0-500 and 1000-1500, but are asked for
223 * we supply 0-500 and stop.
226 mem_hdr :: copy ( StoreIOBuffer
const & target
) const
229 assert ( target
. range (). end
> target
. range (). start
);
230 debugs ( 19 , 6 , "memCopy: " << this << " " << target
. range ());
232 /* we shouldn't ever ask for absent offsets */
234 if ( nodes
. size () == 0 ) {
235 debugs ( 19 , DBG_IMPORTANT
, "mem_hdr::copy: No data to read" );
241 /* RC: the next assert is nearly useless */
242 assert ( target
. length
> 0 );
244 /* Seek our way into store */
245 mem_node
* p
= getBlockContainingLocation ( target
. offset
);
248 debugs ( 19 , DBG_IMPORTANT
, "memCopy: could not find start of " << target
. range () <<
251 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 " );
255 size_t bytes_to_go
= target
. length
;
256 char * ptr_to_buf
= target
. data
;
257 int64_t location
= target
. offset
;
259 /* Start copying beginning with this block until
262 while ( p
&& bytes_to_go
> 0 ) {
263 size_t bytes_to_copy
= copyAvailable ( p
,
264 location
, bytes_to_go
, ptr_to_buf
);
266 /* hit a sparse patch */
268 if ( bytes_to_copy
== 0 )
269 return target
. length
- bytes_to_go
;
271 location
+= bytes_to_copy
;
273 ptr_to_buf
+= bytes_to_copy
;
275 bytes_to_go
-= bytes_to_copy
;
277 p
= getBlockContainingLocation ( location
);
280 return target
. length
- bytes_to_go
;
284 mem_hdr :: hasContigousContentRange ( Range
< int64_t > const & range
) const
286 int64_t currentStart
= range
. start
;
288 while ( mem_node
* curr
= getBlockContainingLocation ( currentStart
)) {
289 currentStart
= curr
-> end ();
291 if ( currentStart
>= range
. end
)
295 return ! range
. size (); // empty range is contiguous
299 mem_hdr :: unionNotEmpty ( StoreIOBuffer
const & candidate
)
301 assert ( candidate
. offset
>= 0 );
302 mem_node
target ( candidate
. offset
);
303 target
. nodeBuffer
. length
= candidate
. length
;
304 return nodes
. find (& target
, NodeCompare
);
308 mem_hdr :: nodeToRecieve ( int64_t offset
)
310 /* case 1: Nothing in memory */
313 appendNode ( new mem_node ( offset
));
314 return nodes
. start ()-> data
;
317 mem_node
* candidate
= NULL
;
318 /* case 2: location fits within an extant node */
321 mem_node
search ( offset
- 1 );
322 search
. nodeBuffer
. length
= 1 ;
323 mem_node
* const * leadup
= nodes
. find (& search
, NodeCompare
);
329 if ( candidate
&& candidate
-> canAccept ( offset
))
332 /* candidate can't accept, so we need a new node */
333 candidate
= new mem_node ( offset
);
335 appendNode ( candidate
);
337 /* simpler to write than a indented if */
342 mem_hdr :: write ( StoreIOBuffer
const & writeBuffer
)
344 PROF_start ( mem_hdr_write
);
345 debugs ( 19 , 6 , "mem_hdr::write: " << this << " " << writeBuffer
. range () << " object end " << endOffset ());
347 if ( unionNotEmpty ( writeBuffer
)) {
348 debugs ( 19 , DBG_CRITICAL
, "mem_hdr::write: writeBuffer: " << writeBuffer
. range ());
350 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 " );
351 PROF_stop ( mem_hdr_write
);
355 assert ( writeBuffer
. offset
>= 0 );
358 int64_t currentOffset
= writeBuffer
. offset
;
359 char * currentSource
= writeBuffer
. data
;
360 size_t len
= writeBuffer
. length
;
362 while ( len
&& ( target
= nodeToRecieve ( currentOffset
))) {
363 size_t wrote
= writeAvailable ( target
, currentOffset
, len
, currentSource
);
366 currentOffset
+= wrote
;
367 currentSource
+= wrote
;
370 PROF_stop ( mem_hdr_write
);
374 mem_hdr :: mem_hdr () : inmem_hi ( 0 )
376 debugs ( 19 , 9 , HERE
<< this << " hi: " << inmem_hi
);
384 /* splay of mem nodes:
386 * a = b if a.intersection(b).size > 0;
390 mem_hdr :: NodeCompare ( mem_node
* const & left
, mem_node
* const & right
)
392 // possibly Range can help us at some point.
394 if ( left
-> dataRange (). intersection ( right
-> dataRange ()). size () > 0 )
397 return * left
< * right
? - 1 : 1 ;
401 mem_hdr :: dump () const
403 debugs ( 20 , DBG_IMPORTANT
, "mem_hdr: " << ( void *) this << " nodes.start() " << nodes
. start ());
404 debugs ( 20 , DBG_IMPORTANT
, "mem_hdr: " << ( void *) this << " nodes.finish() " << nodes
. finish ());
408 mem_hdr :: size () const
414 mem_hdr :: start () const
416 const SplayNode
< mem_node
*> * result
= nodes
. start ();
424 const Splay
< mem_node
*> &
425 mem_hdr :: getNodes () const