Nagios  4.0.1
Dev docs for Nagios core and neb-module hackers
 All Data Structures Files Functions Variables Typedefs Macros Pages
nsutils.h
Go to the documentation of this file.
1 #ifndef LIBNAGIOS_nsutils_h__
2 #define LIBNAGIOS_nsutils_h__
3 #include <sys/types.h>
4 
5 /**
6  * @file nsutils.h
7  * @brief Non-Standard (or Nagios) utility functions and macros.
8  *
9  * This is where we house all helpers and macros that fall outside
10  * the "standard-ish" norm. The prefixes "nsu_" and NSU_ are
11  * reserved for this purpose, so we avoid clashing with other
12  * applications that may have similarly-acting functions with
13  * identical names.
14  *
15  * The functions already here lack the nsu_ prefix for backwards
16  * compatibility reasons. It's possible we'll have to fix that
17  * some day, but let's leave that for later.
18  *
19  * @{
20  */
21 
22 /** Macro for dynamically increasing vector lengths */
23 #define alloc_nr(x) (((x)+16)*3/2)
24 
25 /**
26  * Check if a number is a power of 2
27  * @param x The number to check
28  * @return 1 if the number is a power of 2, 0 if it's not
29  */
30 static inline int nsu_ispow2(unsigned int x)
31 {
32  return x > 1 ? !(x & (x - 1)) : 0;
33 }
34 
35 /**
36  * Round up to a power of 2
37  * Yes, this is the most cryptic function name in all of Nagios, but I
38  * like it, so shush.
39  * @param r The number to round up
40  * @return r, rounded up to the nearest power of 2.
41  */
42 static inline unsigned int rup2pof2(unsigned int r)
43 {
44  r--;
45  if (!r)
46  return 2;
47  r |= r >> 1;
48  r |= r >> 2;
49  r |= r >> 4;
50  r |= r >> 8;
51  r |= r >> 16;
52 
53  return r + 1;
54 }
55 
56 /**
57  * Grab a random unsigned int in the range between low and high.
58  * Note that the PRNG has to be seeded prior to calling this.
59  * @param low The lower bound, inclusive
60  * @param high The higher bound, inclusive
61  * @return An unsigned integer in the mathematical range [low, high]
62  */
63 static inline unsigned int ranged_urand(unsigned int low, unsigned int high)
64 {
65  return low + (rand() * (1.0 / (RAND_MAX + 1.0)) * (high - low));
66 }
67 
68 /**
69  * Get number of online cpus
70  * @return Active cpu cores detected on success. 0 on failure.
71  */
72 extern int real_online_cpus(void);
73 
74 /**
75  * Wrapper for real_online_cpus(), returning 1 in case we can't
76  * detect any active cpus.
77  * @return Number of active cpu cores on success. 1 on failure.
78  */
79 extern int online_cpus(void);
80 
81 /**
82  * Create a short-lived string in stack-allocated memory
83  * The number and size of strings is limited (currently to 256 strings of
84  * 32 bytes each), so beware and use this sensibly. Intended for
85  * number-to-string conversion and other short strings.
86  * @note The returned string must *not* be free()'d!
87  * @param[in] fmt The format string
88  * @return A pointer to the formatted string on success. Undefined on errors
89  */
90 extern const char *mkstr(const char *fmt, ...)
91  __attribute__((__format__(__printf__, 1, 2)));
92 
93 /**
94  * Calculate the millisecond delta between two timeval structs
95  * @param[in] start The start time
96  * @param[in] stop The stop time
97  * @return The millisecond delta between the two structs
98  */
99 extern int tv_delta_msec(const struct timeval *start, const struct timeval *stop);
100 
101 
102 /**
103  * Get timeval delta as seconds
104  * @param start The start time
105  * @param stop The stop time
106  * @return time difference in fractions of seconds
107  */
108 extern float tv_delta_f(const struct timeval *start, const struct timeval *stop);
109 
110 /** @} */
111 #endif /* LIBNAGIOS_nsutils_h__ */
const char * mkstr(const char *fmt,...) __attribute__((__format__(__printf__
Create a short-lived string in stack-allocated memory The number and size of strings is limited (curr...
float tv_delta_f(const struct timeval *start, const struct timeval *stop)
Get timeval delta as seconds.
const char int tv_delta_msec(const struct timeval *start, const struct timeval *stop)
Calculate the millisecond delta between two timeval structs.
int real_online_cpus(void)
Get number of online cpus.
int online_cpus(void)
Wrapper for real_online_cpus(), returning 1 in case we can&#39;t detect any active cpus.