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