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