You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BufferedInStream.cxx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2020 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <rdr/BufferedInStream.h>
  23. #include <rdr/Exception.h>
  24. using namespace rdr;
  25. static const size_t DEFAULT_BUF_SIZE = 8192;
  26. static const size_t MAX_BUF_SIZE = 4 * 1024 * 1024;
  27. BufferedInStream::BufferedInStream()
  28. : bufSize(DEFAULT_BUF_SIZE), offset(0)
  29. {
  30. ptr = end = start = new U8[bufSize];
  31. gettimeofday(&lastSizeCheck, NULL);
  32. peakUsage = 0;
  33. }
  34. BufferedInStream::~BufferedInStream()
  35. {
  36. delete [] start;
  37. }
  38. size_t BufferedInStream::pos()
  39. {
  40. return offset + ptr - start;
  41. }
  42. bool BufferedInStream::overrun(size_t needed)
  43. {
  44. struct timeval now;
  45. if (needed > bufSize) {
  46. size_t newSize;
  47. U8* newBuffer;
  48. if (needed > MAX_BUF_SIZE)
  49. throw Exception("BufferedInStream overrun: requested size of "
  50. "%lu bytes exceeds maximum of %lu bytes",
  51. (long unsigned)needed, (long unsigned)MAX_BUF_SIZE);
  52. newSize = DEFAULT_BUF_SIZE;
  53. while (newSize < needed)
  54. newSize *= 2;
  55. newBuffer = new U8[newSize];
  56. memcpy(newBuffer, ptr, end - ptr);
  57. delete [] start;
  58. bufSize = newSize;
  59. offset += ptr - start;
  60. end = newBuffer + (end - ptr);
  61. ptr = start = newBuffer;
  62. gettimeofday(&lastSizeCheck, NULL);
  63. peakUsage = needed;
  64. }
  65. if (needed > peakUsage)
  66. peakUsage = needed;
  67. // Time to shrink an excessive buffer?
  68. gettimeofday(&now, NULL);
  69. if ((avail() == 0) && (bufSize > DEFAULT_BUF_SIZE) &&
  70. ((now.tv_sec < lastSizeCheck.tv_sec) ||
  71. (now.tv_sec > (lastSizeCheck.tv_sec + 5)))) {
  72. if (peakUsage < (bufSize / 2)) {
  73. size_t newSize;
  74. newSize = DEFAULT_BUF_SIZE;
  75. while (newSize < peakUsage)
  76. newSize *= 2;
  77. // We know the buffer is empty, so just reset everything
  78. delete [] start;
  79. ptr = end = start = new U8[newSize];
  80. bufSize = newSize;
  81. }
  82. gettimeofday(&lastSizeCheck, NULL);
  83. peakUsage = needed;
  84. }
  85. // Do we need to shuffle things around?
  86. if ((bufSize - (ptr - start)) < needed) {
  87. memmove(start, ptr, end - ptr);
  88. offset += ptr - start;
  89. end -= ptr - start;
  90. ptr = start;
  91. }
  92. while (avail() < needed) {
  93. if (!fillBuffer(start + bufSize - end))
  94. return false;
  95. }
  96. return true;
  97. }