]> git.ozlabs.org Git - ccan/commitdiff
compiler: use everywhere.
authorRusty Russell <rusty@rustcorp.com.au>
Sun, 26 Sep 2010 07:36:09 +0000 (17:06 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Sun, 26 Sep 2010 07:36:09 +0000 (17:06 +0930)
This includes renaming ATTRIBUTE_UNLIKELY_FUNCTION to ATTRIBUTE_COLD,
and removing unlikely_func macro from ccan/likely.

15 files changed:
ccan/alloc/_info
ccan/alloc/alloc.c
ccan/array/array.h
ccan/compiler/compiler.h
ccan/ilog/ilog.h
ccan/likely/likely.h
ccan/likely/test/run.c
ccan/talloc/_info
ccan/talloc/talloc.h
ccan/tap/_info
ccan/tap/tap.h
ccan/tdb/_info
ccan/tdb/tdb.h
ccan/tdb2/_info
ccan/tdb2/tdb2.h

index bd303bd5d35b69817677c59b8d29f96599cf74ed..470b83e94e87132846d4b0e1711ed6282534b4f8 100644 (file)
@@ -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");
        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;
                printf("ccan/likely\n");
                printf("ccan/short_types\n");
                return 0;
index 40b5b6ec6ddbedd9f16cb0aade0e75960727e809..0aac015595019aa1b201a0ab737655e19ef04cd6 100644 (file)
@@ -11,6 +11,7 @@
 #include <ccan/likely/likely.h>
 #include <ccan/alignof/alignof.h>
 #include <ccan/short_types/short_types.h>
 #include <ccan/likely/likely.h>
 #include <ccan/alignof/alignof.h>
 #include <ccan/short_types/short_types.h>
+#include <ccan/compiler/compiler.h>
 #include "config.h"
 
 /*
 #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). */
 }
 
 /* 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;
 {
        struct header *head = pool;
        struct huge_alloc *ha;
@@ -645,8 +647,8 @@ done:
        return (char *)pool + ha->off;
 }
 
        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;
 {
        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);
 }
 
        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;
 {
        unsigned long i, off = (char *)p - (char *)head;
        struct huge_alloc *ha;
index 229235271bde053e0c28f02760178777dde497fd..c2efa3ce707d74678ca51e503e2e6b7644b62774 100644 (file)
 #include <ccan/talloc/talloc.h>
 #endif
 
 #include <ccan/talloc/talloc.h>
 #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
 //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
 #define array_alias(ptr) /* nothing */
 #define array(type) struct {type *item; size_t size; size_t alloc;} __attribute__((__may_alias__))
 #else
index 3601749003894974267a59e515db9a1cce6c8235..be229670fd127a5f89e9e9bc6b14e4a7cc48ddce 100644 (file)
@@ -4,20 +4,20 @@
 
 #if HAVE_ATTRIBUTE_COLD
 /**
 
 #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:
  *
  * 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));
  *     }
  */
  *     {
  *             fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
  *     }
  */
-#define UNLIKELY_FUNCTION_ATTRIBUTE __attribute__((cold))
+#define COLD_ATTRIBUTE __attribute__((cold))
 #else
 #else
-#define UNLIKELY_FUNCTION_ATTRIBUTE
+#define COLD_ATTRIBUTE
 #endif
 
 #if HAVE_ATTRIBUTE_PRINTF
 #endif
 
 #if HAVE_ATTRIBUTE_PRINTF
index 29689f5f5ff69825bf3bca6c1d215ac8ac983315..ed91f4eaf830de7192cf27c84a51b145eb899f2c 100644 (file)
@@ -1,41 +1,28 @@
 #if !defined(_ilog_H)
 # define _ilog_H (1)
 #if !defined(_ilog_H)
 # define _ilog_H (1)
+# include "config.h"
 # include <stdint.h>
 # include <stdint.h>
-
-# 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 <limits.h>
+# include <ccan/compiler/compiler.h>
+# include <limits.h>
 /*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.*/
 /*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
 
 # 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
 
 
 # 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.
  */
  * 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.
 /**
  * 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.
  */
  * 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)
 
 
 # if defined(CLZ32)
index 815ee2287fd0566248075217d9e36b56b2b45009..62a203727507f359dcfa46ef4209629dfa79a848 100644 (file)
@@ -17,7 +17,7 @@
  * 99%; marginal cases should not be marked either way.
  *
  * See Also:
  * 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.
  *
  * Example:
  *     // Returns false if we overflow.
@@ -39,7 +39,7 @@
  * code path and optimize appropriately; see likely() above.
  *
  * See Also:
  * 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.
  *
  * 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
 
                   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()
 #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
  *
  * 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
  *
  * This function returns a malloc'ed description of the least-correct
  * usage of likely() or unlikely().  It ignores places which have been
index b6413b15ce29244714d75037ac11ac0e4b5085c9..fa1dc9f6eade527431deae6b2d1b1ec470630357 100644 (file)
@@ -17,20 +17,14 @@ static bool one_seems_unlikely(unsigned int val)
        return false;
 }
 
        return false;
 }
 
-static unlikely_func bool calling_is_unlikely(void)
-{
-       return true;
-}
-
 int main(int argc, char *argv[])
 {
 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));
 
        /* 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());
 }
        exit(exit_status());
 }
index ed1a5dda44fc7d18faa43c874ef1f840db3d0e3c..339f5bccf887acc8fe52148a9487960675a179b3 100644 (file)
@@ -97,6 +97,7 @@ int main(int argc, char *argv[])
                return 1;
 
        if (strcmp(argv[1], "depends") == 0) {
                return 1;
 
        if (strcmp(argv[1], "depends") == 0) {
+               printf("ccan/compiler\n");
                printf("ccan/typesafe_cb\n");
                return 0;
        }
                printf("ccan/typesafe_cb\n");
                return 0;
        }
index e4f15aeecbb1b9dde7909525da89e88c727936e9..fb2cbad7446d013076971da5c4e5a227426c9402 100644 (file)
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include <ccan/typesafe_cb/typesafe_cb.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <ccan/typesafe_cb/typesafe_cb.h>
+#include <ccan/compiler/compiler.h>
 #include "config.h"
 
 /*
 #include "config.h"
 
 /*
 #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
 #endif
 
 #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
 /* try to make talloc_set_destructor() and talloc_steal() type safe,
    if we have a recent gcc */
 #if HAVE_TYPEOF
index 979cb7233ea2d0d477c28e4326a10cb2ebe0c545..f60969745d5a8aec706809f5e1be16746c9519f9 100644 (file)
@@ -50,8 +50,10 @@ int main(int argc, char *argv[])
        if (argc != 2)
                return 1;
 
        if (argc != 2)
                return 1;
 
-       if (strcmp(argv[1], "depends") == 0)
+       if (strcmp(argv[1], "depends") == 0) {
+               printf("ccan/compiler\n");
                return 0;
                return 0;
+       }
 
        return 1;
 }
 
        return 1;
 }
index 1dad63650e40a2c26f62f99d90964e6c9bedcd93..8c961983555dc4869ba0f6ebd11e79b3c86d0a8a 100644 (file)
@@ -23,6 +23,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+#include <ccan/compiler/compiler.h>
 
 /**
  * plan_tests - announce the number of tests you plan to run
 
 /**
  * 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)
 
 
 # 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);
 
 unsigned int _gen_result(int, const char *, const char *, unsigned int,
    const char *, ...) PRINTF_ATTRIBUTE(5, 6);
 
index ec9654c6369399a37ea1f74a8eca405e947b9ab4..8c1e5dc47459dcc2aa0093168ee849d5b1befbbf 100644 (file)
@@ -73,6 +73,7 @@ int main(int argc, char *argv[])
                return 1;
 
        if (strcmp(argv[1], "depends") == 0) {
                return 1;
 
        if (strcmp(argv[1], "depends") == 0) {
+               printf("ccan/compiler\n");
                printf("ccan/tally\n");
                return 0;
        }
                printf("ccan/tally\n");
                return 0;
        }
index 070f09b4e19075e05f7bdfebff634d93fddcc89d..01ff905ae2af29a8ff4e6e2d4ea7872b4013def5 100644 (file)
@@ -38,6 +38,7 @@ extern "C" {
 /* For sig_atomic_t. */
 #include <signal.h>
 #endif
 /* For sig_atomic_t. */
 #include <signal.h>
 #endif
+#include <ccan/compiler/compiler.h>
 
 /* flags to tdb_store() */
 #define TDB_REPLACE 1          /* Unused */
 
 /* flags to tdb_store() */
 #define TDB_REPLACE 1          /* Unused */
@@ -77,18 +78,6 @@ typedef struct TDB_DATA {
        size_t dsize;
 } 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;
 
 /* this is the context structure that is returned from a db open */
 typedef struct tdb_context TDB_CONTEXT;
 
index 661fb7d2db285bc8f9efba41da78d1f68933883c..66d3cb8bacf139100e5ef2b40b37ca094fa22161 100644 (file)
@@ -77,6 +77,7 @@ int main(int argc, char *argv[])
                printf("ccan/hash\n");
                printf("ccan/likely\n");
                printf("ccan/asearch\n");
                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;
                printf("ccan/build_assert\n");
                printf("ccan/tally\n");
                return 0;
index 5e74bfe7883e48e7f1e52e1b5d1696d1ebc433fe..343264d588f0a04db188556f6f1f1f542cd4cd2b 100644 (file)
@@ -40,6 +40,7 @@ extern "C" {
 /* For uint64_t */
 #include <stdint.h>
 #endif
 /* For uint64_t */
 #include <stdint.h>
 #endif
+#include <ccan/compiler/compiler.h>
 
 /* flags to tdb_store() */
 #define TDB_REPLACE 1          /* Unused */
 
 /* flags to tdb_store() */
 #define TDB_REPLACE 1          /* Unused */
@@ -75,18 +76,6 @@ typedef struct tdb_data {
        size_t dsize;
 } 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 */
 struct tdb_context;
 
 /* FIXME: Make typesafe */