]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: buffer: replace bo_getblk() with direction agnostic b_getblk()
authorWilly Tarreau <w@1wt.eu>
Fri, 15 Jun 2018 12:20:26 +0000 (14:20 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 19 Jul 2018 14:23:40 +0000 (16:23 +0200)
This new functoin limits itself to the amount of data available in the
buffer and doesn't care about the direction anymore. It's only called
from co_getblk() which already checks that no more than the available
output bytes is requested.

include/common/buf.h
include/common/buffer.h
src/channel.c

index 33830f7097179b9065be8dff8336190e2f0c78df..ad2e2ba0d03b7b56a6b502be79a042664abba7fc 100644 (file)
@@ -311,6 +311,38 @@ static inline size_t b_contig_space(const struct buffer *b)
        return right - left;
 }
 
+/* b_getblk() : gets one full block of data at once from a buffer, starting
+ * from offset <offset> after the buffer's head, and limited to no more than
+ * <len> bytes. The caller is responsible for ensuring that neither <offset>
+ * nor <offset>+<len> exceed the total number of bytes available in the buffer.
+ * Return values :
+ *   >0 : number of bytes read, equal to requested size.
+ *   =0 : not enough data available. <blk> is left undefined.
+ * The buffer is left unaffected.
+ */
+static inline size_t b_getblk(const struct buffer *buf, char *blk, size_t len, size_t offset)
+{
+       size_t firstblock;
+
+       if (len + offset > b_data(buf))
+               return 0;
+
+       firstblock = b_wrap(buf) - b_head(buf);
+       if (firstblock > offset) {
+               if (firstblock >= len + offset) {
+                       memcpy(blk, b_head(buf) + offset, len);
+                       return len;
+               }
+
+               memcpy(blk, b_head(buf) + offset, firstblock - offset);
+               memcpy(blk + firstblock - offset, b_orig(buf), len - firstblock + offset);
+               return len;
+       }
+
+       memcpy(blk, b_orig(buf) + offset - firstblock, len);
+       return len;
+}
+
 
 /*********************************************/
 /* Functions used to modify the buffer state */
index 10ff640b50f7c0f102c72dc97e7f596c53b357e4..896e050628480f7e0d4b1d5db9c26b891fe823fb 100644 (file)
@@ -326,35 +326,6 @@ static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
        return bo_putblk(b, chk->str, chk->len);
 }
 
-/* Gets one full block of data at once from a buffer's output, optionally
- * starting at a specific offset. Return values :
- *   >0 : number of bytes read, equal to requested size.
- *   =0 : not enough data available. <blk> is left undefined.
- * The buffer is left unaffected.
- */
-static inline int bo_getblk(const struct buffer *buf, char *blk, int len, int offset)
-{
-       int firstblock;
-
-       if (len + offset > buf->o)
-               return 0;
-
-       firstblock = buf->data + buf->size - b_head(buf);
-       if (firstblock > offset) {
-               if (firstblock >= len + offset) {
-                       memcpy(blk, b_head(buf) + offset, len);
-                       return len;
-               }
-
-               memcpy(blk, b_head(buf) + offset, firstblock - offset);
-               memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset);
-               return len;
-       }
-
-       memcpy(blk, buf->data + offset - firstblock, len);
-       return len;
-}
-
 /* Gets one or two blocks of data at once from a buffer's output.
  * Return values :
  *   >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
index fc1bf1279ef25e658f26e4f5f4136b1533184f81..42514179b22250e37c06d94c116328aa3714439d 100644 (file)
@@ -295,13 +295,13 @@ int co_getblk(const struct channel *chn, char *blk, int len, int offset)
        if (chn->flags & CF_SHUTW)
                return -1;
 
-       if (len + offset > chn->buf->o) {
+       if (len + offset > co_data(chn)) {
                if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
                        return -1;
                return 0;
        }
 
-       return bo_getblk(chn->buf, blk, len, offset);
+       return b_getblk(chn->buf, blk, len, offset);
 }
 
 /* Gets one or two blocks of data at once from a channel's output buffer.