7977be4dad2c64d53624cbf215158008a8c2807d
[lttng-ust.git] / include / lttng / core.h
1 #ifndef UST_CORE_H
2 #define UST_CORE_H
3
4 /*
5 * Copyright (C) 2010 Pierre-Marc Fournier
6 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
10 * License as published by the Free Software Foundation; version 2.1 of
11 * the License.
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
23 #include <sys/types.h>
24 #include <lttng/config.h>
25 #include <urcu/arch.h>
26 #include <urcu/compiler.h>
27
28 /* ALIGNMENT SHORTCUTS */
29
30 #include <unistd.h>
31
32 #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
33 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
34 #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
35 #define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
36 #define PAGE_MASK (~(PAGE_SIZE-1))
37
38 /* ERROR OPS */
39 #define MAX_ERRNO 4095
40
41 #define IS_ERR_VALUE(x) caa_unlikely((x) >= (unsigned long)-MAX_ERRNO)
42
43 static inline void *ERR_PTR(long error)
44 {
45 return (void *) error;
46 }
47
48 static inline long PTR_ERR(const void *ptr)
49 {
50 return (long) ptr;
51 }
52
53 static inline long IS_ERR(const void *ptr)
54 {
55 return IS_ERR_VALUE((unsigned long)ptr);
56 }
57
58
59 /* Min / Max */
60
61 #define min_t(type, x, y) ({ \
62 type __min1 = (x); \
63 type __min2 = (y); \
64 __min1 < __min2 ? __min1: __min2; })
65
66 #define max_t(type, x, y) ({ \
67 type __max1 = (x); \
68 type __max2 = (y); \
69 __max1 > __max2 ? __max1: __max2; })
70
71
72 /* MALLOCATION */
73
74 #include <stdlib.h>
75
76 static inline
77 void *zmalloc(size_t len)
78 {
79 return calloc(1, len);
80 }
81
82 static inline
83 void *malloc_align(size_t len)
84 {
85 return malloc(ALIGN(len, CAA_CACHE_LINE_SIZE));
86 }
87
88 static inline
89 void *zmalloc_align(size_t len)
90 {
91 return calloc(1, ALIGN(len, CAA_CACHE_LINE_SIZE));
92 }
93
94 /* MATH */
95
96 static inline unsigned int hweight32(unsigned int w)
97 {
98 unsigned int res = w - ((w >> 1) & 0x55555555);
99 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
100 res = (res + (res >> 4)) & 0x0F0F0F0F;
101 res = res + (res >> 8);
102 return (res + (res >> 16)) & 0x000000FF;
103 }
104
105 #endif /* UST_CORE_H */
This page took 0.035146 seconds and 3 git commands to generate.