2 * Copyright (C) 1996-2022 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 "RemovalPolicy.h"
16 #include "SquidString.h"
18 #include "store/forward.h"
19 #include "StoreIOBuffer.h"
20 #include "StoreIOState.h"
21 #include "typedefs.h" //for IRCB
27 typedef void STMCB (void *data
, StoreIOBuffer wroteBuffer
);
34 MEMPROXY_CLASS(MemObject
);
37 static size_t inUseCount();
43 /// Sets store ID, log URI, and request method (unless already set). Does
44 /// not clobber the method so that, say, a HEAD hit for a GET entry keeps
45 /// the GET method that matches the entry key. Same for the other parts of
46 /// the trio because the entry filling code may expect them to be constant.
47 /// XXX: Avoid this method. We plan to remove it and make the trio constant
48 /// after addressing the XXX in MemStore::get().
49 void setUris(char const *aStoreId
, char const *aLogUri
, const HttpRequestMethod
&aMethod
);
51 /// whether setUris() has been called
54 void write(const StoreIOBuffer
&buf
);
55 void unlinkRequest() { request
= nullptr; }
57 /// HTTP response before 304 (Not Modified) updates
58 /// starts "empty"; modified via replaceBaseReply() or adjustableBaseReply()
59 const HttpReply
&baseReply() const { return *reply_
; }
61 /// \returns nil -- if no 304 updates since replaceBaseReply()
62 /// \returns combination of baseReply() and 304 updates -- after updates
63 const HttpReplyPointer
&updatedReply() const { return updatedReply_
; }
65 /// \returns the updated-by-304(s) response (if it exists)
66 /// \returns baseReply() (otherwise)
67 const HttpReply
&freshestReply() const {
69 return *updatedReply_
;
74 /// \returns writable base reply for parsing and other initial modifications
75 /// Base modifications can only be done when forming/loading the entry.
76 /// After that, use replaceBaseReply() to reset all of the replies.
77 HttpReply
&adjustableBaseReply();
79 /// (re)sets base reply, usually just replacing the initial/empty object
80 /// also forgets the updated reply (if any)
81 void replaceBaseReply(const HttpReplyPointer
&r
);
83 /// (re)sets updated reply; \see updatedReply()
84 void updateReply(const HttpReply
&r
) { updatedReply_
= &r
; }
86 /// reflects past Controller::updateOnNotModified(old, e304) calls:
87 /// for HTTP 304 entries: whether our entry was used as "e304"
88 /// for other entries: whether our entry was updated as "old"
89 bool appliedUpdates
= false;
91 void stat (MemBuf
* mb
) const;
92 int64_t endOffset () const;
94 /// sets baseReply().hdr_sz (i.e. written reply headers size) to endOffset()
95 void markEndOfReplyHeaders();
97 /// negative if unknown; otherwise, expected object_sz, expected endOffset
98 /// maximum, and stored reply headers+body size (all three are the same)
99 int64_t expectedReplySize() const;
100 int64_t size() const;
102 int64_t lowestMemReaderOffset() const;
103 bool readAheadPolicyCanRead() const;
104 void addClient(store_client
*);
105 /* XXX belongs in MemObject::swapout, once swaphdrsz is managed
108 int64_t objectBytesOnDisk() const;
109 int64_t policyLowestOffsetToKeep(bool swap
) const;
110 int64_t availableForSwapOut() const; ///< buffered bytes we have not swapped out yet
111 void trimSwappable();
112 void trimUnSwappable();
113 bool isContiguous() const;
114 int mostBytesWanted(int max
, bool ignoreDelayPools
) const;
115 void setNoDelay(bool const newValue
);
117 DelayId
mostBytesAllowed() const;
120 #if URL_CHECKSUM_DEBUG
122 void checkUrlChecksum() const;
125 /// Before StoreID, code assumed that MemObject stores Request URI.
126 /// After StoreID, some old code still incorrectly assumes that.
127 /// Use this method to mark that incorrect assumption.
128 const char *urlXXX() const { return storeId(); }
130 /// Entry StoreID (usually just Request URI); if a buggy code requests this
131 /// before the information is available, returns an "[unknown_URI]" string.
132 const char *storeId() const;
134 /// client request URI used for logging; storeId() by default
135 const char *logUri() const;
137 HttpRequestMethod method
;
139 int64_t inmem_lo
= 0;
142 size_t clientCount() const {return nclients
;}
144 bool clientIsFirst(void *sc
) const {return (clients
.head
&& sc
== clients
.head
->data
);}
151 int64_t queue_offset
= 0; ///< number of bytes sent to SwapDir for writing
152 StoreIOState::Pointer sio
;
154 /// Decision states for StoreEntry::swapoutPossible() and related code.
155 typedef enum { swNeedsCheck
= 0, swImpossible
= -1, swPossible
= +1, swStarted
} Decision
;
156 Decision decision
= swNeedsCheck
; ///< current decision state
161 /* TODO: Remove this change-minimizing hack */
162 using Io
= Store::IoStatus
;
163 static constexpr Io ioUndecided
= Store::ioUndecided
;
164 static constexpr Io ioReading
= Store::ioReading
;
165 static constexpr Io ioWriting
= Store::ioWriting
;
166 static constexpr Io ioDone
= Store::ioDone
;
168 /// State of an entry with regards to the [shared] in-transit table.
172 int32_t index
= -1; ///< entry position inside the in-transit table
173 Io io
= ioUndecided
; ///< current I/O state
175 XitTable xitTable
; ///< current [shared] memory caching state for the entry
177 /// State of an entry with regards to the [shared] memory caching.
181 int32_t index
= -1; ///< entry position inside the memory cache
182 int64_t offset
= 0; ///< bytes written/read to/from the memory cache so far
184 Io io
= ioUndecided
; ///< current I/O state
186 MemCache memCache
; ///< current [shared] memory caching state for the entry
188 HttpRequestPointer request
;
190 struct timeval start_ping
;
191 IRCB
*ping_reply_callback
;
192 PeerSelector
*ircb_data
= nullptr;
194 /// used for notifying StoreEntry writers about 3rd-party initiated aborts
195 AsyncCallPointer abortCallback
;
196 RemovalPolicyNode repl
;
198 int64_t object_sz
= -1;
199 size_t swap_hdr_sz
= 0;
200 #if URL_CHECKSUM_DEBUG
201 unsigned int chksum
= 0;
206 void delayRead(const AsyncCallPointer
&);
210 HttpReplyPointer reply_
; ///< \see baseReply()
211 HttpReplyPointer updatedReply_
; ///< \see updatedReply()
213 mutable String storeId_
; ///< StoreId for our entry (usually request URI)
214 mutable String logUri_
; ///< URI used for logging (usually request URI)
216 DelayedAsyncCalls deferredReads
;
219 /** global current memory removal policy */
220 extern RemovalPolicy
*mem_policy
;
222 #endif /* SQUID_MEMOBJECT_H */