annotate win32-mingw/include/kj/io.h @ 83:ae30d91d2ffe

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam
date Fri, 07 Feb 2020 11:51:13 +0000
parents eccd51b72864
children
rev   line source
Chris@64 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@64 2 // Licensed under the MIT License:
Chris@64 3 //
Chris@64 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@64 5 // of this software and associated documentation files (the "Software"), to deal
Chris@64 6 // in the Software without restriction, including without limitation the rights
Chris@64 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@64 8 // copies of the Software, and to permit persons to whom the Software is
Chris@64 9 // furnished to do so, subject to the following conditions:
Chris@64 10 //
Chris@64 11 // The above copyright notice and this permission notice shall be included in
Chris@64 12 // all copies or substantial portions of the Software.
Chris@64 13 //
Chris@64 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@64 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@64 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@64 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@64 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@64 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@64 20 // THE SOFTWARE.
Chris@64 21
Chris@64 22 #ifndef KJ_IO_H_
Chris@64 23 #define KJ_IO_H_
Chris@64 24
Chris@64 25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
Chris@64 26 #pragma GCC system_header
Chris@64 27 #endif
Chris@64 28
Chris@64 29 #include <stddef.h>
Chris@64 30 #include "common.h"
Chris@64 31 #include "array.h"
Chris@64 32 #include "exception.h"
Chris@64 33
Chris@64 34 namespace kj {
Chris@64 35
Chris@64 36 // =======================================================================================
Chris@64 37 // Abstract interfaces
Chris@64 38
Chris@64 39 class InputStream {
Chris@64 40 public:
Chris@64 41 virtual ~InputStream() noexcept(false);
Chris@64 42
Chris@64 43 size_t read(void* buffer, size_t minBytes, size_t maxBytes);
Chris@64 44 // Reads at least minBytes and at most maxBytes, copying them into the given buffer. Returns
Chris@64 45 // the size read. Throws an exception on errors. Implemented in terms of tryRead().
Chris@64 46 //
Chris@64 47 // maxBytes is the number of bytes the caller really wants, but minBytes is the minimum amount
Chris@64 48 // needed by the caller before it can start doing useful processing. If the stream returns less
Chris@64 49 // than maxBytes, the caller will usually call read() again later to get the rest. Returning
Chris@64 50 // less than maxBytes is useful when it makes sense for the caller to parallelize processing
Chris@64 51 // with I/O.
Chris@64 52 //
Chris@64 53 // Never blocks if minBytes is zero. If minBytes is zero and maxBytes is non-zero, this may
Chris@64 54 // attempt a non-blocking read or may just return zero. To force a read, use a non-zero minBytes.
Chris@64 55 // To detect EOF without throwing an exception, use tryRead().
Chris@64 56 //
Chris@64 57 // If the InputStream can't produce minBytes, it MUST throw an exception, as the caller is not
Chris@64 58 // expected to understand how to deal with partial reads.
Chris@64 59
Chris@64 60 virtual size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) = 0;
Chris@64 61 // Like read(), but may return fewer than minBytes on EOF.
Chris@64 62
Chris@64 63 inline void read(void* buffer, size_t bytes) { read(buffer, bytes, bytes); }
Chris@64 64 // Convenience method for reading an exact number of bytes.
Chris@64 65
Chris@64 66 virtual void skip(size_t bytes);
Chris@64 67 // Skips past the given number of bytes, discarding them. The default implementation read()s
Chris@64 68 // into a scratch buffer.
Chris@64 69 };
Chris@64 70
Chris@64 71 class OutputStream {
Chris@64 72 public:
Chris@64 73 virtual ~OutputStream() noexcept(false);
Chris@64 74
Chris@64 75 virtual void write(const void* buffer, size_t size) = 0;
Chris@64 76 // Always writes the full size. Throws exception on error.
Chris@64 77
Chris@64 78 virtual void write(ArrayPtr<const ArrayPtr<const byte>> pieces);
Chris@64 79 // Equivalent to write()ing each byte array in sequence, which is what the default implementation
Chris@64 80 // does. Override if you can do something better, e.g. use writev() to do the write in a single
Chris@64 81 // syscall.
Chris@64 82 };
Chris@64 83
Chris@64 84 class BufferedInputStream: public InputStream {
Chris@64 85 // An input stream which buffers some bytes in memory to reduce system call overhead.
Chris@64 86 // - OR -
Chris@64 87 // An input stream that actually reads from some in-memory data structure and wants to give its
Chris@64 88 // caller a direct pointer to that memory to potentially avoid a copy.
Chris@64 89
Chris@64 90 public:
Chris@64 91 virtual ~BufferedInputStream() noexcept(false);
Chris@64 92
Chris@64 93 ArrayPtr<const byte> getReadBuffer();
Chris@64 94 // Get a direct pointer into the read buffer, which contains the next bytes in the input. If the
Chris@64 95 // caller consumes any bytes, it should then call skip() to indicate this. This always returns a
Chris@64 96 // non-empty buffer or throws an exception. Implemented in terms of tryGetReadBuffer().
Chris@64 97
Chris@64 98 virtual ArrayPtr<const byte> tryGetReadBuffer() = 0;
Chris@64 99 // Like getReadBuffer() but may return an empty buffer on EOF.
Chris@64 100 };
Chris@64 101
Chris@64 102 class BufferedOutputStream: public OutputStream {
Chris@64 103 // An output stream which buffers some bytes in memory to reduce system call overhead.
Chris@64 104 // - OR -
Chris@64 105 // An output stream that actually writes into some in-memory data structure and wants to give its
Chris@64 106 // caller a direct pointer to that memory to potentially avoid a copy.
Chris@64 107
Chris@64 108 public:
Chris@64 109 virtual ~BufferedOutputStream() noexcept(false);
Chris@64 110
Chris@64 111 virtual ArrayPtr<byte> getWriteBuffer() = 0;
Chris@64 112 // Get a direct pointer into the write buffer. The caller may choose to fill in some prefix of
Chris@64 113 // this buffer and then pass it to write(), in which case write() may avoid a copy. It is
Chris@64 114 // incorrect to pass to write any slice of this buffer which is not a prefix.
Chris@64 115 };
Chris@64 116
Chris@64 117 // =======================================================================================
Chris@64 118 // Buffered streams implemented as wrappers around regular streams
Chris@64 119
Chris@64 120 class BufferedInputStreamWrapper: public BufferedInputStream {
Chris@64 121 // Implements BufferedInputStream in terms of an InputStream.
Chris@64 122 //
Chris@64 123 // Note that the underlying stream's position is unpredictable once the wrapper is destroyed,
Chris@64 124 // unless the entire stream was consumed. To read a predictable number of bytes in a buffered
Chris@64 125 // way without going over, you'd need this wrapper to wrap some other wrapper which itself
Chris@64 126 // implements an artificial EOF at the desired point. Such a stream should be trivial to write
Chris@64 127 // but is not provided by the library at this time.
Chris@64 128
Chris@64 129 public:
Chris@64 130 explicit BufferedInputStreamWrapper(InputStream& inner, ArrayPtr<byte> buffer = nullptr);
Chris@64 131 // Creates a buffered stream wrapping the given non-buffered stream. No guarantee is made about
Chris@64 132 // the position of the inner stream after a buffered wrapper has been created unless the entire
Chris@64 133 // input is read.
Chris@64 134 //
Chris@64 135 // If the second parameter is non-null, the stream uses the given buffer instead of allocating
Chris@64 136 // its own. This may improve performance if the buffer can be reused.
Chris@64 137
Chris@64 138 KJ_DISALLOW_COPY(BufferedInputStreamWrapper);
Chris@64 139 ~BufferedInputStreamWrapper() noexcept(false);
Chris@64 140
Chris@64 141 // implements BufferedInputStream ----------------------------------
Chris@64 142 ArrayPtr<const byte> tryGetReadBuffer() override;
Chris@64 143 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
Chris@64 144 void skip(size_t bytes) override;
Chris@64 145
Chris@64 146 private:
Chris@64 147 InputStream& inner;
Chris@64 148 Array<byte> ownedBuffer;
Chris@64 149 ArrayPtr<byte> buffer;
Chris@64 150 ArrayPtr<byte> bufferAvailable;
Chris@64 151 };
Chris@64 152
Chris@64 153 class BufferedOutputStreamWrapper: public BufferedOutputStream {
Chris@64 154 // Implements BufferedOutputStream in terms of an OutputStream. Note that writes to the
Chris@64 155 // underlying stream may be delayed until flush() is called or the wrapper is destroyed.
Chris@64 156
Chris@64 157 public:
Chris@64 158 explicit BufferedOutputStreamWrapper(OutputStream& inner, ArrayPtr<byte> buffer = nullptr);
Chris@64 159 // Creates a buffered stream wrapping the given non-buffered stream.
Chris@64 160 //
Chris@64 161 // If the second parameter is non-null, the stream uses the given buffer instead of allocating
Chris@64 162 // its own. This may improve performance if the buffer can be reused.
Chris@64 163
Chris@64 164 KJ_DISALLOW_COPY(BufferedOutputStreamWrapper);
Chris@64 165 ~BufferedOutputStreamWrapper() noexcept(false);
Chris@64 166
Chris@64 167 void flush();
Chris@64 168 // Force the wrapper to write any remaining bytes in its buffer to the inner stream. Note that
Chris@64 169 // this only flushes this object's buffer; this object has no idea how to flush any other buffers
Chris@64 170 // that may be present in the underlying stream.
Chris@64 171
Chris@64 172 // implements BufferedOutputStream ---------------------------------
Chris@64 173 ArrayPtr<byte> getWriteBuffer() override;
Chris@64 174 void write(const void* buffer, size_t size) override;
Chris@64 175
Chris@64 176 private:
Chris@64 177 OutputStream& inner;
Chris@64 178 Array<byte> ownedBuffer;
Chris@64 179 ArrayPtr<byte> buffer;
Chris@64 180 byte* bufferPos;
Chris@64 181 UnwindDetector unwindDetector;
Chris@64 182 };
Chris@64 183
Chris@64 184 // =======================================================================================
Chris@64 185 // Array I/O
Chris@64 186
Chris@64 187 class ArrayInputStream: public BufferedInputStream {
Chris@64 188 public:
Chris@64 189 explicit ArrayInputStream(ArrayPtr<const byte> array);
Chris@64 190 KJ_DISALLOW_COPY(ArrayInputStream);
Chris@64 191 ~ArrayInputStream() noexcept(false);
Chris@64 192
Chris@64 193 // implements BufferedInputStream ----------------------------------
Chris@64 194 ArrayPtr<const byte> tryGetReadBuffer() override;
Chris@64 195 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
Chris@64 196 void skip(size_t bytes) override;
Chris@64 197
Chris@64 198 private:
Chris@64 199 ArrayPtr<const byte> array;
Chris@64 200 };
Chris@64 201
Chris@64 202 class ArrayOutputStream: public BufferedOutputStream {
Chris@64 203 public:
Chris@64 204 explicit ArrayOutputStream(ArrayPtr<byte> array);
Chris@64 205 KJ_DISALLOW_COPY(ArrayOutputStream);
Chris@64 206 ~ArrayOutputStream() noexcept(false);
Chris@64 207
Chris@64 208 ArrayPtr<byte> getArray() {
Chris@64 209 // Get the portion of the array which has been filled in.
Chris@64 210 return arrayPtr(array.begin(), fillPos);
Chris@64 211 }
Chris@64 212
Chris@64 213 // implements BufferedInputStream ----------------------------------
Chris@64 214 ArrayPtr<byte> getWriteBuffer() override;
Chris@64 215 void write(const void* buffer, size_t size) override;
Chris@64 216
Chris@64 217 private:
Chris@64 218 ArrayPtr<byte> array;
Chris@64 219 byte* fillPos;
Chris@64 220 };
Chris@64 221
Chris@64 222 class VectorOutputStream: public BufferedOutputStream {
Chris@64 223 public:
Chris@64 224 explicit VectorOutputStream(size_t initialCapacity = 4096);
Chris@64 225 KJ_DISALLOW_COPY(VectorOutputStream);
Chris@64 226 ~VectorOutputStream() noexcept(false);
Chris@64 227
Chris@64 228 ArrayPtr<byte> getArray() {
Chris@64 229 // Get the portion of the array which has been filled in.
Chris@64 230 return arrayPtr(vector.begin(), fillPos);
Chris@64 231 }
Chris@64 232
Chris@64 233 // implements BufferedInputStream ----------------------------------
Chris@64 234 ArrayPtr<byte> getWriteBuffer() override;
Chris@64 235 void write(const void* buffer, size_t size) override;
Chris@64 236
Chris@64 237 private:
Chris@64 238 Array<byte> vector;
Chris@64 239 byte* fillPos;
Chris@64 240
Chris@64 241 void grow(size_t minSize);
Chris@64 242 };
Chris@64 243
Chris@64 244 // =======================================================================================
Chris@64 245 // File descriptor I/O
Chris@64 246
Chris@64 247 class AutoCloseFd {
Chris@64 248 // A wrapper around a file descriptor which automatically closes the descriptor when destroyed.
Chris@64 249 // The wrapper supports move construction for transferring ownership of the descriptor. If
Chris@64 250 // close() returns an error, the destructor throws an exception, UNLESS the destructor is being
Chris@64 251 // called during unwind from another exception, in which case the close error is ignored.
Chris@64 252 //
Chris@64 253 // If your code is not exception-safe, you should not use AutoCloseFd. In this case you will
Chris@64 254 // have to call close() yourself and handle errors appropriately.
Chris@64 255
Chris@64 256 public:
Chris@64 257 inline AutoCloseFd(): fd(-1) {}
Chris@64 258 inline AutoCloseFd(decltype(nullptr)): fd(-1) {}
Chris@64 259 inline explicit AutoCloseFd(int fd): fd(fd) {}
Chris@64 260 inline AutoCloseFd(AutoCloseFd&& other) noexcept: fd(other.fd) { other.fd = -1; }
Chris@64 261 KJ_DISALLOW_COPY(AutoCloseFd);
Chris@64 262 ~AutoCloseFd() noexcept(false);
Chris@64 263
Chris@64 264 inline AutoCloseFd& operator=(AutoCloseFd&& other) {
Chris@64 265 AutoCloseFd old(kj::mv(*this));
Chris@64 266 fd = other.fd;
Chris@64 267 other.fd = -1;
Chris@64 268 return *this;
Chris@64 269 }
Chris@64 270
Chris@64 271 inline AutoCloseFd& operator=(decltype(nullptr)) {
Chris@64 272 AutoCloseFd old(kj::mv(*this));
Chris@64 273 return *this;
Chris@64 274 }
Chris@64 275
Chris@64 276 inline operator int() const { return fd; }
Chris@64 277 inline int get() const { return fd; }
Chris@64 278
Chris@64 279 operator bool() const = delete;
Chris@64 280 // Deleting this operator prevents accidental use in boolean contexts, which
Chris@64 281 // the int conversion operator above would otherwise allow.
Chris@64 282
Chris@64 283 inline bool operator==(decltype(nullptr)) { return fd < 0; }
Chris@64 284 inline bool operator!=(decltype(nullptr)) { return fd >= 0; }
Chris@64 285
Chris@64 286 private:
Chris@64 287 int fd;
Chris@64 288 UnwindDetector unwindDetector;
Chris@64 289 };
Chris@64 290
Chris@64 291 inline auto KJ_STRINGIFY(const AutoCloseFd& fd)
Chris@64 292 -> decltype(kj::toCharSequence(implicitCast<int>(fd))) {
Chris@64 293 return kj::toCharSequence(implicitCast<int>(fd));
Chris@64 294 }
Chris@64 295
Chris@64 296 class FdInputStream: public InputStream {
Chris@64 297 // An InputStream wrapping a file descriptor.
Chris@64 298
Chris@64 299 public:
Chris@64 300 explicit FdInputStream(int fd): fd(fd) {}
Chris@64 301 explicit FdInputStream(AutoCloseFd fd): fd(fd), autoclose(mv(fd)) {}
Chris@64 302 KJ_DISALLOW_COPY(FdInputStream);
Chris@64 303 ~FdInputStream() noexcept(false);
Chris@64 304
Chris@64 305 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
Chris@64 306
Chris@64 307 inline int getFd() const { return fd; }
Chris@64 308
Chris@64 309 private:
Chris@64 310 int fd;
Chris@64 311 AutoCloseFd autoclose;
Chris@64 312 };
Chris@64 313
Chris@64 314 class FdOutputStream: public OutputStream {
Chris@64 315 // An OutputStream wrapping a file descriptor.
Chris@64 316
Chris@64 317 public:
Chris@64 318 explicit FdOutputStream(int fd): fd(fd) {}
Chris@64 319 explicit FdOutputStream(AutoCloseFd fd): fd(fd), autoclose(mv(fd)) {}
Chris@64 320 KJ_DISALLOW_COPY(FdOutputStream);
Chris@64 321 ~FdOutputStream() noexcept(false);
Chris@64 322
Chris@64 323 void write(const void* buffer, size_t size) override;
Chris@64 324 void write(ArrayPtr<const ArrayPtr<const byte>> pieces) override;
Chris@64 325
Chris@64 326 inline int getFd() const { return fd; }
Chris@64 327
Chris@64 328 private:
Chris@64 329 int fd;
Chris@64 330 AutoCloseFd autoclose;
Chris@64 331 };
Chris@64 332
Chris@64 333 // =======================================================================================
Chris@64 334 // Win32 Handle I/O
Chris@64 335
Chris@64 336 #ifdef _WIN32
Chris@64 337
Chris@64 338 class AutoCloseHandle {
Chris@64 339 // A wrapper around a Win32 HANDLE which automatically closes the handle when destroyed.
Chris@64 340 // The wrapper supports move construction for transferring ownership of the handle. If
Chris@64 341 // CloseHandle() returns an error, the destructor throws an exception, UNLESS the destructor is
Chris@64 342 // being called during unwind from another exception, in which case the close error is ignored.
Chris@64 343 //
Chris@64 344 // If your code is not exception-safe, you should not use AutoCloseHandle. In this case you will
Chris@64 345 // have to call close() yourself and handle errors appropriately.
Chris@64 346
Chris@64 347 public:
Chris@64 348 inline AutoCloseHandle(): handle((void*)-1) {}
Chris@64 349 inline AutoCloseHandle(decltype(nullptr)): handle((void*)-1) {}
Chris@64 350 inline explicit AutoCloseHandle(void* handle): handle(handle) {}
Chris@64 351 inline AutoCloseHandle(AutoCloseHandle&& other) noexcept: handle(other.handle) {
Chris@64 352 other.handle = (void*)-1;
Chris@64 353 }
Chris@64 354 KJ_DISALLOW_COPY(AutoCloseHandle);
Chris@64 355 ~AutoCloseHandle() noexcept(false);
Chris@64 356
Chris@64 357 inline AutoCloseHandle& operator=(AutoCloseHandle&& other) {
Chris@64 358 AutoCloseHandle old(kj::mv(*this));
Chris@64 359 handle = other.handle;
Chris@64 360 other.handle = (void*)-1;
Chris@64 361 return *this;
Chris@64 362 }
Chris@64 363
Chris@64 364 inline AutoCloseHandle& operator=(decltype(nullptr)) {
Chris@64 365 AutoCloseHandle old(kj::mv(*this));
Chris@64 366 return *this;
Chris@64 367 }
Chris@64 368
Chris@64 369 inline operator void*() const { return handle; }
Chris@64 370 inline void* get() const { return handle; }
Chris@64 371
Chris@64 372 operator bool() const = delete;
Chris@64 373 // Deleting this operator prevents accidental use in boolean contexts, which
Chris@64 374 // the void* conversion operator above would otherwise allow.
Chris@64 375
Chris@64 376 inline bool operator==(decltype(nullptr)) { return handle != (void*)-1; }
Chris@64 377 inline bool operator!=(decltype(nullptr)) { return handle == (void*)-1; }
Chris@64 378
Chris@64 379 private:
Chris@64 380 void* handle; // -1 (aka INVALID_HANDLE_VALUE) if not valid.
Chris@64 381 };
Chris@64 382
Chris@64 383 class HandleInputStream: public InputStream {
Chris@64 384 // An InputStream wrapping a Win32 HANDLE.
Chris@64 385
Chris@64 386 public:
Chris@64 387 explicit HandleInputStream(void* handle): handle(handle) {}
Chris@64 388 explicit HandleInputStream(AutoCloseHandle handle): handle(handle), autoclose(mv(handle)) {}
Chris@64 389 KJ_DISALLOW_COPY(HandleInputStream);
Chris@64 390 ~HandleInputStream() noexcept(false);
Chris@64 391
Chris@64 392 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
Chris@64 393
Chris@64 394 private:
Chris@64 395 void* handle;
Chris@64 396 AutoCloseHandle autoclose;
Chris@64 397 };
Chris@64 398
Chris@64 399 class HandleOutputStream: public OutputStream {
Chris@64 400 // An OutputStream wrapping a Win32 HANDLE.
Chris@64 401
Chris@64 402 public:
Chris@64 403 explicit HandleOutputStream(void* handle): handle(handle) {}
Chris@64 404 explicit HandleOutputStream(AutoCloseHandle handle): handle(handle), autoclose(mv(handle)) {}
Chris@64 405 KJ_DISALLOW_COPY(HandleOutputStream);
Chris@64 406 ~HandleOutputStream() noexcept(false);
Chris@64 407
Chris@64 408 void write(const void* buffer, size_t size) override;
Chris@64 409
Chris@64 410 private:
Chris@64 411 void* handle;
Chris@64 412 AutoCloseHandle autoclose;
Chris@64 413 };
Chris@64 414
Chris@64 415 #endif // _WIN32
Chris@64 416
Chris@64 417 } // namespace kj
Chris@64 418
Chris@64 419 #endif // KJ_IO_H_