Browse Source

compiler: use everywhere.

This includes renaming ATTRIBUTE_UNLIKELY_FUNCTION to ATTRIBUTE_COLD,
and removing unlikely_func macro from ccan/likely.
Rusty Russell 15 years ago
parent
commit
c4c5fed020

+ 1 - 0
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;

+ 8 - 5
ccan/alloc/alloc.c

@@ -11,6 +11,7 @@
 #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"
 
 /*
@@ -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;

+ 1 - 5
ccan/array/array.h

@@ -38,12 +38,8 @@
 #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
-#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

+ 4 - 4
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

+ 20 - 35
ccan/ilog/ilog.h

@@ -1,41 +1,28 @@
 #if !defined(_ilog_H)
 # define _ilog_H (1)
+# include "config.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.*/
-#   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)

+ 3 - 27
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

+ 1 - 7
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());
 }

+ 1 - 0
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;
 	}

+ 1 - 10
ccan/talloc/talloc.h

@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include <ccan/typesafe_cb/typesafe_cb.h>
+#include <ccan/compiler/compiler.h>
 #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

+ 3 - 1
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;
 }

+ 1 - 8
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 <ccan/compiler/compiler.h>
 
 /**
  * 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);
 

+ 1 - 0
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;
 	}

+ 1 - 12
ccan/tdb/tdb.h

@@ -38,6 +38,7 @@ extern "C" {
 /* For sig_atomic_t. */
 #include <signal.h>
 #endif
+#include <ccan/compiler/compiler.h>
 
 /* 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;
 

+ 1 - 0
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;

+ 1 - 12
ccan/tdb2/tdb2.h

@@ -40,6 +40,7 @@ extern "C" {
 /* For uint64_t */
 #include <stdint.h>
 #endif
+#include <ccan/compiler/compiler.h>
 
 /* 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 */