00001 #ifndef LIBNAGIOS_nsutils_h__ 00002 #define LIBNAGIOS_nsutils_h__ 00003 #include <sys/types.h> 00004 00005 /** 00006 * @file nsutils.h 00007 * @brief Non-Standard (or Nagios) utility functions and macros. 00008 * 00009 * This is where we house all helpers and macros that fall outside 00010 * the "standard-ish" norm. The prefixes "nsu_" and NSU_ are 00011 * reserved for this purpose, so we avoid clashing with other 00012 * applications that may have similarly-acting functions with 00013 * identical names. 00014 * 00015 * The functions already here lack the nsu_ prefix for backwards 00016 * compatibility reasons. It's possible we'll have to fix that 00017 * some day, but let's leave that for later. 00018 * 00019 * @{ 00020 */ 00021 00022 /** Macro for dynamically increasing vector lengths */ 00023 #define alloc_nr(x) (((x)+16)*3/2) 00024 00025 /** 00026 * Check if a number is a power of 2 00027 * @param x The number to check 00028 * @return 1 if the number is a power of 2, 0 if it's not 00029 */ 00030 static inline int nsu_ispow2(unsigned int x) 00031 { 00032 return x > 1 ? !(x & (x - 1)) : 0; 00033 } 00034 00035 /** 00036 * Round up to a power of 2 00037 * Yes, this is the most cryptic function name in all of Nagios, but I 00038 * like it, so shush. 00039 * @param r The number to round up 00040 * @return r, rounded up to the nearest power of 2. 00041 */ 00042 static inline unsigned int rup2pof2(unsigned int r) 00043 { 00044 r--; 00045 if (!r) 00046 return 2; 00047 r |= r >> 1; 00048 r |= r >> 2; 00049 r |= r >> 4; 00050 r |= r >> 8; 00051 r |= r >> 16; 00052 00053 return r + 1; 00054 } 00055 00056 /** 00057 * Grab a random unsigned int in the range between low and high. 00058 * Note that the PRNG has to be seeded prior to calling this. 00059 * @param low The lower bound, inclusive 00060 * @param high The higher bound, inclusive 00061 * @return An unsigned integer in the mathematical range [low, high] 00062 */ 00063 static inline unsigned int ranged_urand(unsigned int low, unsigned int high) 00064 { 00065 return low + (rand() * (1.0 / (RAND_MAX + 1.0)) * (high - low)); 00066 } 00067 00068 /** 00069 * Get number of online cpus 00070 * @return Active cpu cores detected on success. 0 on failure. 00071 */ 00072 extern int real_online_cpus(void); 00073 00074 /** 00075 * Wrapper for real_online_cpus(), returning 1 in case we can't 00076 * detect any active cpus. 00077 * @return Number of active cpu cores on success. 1 on failure. 00078 */ 00079 extern int online_cpus(void); 00080 00081 /** 00082 * Create a short-lived string in stack-allocated memory 00083 * The number and size of strings is limited (currently to 256 strings of 00084 * 32 bytes each), so beware and use this sensibly. Intended for 00085 * number-to-string conversion and other short strings. 00086 * @note The returned string must *not* be free()'d! 00087 * @param[in] fmt The format string 00088 * @return A pointer to the formatted string on success. Undefined on errors 00089 */ 00090 extern const char *mkstr(const char *fmt, ...) 00091 __attribute__((__format__(__printf__, 1, 2))); 00092 00093 /** 00094 * Calculate the millisecond delta between two timeval structs 00095 * @param[in] start The start time 00096 * @param[in] stop The stop time 00097 * @return The millisecond delta between the two structs 00098 */ 00099 extern int tv_delta_msec(const struct timeval *start, const struct timeval *stop); 00100 00101 00102 /** 00103 * Get timeval delta as seconds 00104 * @param start The start time 00105 * @param stop The stop time 00106 * @return time difference in fractions of seconds 00107 */ 00108 extern float tv_delta_f(const struct timeval *start, const struct timeval *stop); 00109 00110 /** @} */ 00111 #endif /* LIBNAGIOS_nsutils_h__ */