2 * Copyright (C) 1996-2023 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 #ifndef SQUID_MEMOBJECT_H
10 #define SQUID_MEMOBJECT_H
12 #include "base/DelayedAsyncCalls.h"
14 #include "http/RequestMethod.h"
15 #include "HttpReply.h"
16 #include "RemovalPolicy.h"
17 #include "sbuf/SBuf.h"
19 #include "store/forward.h"
20 #include "StoreIOBuffer.h"
21 #include "StoreIOState.h"
22 #include "typedefs.h" //for IRCB
28 typedef void STMCB (void *data
, StoreIOBuffer wroteBuffer
);
35 MEMPROXY_CLASS(MemObject
);
38 static size_t inUseCount();
44 /// Sets store ID, log URI, and request method (unless already set). Does
45 /// not clobber the method so that, say, a HEAD hit for a GET entry keeps
46 /// the GET method that matches the entry key. Same for the other parts of
47 /// the trio because the entry filling code may expect them to be constant.
48 /// XXX: Avoid this method. We plan to remove it and make the trio constant
49 /// after addressing the XXX in MemStore::get().
50 void setUris(char const *aStoreId
, char const *aLogUri
, const HttpRequestMethod
&aMethod
);
52 /// whether setUris() has been called
55 void write(const StoreIOBuffer
&buf
);
56 void unlinkRequest() { request
= nullptr; }
58 /// HTTP response before 304 (Not Modified) updates
59 /// starts "empty"; modified via replaceBaseReply() or adjustableBaseReply()
60 const HttpReply
&baseReply() const { return *reply_
; }
62 /// \returns nil -- if no 304 updates since replaceBaseReply()
63 /// \returns combination of baseReply() and 304 updates -- after updates
64 const HttpReplyPointer
&updatedReply() const { return updatedReply_
; }
66 /// \returns the updated-by-304(s) response (if it exists)
67 /// \returns baseReply() (otherwise)
68 const HttpReply
&freshestReply() const {
70 return *updatedReply_
;
75 /// \returns writable base reply for parsing and other initial modifications
76 /// Base modifications can only be done when forming/loading the entry.
77 /// After that, use replaceBaseReply() to reset all of the replies.
78 HttpReply
&adjustableBaseReply();
80 /// (re)sets base reply, usually just replacing the initial/empty object
81 /// also forgets the updated reply (if any)
82 void replaceBaseReply(const HttpReplyPointer
&r
);
84 /// (re)sets updated reply; \see updatedReply()
85 void updateReply(const HttpReply
&r
) { updatedReply_
= &r
; }
87 /// reflects past Controller::updateOnNotModified(old, e304) calls:
88 /// for HTTP 304 entries: whether our entry was used as "e304"
89 /// for other entries: whether our entry was updated as "old"
90 bool appliedUpdates
= false;
92 void stat (MemBuf
* mb
) const;
94 /// The offset of the last memory-stored HTTP response byte plus one.
95 /// * HTTP response headers (if any) are stored at offset zero.
96 /// * HTTP response body byte[n] usually has offset (hdr_sz + n), where
97 /// hdr_sz is the size of stored HTTP response headers (zero if none); and
98 /// n is the corresponding byte offset in the whole resource body.
99 /// However, some 206 (Partial Content) response bodies are stored (and
100 /// retrieved) as regular 200 response bodies, disregarding offsets of
101 /// their body parts. \sa HttpStateData::decideIfWeDoRanges().
102 int64_t endOffset () const;
104 /// sets baseReply().hdr_sz (i.e. written reply headers size) to endOffset()
105 void markEndOfReplyHeaders();
107 /// negative if unknown; otherwise, expected object_sz, expected endOffset
108 /// maximum, and stored reply headers+body size (all three are the same)
109 int64_t expectedReplySize() const;
110 int64_t size() const;
112 int64_t lowestMemReaderOffset() const;
113 bool readAheadPolicyCanRead() const;
114 void addClient(store_client
*);
115 /* XXX belongs in MemObject::swapout, once swaphdrsz is managed
118 int64_t objectBytesOnDisk() const;
119 int64_t policyLowestOffsetToKeep(bool swap
) const;
120 int64_t availableForSwapOut() const; ///< buffered bytes we have not swapped out yet
121 void trimSwappable();
122 void trimUnSwappable();
123 bool isContiguous() const;
124 int mostBytesWanted(int max
, bool ignoreDelayPools
) const;
125 void setNoDelay(bool const newValue
);
127 DelayId
mostBytesAllowed() const;
130 #if URL_CHECKSUM_DEBUG
132 void checkUrlChecksum() const;
135 /// Before StoreID, code assumed that MemObject stores Request URI.
136 /// After StoreID, some old code still incorrectly assumes that.
137 /// Use this method to mark that incorrect assumption.
138 const char *urlXXX() const { return storeId(); }
140 /// Entry StoreID (usually just Request URI); if a buggy code requests this
141 /// before the information is available, returns an "[unknown_URI]" string.
142 const char *storeId() const;
144 /// client request URI used for logging; storeId() by default
145 const char *logUri() const;
147 HttpRequestMethod method
;
149 int64_t inmem_lo
= 0;
152 size_t clientCount() const {return nclients
;}
154 bool clientIsFirst(void *sc
) const {return (clients
.head
&& sc
== clients
.head
->data
);}
161 int64_t queue_offset
= 0; ///< number of bytes sent to SwapDir for writing
162 StoreIOState::Pointer sio
;
164 /// Decision states for StoreEntry::swapoutPossible() and related code.
165 typedef enum { swNeedsCheck
= 0, swImpossible
= -1, swPossible
= +1, swStarted
} Decision
;
166 Decision decision
= swNeedsCheck
; ///< current decision state
171 /* TODO: Remove this change-minimizing hack */
172 using Io
= Store::IoStatus
;
173 static constexpr Io ioUndecided
= Store::ioUndecided
;
174 static constexpr Io ioReading
= Store::ioReading
;
175 static constexpr Io ioWriting
= Store::ioWriting
;
176 static constexpr Io ioDone
= Store::ioDone
;
178 /// State of an entry with regards to the [shared] in-transit table.
182 /// associate our StoreEntry with a Transients entry at the given index
183 void open(const int32_t anIndex
, const Io anIo
)
189 /// stop associating our StoreEntry with a Transients entry
196 int32_t index
= -1; ///< entry position inside the in-transit table
197 Io io
= ioUndecided
; ///< current I/O state
199 XitTable xitTable
; ///< current [shared] memory caching state for the entry
201 /// State of an entry with regards to the [shared] memory caching.
205 int32_t index
= -1; ///< entry position inside the memory cache
206 int64_t offset
= 0; ///< bytes written/read to/from the memory cache so far
208 Io io
= ioUndecided
; ///< current I/O state
210 MemCache memCache
; ///< current [shared] memory caching state for the entry
212 HttpRequestPointer request
;
214 struct timeval start_ping
;
215 IRCB
*ping_reply_callback
;
216 PeerSelector
*ircb_data
= nullptr;
218 /// used for notifying StoreEntry writers about 3rd-party initiated aborts
219 AsyncCallPointer abortCallback
;
220 RemovalPolicyNode repl
;
222 int64_t object_sz
= -1;
223 size_t swap_hdr_sz
= 0;
224 #if URL_CHECKSUM_DEBUG
225 unsigned int chksum
= 0;
230 void delayRead(const AsyncCallPointer
&);
234 HttpReplyPointer reply_
; ///< \see baseReply()
235 HttpReplyPointer updatedReply_
; ///< \see updatedReply()
237 mutable SBuf storeId_
; ///< StoreId for our entry (usually request URI)
238 mutable SBuf logUri_
; ///< URI used for logging (usually request URI)
240 DelayedAsyncCalls deferredReads
;
243 /** global current memory removal policy */
244 extern RemovalPolicy
*mem_policy
;
246 #endif /* SQUID_MEMOBJECT_H */