X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Futil.c;h=639424dd6ea683b915c0302e7b5c559066c99d47;hb=0ab14c8e284b7c5c68aff1ccc6795a1d4b58bd49;hp=b9dbdc48063164e1755c0c887901cd2f6d621306;hpb=c1c8308a3971526f5126201a6d1659c9247c1f0a;p=openvswitch diff --git a/lib/util.c b/lib/util.c index b9dbdc48..639424dd 100644 --- a/lib/util.c +++ b/lib/util.c @@ -16,8 +16,11 @@ #include #include "util.h" +#include #include +#include #include +#include #include #include #include @@ -204,9 +207,14 @@ ovs_fatal(int err_no, const char *format, ...) va_list args; va_start(args, format); - ovs_error_valist(err_no, format, args); - va_end(args); + ovs_fatal_valist(err_no, format, args); +} +/* Same as ovs_fatal() except that the arguments are supplied as a va_list. */ +void +ovs_fatal_valist(int err_no, const char *format, va_list args) +{ + ovs_error_valist(err_no, format, args); exit(EXIT_FAILURE); } @@ -591,3 +599,46 @@ abs_file_name(const char *dir, const char *file_name) * its return value. (Note that every scalar type can be implicitly * converted to bool.) */ void ignore(bool x OVS_UNUSED) { } + +/* Returns an appropriate delimiter for inserting just before the 0-based item + * 'index' in a list that has 'total' items in it. */ +const char * +english_list_delimiter(size_t index, size_t total) +{ + return (index == 0 ? "" + : index < total - 1 ? ", " + : total > 2 ? ", and " + : " and "); +} + +/* Given a 32 bit word 'n', calculates floor(log_2('n')). This is equivalent + * to finding the bit position of the most significant one bit in 'n'. It is + * an error to call this function with 'n' == 0. */ +int +log_2_floor(uint32_t n) +{ + assert(n); + +#if !defined(UINT_MAX) || !defined(UINT32_MAX) +#error "Someone screwed up the #includes." +#elif __GNUC__ >= 4 && UINT_MAX == UINT32_MAX + return 31 - __builtin_clz(n); +#else + { + int log = 0; + +#define BIN_SEARCH_STEP(BITS) \ + if (n >= (1 << BITS)) { \ + log += BITS; \ + n >>= BITS; \ + } + BIN_SEARCH_STEP(16); + BIN_SEARCH_STEP(8); + BIN_SEARCH_STEP(4); + BIN_SEARCH_STEP(2); + BIN_SEARCH_STEP(1); +#undef BIN_SEARCH_STEP + return log; + } +#endif +}