8c1c490eedde18b0615741ee1ec1a9773c0589ab
[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 #include <ust/config.h>
23
24 #define likely(x) __builtin_expect(!!(x), 1)
25 #define unlikely(x) __builtin_expect(!!(x), 0)
26
27 /* ARRAYS */
28
29 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
30
31
32 /* ALIGNMENT SHORTCUTS */
33
34 #include <unistd.h>
35
36 #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
37 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
38 #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
39 #define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
40 #define PAGE_MASK (~(PAGE_SIZE-1))
41
42 /* ERROR OPS */
43 #define MAX_ERRNO 4095
44
45 #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
46
47 static inline void *ERR_PTR(long error)
48 {
49 return (void *) error;
50 }
51
52 static inline long PTR_ERR(const void *ptr)
53 {
54 return (long) ptr;
55 }
56
57 static inline long IS_ERR(const void *ptr)
58 {
59 return IS_ERR_VALUE((unsigned long)ptr);
60 }
61
62
63 /* Min / Max */
64
65 #define min_t(type, x, y) ({ \
66 type __min1 = (x); \
67 type __min2 = (y); \
68 __min1 < __min2 ? __min1: __min2; })
69
70 #define max_t(type, x, y) ({ \
71 type __max1 = (x); \
72 type __max2 = (y); \
73 __max1 > __max2 ? __max1: __max2; })
74
75
76 /* MUTEXES */
77
78 #include <pthread.h>
79
80 #define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER;
81 #define DECLARE_MUTEX(m) extern pthread_mutex_t (m);
82
83 /* MALLOCATION */
84
85 #include <stdlib.h>
86
87 static inline
88 void *zmalloc(size_t len)
89 {
90 return calloc(1, len);
91 }
92
93 static inline
94 void *malloc_align(size_t len)
95 {
96 return malloc(ALIGN(len, CAA_CACHE_LINE_SIZE));
97 }
98
99 static inline
100 void *zmalloc_align(size_t len)
101 {
102 return calloc(1, ALIGN(len, CAA_CACHE_LINE_SIZE));
103 }
104
105 /* MATH */
106
107 #include <ust/processor.h>
108 static inline unsigned int hweight32(unsigned int w)
109 {
110 unsigned int res = w - ((w >> 1) & 0x55555555);
111 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
112 res = (res + (res >> 4)) & 0x0F0F0F0F;
113 res = res + (res >> 8);
114 return (res + (res >> 16)) & 0x000000FF;
115 }
116
117 static __inline__ int get_count_order(unsigned int count)
118 {
119 int order;
120
121 order = fls(count) - 1;
122 if (count & (count - 1))
123 order++;
124 return order;
125 }
126
127 #define _ust_container_of(ptr, type, member) ({ \
128 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
129 (type *)( (char *)__mptr - offsetof(type,member) );})
130
131 #ifndef inline_memcpy
132 #define inline_memcpy memcpy
133 #endif
134
135 #ifndef __same_type
136 #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
137 #endif
138
139 #endif /* UST_CORE_H */
This page took 0.031906 seconds and 3 git commands to generate.