annotate agent/src/os/win32/IOBuf.cpp @ 1994:6cd6d394f280

7001033: assert(gch->gc_cause() == GCCause::_scavenge_alot || !gch->incremental_collection_failed()) 7002546: regression on SpecJbb2005 on 7b118 comparing to 7b117 on small heaps Summary: Relaxed assertion checking related to incremental_collection_failed flag to allow for ExplicitGCInvokesConcurrent behaviour where we do not want a failing scavenge to bail to a stop-world collection. Parameterized incremental_collection_will_fail() so we can selectively use, or not use, as appropriate, the statistical prediction at specific use sites. This essentially reverts the scavenge bail-out logic to what it was prior to some recent changes that had inadvertently started using the statistical prediction which can be noisy in the presence of bursty loads. Added some associated verbose non-product debugging messages. Reviewed-by: johnc, tonyp
author ysr
date Tue, 07 Dec 2010 21:55:53 -0800
parents c18cbe5936b8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
2 * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 #include <stdio.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 // This file is currently used for os/solaris/agent too. At some point in time
a61af66fc99e Initial load
duke
parents:
diff changeset
28 // the source will be reorganized to avoid these ifdefs.
a61af66fc99e Initial load
duke
parents:
diff changeset
29
a61af66fc99e Initial load
duke
parents:
diff changeset
30 #ifdef __sun
a61af66fc99e Initial load
duke
parents:
diff changeset
31 #include <string.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
32 #include <inttypes.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
33 #include <sys/byteorder.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
34 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 #include "IOBuf.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
37
a61af66fc99e Initial load
duke
parents:
diff changeset
38 // Formats for printing pointers
a61af66fc99e Initial load
duke
parents:
diff changeset
39 #ifdef _LP64
a61af66fc99e Initial load
duke
parents:
diff changeset
40 # define INTPTR_FORMAT "0x%016lx"
a61af66fc99e Initial load
duke
parents:
diff changeset
41 #else /* ! _LP64 */
a61af66fc99e Initial load
duke
parents:
diff changeset
42 # define INTPTR_FORMAT "0x%08lx"
a61af66fc99e Initial load
duke
parents:
diff changeset
43 #endif /* _LP64 */
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 // Uncomment the #define below to get messages on stderr
a61af66fc99e Initial load
duke
parents:
diff changeset
46 // #define DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
47
a61af66fc99e Initial load
duke
parents:
diff changeset
48 IOBuf::IOBuf(int inLen, int outLen) {
a61af66fc99e Initial load
duke
parents:
diff changeset
49 inBuf = new Buffer(inLen);
a61af66fc99e Initial load
duke
parents:
diff changeset
50 outBuf = new Buffer(outLen);
a61af66fc99e Initial load
duke
parents:
diff changeset
51 fd = INVALID_SOCKET;
a61af66fc99e Initial load
duke
parents:
diff changeset
52 outHandle = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
53 usingSocket = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
54 reset();
a61af66fc99e Initial load
duke
parents:
diff changeset
55 }
a61af66fc99e Initial load
duke
parents:
diff changeset
56
a61af66fc99e Initial load
duke
parents:
diff changeset
57 IOBuf::~IOBuf() {
a61af66fc99e Initial load
duke
parents:
diff changeset
58 delete inBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
59 delete outBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
60 }
a61af66fc99e Initial load
duke
parents:
diff changeset
61
a61af66fc99e Initial load
duke
parents:
diff changeset
62 void
a61af66fc99e Initial load
duke
parents:
diff changeset
63 IOBuf::setSocket(SOCKET sock) {
a61af66fc99e Initial load
duke
parents:
diff changeset
64 fd = sock;
a61af66fc99e Initial load
duke
parents:
diff changeset
65 usingSocket = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
66 }
a61af66fc99e Initial load
duke
parents:
diff changeset
67
a61af66fc99e Initial load
duke
parents:
diff changeset
68 // Reading/writing files is only needed and used on windows.
a61af66fc99e Initial load
duke
parents:
diff changeset
69 #ifdef WIN32
a61af66fc99e Initial load
duke
parents:
diff changeset
70 void
a61af66fc99e Initial load
duke
parents:
diff changeset
71 IOBuf::setOutputFileHandle(HANDLE handle) {
a61af66fc99e Initial load
duke
parents:
diff changeset
72 outHandle = handle;
a61af66fc99e Initial load
duke
parents:
diff changeset
73 usingSocket = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
74 }
a61af66fc99e Initial load
duke
parents:
diff changeset
75 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
76
a61af66fc99e Initial load
duke
parents:
diff changeset
77 void
a61af66fc99e Initial load
duke
parents:
diff changeset
78 IOBuf::reset() {
a61af66fc99e Initial load
duke
parents:
diff changeset
79 gotDataLastTime = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
80 state = TEXT_STATE;
a61af66fc99e Initial load
duke
parents:
diff changeset
81 binPos = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
82 binLength = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
83 }
a61af66fc99e Initial load
duke
parents:
diff changeset
84
a61af66fc99e Initial load
duke
parents:
diff changeset
85 IOBuf::ReadLineResult
a61af66fc99e Initial load
duke
parents:
diff changeset
86 IOBuf::tryReadLine() {
a61af66fc99e Initial load
duke
parents:
diff changeset
87 return doReadLine(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
88 }
a61af66fc99e Initial load
duke
parents:
diff changeset
89
a61af66fc99e Initial load
duke
parents:
diff changeset
90 char*
a61af66fc99e Initial load
duke
parents:
diff changeset
91 IOBuf::readLine() {
a61af66fc99e Initial load
duke
parents:
diff changeset
92 ReadLineResult rr = doReadLine(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
93 if (rr != RL_GOT_DATA) {
a61af66fc99e Initial load
duke
parents:
diff changeset
94 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
95 }
a61af66fc99e Initial load
duke
parents:
diff changeset
96 return getLine();
a61af66fc99e Initial load
duke
parents:
diff changeset
97 }
a61af66fc99e Initial load
duke
parents:
diff changeset
98
a61af66fc99e Initial load
duke
parents:
diff changeset
99 IOBuf::ReadLineResult
a61af66fc99e Initial load
duke
parents:
diff changeset
100 IOBuf::doReadLine(bool shouldWait) {
a61af66fc99e Initial load
duke
parents:
diff changeset
101
a61af66fc99e Initial load
duke
parents:
diff changeset
102 if (!usingSocket) {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 return IOBuf::RL_ERROR;
a61af66fc99e Initial load
duke
parents:
diff changeset
104 }
a61af66fc99e Initial load
duke
parents:
diff changeset
105
a61af66fc99e Initial load
duke
parents:
diff changeset
106 if (gotDataLastTime) {
a61af66fc99e Initial load
duke
parents:
diff changeset
107 curLine.clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
108 }
a61af66fc99e Initial load
duke
parents:
diff changeset
109
a61af66fc99e Initial load
duke
parents:
diff changeset
110 int c;
a61af66fc99e Initial load
duke
parents:
diff changeset
111 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
112 c = readChar(shouldWait);
a61af66fc99e Initial load
duke
parents:
diff changeset
113 if (c >= 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 Action act = processChar((char) c);
a61af66fc99e Initial load
duke
parents:
diff changeset
115 if (act == GOT_LINE) {
a61af66fc99e Initial load
duke
parents:
diff changeset
116 curLine.push_back('\0');
a61af66fc99e Initial load
duke
parents:
diff changeset
117 gotDataLastTime = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
118 return IOBuf::RL_GOT_DATA;
a61af66fc99e Initial load
duke
parents:
diff changeset
119 } else if (act == SKIP_EOL_CHAR) {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 // Do nothing
a61af66fc99e Initial load
duke
parents:
diff changeset
121 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
122 curLine.push_back((char) c);
a61af66fc99e Initial load
duke
parents:
diff changeset
123 }
a61af66fc99e Initial load
duke
parents:
diff changeset
124 }
a61af66fc99e Initial load
duke
parents:
diff changeset
125 } while (shouldWait || c >= 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
126
a61af66fc99e Initial load
duke
parents:
diff changeset
127 gotDataLastTime = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
128 return IOBuf::RL_NO_DATA;
a61af66fc99e Initial load
duke
parents:
diff changeset
129 }
a61af66fc99e Initial load
duke
parents:
diff changeset
130
a61af66fc99e Initial load
duke
parents:
diff changeset
131 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
132 IOBuf::flushImpl(bool moreDataToCome) {
a61af66fc99e Initial load
duke
parents:
diff changeset
133 int numWritten = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
134
a61af66fc99e Initial load
duke
parents:
diff changeset
135 #ifdef WIN32
a61af66fc99e Initial load
duke
parents:
diff changeset
136 // When running on Windows and using IOBufs for inter-process
a61af66fc99e Initial load
duke
parents:
diff changeset
137 // communication, we need to write metadata into the stream
a61af66fc99e Initial load
duke
parents:
diff changeset
138 // indicating how many bytes are coming down. Five bytes are written
a61af66fc99e Initial load
duke
parents:
diff changeset
139 // per flush() call, four containing the integer number of bytes
a61af66fc99e Initial load
duke
parents:
diff changeset
140 // coming (not including the five-byte header) and one (a 0 or 1)
a61af66fc99e Initial load
duke
parents:
diff changeset
141 // indicating whether there is more data coming.
a61af66fc99e Initial load
duke
parents:
diff changeset
142 if (!usingSocket) {
a61af66fc99e Initial load
duke
parents:
diff changeset
143 int numToWrite = outBuf->drainRemaining();
a61af66fc99e Initial load
duke
parents:
diff changeset
144 char moreToCome = (moreDataToCome ? 1 : 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
145 DWORD numBytesWritten;
a61af66fc99e Initial load
duke
parents:
diff changeset
146 if (!WriteFile(outHandle, &numToWrite, sizeof(int), &numBytesWritten, NULL)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
148 }
a61af66fc99e Initial load
duke
parents:
diff changeset
149 if (numBytesWritten != sizeof(int)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
150 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
151 }
a61af66fc99e Initial load
duke
parents:
diff changeset
152 if (!WriteFile(outHandle, &moreToCome, 1, &numBytesWritten, NULL)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
153 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
154 }
a61af66fc99e Initial load
duke
parents:
diff changeset
155 if (numBytesWritten != 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
156 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
157 }
a61af66fc99e Initial load
duke
parents:
diff changeset
158 }
a61af66fc99e Initial load
duke
parents:
diff changeset
159 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
160
a61af66fc99e Initial load
duke
parents:
diff changeset
161 while (outBuf->drainRemaining() != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
162 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
163 fprintf(stderr, "Flushing %d bytes\n", outBuf->drainRemaining());
a61af66fc99e Initial load
duke
parents:
diff changeset
164 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
165 if (usingSocket) {
a61af66fc99e Initial load
duke
parents:
diff changeset
166 numWritten = send(fd, outBuf->drainPos(), outBuf->drainRemaining(), 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
167 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
168 #ifdef WIN32
a61af66fc99e Initial load
duke
parents:
diff changeset
169 DWORD numBytesWritten;
a61af66fc99e Initial load
duke
parents:
diff changeset
170 if (!WriteFile(outHandle, outBuf->drainPos(), outBuf->drainRemaining(), &numBytesWritten, NULL)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
171 numWritten = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
172 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
173 numWritten = numBytesWritten;
a61af66fc99e Initial load
duke
parents:
diff changeset
174 }
a61af66fc99e Initial load
duke
parents:
diff changeset
175 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
176 }
a61af66fc99e Initial load
duke
parents:
diff changeset
177 if (numWritten != -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
178 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
179 fprintf(stderr, "Flushed %d bytes\n", numWritten);
a61af66fc99e Initial load
duke
parents:
diff changeset
180 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
181 outBuf->incrDrainPos(numWritten);
a61af66fc99e Initial load
duke
parents:
diff changeset
182 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
183 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
184 }
a61af66fc99e Initial load
duke
parents:
diff changeset
185 }
a61af66fc99e Initial load
duke
parents:
diff changeset
186
a61af66fc99e Initial load
duke
parents:
diff changeset
187 outBuf->compact();
a61af66fc99e Initial load
duke
parents:
diff changeset
188
a61af66fc99e Initial load
duke
parents:
diff changeset
189 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
190 }
a61af66fc99e Initial load
duke
parents:
diff changeset
191
a61af66fc99e Initial load
duke
parents:
diff changeset
192 int
a61af66fc99e Initial load
duke
parents:
diff changeset
193 IOBuf::readChar(bool block) {
a61af66fc99e Initial load
duke
parents:
diff changeset
194 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
195 int c = inBuf->readByte();
a61af66fc99e Initial load
duke
parents:
diff changeset
196 if (c >= 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
197 return c;
a61af66fc99e Initial load
duke
parents:
diff changeset
198 }
a61af66fc99e Initial load
duke
parents:
diff changeset
199 // See whether we need to compact the input buffer
a61af66fc99e Initial load
duke
parents:
diff changeset
200 if (inBuf->remaining() < inBuf->size() / 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
201 inBuf->compact();
a61af66fc99e Initial load
duke
parents:
diff changeset
202 }
a61af66fc99e Initial load
duke
parents:
diff changeset
203 // See whether socket is ready
a61af66fc99e Initial load
duke
parents:
diff changeset
204 fd_set fds;
a61af66fc99e Initial load
duke
parents:
diff changeset
205 FD_ZERO(&fds);
a61af66fc99e Initial load
duke
parents:
diff changeset
206 FD_SET(fd, &fds);
a61af66fc99e Initial load
duke
parents:
diff changeset
207 struct timeval timeout;
a61af66fc99e Initial load
duke
parents:
diff changeset
208 timeout.tv_sec = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
209 timeout.tv_usec = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
210 if (block || select(1 + fd, &fds, NULL, NULL, &timeout) > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
211 if (block || FD_ISSET(fd, &fds)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
212 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
213 int b = (block ? 1 : 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
214 fprintf(stderr, "calling recv: block = %d\n", b);
a61af66fc99e Initial load
duke
parents:
diff changeset
215 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
216 // Read data from socket
a61af66fc99e Initial load
duke
parents:
diff changeset
217 int numRead = recv(fd, inBuf->fillPos(), inBuf->remaining(), 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
218 if (numRead < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
219 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
220 fprintf(stderr, "recv failed\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
221 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
222 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
223 }
a61af66fc99e Initial load
duke
parents:
diff changeset
224 inBuf->incrFillPos(numRead);
a61af66fc99e Initial load
duke
parents:
diff changeset
225 }
a61af66fc99e Initial load
duke
parents:
diff changeset
226 }
a61af66fc99e Initial load
duke
parents:
diff changeset
227 } while (block);
a61af66fc99e Initial load
duke
parents:
diff changeset
228
a61af66fc99e Initial load
duke
parents:
diff changeset
229 return inBuf->readByte();
a61af66fc99e Initial load
duke
parents:
diff changeset
230 }
a61af66fc99e Initial load
duke
parents:
diff changeset
231
a61af66fc99e Initial load
duke
parents:
diff changeset
232 char*
a61af66fc99e Initial load
duke
parents:
diff changeset
233 IOBuf::getLine() {
a61af66fc99e Initial load
duke
parents:
diff changeset
234 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
235 fprintf(stderr, "Returning (first 10 chars) \"%.10s\"\n", curLine.begin());
a61af66fc99e Initial load
duke
parents:
diff changeset
236 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
237 return curLine.begin();
a61af66fc99e Initial load
duke
parents:
diff changeset
238 }
a61af66fc99e Initial load
duke
parents:
diff changeset
239
a61af66fc99e Initial load
duke
parents:
diff changeset
240 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
241 IOBuf::flush() {
a61af66fc99e Initial load
duke
parents:
diff changeset
242 return flushImpl(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
243 }
a61af66fc99e Initial load
duke
parents:
diff changeset
244
a61af66fc99e Initial load
duke
parents:
diff changeset
245 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
246 IOBuf::writeString(const char* str) {
a61af66fc99e Initial load
duke
parents:
diff changeset
247 int len = strlen(str);
a61af66fc99e Initial load
duke
parents:
diff changeset
248
a61af66fc99e Initial load
duke
parents:
diff changeset
249 if (len > outBuf->size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
250 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
251 }
a61af66fc99e Initial load
duke
parents:
diff changeset
252
a61af66fc99e Initial load
duke
parents:
diff changeset
253 if (len > outBuf->remaining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
254 if (!flushImpl(true)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
255 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
256 }
a61af66fc99e Initial load
duke
parents:
diff changeset
257 }
a61af66fc99e Initial load
duke
parents:
diff changeset
258
a61af66fc99e Initial load
duke
parents:
diff changeset
259 // NOTE we do not copy the null terminator of the string.
a61af66fc99e Initial load
duke
parents:
diff changeset
260
a61af66fc99e Initial load
duke
parents:
diff changeset
261 strncpy(outBuf->fillPos(), str, len);
a61af66fc99e Initial load
duke
parents:
diff changeset
262 outBuf->incrFillPos(len);
a61af66fc99e Initial load
duke
parents:
diff changeset
263 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
264 }
a61af66fc99e Initial load
duke
parents:
diff changeset
265
a61af66fc99e Initial load
duke
parents:
diff changeset
266 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
267 IOBuf::writeInt(int val) {
a61af66fc99e Initial load
duke
parents:
diff changeset
268 char buf[128];
a61af66fc99e Initial load
duke
parents:
diff changeset
269 sprintf(buf, "%d", val);
a61af66fc99e Initial load
duke
parents:
diff changeset
270 return writeString(buf);
a61af66fc99e Initial load
duke
parents:
diff changeset
271 }
a61af66fc99e Initial load
duke
parents:
diff changeset
272
a61af66fc99e Initial load
duke
parents:
diff changeset
273 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
274 IOBuf::writeUnsignedInt(unsigned int val) {
a61af66fc99e Initial load
duke
parents:
diff changeset
275 char buf[128];
a61af66fc99e Initial load
duke
parents:
diff changeset
276 sprintf(buf, "%u", val);
a61af66fc99e Initial load
duke
parents:
diff changeset
277 return writeString(buf);
a61af66fc99e Initial load
duke
parents:
diff changeset
278 }
a61af66fc99e Initial load
duke
parents:
diff changeset
279
a61af66fc99e Initial load
duke
parents:
diff changeset
280 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
281 IOBuf::writeBoolAsInt(bool val) {
a61af66fc99e Initial load
duke
parents:
diff changeset
282 if (val) {
a61af66fc99e Initial load
duke
parents:
diff changeset
283 return writeString("1");
a61af66fc99e Initial load
duke
parents:
diff changeset
284 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
285 return writeString("0");
a61af66fc99e Initial load
duke
parents:
diff changeset
286 }
a61af66fc99e Initial load
duke
parents:
diff changeset
287 }
a61af66fc99e Initial load
duke
parents:
diff changeset
288
a61af66fc99e Initial load
duke
parents:
diff changeset
289 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
290 IOBuf::writeAddress(void* val) {
a61af66fc99e Initial load
duke
parents:
diff changeset
291 char buf[128];
a61af66fc99e Initial load
duke
parents:
diff changeset
292 sprintf(buf, INTPTR_FORMAT, val);
a61af66fc99e Initial load
duke
parents:
diff changeset
293 return writeString(buf);
a61af66fc99e Initial load
duke
parents:
diff changeset
294 }
a61af66fc99e Initial load
duke
parents:
diff changeset
295
a61af66fc99e Initial load
duke
parents:
diff changeset
296 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
297 IOBuf::writeSpace() {
a61af66fc99e Initial load
duke
parents:
diff changeset
298 return writeString(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
299 }
a61af66fc99e Initial load
duke
parents:
diff changeset
300
a61af66fc99e Initial load
duke
parents:
diff changeset
301 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
302 IOBuf::writeEOL() {
a61af66fc99e Initial load
duke
parents:
diff changeset
303 return writeString("\n\r");
a61af66fc99e Initial load
duke
parents:
diff changeset
304 }
a61af66fc99e Initial load
duke
parents:
diff changeset
305
a61af66fc99e Initial load
duke
parents:
diff changeset
306 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
307 IOBuf::writeBinChar(char c) {
a61af66fc99e Initial load
duke
parents:
diff changeset
308 return writeBinBuf((char*) &c, sizeof(c));
a61af66fc99e Initial load
duke
parents:
diff changeset
309 }
a61af66fc99e Initial load
duke
parents:
diff changeset
310
a61af66fc99e Initial load
duke
parents:
diff changeset
311 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
312 IOBuf::writeBinUnsignedShort(unsigned short i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
313 i = htons(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
314 return writeBinBuf((char*) &i, sizeof(i));
a61af66fc99e Initial load
duke
parents:
diff changeset
315 }
a61af66fc99e Initial load
duke
parents:
diff changeset
316
a61af66fc99e Initial load
duke
parents:
diff changeset
317 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
318 IOBuf::writeBinUnsignedInt(unsigned int i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
319 i = htonl(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
320 return writeBinBuf((char*) &i, sizeof(i));
a61af66fc99e Initial load
duke
parents:
diff changeset
321 }
a61af66fc99e Initial load
duke
parents:
diff changeset
322
a61af66fc99e Initial load
duke
parents:
diff changeset
323 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
324 IOBuf::writeBinBuf(char* buf, int size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
325 while (size > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
326 int spaceRemaining = outBuf->remaining();
a61af66fc99e Initial load
duke
parents:
diff changeset
327 if (spaceRemaining == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
328 if (!flushImpl(true)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
329 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
330 }
a61af66fc99e Initial load
duke
parents:
diff changeset
331 spaceRemaining = outBuf->remaining();
a61af66fc99e Initial load
duke
parents:
diff changeset
332 }
a61af66fc99e Initial load
duke
parents:
diff changeset
333 int toCopy = (size > spaceRemaining) ? spaceRemaining : size;
a61af66fc99e Initial load
duke
parents:
diff changeset
334 memcpy(outBuf->fillPos(), buf, toCopy);
a61af66fc99e Initial load
duke
parents:
diff changeset
335 outBuf->incrFillPos(toCopy);
a61af66fc99e Initial load
duke
parents:
diff changeset
336 buf += toCopy;
a61af66fc99e Initial load
duke
parents:
diff changeset
337 size -= toCopy;
a61af66fc99e Initial load
duke
parents:
diff changeset
338 if (size > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
339 if (!flushImpl(true)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
340 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
341 }
a61af66fc99e Initial load
duke
parents:
diff changeset
342 }
a61af66fc99e Initial load
duke
parents:
diff changeset
343 }
a61af66fc99e Initial load
duke
parents:
diff changeset
344 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
345 }
a61af66fc99e Initial load
duke
parents:
diff changeset
346
a61af66fc99e Initial load
duke
parents:
diff changeset
347 #ifdef WIN32
a61af66fc99e Initial load
duke
parents:
diff changeset
348 IOBuf::FillState
a61af66fc99e Initial load
duke
parents:
diff changeset
349 IOBuf::fillFromFileHandle(HANDLE fh, DWORD* numBytesRead) {
a61af66fc99e Initial load
duke
parents:
diff changeset
350 int totalToRead;
a61af66fc99e Initial load
duke
parents:
diff changeset
351 char moreToCome;
a61af66fc99e Initial load
duke
parents:
diff changeset
352
a61af66fc99e Initial load
duke
parents:
diff changeset
353 outBuf->compact();
a61af66fc99e Initial load
duke
parents:
diff changeset
354
a61af66fc99e Initial load
duke
parents:
diff changeset
355 DWORD numRead;
a61af66fc99e Initial load
duke
parents:
diff changeset
356 if (!ReadFile(fh, &totalToRead, sizeof(int), &numRead, NULL)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
357 return FAILED;
a61af66fc99e Initial load
duke
parents:
diff changeset
358 }
a61af66fc99e Initial load
duke
parents:
diff changeset
359 if (numRead != sizeof(int)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
360 return FAILED;
a61af66fc99e Initial load
duke
parents:
diff changeset
361 }
a61af66fc99e Initial load
duke
parents:
diff changeset
362 if (!ReadFile(fh, &moreToCome, 1, &numRead, NULL)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
363 return FAILED;
a61af66fc99e Initial load
duke
parents:
diff changeset
364 }
a61af66fc99e Initial load
duke
parents:
diff changeset
365 if (numRead != 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
366 return FAILED;
a61af66fc99e Initial load
duke
parents:
diff changeset
367 }
a61af66fc99e Initial load
duke
parents:
diff changeset
368 if (outBuf->remaining() < totalToRead) {
a61af66fc99e Initial load
duke
parents:
diff changeset
369 return FAILED;
a61af66fc99e Initial load
duke
parents:
diff changeset
370 }
a61af66fc99e Initial load
duke
parents:
diff changeset
371
a61af66fc99e Initial load
duke
parents:
diff changeset
372 int tmp = totalToRead;
a61af66fc99e Initial load
duke
parents:
diff changeset
373
a61af66fc99e Initial load
duke
parents:
diff changeset
374 while (totalToRead > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
375 if (!ReadFile(fh, outBuf->fillPos(), totalToRead, &numRead, NULL)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
376 return FAILED;
a61af66fc99e Initial load
duke
parents:
diff changeset
377 }
a61af66fc99e Initial load
duke
parents:
diff changeset
378 outBuf->incrFillPos((int) numRead);
a61af66fc99e Initial load
duke
parents:
diff changeset
379 totalToRead -= numRead;
a61af66fc99e Initial load
duke
parents:
diff changeset
380 }
a61af66fc99e Initial load
duke
parents:
diff changeset
381
a61af66fc99e Initial load
duke
parents:
diff changeset
382 *numBytesRead = tmp;
a61af66fc99e Initial load
duke
parents:
diff changeset
383 return ((moreToCome == 0) ? DONE : MORE_DATA_PENDING);
a61af66fc99e Initial load
duke
parents:
diff changeset
384 }
a61af66fc99e Initial load
duke
parents:
diff changeset
385 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
386
a61af66fc99e Initial load
duke
parents:
diff changeset
387 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
388 IOBuf::isBinEscapeChar(char c) {
a61af66fc99e Initial load
duke
parents:
diff changeset
389 return (c == '|');
a61af66fc99e Initial load
duke
parents:
diff changeset
390 }
a61af66fc99e Initial load
duke
parents:
diff changeset
391
a61af66fc99e Initial load
duke
parents:
diff changeset
392 IOBuf::Action
a61af66fc99e Initial load
duke
parents:
diff changeset
393 IOBuf::processChar(char c) {
a61af66fc99e Initial load
duke
parents:
diff changeset
394 Action action = NO_ACTION;
a61af66fc99e Initial load
duke
parents:
diff changeset
395 switch (state) {
a61af66fc99e Initial load
duke
parents:
diff changeset
396 case TEXT_STATE: {
a61af66fc99e Initial load
duke
parents:
diff changeset
397 // Looking for text char, bin escape char, or EOL
a61af66fc99e Initial load
duke
parents:
diff changeset
398 if (isBinEscapeChar(c)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
399 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
400 fprintf(stderr, "[a: '%c'] ", inBuf[0]);
a61af66fc99e Initial load
duke
parents:
diff changeset
401 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
402 binPos = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
403 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
404 fprintf(stderr, "[b: '%c'] ", inBuf[0]);
a61af66fc99e Initial load
duke
parents:
diff changeset
405 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
406 binLength = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
407 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
408 fprintf(stderr, "[c: '%c'] ", inBuf[0]);
a61af66fc99e Initial load
duke
parents:
diff changeset
409 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
410 state = BIN_STATE;
a61af66fc99e Initial load
duke
parents:
diff changeset
411 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
412 fprintf(stderr, "[d: '%c'] ", inBuf[0]);
a61af66fc99e Initial load
duke
parents:
diff changeset
413 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
414 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
415 fprintf(stderr, "\nSwitching to BIN_STATE\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
416 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
417 } else if (isEOL(c)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
418 state = EOL_STATE;
a61af66fc99e Initial load
duke
parents:
diff changeset
419 action = GOT_LINE;
a61af66fc99e Initial load
duke
parents:
diff changeset
420 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
421 fprintf(stderr, "\nSwitching to EOL_STATE (GOT_LINE)\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
422 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
423 }
a61af66fc99e Initial load
duke
parents:
diff changeset
424 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
425 else {
a61af66fc99e Initial load
duke
parents:
diff changeset
426 fprintf(stderr, "'%c' ", c);
a61af66fc99e Initial load
duke
parents:
diff changeset
427 fflush(stderr);
a61af66fc99e Initial load
duke
parents:
diff changeset
428 }
a61af66fc99e Initial load
duke
parents:
diff changeset
429 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
430 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
431 }
a61af66fc99e Initial load
duke
parents:
diff changeset
432
a61af66fc99e Initial load
duke
parents:
diff changeset
433 case BIN_STATE: {
a61af66fc99e Initial load
duke
parents:
diff changeset
434 // Seeking to finish read of input
a61af66fc99e Initial load
duke
parents:
diff changeset
435 if (binPos < 4) {
a61af66fc99e Initial load
duke
parents:
diff changeset
436 int cur = c & 0xFF;
a61af66fc99e Initial load
duke
parents:
diff changeset
437 binLength <<= 8;
a61af66fc99e Initial load
duke
parents:
diff changeset
438 binLength |= cur;
a61af66fc99e Initial load
duke
parents:
diff changeset
439 ++binPos;
a61af66fc99e Initial load
duke
parents:
diff changeset
440 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
441 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
442 fprintf(stderr, "Reading binary byte %d of %d\n",
a61af66fc99e Initial load
duke
parents:
diff changeset
443 binPos - 4, binLength);
a61af66fc99e Initial load
duke
parents:
diff changeset
444 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
445 ++binPos;
a61af66fc99e Initial load
duke
parents:
diff changeset
446 if (binPos == 4 + binLength) {
a61af66fc99e Initial load
duke
parents:
diff changeset
447 state = TEXT_STATE;
a61af66fc99e Initial load
duke
parents:
diff changeset
448 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
449 fprintf(stderr, "Switching to TEXT_STATE\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
450 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
451 }
a61af66fc99e Initial load
duke
parents:
diff changeset
452 }
a61af66fc99e Initial load
duke
parents:
diff changeset
453 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
454 }
a61af66fc99e Initial load
duke
parents:
diff changeset
455
a61af66fc99e Initial load
duke
parents:
diff changeset
456 case EOL_STATE: {
a61af66fc99e Initial load
duke
parents:
diff changeset
457 // More EOL characters just cause us to re-enter this state
a61af66fc99e Initial load
duke
parents:
diff changeset
458 if (isEOL(c)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
459 action = SKIP_EOL_CHAR;
a61af66fc99e Initial load
duke
parents:
diff changeset
460 } else if (isBinEscapeChar(c)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
461 binPos = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
462 binLength = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
463 state = BIN_STATE;
a61af66fc99e Initial load
duke
parents:
diff changeset
464 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
465 state = TEXT_STATE;
a61af66fc99e Initial load
duke
parents:
diff changeset
466 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
467 fprintf(stderr, "'%c' ", c);
a61af66fc99e Initial load
duke
parents:
diff changeset
468 fflush(stderr);
a61af66fc99e Initial load
duke
parents:
diff changeset
469 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
470 }
a61af66fc99e Initial load
duke
parents:
diff changeset
471 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
472 }
a61af66fc99e Initial load
duke
parents:
diff changeset
473
a61af66fc99e Initial load
duke
parents:
diff changeset
474 } // switch
a61af66fc99e Initial load
duke
parents:
diff changeset
475
a61af66fc99e Initial load
duke
parents:
diff changeset
476 return action;
a61af66fc99e Initial load
duke
parents:
diff changeset
477 }
a61af66fc99e Initial load
duke
parents:
diff changeset
478
a61af66fc99e Initial load
duke
parents:
diff changeset
479
a61af66fc99e Initial load
duke
parents:
diff changeset
480 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
481 IOBuf::isEOL(char c) {
a61af66fc99e Initial load
duke
parents:
diff changeset
482 #ifdef WIN32
a61af66fc99e Initial load
duke
parents:
diff changeset
483 return ((c == '\n') || (c == '\r'));
a61af66fc99e Initial load
duke
parents:
diff changeset
484 #elif defined(__sun)
a61af66fc99e Initial load
duke
parents:
diff changeset
485 return c == '\n';
a61af66fc99e Initial load
duke
parents:
diff changeset
486 #else
a61af66fc99e Initial load
duke
parents:
diff changeset
487 #error Please port isEOL() to your platform
a61af66fc99e Initial load
duke
parents:
diff changeset
488 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
489 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
490 }