]> git.ipfire.org Git - thirdparty/squid.git/blob - src/MemObject.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / MemObject.h
1 /*
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 #ifndef SQUID_MEMOBJECT_H
10 #define SQUID_MEMOBJECT_H
11
12 #include "CommRead.h"
13 #include "dlink.h"
14 #include "http/RequestMethod.h"
15 #include "RemovalPolicy.h"
16 #include "sbuf/SBuf.h"
17 #include "SquidString.h"
18 #include "stmem.h"
19 #include "StoreIOBuffer.h"
20 #include "StoreIOState.h"
21 #include "typedefs.h" //for IRCB
22
23 #if USE_DELAY_POOLS
24 #include "DelayId.h"
25 #endif
26
27 typedef void STMCB (void *data, StoreIOBuffer wroteBuffer);
28 typedef void STABH(void *);
29
30 class store_client;
31 class HttpRequest;
32 class HttpReply;
33
34 class MemObject
35 {
36 MEMPROXY_CLASS(MemObject);
37
38 public:
39 static size_t inUseCount();
40
41 void dump() const;
42 MemObject();
43 ~MemObject();
44
45 /// sets store ID, log URI, and request method; TODO: find a better name
46 void setUris(char const *aStoreId, char const *aLogUri, const HttpRequestMethod &aMethod);
47
48 /// whether setUris() has been called
49 bool hasUris() const;
50
51 void write(const StoreIOBuffer &buf);
52 void unlinkRequest();
53 HttpReply const *getReply() const;
54 void replaceHttpReply(HttpReply *newrep);
55 void stat (MemBuf * mb) const;
56 int64_t endOffset () const;
57 void markEndOfReplyHeaders(); ///< sets _reply->hdr_sz to endOffset()
58 /// negative if unknown; otherwise, expected object_sz, expected endOffset
59 /// maximum, and stored reply headers+body size (all three are the same)
60 int64_t expectedReplySize() const;
61 int64_t size() const;
62 void reset();
63 int64_t lowestMemReaderOffset() const;
64 bool readAheadPolicyCanRead() const;
65 void addClient(store_client *);
66 /* XXX belongs in MemObject::swapout, once swaphdrsz is managed
67 * better
68 */
69 int64_t objectBytesOnDisk() const;
70 int64_t policyLowestOffsetToKeep(bool swap) const;
71 int64_t availableForSwapOut() const; ///< buffered bytes we have not swapped out yet
72 void trimSwappable();
73 void trimUnSwappable();
74 bool isContiguous() const;
75 int mostBytesWanted(int max, bool ignoreDelayPools) const;
76 void setNoDelay(bool const newValue);
77 #if USE_DELAY_POOLS
78 DelayId mostBytesAllowed() const;
79 #endif
80
81 #if URL_CHECKSUM_DEBUG
82
83 void checkUrlChecksum() const;
84 #endif
85
86 /// Before StoreID, code assumed that MemObject stores Request URI.
87 /// After StoreID, some old code still incorrectly assumes that.
88 /// Use this method to mark that incorrect assumption.
89 const char *urlXXX() const { return storeId(); }
90
91 /// Entry StoreID (usually just Request URI); if a buggy code requests this
92 /// before the information is available, returns an "[unknown_URI]" string.
93 const char *storeId() const;
94
95 /// client request URI used for logging; storeId() by default
96 const char *logUri() const;
97
98 HttpRequestMethod method;
99 mem_hdr data_hdr;
100 int64_t inmem_lo;
101 dlink_list clients;
102
103 size_t clientCount() const {return nclients;}
104
105 bool clientIsFirst(void *sc) const {return (clients.head && sc == clients.head->data);}
106
107 int nclients;
108
109 class SwapOut
110 {
111 public:
112 SwapOut() : queue_offset(0), decision(swNeedsCheck) {}
113
114 int64_t queue_offset; ///< number of bytes sent to SwapDir for writing
115 StoreIOState::Pointer sio;
116
117 /// Decision states for StoreEntry::swapoutPossible() and related code.
118 typedef enum { swNeedsCheck = 0, swImpossible = -1, swPossible = +1, swStarted } Decision;
119 Decision decision; ///< current decision state
120 };
121
122 SwapOut swapout;
123
124 /// cache "I/O" direction and status
125 typedef enum { ioUndecided, ioWriting, ioReading, ioDone } Io;
126
127 /// State of an entry with regards to the [shared] in-transit table.
128 class XitTable
129 {
130 public:
131 XitTable(): index(-1), io(ioUndecided) {}
132
133 int32_t index; ///< entry position inside the in-transit table
134 Io io; ///< current I/O state
135 };
136 XitTable xitTable; ///< current [shared] memory caching state for the entry
137
138 /// State of an entry with regards to the [shared] memory caching.
139 class MemCache
140 {
141 public:
142 MemCache(): index(-1), offset(0), io(ioUndecided) {}
143
144 int32_t index; ///< entry position inside the memory cache
145 int64_t offset; ///< bytes written/read to/from the memory cache so far
146
147 Io io; ///< current I/O state
148 };
149 MemCache memCache; ///< current [shared] memory caching state for the entry
150
151 bool smpCollapsed; ///< whether this entry gets data from another worker
152
153 /* Read only - this reply must be preserved by store clients */
154 /* The original reply. possibly with updated metadata. */
155 HttpRequest *request;
156
157 struct timeval start_ping;
158 IRCB *ping_reply_callback;
159 void *ircb_data;
160
161 struct {
162 STABH *callback;
163 void *data;
164 } abort;
165 RemovalPolicyNode repl;
166 int id;
167 int64_t object_sz;
168 size_t swap_hdr_sz;
169 #if URL_CHECKSUM_DEBUG
170
171 unsigned int chksum;
172 #endif
173
174 SBuf vary_headers;
175
176 void delayRead(DeferredRead const &);
177 void kickReads();
178
179 private:
180 HttpReply *_reply;
181
182 mutable String storeId_; ///< StoreId for our entry (usually request URI)
183 mutable String logUri_; ///< URI used for logging (usually request URI)
184
185 DeferredReadManager deferredReads;
186 };
187
188 /** global current memory removal policy */
189 extern RemovalPolicy *mem_policy;
190
191 #endif /* SQUID_MEMOBJECT_H */
192