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