aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPierre Ossman <ossman@cendio.se>2020-05-19 20:45:22 +0200
committerPierre Ossman <ossman@cendio.se>2020-05-21 11:34:22 +0200
commitb35ae29f932288b0529eb9eed21c31364a3489a4 (patch)
treed7c680494c170e6d9282dc51b2be1d05604f65ba
parentdc38699af81e8bb953946000121f0a48cf30b962 (diff)
downloadtigervnc-b35ae29f932288b0529eb9eed21c31364a3489a4.tar.gz
tigervnc-b35ae29f932288b0529eb9eed21c31364a3489a4.zip
Add stream avail() methods
Makes it more readable to write code that needs to know how much data/space is available in a stream.
-rw-r--r--common/rdr/FdInStream.cxx2
-rw-r--r--common/rdr/FdOutStream.cxx4
-rw-r--r--common/rdr/FileInStream.cxx2
-rw-r--r--common/rdr/HexInStream.cxx4
-rw-r--r--common/rdr/HexOutStream.cxx2
-rw-r--r--common/rdr/InStream.h12
-rw-r--r--common/rdr/OutStream.h12
-rw-r--r--common/rdr/RandomStream.cxx2
-rw-r--r--common/rdr/TLSInStream.cxx2
-rw-r--r--common/rdr/TLSOutStream.cxx2
-rw-r--r--common/rdr/ZlibInStream.cxx6
-rw-r--r--common/rdr/ZlibOutStream.cxx6
-rw-r--r--common/rfb/JpegCompressor.cxx4
-rw-r--r--tests/perf/decperf.cxx4
-rw-r--r--tests/perf/encperf.cxx4
15 files changed, 42 insertions, 26 deletions
diff --git a/common/rdr/FdInStream.cxx b/common/rdr/FdInStream.cxx
index ceb95c91..eb76ec06 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 f5d07e4b..04c6851a 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 bdb05a3a..8344fcd1 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 a6bc92cd..787edc04 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 eac2eff8..6118c136 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 f71a4d9e..665084f2 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
@@ -45,10 +53,10 @@ namespace rdr {
{
size_t nAvail;
- if (itemSize > (size_t)(end - ptr))
+ if (itemSize > avail())
return overrun(itemSize, nItems, wait);
- 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 6e9069c6..33381d9a 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 1be9b251..0290c24d 100644
--- a/common/rdr/RandomStream.cxx
+++ b/common/rdr/RandomStream.cxx
@@ -127,7 +127,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 cd81f224..15e2a471 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 7d7c3b56..089aa660 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 0fb3ad1e..839cf0d0 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 99d0617a..f102980a 100644
--- a/common/rdr/ZlibOutStream.cxx
+++ b/common/rdr/ZlibOutStream.cxx
@@ -109,7 +109,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;
@@ -131,7 +131,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;
@@ -151,7 +151,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
vlog.debug("calling deflate, avail_in %d, avail_out %d",
diff --git a/common/rfb/JpegCompressor.cxx b/common/rfb/JpegCompressor.cxx
index 23b8f8cf..a4af92fa 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;
}
diff --git a/tests/perf/decperf.cxx b/tests/perf/decperf.cxx
index a6a50899..687ac7a9 100644
--- a/tests/perf/decperf.cxx
+++ b/tests/perf/decperf.cxx
@@ -102,8 +102,8 @@ void DummyOutStream::flush()
size_t DummyOutStream::overrun(size_t itemSize, size_t nItems)
{
flush();
- if (itemSize * nItems > (size_t)(end - ptr))
- nItems = (end - ptr) / itemSize;
+ if (itemSize * nItems > avail())
+ nItems = avail() / itemSize;
return nItems;
}
diff --git a/tests/perf/encperf.cxx b/tests/perf/encperf.cxx
index 1b14151b..4665346f 100644
--- a/tests/perf/encperf.cxx
+++ b/tests/perf/encperf.cxx
@@ -159,8 +159,8 @@ void DummyOutStream::flush()
size_t DummyOutStream::overrun(size_t itemSize, size_t nItems)
{
flush();
- if (itemSize * nItems > (size_t)(end - ptr))
- nItems = (end - ptr) / itemSize;
+ if (itemSize * nItems > avail())
+ nItems = avail() / itemSize;
return nItems;
}