From: Amaury Denoyelle Date: Mon, 28 Nov 2022 14:08:14 +0000 (+0100) Subject: CLEANUP: ncbuf: inline small functions X-Git-Tag: v2.7.0~18 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d64a26f0238f386065e26654e6a8a925f96c8baa;p=thirdparty%2Fhaproxy.git CLEANUP: ncbuf: inline small functions ncbuf API relies on lot of small functions. Mark these functions as inline to reduce call invocations and facilitate compiler optimizations to reduce code size. This should be backported up to 2.6. --- diff --git a/include/haproxy/ncbuf.h b/include/haproxy/ncbuf.h index 5c4a4ea49d..8972793d95 100644 --- a/include/haproxy/ncbuf.h +++ b/include/haproxy/ncbuf.h @@ -3,15 +3,43 @@ #include -int ncb_is_null(const struct ncbuf *buf); +static inline int ncb_is_null(const struct ncbuf *buf) +{ + return buf->size == 0; +} + void ncb_init(struct ncbuf *buf, ncb_sz_t head); struct ncbuf ncb_make(char *area, ncb_sz_t size, ncb_sz_t head); -char *ncb_orig(const struct ncbuf *buf); -char *ncb_head(const struct ncbuf *buf); -char *ncb_wrap(const struct ncbuf *buf); +/* Returns start of allocated buffer area. */ +static inline char *ncb_orig(const struct ncbuf *buf) +{ + return buf->area; +} + +/* Returns current head pointer into buffer area. */ +static inline char *ncb_head(const struct ncbuf *buf) +{ + return buf->area + buf->head; +} + +/* Returns the first byte after the allocated buffer area. */ +static inline char *ncb_wrap(const struct ncbuf *buf) +{ + return buf->area + buf->size; +} + +/* Returns the usable size of for data storage. This is the size of the + * allocated buffer without the reserved header space. + */ +static inline ncb_sz_t ncb_size(const struct ncbuf *buf) +{ + if (ncb_is_null(buf)) + return 0; + + return buf->size - NCB_RESERVED_SZ; +} -ncb_sz_t ncb_size(const struct ncbuf *buf); ncb_sz_t ncb_total_data(const struct ncbuf *buf); int ncb_is_empty(const struct ncbuf *buf); int ncb_is_full(const struct ncbuf *buf); diff --git a/src/ncbuf.c b/src/ncbuf.c index dab9196749..5b661f4c4b 100644 --- a/src/ncbuf.c +++ b/src/ncbuf.c @@ -28,6 +28,8 @@ # define BUG_ON_HOT(x) if (x) { fprintf(stderr, "CRASH ON %s:%d\n", __func__, __LINE__); abort(); } #endif /* DEBUG_DEV */ +#include + /* ******** internal API ******** */ #define NCB_BLK_NULL ((struct ncb_blk){ .st = NULL }) @@ -64,7 +66,7 @@ static char *ncb_reserved(const struct ncbuf *buf) } /* Encode at position in . Support wrapping. */ -static void ncb_write_off(const struct ncbuf *buf, char *st, ncb_sz_t off) +static forceinline void ncb_write_off(const struct ncbuf *buf, char *st, ncb_sz_t off) { int i; @@ -79,7 +81,7 @@ static void ncb_write_off(const struct ncbuf *buf, char *st, ncb_sz_t off) } /* Decode offset stored at position in . Support wrapping. */ -static ncb_sz_t ncb_read_off(const struct ncbuf *buf, char *st) +static forceinline ncb_sz_t ncb_read_off(const struct ncbuf *buf, char *st) { int i; ncb_sz_t off = 0; @@ -97,26 +99,26 @@ static ncb_sz_t ncb_read_off(const struct ncbuf *buf, char *st) } /* Add to the offset stored at in . Support wrapping. */ -static void ncb_inc_off(const struct ncbuf *buf, char *st, ncb_sz_t off) +static forceinline void ncb_inc_off(const struct ncbuf *buf, char *st, ncb_sz_t off) { const ncb_sz_t old = ncb_read_off(buf, st); ncb_write_off(buf, st, old + off); } /* Returns true if a gap cannot be inserted at : a reduced gap must be used. */ -static int ncb_off_reduced(const struct ncbuf *b, ncb_sz_t off) +static forceinline int ncb_off_reduced(const struct ncbuf *b, ncb_sz_t off) { return off + NCB_GAP_MIN_SZ > ncb_size(b); } /* Returns true if is the special NULL block. */ -static int ncb_blk_is_null(const struct ncb_blk *blk) +static forceinline int ncb_blk_is_null(const struct ncb_blk *blk) { return !blk->st; } /* Returns true if is the last block of . */ -static int ncb_blk_is_last(const struct ncbuf *buf, const struct ncb_blk *blk) +static forceinline int ncb_blk_is_last(const struct ncbuf *buf, const struct ncb_blk *blk) { BUG_ON_HOT(blk->off + blk->sz > ncb_size(buf)); return blk->off + blk->sz == ncb_size(buf); @@ -214,7 +216,7 @@ static struct ncb_blk ncb_blk_find(const struct ncbuf *buf, ncb_sz_t off) } /* Transform absolute offset to a relative one from start. */ -static ncb_sz_t ncb_blk_off(const struct ncb_blk *blk, ncb_sz_t off) +static forceinline ncb_sz_t ncb_blk_off(const struct ncb_blk *blk, ncb_sz_t off) { BUG_ON_HOT(off < blk->off || off > blk->off + blk->sz); BUG_ON_HOT(off - blk->off > blk->sz); @@ -375,11 +377,6 @@ static ncb_sz_t ncb_fill_gap_blk(const struct ncbuf *buf, /* ******** public API ******** */ -int ncb_is_null(const struct ncbuf *buf) -{ - return buf->size == 0; -} - /* Initialize or reset by clearing all data. Its size is untouched. * Buffer is positioned to offset. Use 0 to realign it. must not * be NCBUF_NULL. @@ -413,35 +410,6 @@ struct ncbuf ncb_make(char *area, ncb_sz_t size, ncb_sz_t head) return buf; } -/* Returns start of allocated buffer area. */ -char *ncb_orig(const struct ncbuf *buf) -{ - return buf->area; -} - -/* Returns current head pointer into buffer area. */ -char *ncb_head(const struct ncbuf *buf) -{ - return buf->area + buf->head; -} - -/* Returns the first byte after the allocated buffer area. */ -char *ncb_wrap(const struct ncbuf *buf) -{ - return buf->area + buf->size; -} - -/* Returns the usable size of for data storage. This is the size of the - * allocated buffer without the reserved header space. - */ -ncb_sz_t ncb_size(const struct ncbuf *buf) -{ - if (ncb_is_null(buf)) - return 0; - - return buf->size - NCB_RESERVED_SZ; -} - /* Returns the total number of bytes stored in whole . */ ncb_sz_t ncb_total_data(const struct ncbuf *buf) {