From 7f90205cf2f06a461b1642f8a6aca63f234e659c Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Tue, 19 May 2020 20:45:22 +0200 Subject: [PATCH] Add stream avail() methods Makes it more readable to write code that needs to know how much data/space is available in a stream. --- common/rdr/FdInStream.cxx | 2 +- common/rdr/FdOutStream.cxx | 4 ++-- common/rdr/FileInStream.cxx | 2 +- common/rdr/HexInStream.cxx | 4 ++-- common/rdr/HexOutStream.cxx | 2 +- common/rdr/InStream.h | 12 ++++++++++-- common/rdr/OutStream.h | 12 ++++++++++-- common/rdr/RandomStream.cxx | 2 +- common/rdr/TLSInStream.cxx | 2 +- common/rdr/TLSOutStream.cxx | 2 +- common/rdr/ZlibInStream.cxx | 6 +++--- common/rdr/ZlibOutStream.cxx | 6 +++--- common/rfb/JpegCompressor.cxx | 4 ++-- 13 files changed, 38 insertions(+), 22 deletions(-) diff --git a/common/rdr/FdInStream.cxx b/common/rdr/FdInStream.cxx index ceb95c9..eb76ec0 100644 --- a/common/rdr/FdInStream.cxx +++ b/common/rdr/FdInStream.cxx @@ -126,7 +126,7 @@ size_t FdInStream::overrun(size_t itemSize, size_t nItems, bool wait) } size_t nAvail; - nAvail = (end - ptr) / itemSize; + nAvail = avail() / itemSize; if (nAvail < nItems) return nAvail; diff --git a/common/rdr/FdOutStream.cxx b/common/rdr/FdOutStream.cxx index f5d07e4..04c6851 100644 --- a/common/rdr/FdOutStream.cxx +++ b/common/rdr/FdOutStream.cxx @@ -129,7 +129,7 @@ size_t FdOutStream::overrun(size_t itemSize, size_t nItems) flush(); // Still not enough space? - if (itemSize > (size_t)(end - ptr)) { + if (itemSize > avail()) { // Can we shuffle things around? // (don't do this if it gains us less than 25%) if (((size_t)(sentUpTo - start) > bufSize / 4) && @@ -150,7 +150,7 @@ size_t FdOutStream::overrun(size_t itemSize, size_t nItems) } size_t nAvail; - nAvail = (end - ptr) / itemSize; + nAvail = avail() / itemSize; if (nAvail < nItems) return nAvail; diff --git a/common/rdr/FileInStream.cxx b/common/rdr/FileInStream.cxx index bdb05a3..8344fcd 100644 --- a/common/rdr/FileInStream.cxx +++ b/common/rdr/FileInStream.cxx @@ -81,7 +81,7 @@ size_t FileInStream::overrun(size_t itemSize, size_t nItems, bool wait) } size_t nAvail; - nAvail = (end - ptr) / itemSize; + nAvail = avail() / itemSize; if (nAvail < nItems) return nAvail; diff --git a/common/rdr/HexInStream.cxx b/common/rdr/HexInStream.cxx index a6bc92c..787edc0 100644 --- a/common/rdr/HexInStream.cxx +++ b/common/rdr/HexInStream.cxx @@ -91,7 +91,7 @@ size_t HexInStream::overrun(size_t itemSize, size_t nItems, bool wait) { offset += ptr - start; ptr = start; - while ((size_t)(end - ptr) < itemSize) { + while (avail() < itemSize) { size_t n = in_stream.check(2, 1, wait); if (n == 0) return 0; const U8* iptr = in_stream.getptr(); @@ -111,7 +111,7 @@ size_t HexInStream::overrun(size_t itemSize, size_t nItems, bool wait) { } size_t nAvail; - nAvail = (end - ptr) / itemSize; + nAvail = avail() / itemSize; if (nAvail < nItems) return nAvail; diff --git a/common/rdr/HexOutStream.cxx b/common/rdr/HexOutStream.cxx index eac2eff..6118c13 100644 --- a/common/rdr/HexOutStream.cxx +++ b/common/rdr/HexOutStream.cxx @@ -103,7 +103,7 @@ HexOutStream::overrun(size_t itemSize, size_t nItems) { writeBuffer(); size_t nAvail; - nAvail = (end - ptr) / itemSize; + nAvail = avail() / itemSize; if (nAvail < nItems) return nAvail; diff --git a/common/rdr/InStream.h b/common/rdr/InStream.h index a8e515d..f6b6df5 100644 --- a/common/rdr/InStream.h +++ b/common/rdr/InStream.h @@ -35,6 +35,14 @@ namespace rdr { virtual ~InStream() {} + // avail() returns the number of bytes that are currenctly directly + // available from the stream. + + inline size_t avail() + { + return end - ptr; + } + // check() ensures there is buffer data for at least one item of size // itemSize bytes. Returns the number of items in the buffer (up to a // maximum of nItems). If wait is false, then instead of blocking to wait @@ -48,11 +56,11 @@ namespace rdr { if (itemSize == 0 || nItems == 0) return 0; - if (itemSize > (size_t)(end - ptr)) + if (itemSize > avail()) return overrun(itemSize, nItems, wait); // itemSize cannot be zero at this point - nAvail = (end - ptr) / itemSize; + nAvail = avail() / itemSize; if (nAvail < nItems) return nAvail; diff --git a/common/rdr/OutStream.h b/common/rdr/OutStream.h index 0f60ccc..d7b3a6a 100644 --- a/common/rdr/OutStream.h +++ b/common/rdr/OutStream.h @@ -40,6 +40,14 @@ namespace rdr { virtual ~OutStream() {} + // avail() returns the number of bytes that currently be written to the + // stream without any risk of blocking. + + inline size_t avail() + { + return end - ptr; + } + // check() ensures there is buffer space for at least one item of size // itemSize bytes. Returns the number of items which fit (up to a maximum // of nItems). @@ -48,10 +56,10 @@ namespace rdr { { size_t nAvail; - if (itemSize > (size_t)(end - ptr)) + if (itemSize > avail()) return overrun(itemSize, nItems); - nAvail = (end - ptr) / itemSize; + nAvail = avail() / itemSize; if (nAvail < nItems) return nAvail; diff --git a/common/rdr/RandomStream.cxx b/common/rdr/RandomStream.cxx index 6c64ac5..50abbde 100644 --- a/common/rdr/RandomStream.cxx +++ b/common/rdr/RandomStream.cxx @@ -124,7 +124,7 @@ size_t RandomStream::overrun(size_t itemSize, size_t nItems, bool wait) { } size_t nAvail; - nAvail = (end - ptr) / itemSize; + nAvail = avail() / itemSize; if (nAvail < nItems) return nAvail; diff --git a/common/rdr/TLSInStream.cxx b/common/rdr/TLSInStream.cxx index cd81f22..15e2a47 100644 --- a/common/rdr/TLSInStream.cxx +++ b/common/rdr/TLSInStream.cxx @@ -100,7 +100,7 @@ size_t TLSInStream::overrun(size_t itemSize, size_t nItems, bool wait) } size_t nAvail; - nAvail = (end - ptr) / itemSize; + nAvail = avail() / itemSize; if (nAvail < nItems) return nAvail; diff --git a/common/rdr/TLSOutStream.cxx b/common/rdr/TLSOutStream.cxx index 7d7c3b5..089aa66 100644 --- a/common/rdr/TLSOutStream.cxx +++ b/common/rdr/TLSOutStream.cxx @@ -101,7 +101,7 @@ size_t TLSOutStream::overrun(size_t itemSize, size_t nItems) flush(); size_t nAvail; - nAvail = (end - ptr) / itemSize; + nAvail = avail() / itemSize; if (nAvail < nItems) return nAvail; diff --git a/common/rdr/ZlibInStream.cxx b/common/rdr/ZlibInStream.cxx index 0fb3ad1..839cf0d 100644 --- a/common/rdr/ZlibInStream.cxx +++ b/common/rdr/ZlibInStream.cxx @@ -108,13 +108,13 @@ size_t ZlibInStream::overrun(size_t itemSize, size_t nItems, bool wait) end -= ptr - start; ptr = start; - while ((size_t)(end - ptr) < itemSize) { + while (avail() < itemSize) { if (!decompress(wait)) return 0; } size_t nAvail; - nAvail = (end - ptr) / itemSize; + nAvail = avail() / itemSize; if (nAvail < nItems) return nAvail; @@ -136,7 +136,7 @@ bool ZlibInStream::decompress(bool wait) size_t n = underlying->check(1, 1, wait); if (n == 0) return false; zs->next_in = (U8*)underlying->getptr(); - zs->avail_in = underlying->getend() - underlying->getptr(); + zs->avail_in = underlying->avail(); if (zs->avail_in > bytesIn) zs->avail_in = bytesIn; diff --git a/common/rdr/ZlibOutStream.cxx b/common/rdr/ZlibOutStream.cxx index 1cccb2b..fac08bd 100644 --- a/common/rdr/ZlibOutStream.cxx +++ b/common/rdr/ZlibOutStream.cxx @@ -106,7 +106,7 @@ size_t ZlibOutStream::overrun(size_t itemSize, size_t nItems) checkCompressionLevel(); - while ((size_t)(end - ptr) < itemSize) { + while (avail() < itemSize) { zs->next_in = start; zs->avail_in = ptr - start; @@ -128,7 +128,7 @@ size_t ZlibOutStream::overrun(size_t itemSize, size_t nItems) } size_t nAvail; - nAvail = (end - ptr) / itemSize; + nAvail = avail() / itemSize; if (nAvail < nItems) return nAvail; @@ -148,7 +148,7 @@ void ZlibOutStream::deflate(int flush) do { underlying->check(1); zs->next_out = underlying->getptr(); - zs->avail_out = underlying->getend() - underlying->getptr(); + zs->avail_out = underlying->avail(); #ifdef ZLIBOUT_DEBUG fprintf(stderr,"zos: calling deflate, avail_in %d, avail_out %d\n", diff --git a/common/rfb/JpegCompressor.cxx b/common/rfb/JpegCompressor.cxx index 27cb9de..9a5f8d2 100644 --- a/common/rfb/JpegCompressor.cxx +++ b/common/rfb/JpegCompressor.cxx @@ -85,7 +85,7 @@ JpegInitDestination(j_compress_ptr cinfo) jc->clear(); dest->pub.next_output_byte = jc->getptr(); - dest->pub.free_in_buffer = jc->getend() - jc->getptr(); + dest->pub.free_in_buffer = jc->avail(); } static boolean @@ -97,7 +97,7 @@ JpegEmptyOutputBuffer(j_compress_ptr cinfo) jc->setptr(jc->getend()); jc->overrun(jc->getend() - jc->getstart(), 1); dest->pub.next_output_byte = jc->getptr(); - dest->pub.free_in_buffer = jc->getend() - jc->getptr(); + dest->pub.free_in_buffer = jc->avail(); return TRUE; }