]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/stmem.cc
b0c7ae5584d9e80daaf88a77e6c9d08c9b96ca44
2 * Copyright (C) 1996-2015 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 */
14 #include "MemObject.h"
15 #include "profiler/Profiler.h"
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
26 mem_hdr :: NodeGet ( mem_node
* aNode
)
28 assert (! aNode
-> write_pending
);
29 aNode
-> write_pending
= true ;
34 mem_hdr :: lowestOffset () const
36 const SplayNode
< mem_node
*> * theStart
= nodes
. start ();
39 return theStart
-> data
-> nodeBuffer
. offset
;
45 mem_hdr :: endOffset () const
48 const SplayNode
< mem_node
*> * theEnd
= nodes
. finish ();
51 result
= theEnd
-> data
-> dataRange (). end
;
53 assert ( result
== inmem_hi
);
59 mem_hdr :: freeContent ()
63 debugs ( 19 , 9 , HERE
<< this << " hi: " << inmem_hi
);
67 mem_hdr :: unlink ( mem_node
* aNode
)
69 if ( aNode
-> write_pending
) {
70 debugs ( 0 , DBG_CRITICAL
, "cannot unlink mem_node " << aNode
<< " while write_pending" );
74 debugs ( 19 , 8 , this << " removing " << aNode
);
75 nodes
. remove ( aNode
, NodeCompare
);
81 mem_hdr :: freeDataUpto ( int64_t target_offset
)
83 debugs ( 19 , 8 , this << " up to " << target_offset
);
84 /* keep the last one to avoid change to other part of code */
85 SplayNode
< mem_node
*> const * theStart
;
87 while (( theStart
= nodes
. start ())) {
88 if ( theStart
== nodes
. finish ())
91 if ( theStart
-> data
-> end () > target_offset
)
94 if (! unlink ( theStart
-> data
))
98 assert ( lowestOffset () <= target_offset
);
100 return lowestOffset ();
104 mem_hdr :: appendToNode ( mem_node
* aNode
, const char * data
, int maxLength
)
106 size_t result
= writeAvailable ( aNode
, aNode
-> nodeBuffer
. offset
+ aNode
-> nodeBuffer
. length
, maxLength
, data
);
111 mem_hdr :: writeAvailable ( mem_node
* aNode
, int64_t location
, size_t amount
, char const * source
)
113 /* if we attempt to overwrite existing data or leave a gap within a node */
114 assert ( location
== aNode
-> nodeBuffer
. offset
+ ( int64_t ) aNode
-> nodeBuffer
. length
);
115 /* And we are not at the end of the node */
116 assert ( aNode
-> canAccept ( location
));
118 /* these two can go I think */
119 assert ( location
- aNode
-> nodeBuffer
. offset
== ( int64_t ) aNode
-> nodeBuffer
. length
);
120 size_t copyLen
= min ( amount
, aNode
-> space ());
122 memcpy ( aNode
-> nodeBuffer
. data
+ aNode
-> nodeBuffer
. length
, source
, copyLen
);
124 debugs ( 19 , 9 , HERE
<< this << " hi: " << inmem_hi
);
125 if ( inmem_hi
<= location
)
126 inmem_hi
= location
+ copyLen
;
128 /* Adjust the ptr and len according to what was deposited in the page */
129 aNode
-> nodeBuffer
. length
+= copyLen
;
131 debugs ( 19 , 9 , HERE
<< this << " hi: " << inmem_hi
);
132 debugs ( 19 , 9 , HERE
<< this << " hi: " << endOffset ());
137 mem_hdr :: appendNode ( mem_node
* aNode
)
139 nodes
. insert ( aNode
, NodeCompare
);
143 mem_hdr :: makeAppendSpace ()
146 appendNode ( new mem_node ( 0 ));
150 if (! nodes
. finish ()-> data
-> space ())
151 appendNode ( new mem_node ( endOffset ()));
153 assert ( nodes
. finish ()-> data
-> space ());
157 mem_hdr :: internalAppend ( const char * data
, int len
)
159 debugs ( 19 , 6 , "memInternalAppend: " << this << " len " << len
);
163 int copied
= appendToNode ( nodes
. finish ()-> data
, data
, len
);
171 /* returns a mem_node that contains location..
172 * If no node contains the start, it returns NULL.
175 mem_hdr :: getBlockContainingLocation ( int64_t location
) const
177 // Optimize: do not create a whole mem_node just to store location
178 mem_node
target ( location
);
179 target
. nodeBuffer
. length
= 1 ;
180 mem_node
* const * result
= nodes
. find (& target
, NodeCompare
);
189 mem_hdr :: copyAvailable ( mem_node
* aNode
, int64_t location
, size_t amount
, char * target
) const
191 if ( aNode
-> nodeBuffer
. offset
> location
)
194 assert ( aNode
-> nodeBuffer
. offset
<= location
);
196 assert ( aNode
-> end () > location
);
198 size_t copyOffset
= location
- aNode
-> nodeBuffer
. offset
;
200 size_t copyLen
= min ( amount
, aNode
-> nodeBuffer
. length
- copyOffset
);
202 memcpy ( target
, aNode
-> nodeBuffer
. data
+ copyOffset
, copyLen
);
208 mem_hdr :: debugDump () const
210 debugs ( 19 , 0 , "mem_hdr::debugDump: lowest offset: " << lowestOffset () << " highest offset + 1: " << endOffset () << "." );
211 std :: ostringstream result
;
212 PointerPrinter
< mem_node
*> foo ( result
, " - " );
213 getNodes (). visit ( foo
);
214 debugs ( 19 , 0 , "mem_hdr::debugDump: Current available data is: " << result
. str () << "." );
217 /* FIXME: how do we deal with sparse results -
218 * where we have (say)
219 * 0-500 and 1000-1500, but are asked for
222 * we supply 0-500 and stop.
225 mem_hdr :: copy ( StoreIOBuffer
const & target
) const
228 assert ( target
. range (). end
> target
. range (). start
);
229 debugs ( 19 , 6 , "memCopy: " << this << " " << target
. range ());
231 /* we shouldn't ever ask for absent offsets */
233 if ( nodes
. size () == 0 ) {
234 debugs ( 19 , DBG_IMPORTANT
, "mem_hdr::copy: No data to read" );
240 /* RC: the next assert is nearly useless */
241 assert ( target
. length
> 0 );
243 /* Seek our way into store */
244 mem_node
* p
= getBlockContainingLocation ( target
. offset
);
247 debugs ( 19 , DBG_IMPORTANT
, "memCopy: could not find start of " << target
. range () <<
250 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 " );
254 size_t bytes_to_go
= target
. length
;
255 char * ptr_to_buf
= target
. data
;
256 int64_t location
= target
. offset
;
258 /* Start copying begining with this block until
261 while ( p
&& bytes_to_go
> 0 ) {
262 size_t bytes_to_copy
= copyAvailable ( p
,
263 location
, bytes_to_go
, ptr_to_buf
);
265 /* hit a sparse patch */
267 if ( bytes_to_copy
== 0 )
268 return target
. length
- bytes_to_go
;
270 location
+= bytes_to_copy
;
272 ptr_to_buf
+= bytes_to_copy
;
274 bytes_to_go
-= bytes_to_copy
;
276 p
= getBlockContainingLocation ( location
);
279 return target
. length
- bytes_to_go
;
283 mem_hdr :: hasContigousContentRange ( Range
< int64_t > const & range
) const
285 int64_t currentStart
= range
. start
;
287 while ( mem_node
* curr
= getBlockContainingLocation ( currentStart
)) {
288 currentStart
= curr
-> end ();
290 if ( currentStart
>= range
. end
)
294 return ! range
. size (); // empty range is contigous
298 mem_hdr :: unionNotEmpty ( StoreIOBuffer
const & candidate
)
300 assert ( candidate
. offset
>= 0 );
301 mem_node
target ( candidate
. offset
);
302 target
. nodeBuffer
. length
= candidate
. length
;
303 return nodes
. find (& target
, NodeCompare
);
307 mem_hdr :: nodeToRecieve ( int64_t offset
)
309 /* case 1: Nothing in memory */
312 appendNode ( new mem_node ( offset
));
313 return nodes
. start ()-> data
;
316 mem_node
* candidate
= NULL
;
317 /* case 2: location fits within an extant node */
320 mem_node
search ( offset
- 1 );
321 search
. nodeBuffer
. length
= 1 ;
322 mem_node
* const * leadup
= nodes
. find (& search
, NodeCompare
);
328 if ( candidate
&& candidate
-> canAccept ( offset
))
331 /* candidate can't accept, so we need a new node */
332 candidate
= new mem_node ( offset
);
334 appendNode ( candidate
);
336 /* simpler to write than a indented if */
341 mem_hdr :: write ( StoreIOBuffer
const & writeBuffer
)
343 PROF_start ( mem_hdr_write
);
344 debugs ( 19 , 6 , "mem_hdr::write: " << this << " " << writeBuffer
. range () << " object end " << endOffset ());
346 if ( unionNotEmpty ( writeBuffer
)) {
347 debugs ( 19 , DBG_CRITICAL
, "mem_hdr::write: writeBuffer: " << writeBuffer
. range ());
349 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 " );
350 PROF_stop ( mem_hdr_write
);
354 assert ( writeBuffer
. offset
>= 0 );
357 int64_t currentOffset
= writeBuffer
. offset
;
358 char * currentSource
= writeBuffer
. data
;
359 size_t len
= writeBuffer
. length
;
361 while ( len
&& ( target
= nodeToRecieve ( currentOffset
))) {
362 size_t wrote
= writeAvailable ( target
, currentOffset
, len
, currentSource
);
365 currentOffset
+= wrote
;
366 currentSource
+= wrote
;
369 PROF_stop ( mem_hdr_write
);
373 mem_hdr :: mem_hdr () : inmem_hi ( 0 )
375 debugs ( 19 , 9 , HERE
<< this << " hi: " << inmem_hi
);
383 /* splay of mem nodes:
385 * a = b if a.intersection(b).size > 0;
389 mem_hdr :: NodeCompare ( mem_node
* const & left
, mem_node
* const & right
)
391 // possibly Range can help us at some point.
393 if ( left
-> dataRange (). intersection ( right
-> dataRange ()). size () > 0 )
396 return * left
< * right
? - 1 : 1 ;
400 mem_hdr :: dump () const
402 debugs ( 20 , DBG_IMPORTANT
, "mem_hdr: " << ( void *) this << " nodes.start() " << nodes
. start ());
403 debugs ( 20 , DBG_IMPORTANT
, "mem_hdr: " << ( void *) this << " nodes.finish() " << nodes
. finish ());
407 mem_hdr :: size () const
413 mem_hdr :: start () const
415 const SplayNode
< mem_node
*> * result
= nodes
. start ();
423 const Splay
< mem_node
*> &
424 mem_hdr :: getNodes () const