From c4c5fed020ba44b9930119672a36a1cb33aff090 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sun, 26 Sep 2010 17:06:09 +0930 Subject: [PATCH] compiler: use everywhere. This includes renaming ATTRIBUTE_UNLIKELY_FUNCTION to ATTRIBUTE_COLD, and removing unlikely_func macro from ccan/likely. --- ccan/alloc/_info | 1 + ccan/alloc/alloc.c | 13 ++++++---- ccan/array/array.h | 6 +---- ccan/compiler/compiler.h | 8 +++--- ccan/ilog/ilog.h | 55 +++++++++++++++------------------------- ccan/likely/likely.h | 30 +++------------------- ccan/likely/test/run.c | 8 +----- ccan/talloc/_info | 1 + ccan/talloc/talloc.h | 11 +------- ccan/tap/_info | 4 ++- ccan/tap/tap.h | 9 +------ ccan/tdb/_info | 1 + ccan/tdb/tdb.h | 13 +--------- ccan/tdb2/_info | 1 + ccan/tdb2/tdb2.h | 13 +--------- 15 files changed, 48 insertions(+), 126 deletions(-) diff --git a/ccan/alloc/_info b/ccan/alloc/_info index bd303bd5..470b83e9 100644 --- a/ccan/alloc/_info +++ b/ccan/alloc/_info @@ -103,6 +103,7 @@ int main(int argc, char *argv[]) if (strcmp(argv[1], "depends") == 0) { printf("ccan/alignof\n"); printf("ccan/build_assert\n"); + printf("ccan/compiler\n"); printf("ccan/likely\n"); printf("ccan/short_types\n"); return 0; diff --git a/ccan/alloc/alloc.c b/ccan/alloc/alloc.c index 40b5b6ec..0aac0155 100644 --- a/ccan/alloc/alloc.c +++ b/ccan/alloc/alloc.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "config.h" /* @@ -526,8 +527,9 @@ static bool huge_allocated(struct header *head, unsigned long offset) } /* They want something really big. Aim for contiguous pages (slow). */ -static void *unlikely_func huge_alloc(void *pool, unsigned long poolsize, - unsigned long size, unsigned long align) +static COLD_ATTRIBUTE +void *huge_alloc(void *pool, unsigned long poolsize, + unsigned long size, unsigned long align) { struct header *head = pool; struct huge_alloc *ha; @@ -645,8 +647,8 @@ done: return (char *)pool + ha->off; } -static void unlikely_func huge_free(struct header *head, - unsigned long poolsize, void *free) +static COLD_ATTRIBUTE void +huge_free(struct header *head, unsigned long poolsize, void *free) { unsigned long i, off, pgnum, free_off = (char *)free - (char *)head; unsigned int sp_bits, lp_bits; @@ -681,7 +683,8 @@ static void unlikely_func huge_free(struct header *head, alloc_free(head, poolsize, ha); } -static unsigned long unlikely_func huge_size(struct header *head, void *p) +static COLD_ATTRIBUTE unsigned long +huge_size(struct header *head, void *p) { unsigned long i, off = (char *)p - (char *)head; struct huge_alloc *ha; diff --git a/ccan/array/array.h b/ccan/array/array.h index 22923527..c2efa3ce 100644 --- a/ccan/array/array.h +++ b/ccan/array/array.h @@ -38,12 +38,8 @@ #include #endif -#ifndef HAVE_ATTRIBUTE_MAY_ALIAS -#define HAVE_ATTRIBUTE_MAY_ALIAS 1 -#endif - //Use the array_alias macro to indicate that a pointer has changed but strict aliasing rules are too stupid to know it -#if HAVE_ATTRIBUTE_MAY_ALIAS==1 +#if HAVE_ATTRIBUTE_MAY_ALIAS #define array_alias(ptr) /* nothing */ #define array(type) struct {type *item; size_t size; size_t alloc;} __attribute__((__may_alias__)) #else diff --git a/ccan/compiler/compiler.h b/ccan/compiler/compiler.h index 36017490..be229670 100644 --- a/ccan/compiler/compiler.h +++ b/ccan/compiler/compiler.h @@ -4,20 +4,20 @@ #if HAVE_ATTRIBUTE_COLD /** - * UNLIKELY_FUNCTION_ATTRIBUTE - a function is unlikely to be called. + * COLD_ATTRIBUTE - a function is unlikely to be called. * * Used to mark an unlikely code path and optimize appropriately. * It is usually used on logging or error routines. * * Example: - * void UNLIKELY_FUNCTION_ATTRIBUTE moan(const char *reason) + * void COLD_ATTRIBUTE moan(const char *reason) * { * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno)); * } */ -#define UNLIKELY_FUNCTION_ATTRIBUTE __attribute__((cold)) +#define COLD_ATTRIBUTE __attribute__((cold)) #else -#define UNLIKELY_FUNCTION_ATTRIBUTE +#define COLD_ATTRIBUTE #endif #if HAVE_ATTRIBUTE_PRINTF diff --git a/ccan/ilog/ilog.h b/ccan/ilog/ilog.h index 29689f5f..ed91f4ea 100644 --- a/ccan/ilog/ilog.h +++ b/ccan/ilog/ilog.h @@ -1,41 +1,28 @@ #if !defined(_ilog_H) # define _ilog_H (1) +# include "config.h" # include - -# ifdef __GNUC_PREREQ -/*Tag our functions as idempotent to aid optimization, if possible.*/ -# if __GNUC_PREREQ(2,5) -# define IDEMPOTENT __attribute__((const)) -# endif -# if __GNUC_PREREQ(3,4) -# include +# include +# include /*Note the casts to (int) below: this prevents CLZ{32|64}_OFFS from "upgrading" the type of an entire expression to an (unsigned) size_t.*/ -# if INT_MAX>=2147483647 -# define CLZ32_OFFS ((int)sizeof(unsigned)*CHAR_BIT) -# define CLZ32(_x) (__builtin_clz(_x)) -# elif LONG_MAX>=2147483647L -# define CLZ32_OFFS ((int)sizeof(unsigned long)*CHAR_BIT) -# define CLZ32(_x) (__builtin_clzl(_x)) -# endif -# if INT_MAX>=9223372036854775807LL -# define CLZ64_OFFS ((int)sizeof(unsigned)*CHAR_BIT) -# define CLZ64(_x) (__builtin_clz(_x)) -# elif LONG_MAX>=9223372036854775807LL -# define CLZ64_OFFS ((int)sizeof(unsigned long)*CHAR_BIT) -# define CLZ64(_x) (__builtin_clzl(_x)) -# else /* long long must be >= 64 bits according to ISO C */ -# define CLZ64_OFFS ((int)sizeof(unsigned long long)*CHAR_BIT) -# define CLZ64(_x) (__builtin_clzll(_x)) -# endif -# endif +# if HAVE_BUILTIN_CLZ && INT_MAX>=2147483647 +# define CLZ32_OFFS ((int)sizeof(unsigned)*CHAR_BIT) +# define CLZ32(_x) (__builtin_clz(_x)) +# elif HAVE_BUILTIN_CLZL && LONG_MAX>=2147483647L +# define CLZ32_OFFS ((int)sizeof(unsigned long)*CHAR_BIT) +# define CLZ32(_x) (__builtin_clzl(_x)) # endif -/*If you have some other compiler which defines its own clz-style builtin, - implement a check for it here.*/ - -# if !defined(IDEMPOTENT) -# define IDEMPOTENT +# if HAVE_BUILTIN_CLZ && INT_MAX>=9223372036854775807LL +# define CLZ64_OFFS ((int)sizeof(unsigned)*CHAR_BIT) +# define CLZ64(_x) (__builtin_clz(_x)) +# elif HAVE_BUILTIN_CLZL && LONG_MAX>=9223372036854775807LL +# define CLZ64_OFFS ((int)sizeof(unsigned long)*CHAR_BIT) +# define CLZ64(_x) (__builtin_clzl(_x)) +# elif HAVE_BUILTIN_CLZLL /* long long must be >= 64 bits according to ISO C */ +# define CLZ64_OFFS ((int)sizeof(unsigned long long)*CHAR_BIT) +# define CLZ64(_x) (__builtin_clzll(_x)) # endif @@ -49,7 +36,7 @@ * The ILOG_32() or ILOGNZ_32() macros may be able to use a builtin function * instead, which should be faster. */ -int ilog32(uint32_t _v)IDEMPOTENT; +int ilog32(uint32_t _v) IDEMPOTENT_ATTRIBUTE; /** * ilog64 - Integer binary logarithm of a 64-bit value. * @_v: A 64-bit value. @@ -59,9 +46,7 @@ int ilog32(uint32_t _v)IDEMPOTENT; * The ILOG_64() or ILOGNZ_64() macros may be able to use a builtin function * instead, which should be faster. */ -int ilog64(uint64_t _v)IDEMPOTENT; - -# undef IDEMPOTENT +int ilog64(uint64_t _v) IDEMPOTENT_ATTRIBUTE; # if defined(CLZ32) diff --git a/ccan/likely/likely.h b/ccan/likely/likely.h index 815ee228..62a20372 100644 --- a/ccan/likely/likely.h +++ b/ccan/likely/likely.h @@ -17,7 +17,7 @@ * 99%; marginal cases should not be marked either way. * * See Also: - * unlikely(), unlikely_func, likely_stats() + * unlikely(), likely_stats() * * Example: * // Returns false if we overflow. @@ -39,7 +39,7 @@ * code path and optimize appropriately; see likely() above. * * See Also: - * likely(), unlikely_func, likely_stats() + * likely(), likely_stats(), UNLIKELY_FUNCTION_ATTRIBUTE (compiler.h) * * Example: * // Prints a warning if we overflow. @@ -66,30 +66,6 @@ long _likely_trace(bool cond, bool expect, const char *file, unsigned int line); #endif -#if HAVE_ATTRIBUTE_COLD -/** - * unlikely_func - indicate that a function is unlikely to be called. - * - * This uses a compiler extension where available to indicate an unlikely - * code path and optimize appropriately; see unlikely() above. - * - * It is usually used on logging or error routines. - * - * See Also: - * unlikely() - * - * Example: - * void unlikely_func die_moaning(const char *reason) - * { - * fprintf(stderr, "Dying: %s\n", reason); - * exit(1); - * } - */ -#define unlikely_func __attribute__((cold)) -#else -#define unlikely_func -#endif - #ifdef DEBUG /** * likely_stats - return description of abused likely()/unlikely() @@ -98,7 +74,7 @@ long _likely_trace(bool cond, bool expect, * * When DEBUG is defined, likely() and unlikely() trace their results: this * causes a significant slowdown, but allows analysis of whether the stats - * are correct (unlikely_func can't traced). + * are correct. * * This function returns a malloc'ed description of the least-correct * usage of likely() or unlikely(). It ignores places which have been diff --git a/ccan/likely/test/run.c b/ccan/likely/test/run.c index b6413b15..fa1dc9f6 100644 --- a/ccan/likely/test/run.c +++ b/ccan/likely/test/run.c @@ -17,20 +17,14 @@ static bool one_seems_unlikely(unsigned int val) return false; } -static unlikely_func bool calling_is_unlikely(void) -{ - return true; -} - int main(int argc, char *argv[]) { - plan_tests(5); + plan_tests(4); /* Without debug, we can only check that it doesn't effect functions. */ ok1(one_seems_likely(1)); ok1(!one_seems_likely(2)); ok1(one_seems_unlikely(1)); ok1(!one_seems_unlikely(2)); - ok1(calling_is_unlikely()); exit(exit_status()); } diff --git a/ccan/talloc/_info b/ccan/talloc/_info index ed1a5dda..339f5bcc 100644 --- a/ccan/talloc/_info +++ b/ccan/talloc/_info @@ -97,6 +97,7 @@ int main(int argc, char *argv[]) return 1; if (strcmp(argv[1], "depends") == 0) { + printf("ccan/compiler\n"); printf("ccan/typesafe_cb\n"); return 0; } diff --git a/ccan/talloc/talloc.h b/ccan/talloc/talloc.h index e4f15aee..fb2cbad7 100644 --- a/ccan/talloc/talloc.h +++ b/ccan/talloc/talloc.h @@ -27,6 +27,7 @@ #include #include #include +#include #include "config.h" /* @@ -39,16 +40,6 @@ #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__ #endif -#if HAVE_ATTRIBUTE_PRINTF -/** Use gcc attribute to check printf fns. a1 is the 1-based index of - * the parameter containing the format, and a2 the index of the first - * argument. Note that some gcc 2.x versions don't handle this - * properly **/ -#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2))) -#else -#define PRINTF_ATTRIBUTE(a1, a2) -#endif - /* try to make talloc_set_destructor() and talloc_steal() type safe, if we have a recent gcc */ #if HAVE_TYPEOF diff --git a/ccan/tap/_info b/ccan/tap/_info index 979cb723..f6096974 100644 --- a/ccan/tap/_info +++ b/ccan/tap/_info @@ -50,8 +50,10 @@ int main(int argc, char *argv[]) if (argc != 2) return 1; - if (strcmp(argv[1], "depends") == 0) + if (strcmp(argv[1], "depends") == 0) { + printf("ccan/compiler\n"); return 0; + } return 1; } diff --git a/ccan/tap/tap.h b/ccan/tap/tap.h index 1dad6365..8c961983 100644 --- a/ccan/tap/tap.h +++ b/ccan/tap/tap.h @@ -23,6 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#include /** * plan_tests - announce the number of tests you plan to run @@ -116,14 +117,6 @@ void plan_tests(unsigned int tests); # define skip_end } while(0) -#ifndef PRINTF_ATTRIBUTE -#ifdef __GNUC__ -#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2))) -#else -#define PRINTF_ATTRIBUTE(a1, a2) -#endif -#endif - unsigned int _gen_result(int, const char *, const char *, unsigned int, const char *, ...) PRINTF_ATTRIBUTE(5, 6); diff --git a/ccan/tdb/_info b/ccan/tdb/_info index ec9654c6..8c1e5dc4 100644 --- a/ccan/tdb/_info +++ b/ccan/tdb/_info @@ -73,6 +73,7 @@ int main(int argc, char *argv[]) return 1; if (strcmp(argv[1], "depends") == 0) { + printf("ccan/compiler\n"); printf("ccan/tally\n"); return 0; } diff --git a/ccan/tdb/tdb.h b/ccan/tdb/tdb.h index 070f09b4..01ff905a 100644 --- a/ccan/tdb/tdb.h +++ b/ccan/tdb/tdb.h @@ -38,6 +38,7 @@ extern "C" { /* For sig_atomic_t. */ #include #endif +#include /* flags to tdb_store() */ #define TDB_REPLACE 1 /* Unused */ @@ -77,18 +78,6 @@ typedef struct TDB_DATA { size_t dsize; } TDB_DATA; -#ifndef PRINTF_ATTRIBUTE -#if (__GNUC__ >= 3) -/** Use gcc attribute to check printf fns. a1 is the 1-based index of - * the parameter containing the format, and a2 the index of the first - * argument. Note that some gcc 2.x versions don't handle this - * properly **/ -#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2))) -#else -#define PRINTF_ATTRIBUTE(a1, a2) -#endif -#endif - /* this is the context structure that is returned from a db open */ typedef struct tdb_context TDB_CONTEXT; diff --git a/ccan/tdb2/_info b/ccan/tdb2/_info index 661fb7d2..66d3cb8b 100644 --- a/ccan/tdb2/_info +++ b/ccan/tdb2/_info @@ -77,6 +77,7 @@ int main(int argc, char *argv[]) printf("ccan/hash\n"); printf("ccan/likely\n"); printf("ccan/asearch\n"); + printf("ccan/compiler\n"); printf("ccan/build_assert\n"); printf("ccan/tally\n"); return 0; diff --git a/ccan/tdb2/tdb2.h b/ccan/tdb2/tdb2.h index 5e74bfe7..343264d5 100644 --- a/ccan/tdb2/tdb2.h +++ b/ccan/tdb2/tdb2.h @@ -40,6 +40,7 @@ extern "C" { /* For uint64_t */ #include #endif +#include /* flags to tdb_store() */ #define TDB_REPLACE 1 /* Unused */ @@ -75,18 +76,6 @@ typedef struct tdb_data { size_t dsize; } TDB_DATA; -#ifndef PRINTF_ATTRIBUTE -#if (__GNUC__ >= 3) -/** Use gcc attribute to check printf fns. a1 is the 1-based index of - * the parameter containing the format, and a2 the index of the first - * argument. Note that some gcc 2.x versions don't handle this - * properly **/ -#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2))) -#else -#define PRINTF_ATTRIBUTE(a1, a2) -#endif -#endif - struct tdb_context; /* FIXME: Make typesafe */ -- 2.39.2