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> |
4318ae1b | 24 | #include <lttng/config.h> |
a6352fd4 | 25 | #include <urcu/arch.h> |
b5a3dfa5 | 26 | #include <urcu/compiler.h> |
518d7abb | 27 | |
518d7abb PMF |
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 | ||
518d7abb PMF |
38 | /* Min / Max */ |
39 | ||
40 | #define min_t(type, x, y) ({ \ | |
41 | type __min1 = (x); \ | |
42 | type __min2 = (y); \ | |
43 | __min1 < __min2 ? __min1: __min2; }) | |
44 | ||
45 | #define max_t(type, x, y) ({ \ | |
46 | type __max1 = (x); \ | |
47 | type __max2 = (y); \ | |
48 | __max1 > __max2 ? __max1: __max2; }) | |
49 | ||
50 | ||
518d7abb PMF |
51 | /* MALLOCATION */ |
52 | ||
14641deb MD |
53 | #include <stdlib.h> |
54 | ||
55 | static inline | |
56 | void *zmalloc(size_t len) | |
57 | { | |
58 | return calloc(1, len); | |
59 | } | |
60 | ||
61 | static inline | |
62 | void *malloc_align(size_t len) | |
63 | { | |
64 | return malloc(ALIGN(len, CAA_CACHE_LINE_SIZE)); | |
65 | } | |
66 | ||
67 | static inline | |
68 | void *zmalloc_align(size_t len) | |
69 | { | |
70 | return calloc(1, ALIGN(len, CAA_CACHE_LINE_SIZE)); | |
71 | } | |
518d7abb | 72 | |
518d7abb PMF |
73 | /* MATH */ |
74 | ||
518d7abb PMF |
75 | static inline unsigned int hweight32(unsigned int w) |
76 | { | |
77 | unsigned int res = w - ((w >> 1) & 0x55555555); | |
78 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); | |
79 | res = (res + (res >> 4)) & 0x0F0F0F0F; | |
80 | res = res + (res >> 8); | |
81 | return (res + (res >> 16)) & 0x000000FF; | |
82 | } | |
83 | ||
12e81b07 | 84 | #endif /* UST_CORE_H */ |