armv5 archs require write alignment
[ust.git] / include / ust / core.h
CommitLineData
518d7abb
PMF
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
12e81b07
PMF
18#ifndef UST_CORE_H
19#define UST_CORE_H
20
518d7abb 21#include <sys/types.h>
2699970a 22#include <ust/config.h>
518d7abb
PMF
23
24#define likely(x) __builtin_expect(!!(x), 1)
25#define unlikely(x) __builtin_expect(!!(x), 0)
26
2699970a 27#ifndef HAVE_EFFICIENT_UNALIGNED_ACCESS
12e81b07
PMF
28
29/*
30 * Calculate the offset needed to align the type.
31 * size_of_type must be non-zero.
32 */
33static inline unsigned int ltt_align(size_t align_drift, size_t size_of_type)
34{
35 size_t alignment = min(sizeof(void *), size_of_type);
36 return (alignment - align_drift) & (alignment - 1);
37}
38/* Default arch alignment */
39#define LTT_ALIGN
40
41static inline int ltt_get_alignment(void)
42{
43 return sizeof(void *);
44}
45
2699970a 46#else /* HAVE_EFFICIENT_UNALIGNED_ACCESS */
12e81b07
PMF
47
48static inline unsigned int ltt_align(size_t align_drift,
49 size_t size_of_type)
50{
51 return 0;
52}
53
54#define LTT_ALIGN __attribute__((packed))
55
56static inline int ltt_get_alignment(void)
57{
58 return 0;
59}
2699970a 60#endif /* HAVE_EFFICIENT_UNALIGNED_ACCESS */
12e81b07 61
518d7abb
PMF
62
63/* ARRAYS */
64
65#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
66
67
68/* ALIGNMENT SHORTCUTS */
69
70#include <unistd.h>
71
72#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
73#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
74#define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
75#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
76#define PAGE_MASK (~(PAGE_SIZE-1))
77
78/* ERROR OPS */
79#define MAX_ERRNO 4095
80
81#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
82
83static inline void *ERR_PTR(long error)
84{
85 return (void *) error;
86}
87
88static inline long PTR_ERR(const void *ptr)
89{
90 return (long) ptr;
91}
92
93static inline long IS_ERR(const void *ptr)
94{
95 return IS_ERR_VALUE((unsigned long)ptr);
96}
97
98
99/* Min / Max */
100
101#define min_t(type, x, y) ({ \
102 type __min1 = (x); \
103 type __min2 = (y); \
104 __min1 < __min2 ? __min1: __min2; })
105
106#define max_t(type, x, y) ({ \
107 type __max1 = (x); \
108 type __max2 = (y); \
109 __max1 > __max2 ? __max1: __max2; })
110
111
112/* MUTEXES */
113
114#include <pthread.h>
115
116#define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER;
117#define DECLARE_MUTEX(m) extern pthread_mutex_t (m);
118
518d7abb
PMF
119/* MALLOCATION */
120
121#define zmalloc(s) calloc(1, s)
122
518d7abb
PMF
123/* MATH */
124
125#include <ust/processor.h>
126static inline unsigned int hweight32(unsigned int w)
127{
128 unsigned int res = w - ((w >> 1) & 0x55555555);
129 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
130 res = (res + (res >> 4)) & 0x0F0F0F0F;
131 res = res + (res >> 8);
132 return (res + (res >> 16)) & 0x000000FF;
133}
134
135static __inline__ int get_count_order(unsigned int count)
136{
137 int order;
138
139 order = fls(count) - 1;
140 if (count & (count - 1))
141 order++;
142 return order;
143}
144
1e4b909b 145#define _ust_container_of(ptr, type, member) ({ \
518d7abb
PMF
146 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
147 (type *)( (char *)__mptr - offsetof(type,member) );})
148
12e81b07 149#endif /* UST_CORE_H */
This page took 0.035031 seconds and 4 git commands to generate.