[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] fix bug with overzealous shrinking; add more comments.



Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/tmp/cvs-serv7976

Modified Files:
	buffers.c 
Log Message:
fix bug with overzealous shrinking; add more comments.

Index: buffers.c
===================================================================
RCS file: /home/or/cvsroot/src/or/buffers.c,v
retrieving revision 1.47
retrieving revision 1.48
diff -u -d -r1.47 -r1.48
--- buffers.c	14 Oct 2003 03:06:48 -0000	1.47
+++ buffers.c	15 Oct 2003 19:07:07 -0000	1.48
@@ -38,13 +38,17 @@
 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 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;