7 * Miscellanous utility macros:
8 * - portability wrappers for various gcc extensions like __attribute__()
11 * Copied from glib, names replaced to avoid clashing with glib.
15 /* Here we provide RPM_GNUC_EXTENSION as an alias for __extension__,
16 * where this is valid. This allows for warningless compilation of
17 * "long long" types even in the presence of '-ansi -pedantic'.
19 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
20 # define RPM_GNUC_EXTENSION __extension__
22 # define RPM_GNUC_EXTENSION
25 /* Provide macros to feature the GCC function attribute.
27 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
28 #define RPM_GNUC_PURE \
29 __attribute__((__pure__))
30 #define RPM_GNUC_MALLOC \
31 __attribute__((__malloc__))
34 #define RPM_GNUC_MALLOC
37 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
38 #define RPM_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
39 #define RPM_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y)))
41 #define RPM_GNUC_ALLOC_SIZE(x)
42 #define RPM_GNUC_ALLOC_SIZE2(x,y)
46 #define RPM_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
48 #define RPM_GNUC_NULL_TERMINATED
51 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
52 #define RPM_GNUC_PRINTF( format_idx, arg_idx ) \
53 __attribute__((__format__ (__printf__, format_idx, arg_idx)))
54 #define RPM_GNUC_SCANF( format_idx, arg_idx ) \
55 __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
56 #define RPM_GNUC_FORMAT( arg_idx ) \
57 __attribute__((__format_arg__ (arg_idx)))
58 #define RPM_GNUC_NORETURN \
59 __attribute__((__noreturn__))
60 #define RPM_GNUC_CONST \
61 __attribute__((__const__))
62 #define RPM_GNUC_UNUSED \
63 __attribute__((__unused__))
64 #define RPM_GNUC_NO_INSTRUMENT \
65 __attribute__((__no_instrument_function__))
67 #define RPM_GNUC_PRINTF( format_idx, arg_idx )
68 #define RPM_GNUC_SCANF( format_idx, arg_idx )
69 #define RPM_GNUC_FORMAT( arg_idx )
70 #define RPM_GNUC_NORETURN
71 #define RPM_GNUC_CONST
72 #define RPM_GNUC_UNUSED
73 #define RPM_GNUC_NO_INSTRUMENT
74 #endif /* !__GNUC__ */
76 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
77 #define RPM_GNUC_DEPRECATED \
78 __attribute__((__deprecated__))
80 #define RPM_GNUC_DEPRECATED
83 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
84 #define RPM_GNUC_MAY_ALIAS __attribute__((may_alias))
85 #define RPM_GNUC_NONNULL( ... ) \
86 __attribute__((__nonnull__ (__VA_ARGS__)))
88 #define RPM_GNUC_MAY_ALIAS
89 #define RPM_GNUC_NONNULL( ... )
92 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
93 #define RPM_GNUC_WARN_UNUSED_RESULT \
94 __attribute__((warn_unused_result))
96 #define RPM_GNUC_WARN_UNUSED_RESULT
99 #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
100 # define RPM_GNUC_INTERNAL __attribute__((visibility("hidden")))
102 # define RPM_GNUC_INTERNAL
106 /* Guard C code in headers, while including them from C++ */
108 # define RPM_BEGIN_DECLS extern "C" {
109 # define RPM_END_DECLS }
111 # define RPM_BEGIN_DECLS
112 # define RPM_END_DECLS
119 /* Rpm specific allocators which never return NULL but terminate on failure */
120 RPM_GNUC_MALLOC RPM_GNUC_ALLOC_SIZE(1)
121 void * rmalloc(size_t size);
123 RPM_GNUC_MALLOC RPM_GNUC_ALLOC_SIZE2(1,2)
124 void * rcalloc(size_t nmemb, size_t size);
126 RPM_GNUC_ALLOC_SIZE(2)
127 void * rrealloc(void *ptr, size_t size);
129 char * rstrdup(const char *str);
131 /* Rpm specific free() which returns NULL */
132 void * rfree(void *ptr);
135 * Memory allocation failure callback prototype. When registered through
136 * rpmSetMemFail(), this gets called if memory allocation through rmalloc()
137 * and friends fails. If the application can somehow recover memory here,
138 * it can return a newly allocated memory block of requested size, otherwise
139 * it must return NULL after performing it's own shutdown deeds or
141 * @param size Size of allocation request in bytes
142 * @param data User data (or NULL)
143 * @return Allocated memory block of requested size or NULL
145 typedef void * (*rpmMemFailFunc) (size_t size, void *data);
148 * Set memory allocation failure callback.
149 * @param func Allocation failure callback function
150 * @param data User data (or NULL)
151 * @return Previous callback function
153 rpmMemFailFunc rpmSetMemFail(rpmMemFailFunc func, void *data);
159 #endif /* _RPMUTIL_H */