Nagios 4.4.2
Dev docs for Nagios core and neb-module hackers

nsutils.h

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