X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Flikely%2Flikely.h;h=410772dbde78a37ec99c64261758961ae2f2821b;hp=815ee2287fd0566248075217d9e36b56b2b45009;hb=0e5d0e30b30bb07b6605843e5ff224210d8083d8;hpb=9f8c65b28acba8e5eabea5d7abd98b19e62d06fe diff --git a/ccan/likely/likely.h b/ccan/likely/likely.h index 815ee228..410772db 100644 --- a/ccan/likely/likely.h +++ b/ccan/likely/likely.h @@ -1,10 +1,11 @@ +/* Licensed under LGPLv2.1+ - see LICENSE file for details */ #ifndef CCAN_LIKELY_H #define CCAN_LIKELY_H #include "config.h" #include #include -#ifndef DEBUG +#ifndef CCAN_LIKELY_DEBUG #if HAVE_BUILTIN_EXPECT /** * likely - indicate that a condition is likely to be true. @@ -17,13 +18,13 @@ * 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. * static inline bool inc_int(unsigned int *val) * { - * *(val)++; + * (*val)++; * if (likely(*val)) * return true; * return false; @@ -39,13 +40,13 @@ * code path and optimize appropriately; see likely() above. * * See Also: - * likely(), unlikely_func, likely_stats() + * likely(), likely_stats(), COLD (compiler.h) * * Example: * // Prints a warning if we overflow. * static inline void inc_int(unsigned int *val) * { - * *(val)++; + * (*val)++; * if (unlikely(*val == 0)) * fprintf(stderr, "Overflow!"); * } @@ -55,7 +56,7 @@ #define likely(cond) (!!(cond)) #define unlikely(cond) (!!(cond)) #endif -#else /* DEBUG versions */ +#else /* CCAN_LIKELY_DEBUG versions */ #define likely(cond) \ (_likely_trace(!!(cond), 1, stringify(cond), __FILE__, __LINE__)) #define unlikely(cond) \ @@ -66,39 +67,15 @@ 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 +#ifdef CCAN_LIKELY_DEBUG /** * likely_stats - return description of abused likely()/unlikely() * @min_hits: minimum number of hits * @percent: maximum percentage correct * - * 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). + * When CCAN_LIKELY_DEBUG is defined, likely() and unlikely() trace their + * results: this causes a significant slowdown, but allows analysis of + * whether the branches are labelled correctly. * * This function returns a malloc'ed description of the least-correct * usage of likely() or unlikely(). It ignores places which have been @@ -114,7 +91,7 @@ long _likely_trace(bool cond, bool expect, * // Print every place hit more than twice which was wrong > 5%. * static void report_stats(void) * { - * #ifdef DEBUG + * #ifdef CCAN_LIKELY_DEBUG * const char *bad; * * while ((bad = likely_stats(2, 95)) != NULL) { @@ -124,6 +101,13 @@ long _likely_trace(bool cond, bool expect, * #endif * } */ -const char *likely_stats(unsigned int min_hits, unsigned int percent); -#endif /* DEBUG */ +char *likely_stats(unsigned int min_hits, unsigned int percent); + +/** + * likely_stats_reset - free up memory of likely()/unlikely() branches. + * + * This can also plug memory leaks. + */ +void likely_stats_reset(void); +#endif /* CCAN_LIKELY_DEBUG */ #endif /* CCAN_LIKELY_H */