forked from cornelisnetworks/opa-psm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psm_help.h
190 lines (148 loc) · 6.05 KB
/
psm_help.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
This file is provided under a dual BSD/GPLv2 license. When using or
redistributing this file, you may do so under either license.
GPL LICENSE SUMMARY
Copyright(c) 2016 Intel Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
Contact Information:
Intel Corporation, www.intel.com
BSD LICENSE
Copyright(c) 2016 Intel Corporation.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2003-2016 Intel Corporation. All rights reserved. */
#ifndef _PSMI_HELP_H
#define _PSMI_HELP_H
#include "psm_log.h"
/* XXX gcc only */
#define PSMI_INLINE(FN) \
static inline FN
#define PSMI_ALWAYS_INLINE(FN) \
static __inline__ FN __attribute__((always_inline)); \
static __inline__ FN
#define PSMI_NEVER_INLINE(FN) \
static FN __attribute__((noinline)); \
static FN
#define _PPragma(x) _Pragma(x)
#define STRINGIFY(s) _STRINGIFY(s)
#define _STRINGIFY(s) #s
#define PSMI_CURLOC __FILE__ ":" STRINGIFY(__LINE__)
#define psmi_assert_always_loc(x, curloc) \
do { \
if_pf(!(x)) { \
psmi_handle_error(PSMI_EP_NORETURN, PSM2_INTERNAL_ERR, \
"Assertion failure at %s: %s", curloc, \
STRINGIFY(x)); \
} } while (0)
#define psmi_assert_always(x) psmi_assert_always_loc(x, PSMI_CURLOC)
#ifdef PSM_DEBUG
# define psmi_assert(x) psmi_assert_always(x)
# define PSMI_ASSERT_INITIALIZED() psmi_assert_always(psmi_isinitialized())
#else
# define psmi_assert(x)
# define PSMI_ASSERT_INITIALIZED()
#endif
#define _PSMI_API_NAME(FN) __ ## FN
#define _PSMI_API_STR(FN) _STRINGIFY(__ ## FN)
#define PSMI_API_DECL(FN) \
typeof(_PSMI_API_NAME(FN)) FN __attribute__((weak, alias(_PSMI_API_STR(FN))));
#define PSMI_ERR_UNLESS_INITIALIZED(ep) \
do { \
if (!psmi_isinitialized()) { \
PSM2_LOG_MSG("leaving"); \
return psmi_handle_error(ep, PSM2_INIT_NOT_INIT, \
"PSM2 has not been initialized"); \
} \
} while (0)
#define PSMI_CHECKMEM(err, mem) \
do { \
if ((mem) == NULL) { \
(err) = PSM2_NO_MEMORY; \
goto fail; \
} \
} while (0)
#define PSMI_CACHEALIGN __attribute__((aligned(64)))
/* Easy way to ignore the OK_NO_PROGRESS case */
PSMI_ALWAYS_INLINE(psm2_error_t psmi_err_only(psm2_error_t err))
{
if (err > PSM2_OK_NO_PROGRESS)
return err;
else
return PSM2_OK;
}
#ifdef min
#undef min
#endif
#define min(a, b) ((a) < (b) ? (a) : (b))
#ifdef max
#undef max
#endif
#define max(a, b) ((a) > (b) ? (a) : (b))
#define SEC_ULL 1000000000ULL
#define MSEC_ULL 1000000ULL
#define USEC_ULL 1000ULL
#define NSEC_ULL 1ULL
#define PSMI_TRUE 1
#define PSMI_FALSE 0
#define PSMI_CYCLES_TO_SECSF(cycles) \
((double) cycles_to_nanosecs(cycles) / 1.0e9)
#define PSMI_PAGESIZE psmi_getpagesize()
#define PSMI_POWEROFTWO(P) (((P)&((P)-1)) == 0)
#define PSMI_ALIGNDOWN(p, P) (((uintptr_t)(p))&~((uintptr_t)((P)-1)))
#define PSMI_ALIGNUP(p, P) (PSMI_ALIGNDOWN((uintptr_t)(p)+((uintptr_t)((P)-1)), (P)))
#define PSMI_MAKE_DRIVER_VERSION(major, minor) ((major)<<16 | ((minor) & 0xffff))
#ifdef PSM_DEBUG
/* The intent of the following two macros is to emit an internal error if a size of a
'member' is not as expected, violating an assumption in the code. There are some
problems with the implementation of this code:
The first macro creates a static const variable with ABSOLUTELY NO references
to them. For example there are ABSOLUTELY NO uses of the second macro in the
PSM code. This is not completely pure. GCC version 5, for example, emits a
warning for defining a static const when it is not referenced.
A better implementation of the intent of this code is to use static_assert()
so that at compile time the violations can be caught and corrected - not at
run time. */
#define PSMI_STRICT_SIZE_DECL(member, sz) static const size_t __psm2_ss_ ## member = sz
#define PSMI_STRICT_SIZE_VERIFY(member, sz) \
do { \
if (__psm2_ss_ ## member != (sz)) { \
char errmsg[64]; \
snprintf(errmsg, 32, "Internal error: %s " \
"size doesn't match expected %d bytes", \
STRINGIFY(member), (int) __psm2_ss_ ## member); \
exit(-1); \
} \
} while (0)
#else
#define PSMI_STRICT_SIZE_DECL(member, sz) /* nothing */
#define PSMI_STRICT_SIZE_VERIFY(member, sz) /* nothing */
#endif /* PSM_DEBUG */
#endif /* _PSMI_HELP_H */