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