Commit 1c005e12 authored by Rusty Russell's avatar Rusty Russell

compiler: shorten names of attributes, add UNUSED

The long names were unwieldy in practice; at risk of clashing, replace
with shorter versions.
parent 56dd9548
......@@ -527,9 +527,8 @@ static bool huge_allocated(struct header *head, unsigned long offset)
}
/* They want something really big. Aim for contiguous pages (slow). */
static COLD_ATTRIBUTE
void *huge_alloc(void *pool, unsigned long poolsize,
unsigned long size, unsigned long align)
static COLD void *huge_alloc(void *pool, unsigned long poolsize,
unsigned long size, unsigned long align)
{
struct header *head = pool;
struct huge_alloc *ha;
......@@ -647,7 +646,7 @@ done:
return (char *)pool + ha->off;
}
static COLD_ATTRIBUTE void
static COLD void
huge_free(struct header *head, unsigned long poolsize, void *free)
{
unsigned long i, off, pgnum, free_off = (char *)free - (char *)head;
......@@ -687,8 +686,7 @@ huge_free(struct header *head, unsigned long poolsize, void *free)
alloc_free(head, poolsize, ha);
}
static COLD_ATTRIBUTE unsigned long
huge_size(struct header *head, void *p)
static COLD unsigned long huge_size(struct header *head, void *p)
{
unsigned long i, off = (char *)p - (char *)head;
struct huge_alloc *ha;
......
......@@ -6,16 +6,18 @@
* compiler - macros for common compiler extensions
*
* Abstracts away some compiler hints. Currently these include:
* - COLD_ATTRIBUTE
* - COLD
* For functions not called in fast paths (aka. cold functions)
* - PRINTF_ATTRIBUTE
* - PRINTF_FMT
* For functions which take printf-style parameters.
* - IDEMPOTENT_ATTRIBUTE
* - IDEMPOTENT
* For functions which return the same value for same parameters.
* - NEEDED_ATTRIBUTE
* - NEEDED
* For functions and variables which must be emitted even if unused.
* - UNNEEDED_ATTRIBUTE
* - UNNEEDED
* For functions and variables which need not be emitted if unused.
* - UNUSED
* For parameters which are not used.
* - IS_COMPILE_CONSTANT
* For using different tradeoffs for compiletime vs runtime evaluation.
*
......@@ -29,7 +31,7 @@
*
* // Example of a (slow-path) logging function.
* static int log_threshold = 2;
* static void COLD_ATTRIBUTE PRINTF_ATTRIBUTE(2,3)
* static void COLD PRINTF_FMT(2,3)
* logger(int level, const char *fmt, ...)
* {
* va_list ap;
......
......@@ -4,95 +4,111 @@
#if HAVE_ATTRIBUTE_COLD
/**
* COLD_ATTRIBUTE - a function is unlikely to be called.
* COLD - 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:
* static void COLD_ATTRIBUTE moan(const char *reason)
* static void COLD moan(const char *reason)
* {
* fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
* }
*/
#define COLD_ATTRIBUTE __attribute__((cold))
#define COLD __attribute__((cold))
#else
#define COLD_ATTRIBUTE
#define COLD
#endif
#if HAVE_ATTRIBUTE_PRINTF
/**
* PRINTF_ATTRIBUTE - a function takes printf-style arguments
* PRINTF_FMT - a function takes printf-style arguments
* @nfmt: the 1-based number of the function's format argument.
* @narg: the 1-based number of the function's first variable argument.
*
* This allows the compiler to check your parameters as it does for printf().
*
* Example:
* void PRINTF_ATTRIBUTE(2,3) my_printf(const char *prefix,
* const char *fmt, ...);
* void PRINTF_FMT(2,3) my_printf(const char *prefix, const char *fmt, ...);
*/
#define PRINTF_ATTRIBUTE(nfmt, narg) \
#define PRINTF_FMT(nfmt, narg) \
__attribute__((format(__printf__, nfmt, narg)))
#else
#define PRINTF_ATTRIBUTE(nfmt, narg)
#define PRINTF_FMT(nfmt, narg)
#endif
#if HAVE_ATTRIBUTE_CONST
/**
* IDEMPOTENT_ATTRIBUTE - a function's return depends only on its argument
* IDEMPOTENT - a function's return depends only on its argument
*
* This allows the compiler to assume that the function will return the exact
* same value for the exact same arguments. This implies that the function
* must not use global variables, or dereference pointer arguments.
*/
#define IDEMPOTENT_ATTRIBUTE __attribute__((const))
#define IDEMPOTENT __attribute__((const))
#else
#define IDEMPOTENT_ATTRIBUTE
#define IDEMPOTENT
#endif
#if HAVE_ATTRIBUTE_UNUSED
/**
* UNNEEDED_ATTRIBUTE - a parameter/variable/function may not be needed
* UNNEEDED - a variable/function may not be needed
*
* This suppresses warnings about unused variables or parameters, but tells
* This suppresses warnings about unused variables or functions, but tells
* the compiler that if it is unused it need not emit it into the source code.
*
* Example:
* // With some preprocessor options, this is unnecessary.
* static UNNEEDED_ATTRIBUTE int counter;
* static UNNEEDED int counter;
*
* // With some preprocessor options, this is unnecessary.
* static UNNEEDED_ATTRIBUTE void add_to_counter(int add)
* static UNNEEDED void add_to_counter(int add)
* {
* counter += add;
* }
*/
#define UNNEEDED_ATTRIBUTE __attribute__((unused))
#define UNNEEDED __attribute__((unused))
#if HAVE_ATTRIBUTE_USED
/**
* NEEDED_ATTRIBUTE - a parameter/variable/function is needed
* NEEDED - a variable/function is needed
*
* This suppresses warnings about unused variables or parameters, but tells
* This suppresses warnings about unused variables or functions, but tells
* the compiler that it must exist even if it (seems) unused.
*
* Example:
* // Even if this is unused, these are vital for debugging.
* static UNNEEDED_ATTRIBUTE int counter;
* static UNNEEDED_ATTRIBUTE void dump_counter(void)
* static NEEDED int counter;
* static NEEDED void dump_counter(void)
* {
* printf("Counter is %i\n", counter);
* }
*/
#define NEEDED_ATTRIBUTE __attribute__((used))
#define NEEDED __attribute__((used))
#else
/* Before used, unused functions and vars were always emitted. */
#define NEEDED_ATTRIBUTE __attribute__((unused))
#define NEEDED __attribute__((unused))
#endif
/**
* UNUSED - a parameter is unused
*
* Some compilers (eg. gcc with -W or -Wunused) warn about unused
* function parameters. This suppresses such warnings and indicates
* to the reader that it's deliberate.
*
* Example:
* // This is used as a callback, so needs to have this prototype.
* static int some_callback(void *unused UNUSED)
* {
* return 0;
* }
*/
#define UNUSED __attribute__((unused))
#else
#define UNNEEDED_ATTRIBUTE
#define NEEDED_ATTRIBUTE
#define UNNEEDED
#define NEEDED
#define UNUSED
#endif
#if HAVE_BUILTIN_CONSTANT_P
......
#include <ccan/compiler/compiler.h>
static void PRINTF_ATTRIBUTE(2,3) my_printf(int x, const char *fmt, ...)
static void PRINTF_FMT(2,3) my_printf(int x, const char *fmt, ...)
{
}
......
......@@ -155,7 +155,7 @@ static void ht_add(struct htable *ht, const void *new, size_t h)
ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
}
static COLD_ATTRIBUTE bool double_table(struct htable *ht)
static COLD bool double_table(struct htable *ht)
{
unsigned int i;
size_t oldnum = (size_t)1 << ht->bits;
......@@ -192,7 +192,7 @@ static COLD_ATTRIBUTE bool double_table(struct htable *ht)
return true;
}
static COLD_ATTRIBUTE void rehash_table(struct htable *ht)
static COLD void rehash_table(struct htable *ht)
{
size_t start, i;
uintptr_t e;
......@@ -217,7 +217,7 @@ static COLD_ATTRIBUTE void rehash_table(struct htable *ht)
}
/* We stole some bits, now we need to put them back... */
static COLD_ATTRIBUTE void update_common(struct htable *ht, const void *p)
static COLD void update_common(struct htable *ht, const void *p)
{
unsigned int i;
uintptr_t maskdiff, bitsdiff;
......
......@@ -16,7 +16,7 @@
year=1998,
note="\url{http://supertech.csail.mit.edu/papers/debruijn.pdf}"
}*/
static UNNEEDED_ATTRIBUTE const unsigned char DEBRUIJN_IDX32[32]={
static UNNEEDED const unsigned char DEBRUIJN_IDX32[32]={
0, 1,28, 2,29,14,24, 3,30,22,20,15,25,17, 4, 8,
31,27,13,23,21,19,16, 7,26,12,18, 6,11, 5,10, 9
};
......
......@@ -24,7 +24,7 @@
* return 1U << ilog32(i-1);
* }
*/
int ilog32(uint32_t _v) IDEMPOTENT_ATTRIBUTE;
int ilog32(uint32_t _v) IDEMPOTENT;
/**
* ilog32_nz - Integer binary logarithm of a non-zero 32-bit value.
......@@ -43,7 +43,7 @@ int ilog32(uint32_t _v) IDEMPOTENT_ATTRIBUTE;
* return ilog32_nz(i) - 1;
* }
*/
int ilog32_nz(uint32_t _v) IDEMPOTENT_ATTRIBUTE;
int ilog32_nz(uint32_t _v) IDEMPOTENT;
/**
* ilog64 - Integer binary logarithm of a 64-bit value.
......@@ -55,7 +55,7 @@ int ilog32_nz(uint32_t _v) IDEMPOTENT_ATTRIBUTE;
* See Also:
* ilog64_nz(), ilog32()
*/
int ilog64(uint64_t _v) IDEMPOTENT_ATTRIBUTE;
int ilog64(uint64_t _v) IDEMPOTENT;
/**
* ilog64_nz - Integer binary logarithm of a non-zero 64-bit value.
......@@ -67,7 +67,7 @@ int ilog64(uint64_t _v) IDEMPOTENT_ATTRIBUTE;
* See Also:
* ilog64(), ilog32_nz()
*/
int ilog64_nz(uint64_t _v) IDEMPOTENT_ATTRIBUTE;
int ilog64_nz(uint64_t _v) IDEMPOTENT;
/**
* STATIC_ILOG_32 - The integer logarithm of an (unsigned, 32-bit) constant.
......
......@@ -268,7 +268,7 @@ int iscsi_logout_async(struct iscsi_context *iscsi, iscsi_command_cb cb, void *p
return 0;
}
int iscsi_process_logout_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size UNNEEDED_ATTRIBUTE)
int iscsi_process_logout_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size UNUSED)
{
iscsi->is_loggedin = 0;
pdu->callback(iscsi, ISCSI_STATUS_GOOD, NULL, pdu->private_data);
......
......@@ -201,7 +201,7 @@ struct scsi_task *scsi_cdb_readcapacity10(int lba, int pmi)
* parse the data in blob and calcualte the size of a full readcapacity10 datain structure
*/
static int scsi_readcapacity10_datain_getfullsize(struct scsi_task *task
UNNEEDED_ATTRIBUTE)
UNUSED)
{
return 8;
}
......
......@@ -33,7 +33,7 @@ struct jbitset {
JError_t err;
const char *errstr;
};
const char *COLD_ATTRIBUTE jbit_error_(struct jbitset *set);
const char *COLD jbit_error_(struct jbitset *set);
/**
* jbit_error - test for an error in the a previous jbit_ operation.
......
......@@ -44,7 +44,7 @@ struct jmap {
unsigned long acc_index;
const char *funcname;
};
const char *COLD_ATTRIBUTE jmap_error_(struct jmap *map);
const char *COLD jmap_error_(struct jmap *map);
/* Debugging checks. */
static inline void jmap_debug_add_access(const struct jmap *map,
......
......@@ -39,7 +39,7 @@
* code path and optimize appropriately; see likely() above.
*
* See Also:
* likely(), likely_stats(), UNLIKELY_FUNCTION_ATTRIBUTE (compiler.h)
* likely(), likely_stats(), COLD (compiler.h)
*
* Example:
* // Prints a warning if we overflow.
......
......@@ -664,7 +664,7 @@ int talloc_unlink(const void *context, void *ptr)
/*
add a name to an existing pointer - va_list version
*/
static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_FMT(2,0);
static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
{
......
......@@ -580,7 +580,7 @@ char *talloc_strndup(const void *t, const char *p, size_t n);
*
* talloc_set_name_const(ptr, ptr)
*/
char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_FMT(2,3);
/**
* talloc_append_string - concatenate onto a tallocated string
......@@ -610,7 +610,7 @@ char *WARN_UNUSED_RESULT talloc_append_string(char *orig, const char *append);
* talloc_set_name_const(ptr, ptr)
*/
char *WARN_UNUSED_RESULT talloc_asprintf_append(char *s, const char *fmt, ...)
PRINTF_ATTRIBUTE(2,3);
PRINTF_FMT(2,3);
/**
* talloc_vasprintf - vsprintf into a talloc buffer.
......@@ -626,7 +626,8 @@ char *WARN_UNUSED_RESULT talloc_asprintf_append(char *s, const char *fmt, ...)
*
* talloc_set_name_const(ptr, ptr)
*/
char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
PRINTF_FMT(2,0);
/**
* talloc_vasprintf_append - sprintf onto the end of a talloc buffer.
......@@ -638,7 +639,7 @@ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIB
* talloc_asprintf_append(), except it takes a va_list.
*/
char *WARN_UNUSED_RESULT talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
PRINTF_ATTRIBUTE(2,0);
PRINTF_FMT(2,0);
/**
* talloc_set_type - force the name of a pointer to a particular type
......@@ -714,7 +715,8 @@ int talloc_increase_ref_count(const void *ptr);
* without releasing the name. All of the memory is released when the ptr is
* freed using talloc_free().
*/
const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
const char *talloc_set_name(const void *ptr, const char *fmt, ...)
PRINTF_FMT(2,3);
/**
* talloc_set_name_const - set a talloc pointer name to a string constant
......@@ -745,7 +747,7 @@ void talloc_set_name_const(const void *ptr, const char *name);
* talloc_set_name(ptr, fmt, ....);
*/
void *talloc_named(const void *context, size_t size,
const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
const char *fmt, ...) PRINTF_FMT(3,4);
/**
* talloc_named_const - create a specifically-named talloc pointer
......@@ -788,7 +790,7 @@ void *talloc_check_name(const void *ptr, const char *name);
*
* talloc_named(NULL, 0, fmt, ...);
*/
void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
void *talloc_init(const char *fmt, ...) PRINTF_FMT(1,2);
/**
* talloc_total_size - get the bytes used by the pointer and its children
......
......@@ -118,7 +118,7 @@ void plan_tests(unsigned int tests);
# define skip_end } while(0)
unsigned int _gen_result(int, const char *, const char *, unsigned int,
const char *, ...) PRINTF_ATTRIBUTE(5, 6);
const char *, ...) PRINTF_FMT(5, 6);
/**
* diag - print a diagnostic message (use instead of printf/fprintf)
......@@ -130,7 +130,7 @@ unsigned int _gen_result(int, const char *, const char *, unsigned int,
* Example:
* diag("Now running complex tests");
*/
void diag(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
void diag(const char *fmt, ...) PRINTF_FMT(1, 2);
/**
* skip - print a diagnostic message (use instead of printf/fprintf)
......@@ -153,7 +153,7 @@ void diag(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
* skip(1, "Don't have SOME_FEATURE");
* #endif
*/
void skip(unsigned int n, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
void skip(unsigned int n, const char *fmt, ...) PRINTF_FMT(2, 3);
/**
* todo_start - mark tests that you expect to fail.
......@@ -183,7 +183,7 @@ void skip(unsigned int n, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
* ok(dwim(), "Did what the user wanted");
* todo_end();
*/
void todo_start(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
void todo_start(const char *fmt, ...) PRINTF_FMT(1, 2);
/**
* todo_end - end of tests you expect to fail.
......
......@@ -151,7 +151,7 @@ struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
}
/* a default logging function */
static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_FMT(3, 4);
static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
{
}
......
......@@ -82,7 +82,7 @@ typedef struct TDB_DATA {
typedef struct tdb_context TDB_CONTEXT;
typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
typedef void (*tdb_log_func)(struct tdb_context *, enum tdb_debug_level, const char *, ...) PRINTF_ATTRIBUTE(3, 4);
typedef void (*tdb_log_func)(struct tdb_context *, enum tdb_debug_level, const char *, ...) PRINTF_FMT(3, 4);
typedef unsigned int (*tdb_hash_func)(TDB_DATA *key);
struct tdb_logging_context {
......
......@@ -41,8 +41,8 @@ static int loopnum;
static int count_pipe;
static struct tdb_logging_context log_ctx;
#ifdef PRINTF_ATTRIBUTE
static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
#ifdef PRINTF_FMT
static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_FMT(3,4);
#endif
static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
{
......
......@@ -10,7 +10,7 @@ struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
/* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
static struct tdb_context *tdbs = NULL;
PRINTF_ATTRIBUTE(4, 5) static void
PRINTF_FMT(4, 5) static void
null_log_fn(struct tdb_context *tdb,
enum tdb_debug_level level, void *priv,
const char *fmt, ...)
......
......@@ -80,7 +80,7 @@ struct tdb_context;
/* FIXME: Make typesafe */
typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
typedef void (*tdb_logfn_t)(struct tdb_context *, enum tdb_debug_level, void *priv, const char *, ...) PRINTF_ATTRIBUTE(4, 5);
typedef void (*tdb_logfn_t)(struct tdb_context *, enum tdb_debug_level, void *priv, const char *, ...) PRINTF_FMT(4, 5);
typedef uint64_t (*tdb_hashfn_t)(const void *key, size_t len, uint64_t seed,
void *priv);
......
......@@ -43,8 +43,8 @@ static int count_pipe;
static union tdb_attribute log_attr;
static union tdb_attribute seed_attr;
#ifdef PRINTF_ATTRIBUTE
static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *private, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
#ifdef PRINTF_FMT
static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *private, const char *format, ...) PRINTF_FMT(4,5);
#endif
static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *private, const char *format, ...)
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment