]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: htx: Use an array of char to store HTX blocks
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 12 Jun 2019 09:28:11 +0000 (11:28 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 19 Jul 2019 07:18:27 +0000 (09:18 +0200)
Instead of using a array of (struct block), it is more natural and intuitive to
use an array of char. Indeed, not only (struct block) are stored in this array,
but also their payload.

include/common/htx.h
src/htx.c

index 3c9aa68ad257ad3f48a010ca9a9383dec536926a..1b67c213966af00cefe7cf8ded33c3c1bd97a210 100644 (file)
@@ -216,7 +216,7 @@ struct htx {
        /* XXX 4 bytes unused */
 
        /* Blocks representing the HTTP message itself */
-       struct htx_blk blocks[0] __attribute__((aligned(8)));
+       char blocks[0] __attribute__((aligned(8)));
 };
 
 
@@ -329,23 +329,23 @@ static inline struct ist htx_sl_res_reason(const struct htx_sl *sl)
 }
 
 /* Converts a position to the corresponding relative address */
-static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos)
+static inline uint32_t htx_pos_to_addr(const struct htx *htx, uint32_t pos)
 {
-       return ((htx->size / sizeof(htx->blocks[0])) - pos - 1);
+       return htx->size - (pos + 1) * sizeof(struct htx_blk);
 }
 
 /* Returns the position of the block <blk>. It is the caller responsibility to
  * be sure <blk> is part of <htx>. */
 static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
 {
-       return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1);
+       return ((htx->blocks + htx->size - (char *)blk) / sizeof(struct htx_blk) - 1);
 }
 
 /* Returns the block at the position <pos>. It is the caller responsibility to
  * be sure the block at the position <pos> exists. */
 static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
 {
-       return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos));
+       return (struct htx_blk *)(htx->blocks + htx_pos_to_addr(htx, pos));
 }
 
 /* Returns the type of the block <blk> */
@@ -635,7 +635,7 @@ static inline uint32_t htx_meta_space(const struct htx *htx)
        if (htx->tail == -1)
                return 0;
 
-       return ((htx->tail + 1 - htx->head) * sizeof(htx->blocks[0]));
+       return ((htx->tail + 1 - htx->head) * sizeof(struct htx_blk));
 }
 
 /* Returns the space used (payload + metadata) in <htx> */
@@ -657,9 +657,9 @@ static inline uint32_t htx_free_data_space(const struct htx *htx)
 {
        uint32_t free = htx_free_space(htx);
 
-       if (free < sizeof(htx->blocks[0]))
+       if (free < sizeof(struct htx_blk))
                return 0;
-       return (free - sizeof(htx->blocks[0]));
+       return (free - sizeof(struct htx_blk));
 }
 
 /* Returns the maximum size for a block, not exceeding <max> bytes. <max> may be
@@ -671,9 +671,9 @@ static inline uint32_t htx_get_max_blksz(const struct htx *htx, int32_t max)
 
        if (max != -1 && free > max)
                free = max;
-       if (free < sizeof(htx->blocks[0]))
+       if (free < sizeof(struct htx_blk))
                return 0;
-       return (free - sizeof(htx->blocks[0]));
+       return (free - sizeof(struct htx_blk));
 }
 
 /* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
index bbf1f8fc1d6bfdfa5b65463b965eea80361778ae..93acbaaae54df94794533c3e7fc0f75e25553fac 100644 (file)
--- a/src/htx.c
+++ b/src/htx.c
@@ -137,12 +137,12 @@ static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
         * message.
         */
        tail = htx->tail + 1;
-       if (sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, tail) >= htx->tail_addr)
+       if (htx_pos_to_addr(htx, tail) >= htx->tail_addr)
                ;
        else if (htx->head > 0) {
                htx_defrag_blks(htx);
                tail = htx->tail + 1;
-               BUG_ON(sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, tail) < htx->tail_addr);
+               BUG_ON(htx_pos_to_addr(htx, tail) < htx->tail_addr);
        }
        else
                goto defrag;
@@ -157,9 +157,7 @@ static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
          *     used, the other one is never used again, until the next defrag.
         */
        headroom = (htx->end_addr - htx->head_addr);
-       tailroom = (!htx->head_addr
-                   ? sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, tail) - htx->tail_addr
-                   : 0);
+       tailroom = (!htx->head_addr ? htx_pos_to_addr(htx, tail) - htx->tail_addr : 0);
        BUG_ON((int32_t)headroom < 0);
        BUG_ON((int32_t)tailroom < 0);
 
@@ -223,7 +221,7 @@ static int htx_prepare_blk_expansion(struct htx *htx, struct htx_blk *blk, int32
        BUG_ON(htx->head == -1);
 
        headroom = (htx->end_addr - htx->head_addr);
-       tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
+       tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
        BUG_ON((int32_t)headroom < 0);
        BUG_ON((int32_t)tailroom < 0);
 
@@ -492,7 +490,7 @@ struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
         * Same type and enough space: append data
         */
        headroom = (htx->end_addr - htx->head_addr);
-       tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
+       tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
        BUG_ON((int32_t)headroom < 0);
        BUG_ON((int32_t)tailroom < 0);
 
@@ -952,7 +950,7 @@ size_t htx_add_data(struct htx *htx, const struct ist data)
        if (!htx->head_addr) {
                if (tailblk->addr+sz != htx->tail_addr)
                        goto add_new_block;
-               room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
+               room = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
        }
        else {
                if (tailblk->addr+sz != htx->head_addr)