From 214ad3a92e70ba2a1bbf8d274f3c53c53baadc55 Mon Sep 17 00:00:00 2001 From: leitner Date: Sun, 19 Apr 2015 02:11:41 +0000 Subject: [PATCH] use gcc magic and a #define to convert buffer_puts to buffer_put if the string argument length is known at compile time (same for buffer_putsflush) --- buffer.h | 8 ++++++++ buffer/buffer_puts.c | 1 + buffer/buffer_putsflush.c | 1 + 3 files changed, 10 insertions(+) diff --git a/buffer.h b/buffer.h index 3fff05d..e3dca0b 100644 --- a/buffer.h +++ b/buffer.h @@ -43,6 +43,14 @@ int buffer_puts(buffer* b,const char* x); int buffer_putsalign(buffer* b,const char* x); int buffer_putsflush(buffer* b,const char* x); +#if defined(__GNUC__) && !defined(__LIBOWFAT_INTERNAL) +/* as a little gcc-specific hack, if somebody calls buffer_puts with a + * constant string, where we know its length at compile-time, call + * buffer_put with the known length instead */ +#define buffer_puts(b,s) (__builtin_constant_p(s) ? buffer_put(b,s,sizeof(s)-1) : buffer_puts(b,s)) +#define buffer_putsflush(b,s) (__builtin_constant_p(s) ? buffer_putflush(b,s,sizeof(s)-1) : buffer_putsflush(b,s)) +#endif + int buffer_putm_internal(buffer*b,...); int buffer_putm_internal_flush(buffer*b,...); #define buffer_putm(b,...) buffer_putm_internal(b,__VA_ARGS__,(char*)0) diff --git a/buffer/buffer_puts.c b/buffer/buffer_puts.c index e53f675..af66bf1 100644 --- a/buffer/buffer_puts.c +++ b/buffer/buffer_puts.c @@ -1,3 +1,4 @@ +#define __LIBOWFAT_INTERNAL #include "str.h" #include "buffer.h" diff --git a/buffer/buffer_putsflush.c b/buffer/buffer_putsflush.c index e01762d..704b5c8 100644 --- a/buffer/buffer_putsflush.c +++ b/buffer/buffer_putsflush.c @@ -1,3 +1,4 @@ +#define __LIBOWFAT_INTERNAL #include "str.h" #include "buffer.h"