From: Ian Jackson Date: Sat, 25 Oct 2014 18:04:31 +0000 (+0100) Subject: Static buffers: Provide new rotating static buffer macros X-Git-Tag: proposed.ipv6-polypath-fixes.v1~2 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=secnet.git;a=commitdiff_plain;h=5e7a63be9a512bf979e5538e6a694d5fd66b9380 Static buffers: Provide new rotating static buffer macros Provide new macros SBUF_DEFINE and SBUF which replace the open coded rotating static buffers in ipaddr_getbuf (ipaddr_to_string and subnet_to_string) and iaddr_to_string. No functional change. Signed-off-by: Ian Jackson --- diff --git a/ipaddr.c b/ipaddr.c index 1506ae6..9302925 100644 --- a/ipaddr.c +++ b/ipaddr.c @@ -313,17 +313,11 @@ struct subnet_list *ipset_to_subnet_list(struct ipset *is) return r; } -#define IPADDR_NBUFS_SHIFT 4 -#define IPADDR_NBUFS (1 << IPADDR_NBUFS_SHIFT) #define IPADDR_BUFLEN 20 static char *ipaddr_getbuf(void) { - static int b; - static char bufs[IPADDR_NBUFS][IPADDR_BUFLEN]; - - b++; - b &= IPADDR_NBUFS-1; + SBUF_DEFINE(16, IPADDR_BUFLEN); return SBUF; } diff --git a/util.c b/util.c index 478d779..59ed427 100644 --- a/util.c +++ b/util.c @@ -543,19 +543,13 @@ void string_item_to_iaddr(const item_t *item, uint16_t port, union iaddr *ia, #endif /* CONFIG_IPV6 */ } -#define IADDR_NBUFS_SHIFT 3 -#define IADDR_NBUFS (1 << IADDR_NBUFS_SHIFT) +#define IADDR_NBUFS 8 const char *iaddr_to_string(const union iaddr *ia) { - static int b; - - b++; - b &= IADDR_NBUFS-1; - #ifndef CONFIG_IPV6 - static char bufs[IADDR_NBUFS][100]; + SBUF_DEFINE(IADDR_NBUFS, 100); assert(ia->sa.sa_family == AF_INET); @@ -565,7 +559,7 @@ const char *iaddr_to_string(const union iaddr *ia) #else /* CONFIG_IPV6 => we have adns_addr2text */ - static char bufs[IADDR_NBUFS][1+ADNS_ADDR2TEXT_BUFLEN+20]; + SBUF_DEFINE(IADDR_NBUFS, 1+ADNS_ADDR2TEXT_BUFLEN+20); int port; diff --git a/util.h b/util.h index d4663db..de1aaa5 100644 --- a/util.h +++ b/util.h @@ -89,7 +89,20 @@ void string_item_to_iaddr(const item_t *item, uint16_t port, union iaddr *ia, const char *desc); -#define SBUF (bufs[b]) /* temporary macro */ +/* + * SBUF_DEFINE(int nbufs, size_t size); + * // Generates a number of definitions and statements organising + * // nbufs rotating char[size] buffers such that subsequent code + * // may refer to: + * char *const SBUF; + */ +#define SBUF_DEFINE(nbufs, size) \ + static int static_bufs__bufnum; \ + static char static_bufs__bufs[(nbufs)][(size)]; \ + static_bufs__bufnum++; \ + static_bufs__bufnum %= (nbufs); \ + static_bufs__bufs[static_bufs__bufnum] +#define SBUF (static_bufs__bufs[static_bufs__bufnum]) /*----- line-buffered asynch input -----*/