Commit | Line | Data |
---|---|---|
4fbf9cd1 MD |
1 | #ifndef UST_CORE_H |
2 | #define UST_CORE_H | |
3 | ||
4 | /* | |
5 | * Copyright (C) 2010 Pierre-Marc Fournier | |
b5234c06 | 6 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
518d7abb PMF |
7 | * |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
b5234c06 MD |
10 | * License as published by the Free Software Foundation; version 2.1 of |
11 | * the License. | |
518d7abb PMF |
12 | * |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
518d7abb | 23 | #include <sys/types.h> |
2699970a | 24 | #include <ust/config.h> |
a6352fd4 | 25 | #include <urcu/arch.h> |
518d7abb PMF |
26 | |
27 | #define likely(x) __builtin_expect(!!(x), 1) | |
28 | #define unlikely(x) __builtin_expect(!!(x), 0) | |
29 | ||
518d7abb PMF |
30 | /* ARRAYS */ |
31 | ||
32 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) | |
33 | ||
34 | ||
35 | /* ALIGNMENT SHORTCUTS */ | |
36 | ||
37 | #include <unistd.h> | |
38 | ||
39 | #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) | |
40 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) | |
41 | #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE) | |
42 | #define PAGE_SIZE sysconf(_SC_PAGE_SIZE) | |
43 | #define PAGE_MASK (~(PAGE_SIZE-1)) | |
44 | ||
45 | /* ERROR OPS */ | |
46 | #define MAX_ERRNO 4095 | |
47 | ||
48 | #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO) | |
49 | ||
50 | static inline void *ERR_PTR(long error) | |
51 | { | |
52 | return (void *) error; | |
53 | } | |
54 | ||
55 | static inline long PTR_ERR(const void *ptr) | |
56 | { | |
57 | return (long) ptr; | |
58 | } | |
59 | ||
60 | static inline long IS_ERR(const void *ptr) | |
61 | { | |
62 | return IS_ERR_VALUE((unsigned long)ptr); | |
63 | } | |
64 | ||
65 | ||
66 | /* Min / Max */ | |
67 | ||
68 | #define min_t(type, x, y) ({ \ | |
69 | type __min1 = (x); \ | |
70 | type __min2 = (y); \ | |
71 | __min1 < __min2 ? __min1: __min2; }) | |
72 | ||
73 | #define max_t(type, x, y) ({ \ | |
74 | type __max1 = (x); \ | |
75 | type __max2 = (y); \ | |
76 | __max1 > __max2 ? __max1: __max2; }) | |
77 | ||
78 | ||
79 | /* MUTEXES */ | |
80 | ||
81 | #include <pthread.h> | |
82 | ||
83 | #define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER; | |
84 | #define DECLARE_MUTEX(m) extern pthread_mutex_t (m); | |
85 | ||
518d7abb PMF |
86 | /* MALLOCATION */ |
87 | ||
14641deb MD |
88 | #include <stdlib.h> |
89 | ||
90 | static inline | |
91 | void *zmalloc(size_t len) | |
92 | { | |
93 | return calloc(1, len); | |
94 | } | |
95 | ||
96 | static inline | |
97 | void *malloc_align(size_t len) | |
98 | { | |
99 | return malloc(ALIGN(len, CAA_CACHE_LINE_SIZE)); | |
100 | } | |
101 | ||
102 | static inline | |
103 | void *zmalloc_align(size_t len) | |
104 | { | |
105 | return calloc(1, ALIGN(len, CAA_CACHE_LINE_SIZE)); | |
106 | } | |
518d7abb | 107 | |
518d7abb PMF |
108 | /* MATH */ |
109 | ||
110 | #include <ust/processor.h> | |
111 | static inline unsigned int hweight32(unsigned int w) | |
112 | { | |
113 | unsigned int res = w - ((w >> 1) & 0x55555555); | |
114 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); | |
115 | res = (res + (res >> 4)) & 0x0F0F0F0F; | |
116 | res = res + (res >> 8); | |
117 | return (res + (res >> 16)) & 0x000000FF; | |
118 | } | |
119 | ||
120 | static __inline__ int get_count_order(unsigned int count) | |
121 | { | |
122 | int order; | |
123 | ||
124 | order = fls(count) - 1; | |
125 | if (count & (count - 1)) | |
126 | order++; | |
127 | return order; | |
128 | } | |
129 | ||
1e4b909b | 130 | #define _ust_container_of(ptr, type, member) ({ \ |
518d7abb PMF |
131 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
132 | (type *)( (char *)__mptr - offsetof(type,member) );}) | |
133 | ||
14641deb MD |
134 | #ifndef inline_memcpy |
135 | #define inline_memcpy memcpy | |
136 | #endif | |
137 | ||
138 | #ifndef __same_type | |
139 | #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) | |
140 | #endif | |
141 | ||
9f3fdbc6 MD |
142 | #ifndef UST_VALGRIND |
143 | ||
144 | static __inline__ int ust_get_cpu(void) | |
145 | { | |
146 | int cpu; | |
147 | ||
148 | cpu = sched_getcpu(); | |
149 | if (likely(cpu >= 0)) | |
150 | return cpu; | |
151 | /* | |
152 | * If getcpu(2) is not implemented in the Kernel use CPU 0 as fallback. | |
153 | */ | |
154 | return 0; | |
155 | } | |
156 | ||
157 | #else /* #else #ifndef UST_VALGRIND */ | |
158 | ||
159 | static __inline__ int ust_get_cpu(void) | |
160 | { | |
161 | /* | |
162 | * Valgrind does not support the sched_getcpu() vsyscall. | |
163 | * It causes it to detect a segfault in the program and stop it. | |
164 | * So if we want to check libust with valgrind, we have to refrain | |
165 | * from using this call. TODO: it would probably be better to return | |
166 | * other values too, to better test it. | |
167 | */ | |
168 | return 0; | |
169 | } | |
170 | ||
171 | #endif /* #else #ifndef UST_VALGRIND */ | |
172 | ||
12e81b07 | 173 | #endif /* UST_CORE_H */ |