Installed headers relicensing and cleanup
[lttng-ust.git] / include / lttng / core.h
CommitLineData
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>
4318ae1b 24#include <lttng/config.h>
a6352fd4 25#include <urcu/arch.h>
b5a3dfa5 26#include <urcu/compiler.h>
518d7abb 27
518d7abb
PMF
28/* ARRAYS */
29
30#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
31
32
33/* ALIGNMENT SHORTCUTS */
34
35#include <unistd.h>
36
37#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
38#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
39#define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
40#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
41#define PAGE_MASK (~(PAGE_SIZE-1))
42
43/* ERROR OPS */
44#define MAX_ERRNO 4095
45
b5a3dfa5 46#define IS_ERR_VALUE(x) caa_unlikely((x) >= (unsigned long)-MAX_ERRNO)
518d7abb
PMF
47
48static inline void *ERR_PTR(long error)
49{
50 return (void *) error;
51}
52
53static inline long PTR_ERR(const void *ptr)
54{
55 return (long) ptr;
56}
57
58static inline long IS_ERR(const void *ptr)
59{
60 return IS_ERR_VALUE((unsigned long)ptr);
61}
62
63
64/* Min / Max */
65
66#define min_t(type, x, y) ({ \
67 type __min1 = (x); \
68 type __min2 = (y); \
69 __min1 < __min2 ? __min1: __min2; })
70
71#define max_t(type, x, y) ({ \
72 type __max1 = (x); \
73 type __max2 = (y); \
74 __max1 > __max2 ? __max1: __max2; })
75
76
77/* MUTEXES */
78
79#include <pthread.h>
80
81#define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER;
82#define DECLARE_MUTEX(m) extern pthread_mutex_t (m);
83
518d7abb
PMF
84/* MALLOCATION */
85
14641deb
MD
86#include <stdlib.h>
87
88static inline
89void *zmalloc(size_t len)
90{
91 return calloc(1, len);
92}
93
94static inline
95void *malloc_align(size_t len)
96{
97 return malloc(ALIGN(len, CAA_CACHE_LINE_SIZE));
98}
99
100static inline
101void *zmalloc_align(size_t len)
102{
103 return calloc(1, ALIGN(len, CAA_CACHE_LINE_SIZE));
104}
518d7abb 105
518d7abb
PMF
106/* MATH */
107
518d7abb
PMF
108static 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
14641deb
MD
117#ifndef inline_memcpy
118#define inline_memcpy memcpy
119#endif
120
121#ifndef __same_type
122#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
123#endif
124
9f3fdbc6
MD
125#ifndef UST_VALGRIND
126
b728d87e
MD
127/*
128 * If getcpu(2) is not implemented in the Kernel use CPU 0 as fallback.
129 */
9f3fdbc6
MD
130static __inline__ int ust_get_cpu(void)
131{
b728d87e 132 int cpu = sched_getcpu();
9f3fdbc6 133
b5a3dfa5 134 if (caa_likely(cpu >= 0))
9f3fdbc6 135 return cpu;
9f3fdbc6
MD
136 return 0;
137}
138
139#else /* #else #ifndef UST_VALGRIND */
140
b728d87e
MD
141/*
142 * Valgrind does not support the sched_getcpu() vsyscall.
143 * It causes it to detect a segfault in the program and stop it.
144 * So if we want to check libust with valgrind, we have to refrain
145 * from using this call. TODO: it would probably be better to return
146 * other values too, to better test it.
147 */
9f3fdbc6
MD
148static __inline__ int ust_get_cpu(void)
149{
9f3fdbc6
MD
150 return 0;
151}
152
153#endif /* #else #ifndef UST_VALGRIND */
154
12e81b07 155#endif /* UST_CORE_H */
This page took 0.032223 seconds and 4 git commands to generate.