UST-wide warning fixes/bugfixes
[lttng-ust.git] / include / ust / core.h
1 /* Copyright (C) 2010 Pierre-Marc Fournier
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #ifndef UST_CORE_H
19 #define UST_CORE_H
20
21 #include <sys/types.h>
22
23 #define likely(x) __builtin_expect(!!(x), 1)
24 #define unlikely(x) __builtin_expect(!!(x), 0)
25
26 #if defined(CONFIG_LTT) && defined(CONFIG_LTT_ALIGNMENT)
27
28 /*
29 * Calculate the offset needed to align the type.
30 * size_of_type must be non-zero.
31 */
32 static inline unsigned int ltt_align(size_t align_drift, size_t size_of_type)
33 {
34 size_t alignment = min(sizeof(void *), size_of_type);
35 return (alignment - align_drift) & (alignment - 1);
36 }
37 /* Default arch alignment */
38 #define LTT_ALIGN
39
40 static inline int ltt_get_alignment(void)
41 {
42 return sizeof(void *);
43 }
44
45 #else
46
47 static inline unsigned int ltt_align(size_t align_drift,
48 size_t size_of_type)
49 {
50 return 0;
51 }
52
53 #define LTT_ALIGN __attribute__((packed))
54
55 static inline int ltt_get_alignment(void)
56 {
57 return 0;
58 }
59 #endif /* defined(CONFIG_LTT) && defined(CONFIG_LTT_ALIGNMENT) */
60
61
62 /* ARRAYS */
63
64 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
65
66
67 /* ALIGNMENT SHORTCUTS */
68
69 #include <unistd.h>
70
71 #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
72 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
73 #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
74 #define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
75 #define PAGE_MASK (~(PAGE_SIZE-1))
76
77 /* ERROR OPS */
78 #define MAX_ERRNO 4095
79
80 #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
81
82 static inline void *ERR_PTR(long error)
83 {
84 return (void *) error;
85 }
86
87 static inline long PTR_ERR(const void *ptr)
88 {
89 return (long) ptr;
90 }
91
92 static inline long IS_ERR(const void *ptr)
93 {
94 return IS_ERR_VALUE((unsigned long)ptr);
95 }
96
97
98 /* Min / Max */
99
100 #define min_t(type, x, y) ({ \
101 type __min1 = (x); \
102 type __min2 = (y); \
103 __min1 < __min2 ? __min1: __min2; })
104
105 #define max_t(type, x, y) ({ \
106 type __max1 = (x); \
107 type __max2 = (y); \
108 __max1 > __max2 ? __max1: __max2; })
109
110
111 /* MUTEXES */
112
113 #include <pthread.h>
114
115 #define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER;
116 #define DECLARE_MUTEX(m) extern pthread_mutex_t (m);
117
118 /* MALLOCATION */
119
120 #define zmalloc(s) calloc(1, s)
121
122 /* MATH */
123
124 #include <ust/processor.h>
125 static inline unsigned int hweight32(unsigned int w)
126 {
127 unsigned int res = w - ((w >> 1) & 0x55555555);
128 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
129 res = (res + (res >> 4)) & 0x0F0F0F0F;
130 res = res + (res >> 8);
131 return (res + (res >> 16)) & 0x000000FF;
132 }
133
134 static __inline__ int get_count_order(unsigned int count)
135 {
136 int order;
137
138 order = fls(count) - 1;
139 if (count & (count - 1))
140 order++;
141 return order;
142 }
143
144 #define _ust_container_of(ptr, type, member) ({ \
145 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
146 (type *)( (char *)__mptr - offsetof(type,member) );})
147
148 #endif /* UST_CORE_H */
This page took 0.031856 seconds and 4 git commands to generate.