aboutsummaryrefslogtreecommitdiffstats
path: root/common/rdr/BufferedInStream.cxx
blob: 3c04bafc34ca9c3fbd6bfbe6fa06166e6f3b1b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * Copyright 2020 Pierre Ossman for Cendio AB
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>

#include <rdr/BufferedInStream.h>
#include <rdr/Exception.h>

using namespace rdr;

static const size_t DEFAULT_BUF_SIZE = 8192;
static const size_t MAX_BUF_SIZE = 32 * 1024 * 1024;

BufferedInStream::BufferedInStream()
  : bufSize(DEFAULT_BUF_SIZE), offset(0)
{
  ptr = end = start = new uint8_t[bufSize];
  gettimeofday(&lastSizeCheck, nullptr);
  peakUsage = 0;
}

BufferedInStream::~BufferedInStream()
{
  delete [] start;
}

size_t BufferedInStream::pos()
{
  return offset + ptr - start;
}

void BufferedInStream::ensureSpace(size_t needed)
{
  struct timeval now;

  // Given argument is how much free space is needed, but for allocation
  // purposes we need to now how much space everything needs, including
  // any existing data already in the buffer
  needed += avail();

  if (needed > bufSize) {
    size_t newSize;
    uint8_t* newBuffer;

    if (needed > MAX_BUF_SIZE)
      throw Exception("BufferedInStream overrun: requested size of "
                      "%lu bytes exceeds maximum of %lu bytes",
                      (long unsigned)needed, (long unsigned)MAX_BUF_SIZE);

    newSize = DEFAULT_BUF_SIZE;
    while (newSize < needed)
      newSize *= 2;

    newBuffer = new uint8_t[newSize];
    memcpy(newBuffer, ptr, end - ptr);
    delete [] start;
    bufSize = newSize;

    offset += ptr - start;
    end = newBuffer + (end - ptr);
    ptr = start = newBuffer;

    gettimeofday(&lastSizeCheck, nullptr);
    peakUsage = needed;
  }

  if (needed > peakUsage)
    peakUsage = needed;

  // Time to shrink an excessive buffer?
  gettimeofday(&now, nullptr);
  if ((avail() == 0) && (bufSize > DEFAULT_BUF_SIZE) &&
      ((now.tv_sec < lastSizeCheck.tv_sec) ||
       (now.tv_sec > (lastSizeCheck.tv_sec + 5)))) {
    if (peakUsage < (bufSize / 2)) {
      size_t newSize;

      newSize = DEFAULT_BUF_SIZE;
      while (newSize < peakUsage)
        newSize *= 2;

      // We know the buffer is empty, so just reset everything
      delete [] start;
      ptr = end = start = new uint8_t[newSize];
      bufSize = newSize;
    }

    gettimeofday(&lastSizeCheck, nullptr);
    peakUsage = needed;
  }

  // Do we need to shuffle things around?
  if ((bufSize - (ptr - start)) < needed) {
    memmove(start, ptr, end - ptr);

    offset += ptr - start;
    end -= ptr - start;
    ptr = start;
  }
}

bool BufferedInStream::overrun(size_t needed)
{
  // Make sure fillBuffer() has room for all the requested data
  assert(needed > avail());
  ensureSpace(needed - avail());

  while (avail() < needed) {
    if (!fillBuffer())
      return false;
  }

  return true;
}