utils: add and use ARRAY_SIZE macro
[platform/upstream/libxkbcommon.git] / src / utils.h
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #ifndef UTILS_H
25 #define UTILS_H 1
26
27 #include <inttypes.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <strings.h>
31
32 #include "darray.h"
33
34 /*
35  * We sometimes malloc strings and then expose them as const char*'s. This
36  * macro is used when we free these strings in order to avoid -Wcast-qual
37  * errors.
38  */
39 #define UNCONSTIFY(const_ptr)  ((void *) (uintptr_t) (const_ptr))
40
41 static inline bool
42 streq(const char *s1, const char *s2)
43 {
44     return strcmp(s1, s2) == 0;
45 }
46
47 static inline bool
48 streq_not_null(const char *s1, const char *s2)
49 {
50     if (!s1 || !s2)
51         return false;
52     return streq(s1, s2);
53 }
54
55 static inline bool
56 istreq(const char *s1, const char *s2)
57 {
58     return strcasecmp(s1, s2) == 0;
59 }
60
61 static inline bool
62 istreq_prefix(const char *s1, const char *s2)
63 {
64     return strncasecmp(s1, s2, strlen(s1)) == 0;
65 }
66
67 static inline char *
68 strdup_safe(const char *s)
69 {
70     return s ? strdup(s) : NULL;
71 }
72
73 static inline bool
74 isempty(const char *s)
75 {
76     return s == NULL || s[0] == '\0';
77 }
78
79 static inline const char *
80 strnull(const char *s)
81 {
82     return s ? s : "(null)";
83 }
84
85 static inline void *
86 memdup(const void *mem, size_t nmemb, size_t size)
87 {
88     void *p = malloc(nmemb * size);
89     if (p)
90         memcpy(p, mem, nmemb * size);
91     return p;
92 }
93
94 #define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
95
96 #define MIN(a, b) ((a) < (b) ? (a) : (b))
97 #define MIN3(a, b, c) MIN(MIN((a), (b)), (c))
98 #define MAX(a, b) ((a) > (b) ? (a) : (b))
99 #define MAX3(a, b, c) MAX(MAX((a), (b)), (c))
100
101 /* Compiler Attributes */
102
103 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__CYGWIN__)
104 # define XKB_EXPORT      __attribute__((visibility("default")))
105 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
106 # define XKB_EXPORT      __global
107 #else /* not gcc >= 4 and not Sun Studio >= 8 */
108 # define XKB_EXPORT
109 #endif
110
111 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)
112 # define ATTR_PRINTF(x,y) __attribute__((__format__(__printf__, x, y)))
113 #else /* not gcc >= 2.3 */
114 # define ATTR_PRINTF(x,y)
115 #endif
116
117 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
118     || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
119 # define ATTR_NORETURN __attribute__((__noreturn__))
120 #else
121 # define ATTR_NORETURN
122 #endif /* GNUC  */
123
124 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 296)
125 #define ATTR_MALLOC  __attribute__((__malloc__))
126 #else
127 #define ATTR_MALLOC
128 #endif
129
130 #if defined(__GNUC__) && (__GNUC__ >= 4)
131 # define ATTR_NULL_SENTINEL __attribute__((__sentinel__))
132 #else
133 # define ATTR_NULL_SENTINEL
134 #endif /* GNUC >= 4 */
135
136 #endif /* UTILS_H */