]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/stmem.cc
5 * DEBUG: section 19 Store Memory Primitives
6 * AUTHOR: Harvest Derived
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
40 #include "MemObject.h"
44 * NodeGet() is called to get the data buffer to pass to storeIOWrite().
45 * By setting the write_pending flag here we are assuming that there
46 * will be no other users of NodeGet(). The storeIOWrite() callback
47 * is memNodeWriteComplete(), which, for whatever reason, lives in
51 mem_hdr :: NodeGet ( mem_node
* aNode
)
53 assert (! aNode
-> write_pending
);
54 aNode
-> write_pending
= 1 ;
59 mem_hdr :: lowestOffset () const
61 const SplayNode
< mem_node
*> * theStart
= nodes
. start ();
64 return theStart
-> data
-> nodeBuffer
. offset
;
70 mem_hdr :: endOffset () const
73 const SplayNode
< mem_node
*> * theEnd
= nodes
. finish ();
76 result
= theEnd
-> data
-> dataRange (). end
;
78 assert ( result
== inmem_hi
);
84 mem_hdr :: freeContent ()
86 nodes
. destroy ( SplayNode
< mem_node
*>:: DefaultFree
);
91 mem_hdr :: unlink ( mem_node
* aNode
)
93 if ( aNode
-> write_pending
) {
94 debugs ( 0 , 0 , "cannot unlink mem_node " << aNode
<< " while write_pending" );
98 nodes
. remove ( aNode
, NodeCompare
);
104 mem_hdr :: freeDataUpto ( int64_t target_offset
)
106 /* keep the last one to avoid change to other part of code */
108 SplayNode
< mem_node
*> const * theStart
= nodes
. start ();
110 while (( theStart
= nodes
. start ())) {
111 if ( theStart
== nodes
. finish ())
114 if ( theStart
-> data
-> end () > target_offset
)
117 if (! unlink ( theStart
-> data
))
121 assert ( lowestOffset () <= target_offset
);
123 return lowestOffset ();
127 mem_hdr :: appendToNode ( mem_node
* aNode
, const char * data
, int maxLength
)
129 size_t result
= writeAvailable ( aNode
, aNode
-> nodeBuffer
. offset
+ aNode
-> nodeBuffer
. length
, maxLength
, data
);
134 mem_hdr :: writeAvailable ( mem_node
* aNode
, int64_t location
, size_t amount
, char const * source
)
136 /* if we attempt to overwrite existing data or leave a gap within a node */
137 assert ( location
== aNode
-> nodeBuffer
. offset
+ ( int64_t ) aNode
-> nodeBuffer
. length
);
138 /* And we are not at the end of the node */
139 assert ( aNode
-> canAccept ( location
));
141 /* these two can go I think */
142 assert ( location
- aNode
-> nodeBuffer
. offset
== ( int64_t ) aNode
-> nodeBuffer
. length
);
143 size_t copyLen
= min ( amount
, aNode
-> space ());
145 xmemcpy ( aNode
-> nodeBuffer
. data
+ aNode
-> nodeBuffer
. length
, source
, copyLen
);
147 if ( inmem_hi
<= location
)
148 inmem_hi
= location
+ copyLen
;
150 /* Adjust the ptr and len according to what was deposited in the page */
151 aNode
-> nodeBuffer
. length
+= copyLen
;
157 mem_hdr :: appendNode ( mem_node
* aNode
)
159 nodes
. insert ( aNode
, NodeCompare
);
163 mem_hdr :: makeAppendSpace ()
166 appendNode ( new mem_node ( 0 ));
170 if (! nodes
. finish ()-> data
-> space ())
171 appendNode ( new mem_node ( endOffset ()));
173 assert ( nodes
. finish ()-> data
-> space ());
177 mem_hdr :: internalAppend ( const char * data
, int len
)
179 debugs ( 19 , 6 , "memInternalAppend: len " << len
);
183 int copied
= appendToNode ( nodes
. finish ()-> data
, data
, len
);
191 /* returns a mem_node that contains location..
192 * If no node contains the start, it returns NULL.
195 mem_hdr :: getBlockContainingLocation ( int64_t location
) const
197 mem_node
target ( location
);
198 target
. nodeBuffer
. length
= 1 ;
199 mem_node
* const * result
= nodes
. find (& target
, NodeCompare
);
208 mem_hdr :: copyAvailable ( mem_node
* aNode
, int64_t location
, size_t amount
, char * target
) const
210 if ( aNode
-> nodeBuffer
. offset
> location
)
213 assert ( aNode
-> nodeBuffer
. offset
<= location
);
215 assert ( aNode
-> end () > location
);
217 size_t copyOffset
= location
- aNode
-> nodeBuffer
. offset
;
219 size_t copyLen
= min ( amount
, aNode
-> nodeBuffer
. length
- copyOffset
);
221 xmemcpy ( target
, aNode
-> nodeBuffer
. data
+ copyOffset
, copyLen
);
227 mem_hdr :: debugDump () const
229 debugs ( 19 , 0 , "mem_hdr::debugDump: lowest offset: " << lowestOffset () << " highest offset + 1: " << endOffset () << "." );
230 std :: ostringstream result
;
231 PointerPrinter
< mem_node
*> foo ( result
, " - " );
232 for_each ( getNodes (). begin (), getNodes (). end (), foo
);
233 debugs ( 19 , 0 , "mem_hdr::debugDump: Current available data is: " << result
. str () << "." );
236 /* FIXME: how do we deal with sparse results -
237 * where we have (say)
238 * 0-500 and 1000-1500, but are asked for
241 * we supply 0-500 and stop.
244 mem_hdr :: copy ( StoreIOBuffer
const & target
) const
247 assert ( target
. range (). end
> target
. range (). start
);
248 debugs ( 19 , 6 , "memCopy: " << target
. range ());
250 /* we shouldn't ever ask for absent offsets */
252 if ( nodes
. size () == 0 ) {
253 debugs ( 19 , 1 , "mem_hdr::copy: No data to read" );
259 /* RC: the next assert is nearly useless */
260 assert ( target
. length
> 0 );
262 /* Seek our way into store */
263 mem_node
* p
= getBlockContainingLocation ( target
. offset
);
266 debugs ( 19 , 1 , "memCopy: could not find start of " << target
. range () <<
269 fatal ( "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 preceeding 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 " );
273 size_t bytes_to_go
= target
. length
;
274 char * ptr_to_buf
= target
. data
;
275 int64_t location
= target
. offset
;
277 /* Start copying begining with this block until
280 while ( p
&& bytes_to_go
> 0 ) {
281 size_t bytes_to_copy
= copyAvailable ( p
,
282 location
, bytes_to_go
, ptr_to_buf
);
284 /* hit a sparse patch */
286 if ( bytes_to_copy
== 0 )
287 return target
. length
- bytes_to_go
;
289 location
+= bytes_to_copy
;
291 ptr_to_buf
+= bytes_to_copy
;
293 bytes_to_go
-= bytes_to_copy
;
295 p
= getBlockContainingLocation ( location
);
298 return target
. length
- bytes_to_go
;
302 mem_hdr :: hasContigousContentRange ( Range
< int64_t > const & range
) const
304 int64_t currentStart
= range
. start
;
306 while ( mem_node
* curr
= getBlockContainingLocation ( currentStart
)) {
307 currentStart
= curr
-> end ();
309 if ( currentStart
>= range
. end
)
317 mem_hdr :: unionNotEmpty ( StoreIOBuffer
const & candidate
)
319 assert ( candidate
. offset
>= 0 );
320 mem_node
target ( candidate
. offset
);
321 target
. nodeBuffer
. length
= candidate
. length
;
322 return nodes
. find (& target
, NodeCompare
);
326 mem_hdr :: nodeToRecieve ( int64_t offset
)
328 /* case 1: Nothing in memory */
331 appendNode ( new mem_node ( offset
));
332 return nodes
. start ()-> data
;
335 mem_node
* candidate
= NULL
;
336 /* case 2: location fits within an extant node */
339 mem_node
search ( offset
- 1 );
340 search
. nodeBuffer
. length
= 1 ;
341 mem_node
* const * leadup
= nodes
. find (& search
, NodeCompare
);
347 if ( candidate
&& candidate
-> canAccept ( offset
))
350 /* candidate can't accept, so we need a new node */
351 candidate
= new mem_node ( offset
);
353 appendNode ( candidate
);
355 /* simpler to write than a indented if */
361 mem_hdr :: write ( StoreIOBuffer
const & writeBuffer
)
363 PROF_start ( mem_hdr_write
);
364 debugs ( 19 , 6 , "mem_hdr::write: " << writeBuffer
. range () << " object end " << endOffset ());
366 if ( unionNotEmpty ( writeBuffer
)) {
367 debugs ( 19 , 0 , "mem_hdr::write: writeBuffer: " << writeBuffer
. range ());
369 fatal ( "Attempt to overwrite already in-memory data. Preceeding 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 " );
370 PROF_stop ( mem_hdr_write
);
374 assert ( writeBuffer
. offset
>= 0 );
377 int64_t currentOffset
= writeBuffer
. offset
;
378 char * currentSource
= writeBuffer
. data
;
379 size_t len
= writeBuffer
. length
;
381 while ( len
&& ( target
= nodeToRecieve ( currentOffset
))) {
382 size_t wrote
= writeAvailable ( target
, currentOffset
, len
, currentSource
);
385 currentOffset
+= wrote
;
386 currentSource
+= wrote
;
389 PROF_stop ( mem_hdr_write
);
393 mem_hdr :: mem_hdr () : inmem_hi ( 0 )
401 /* splay of mem nodes:
403 * a = b if a.intersection(b).size > 0;
407 mem_hdr :: NodeCompare ( mem_node
* const & left
, mem_node
* const & right
)
409 // possibly Range can help us at some point.
411 if ( left
-> dataRange (). intersection ( right
-> dataRange ()). size () > 0 )
414 return * left
< * right
? - 1 : 1 ;
418 mem_hdr :: dump () const
420 debugs ( 20 , 1 , "mem_hdr: " << ( void *) this << " nodes.start() " << nodes
. start ());
421 debugs ( 20 , 1 , "mem_hdr: " << ( void *) this << " nodes.finish() " << nodes
. finish ());
425 mem_hdr :: size () const
431 mem_hdr :: start () const
433 const SplayNode
< mem_node
*> * result
= nodes
. start ();
441 const Splay
< mem_node
*> &
442 mem_hdr :: getNodes () const