aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2003-10-15 19:07:07 +0000
committerNick Mathewson <nickm@torproject.org>2003-10-15 19:07:07 +0000
commit75170f052bb5e2d6a5eac1cf5b6b207d6bab16f6 (patch)
treed1edfa90bd610e072f8fdb47494e0c29f56d9469 /src
parent11a23fc280f5fc7a0d4af50ca73f87b96d2dab6f (diff)
downloadtor-75170f052bb5e2d6a5eac1cf5b6b207d6bab16f6.tar
tor-75170f052bb5e2d6a5eac1cf5b6b207d6bab16f6.tar.gz
fix bug with overzealous shrinking; add more comments.
svn:r597
Diffstat (limited to 'src')
-rw-r--r--src/or/buffers.c16
1 files changed, 13 insertions, 3 deletions
diff --git a/src/or/buffers.c b/src/or/buffers.c
index bb49e45f5..edf9025a7 100644
--- a/src/or/buffers.c
+++ b/src/or/buffers.c
@@ -38,13 +38,17 @@ static INLINE void buf_resize(buf_t *buf, size_t new_capacity)
static INLINE int buf_ensure_capacity(buf_t *buf, size_t capacity)
{
size_t new_len;
- if (buf->len >= capacity)
+ if (buf->len >= capacity) /* Don't grow if we're already big enough. */
return 0;
- if (capacity > MAX_BUF_SIZE)
+ if (capacity > MAX_BUF_SIZE) /* Don't grow past the maximum. */
return -1;
+ /* Find the smallest new_len equal to (2**X)*len for some X; such that
+ * new_len is at least capacity.
+ */
new_len = buf->len*2;
while (new_len < capacity)
new_len *= 2;
+ /* Resize the buffer. */
log_fn(LOG_DEBUG,"Growing buffer from %d to %d bytes.",
(int)buf->len, (int)new_len);
buf_resize(buf,new_len);
@@ -58,8 +62,14 @@ static INLINE int buf_ensure_capacity(buf_t *buf, size_t capacity)
*/
static INLINE void buf_shrink_if_underfull(buf_t *buf) {
size_t new_len;
- if (buf->datalen >= buf->len/4 || buf->len >= 2*MIN_BUF_SHRINK_SIZE)
+ /* If the buffer is at least .25 full, or if shrinking the buffer would
+ * put it onder MIN_BUF_SHRINK_SIZE, don't do it. */
+ if (buf->datalen >= buf->len/4 || buf->len < 2*MIN_BUF_SHRINK_SIZE)
return;
+ /* Shrink new_len by powers of 2 until: datalen is at least 1/4 of
+ * new_len, OR shrinking new_len more would put it under
+ * MIN_BUF_SHRINK_SIZE.
+ */
new_len = buf->len / 2;
while (buf->datalen < new_len/4 && new_len/2 > MIN_BUF_SHRINK_SIZE)
new_len /= 2;