2 * libvirt-utils.h: misc helper APIs for python binding
4 * Copyright (C) 2013 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
22 #ifndef __LIBVIRT_UTILS_H__
23 # define __LIBVIRT_UTILS_H__
25 # define STREQ(a,b) (strcmp(a,b) == 0)
28 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
32 * libvirt.h provides this as of version 1.2.0, but we want to be able
33 * to support older versions of libvirt so copy and paste the macro from
36 # ifndef LIBVIR_CHECK_VERSION
37 # define LIBVIR_CHECK_VERSION(major, minor, micro) \
38 ((major) * 1000000 + (minor) * 1000 + (micro) <= LIBVIR_VERSION_NUMBER)
41 /* Return 1 if an array of N objects, each of size S, cannot exist due
42 to size arithmetic overflow. S must be positive and N must be
43 nonnegative. This is a macro, not a function, so that it
44 works correctly even when SIZE_MAX < N.
46 By gnulib convention, SIZE_MAX represents overflow in size
47 calculations, so the conservative dividend to use here is
48 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
49 However, malloc (SIZE_MAX) fails on all known hosts where
50 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
51 exactly-SIZE_MAX allocations on such hosts; this avoids a test and
52 branch when S is known to be 1. */
53 # define xalloc_oversized(n, s) \
54 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
57 /* The __attribute__((__warn_unused_result__)) feature
58 is available in gcc versions 3.4 and newer,
59 while the typeof feature has been available since 2.7 at least. */
60 # if 3 < __GNUC__ + (4 <= __GNUC_MINOR__)
61 # define ignore_value(x) \
62 (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))
64 # define ignore_value(x) ((void) (x))
69 # ifndef __GNUC_PREREQ
70 # if defined __GNUC__ && defined __GNUC_MINOR__
71 # define __GNUC_PREREQ(maj, min) \
72 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
74 # define __GNUC_PREREQ(maj, min) 0
76 # endif /* __GNUC_PREREQ */
81 * Macro to flag consciously unused parameters to functions
83 # ifndef ATTRIBUTE_UNUSED
84 # define ATTRIBUTE_UNUSED __attribute__((__unused__))
87 /* gcc's handling of attribute nonnull is less than stellar - it does
88 * NOT improve diagnostics, and merely allows gcc to optimize away
89 * null code checks even when the caller manages to pass null in spite
90 * of the attribute, leading to weird crashes. Coverity, on the other
91 * hand, knows how to do better static analysis based on knowing
92 * whether a parameter is nonnull. Make this attribute conditional
93 * based on whether we are compiling for real or for analysis, while
94 * still requiring correct gcc syntax when it is turned off. See also
95 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17308 */
96 # ifndef ATTRIBUTE_NONNULL
97 # if __GNUC_PREREQ (3, 3)
99 # define ATTRIBUTE_NONNULL(m) __attribute__((__nonnull__(m)))
101 # define ATTRIBUTE_NONNULL(m) __attribute__(())
104 # define ATTRIBUTE_NONNULL(m)
108 # ifndef ATTRIBUTE_RETURN_CHECK
109 # if __GNUC_PREREQ (3, 4)
110 # define ATTRIBUTE_RETURN_CHECK __attribute__((__warn_unused_result__))
112 # define ATTRIBUTE_RETURN_CHECK
117 # ifndef ATTRIBUTE_UNUSED
118 # define ATTRIBUTE_UNUSED
120 # ifndef ATTRIBUTE_NONNULL
121 # define ATTRIBUTE_NONNULL(m)
123 # ifndef ATTRIBUTE_RETURN_CHECK
124 # define ATTRIBUTE_RETURN_CHECK
126 # endif /* __GNUC__ */
129 /* Don't call these directly - use the macros below */
130 int virAlloc(void *ptrptr
, size_t size
)
131 ATTRIBUTE_RETURN_CHECK
ATTRIBUTE_NONNULL(1);
132 int virAllocN(void *ptrptr
, size_t size
, size_t count
)
133 ATTRIBUTE_RETURN_CHECK
ATTRIBUTE_NONNULL(1);
134 int virReallocN(void *ptrptr
, size_t size
, size_t count
)
135 ATTRIBUTE_RETURN_CHECK
ATTRIBUTE_NONNULL(1);
136 void virFree(void *ptrptr
) ATTRIBUTE_NONNULL(1);
140 * @ptr: pointer to hold address of allocated memory
142 * Allocate sizeof(*ptr) bytes of memory and store
143 * the address of allocated memory in 'ptr'. Fill the
144 * newly allocated memory with zeros.
146 * This macro is safe to use on arguments with side effects.
148 * Returns -1 on failure (with OOM error reported), 0 on success
150 # define VIR_ALLOC(ptr) virAlloc(&(ptr), sizeof(*(ptr)))
154 * @ptr: pointer to hold address of allocated memory
155 * @count: number of elements to allocate
157 * Allocate an array of 'count' elements, each sizeof(*ptr)
158 * bytes long and store the address of allocated memory in
159 * 'ptr'. Fill the newly allocated memory with zeros.
161 * This macro is safe to use on arguments with side effects.
163 * Returns -1 on failure (with OOM error reported), 0 on success
165 # define VIR_ALLOC_N(ptr, count) virAllocN(&(ptr), sizeof(*(ptr)), (count))
169 * @ptr: pointer to hold address of allocated memory
170 * @count: number of elements to allocate
172 * Re-allocate an array of 'count' elements, each sizeof(*ptr)
173 * bytes long and store the address of allocated memory in
174 * 'ptr'. If 'ptr' grew, the added memory is uninitialized.
176 * This macro is safe to use on arguments with side effects.
178 * Returns -1 on failure (with OOM error reported), 0 on success
180 # define VIR_REALLOC_N(ptr, count) virReallocN(&(ptr), sizeof(*(ptr)), (count))
184 * @ptr: pointer holding address to be freed
186 * Free the memory stored in 'ptr' and update to point
189 * This macro is safe to use on arguments with side effects.
191 # if !STATIC_ANALYSIS
192 /* The ternary ensures that ptr is a pointer and not an integer type,
193 * while evaluating ptr only once. This gives us extra compiler
194 * safety when compiling under gcc. For now, we intentionally cast
195 * away const, since a number of callers safely pass const char *.
197 # define VIR_FREE(ptr) virFree((void *) (1 ? (const void *) &(ptr) : (ptr)))
199 /* The Coverity static analyzer considers the else path of the "?:" and
200 * flags the VIR_FREE() of the address of the address of memory as a
201 * RESOURCE_LEAK resulting in numerous false positives (eg, VIR_FREE(&ptr))
203 # define VIR_FREE(ptr) virFree((void *) &(ptr))
206 /* Don't call this directly - use the macro below */
207 int virFileClose(int *fdptr
)
208 ATTRIBUTE_RETURN_CHECK
;
210 # define VIR_FORCE_CLOSE(FD) \
211 ignore_value(virFileClose(&(FD)))
213 # if ! LIBVIR_CHECK_VERSION(1, 0, 2)
214 void virTypedParamsClear(virTypedParameterPtr params
, int nparams
);
216 void virTypedParamsFree(virTypedParameterPtr params
, int nparams
);
217 # endif /* ! LIBVIR_CHECK_VERSION(1, 0, 2) */
219 #endif /* __LIBVIRT_UTILS_H__ */