Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

BufferedInStream.cxx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <assert.h>
  23. #include <rdr/BufferedInStream.h>
  24. #include <rdr/Exception.h>
  25. using namespace rdr;
  26. static const size_t DEFAULT_BUF_SIZE = 8192;
  27. static const size_t MAX_BUF_SIZE = 32 * 1024 * 1024;
  28. BufferedInStream::BufferedInStream()
  29. : bufSize(DEFAULT_BUF_SIZE), offset(0)
  30. {
  31. ptr = end = start = new uint8_t[bufSize];
  32. gettimeofday(&lastSizeCheck, NULL);
  33. peakUsage = 0;
  34. }
  35. BufferedInStream::~BufferedInStream()
  36. {
  37. delete [] start;
  38. }
  39. size_t BufferedInStream::pos()
  40. {
  41. return offset + ptr - start;
  42. }
  43. void BufferedInStream::ensureSpace(size_t needed)
  44. {
  45. struct timeval now;
  46. // Given argument is how much free space is needed, but for allocation
  47. // purposes we need to now how much space everything needs, including
  48. // any existing data already in the buffer
  49. needed += avail();
  50. if (needed > bufSize) {
  51. size_t newSize;
  52. uint8_t* newBuffer;
  53. if (needed > MAX_BUF_SIZE)
  54. throw Exception("BufferedInStream overrun: requested size of "
  55. "%lu bytes exceeds maximum of %lu bytes",
  56. (long unsigned)needed, (long unsigned)MAX_BUF_SIZE);
  57. newSize = DEFAULT_BUF_SIZE;
  58. while (newSize < needed)
  59. newSize *= 2;
  60. newBuffer = new uint8_t[newSize];
  61. memcpy(newBuffer, ptr, end - ptr);
  62. delete [] start;
  63. bufSize = newSize;
  64. offset += ptr - start;
  65. end = newBuffer + (end - ptr);
  66. ptr = start = newBuffer;
  67. gettimeofday(&lastSizeCheck, NULL);
  68. peakUsage = needed;
  69. }
  70. if (needed > peakUsage)
  71. peakUsage = needed;
  72. // Time to shrink an excessive buffer?
  73. gettimeofday(&now, NULL);
  74. if ((avail() == 0) && (bufSize > DEFAULT_BUF_SIZE) &&
  75. ((now.tv_sec < lastSizeCheck.tv_sec) ||
  76. (now.tv_sec > (lastSizeCheck.tv_sec + 5)))) {
  77. if (peakUsage < (bufSize / 2)) {
  78. size_t newSize;
  79. newSize = DEFAULT_BUF_SIZE;
  80. while (newSize < peakUsage)
  81. newSize *= 2;
  82. // We know the buffer is empty, so just reset everything
  83. delete [] start;
  84. ptr = end = start = new uint8_t[newSize];
  85. bufSize = newSize;
  86. }
  87. gettimeofday(&lastSizeCheck, NULL);
  88. peakUsage = needed;
  89. }
  90. // Do we need to shuffle things around?
  91. if ((bufSize - (ptr - start)) < needed) {
  92. memmove(start, ptr, end - ptr);
  93. offset += ptr - start;
  94. end -= ptr - start;
  95. ptr = start;
  96. }
  97. }
  98. bool BufferedInStream::overrun(size_t needed)
  99. {
  100. // Make sure fillBuffer() has room for all the requested data
  101. assert(needed > avail());
  102. ensureSpace(needed - avail());
  103. while (avail() < needed) {
  104. if (!fillBuffer())
  105. return false;
  106. }
  107. return true;
  108. }