Blender  V3.3
atomic_ops_utils.h
Go to the documentation of this file.
1 /*
2  * Original code from jemalloc with this license:
3  *
4  * Copyright (C) 2002-2013 Jason Evans <jasone@canonware.com>.
5  * All rights reserved.
6  * Copyright (C) 2007-2012 Mozilla Foundation. All rights reserved.
7  * Copyright (C) 2009-2013 Facebook, Inc. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * 1. Redistributions of source code must retain the above copyright notice(s),
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice(s),
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20  * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  * This program is free software; you can redistribute it and/or
28  * modify it under the terms of the GNU General Public License
29  * as published by the Free Software Foundation; either version 2
30  * of the License, or (at your option) any later version.
31  *
32  * This program is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35  * GNU General Public License for more details.
36  *
37  * You should have received a copy of the GNU General Public License
38  * along with this program; if not, write to the Free Software Foundation,
39  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
40  *
41  * The Original Code is Copyright (C) 2016 Blender Foundation.
42  * All rights reserved.
43  *
44  * The Original Code is: adapted from jemalloc.
45  */
46 
51 #ifndef __ATOMIC_OPS_UTILS_H__
52 #define __ATOMIC_OPS_UTILS_H__
53 
54 #include <limits.h>
55 #include <stddef.h>
56 #include <stdint.h>
57 #include <stdlib.h>
58 
59 #include <assert.h>
60 
61 /* little macro so inline keyword works */
62 #if defined(_MSC_VER)
63 # define ATOMIC_INLINE static __forceinline
64 #else
65 # define ATOMIC_INLINE static inline __attribute__((always_inline))
66 #endif
67 
68 #ifdef __GNUC__
69 # define _ATOMIC_LIKELY(x) __builtin_expect(!!(x), 1)
70 # define _ATOMIC_UNLIKELY(x) __builtin_expect(!!(x), 0)
71 # define _ATOMIC_MAYBE_UNUSED __attribute__((unused))
72 #else
73 # define _ATOMIC_LIKELY(x) (x)
74 # define _ATOMIC_UNLIKELY(x) (x)
75 # define _ATOMIC_MAYBE_UNUSED
76 #endif
77 
78 #if defined(__SIZEOF_POINTER__)
79 # define LG_SIZEOF_PTR __SIZEOF_POINTER__
80 #elif defined(UINTPTR_MAX)
81 # if (UINTPTR_MAX == 0xFFFFFFFF)
82 # define LG_SIZEOF_PTR 4
83 # elif (UINTPTR_MAX == 0xFFFFFFFFFFFFFFFF)
84 # define LG_SIZEOF_PTR 8
85 # endif
86 #elif defined(__WORDSIZE) /* Fallback for older glibc and cpp */
87 # if (__WORDSIZE == 32)
88 # define LG_SIZEOF_PTR 4
89 # elif (__WORDSIZE == 64)
90 # define LG_SIZEOF_PTR 8
91 # endif
92 #endif
93 
94 #ifndef LG_SIZEOF_PTR
95 # error "Cannot find pointer size"
96 #endif
97 
98 #if (UINT_MAX == 0xFFFFFFFF)
99 # define LG_SIZEOF_INT 4
100 #elif (UINT_MAX == 0xFFFFFFFFFFFFFFFF)
101 # define LG_SIZEOF_INT 8
102 #else
103 # error "Cannot find int size"
104 #endif
105 
106 /* Copied from BLI_utils... */
107 /* C++ can't use _Static_assert, expects static_assert() but c++0x only,
108  * Coverity also errors out. */
109 #if (!defined(__cplusplus)) && (!defined(__COVERITY__)) && \
110  (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 406)) /* gcc4.6+ only */
111 # define ATOMIC_STATIC_ASSERT(a, msg) __extension__ _Static_assert(a, msg);
112 #else
113 /* Code adapted from http://www.pixelbeat.org/programming/gcc/static_assert.html */
114 /* Note we need the two concats below because arguments to ## are not expanded, so we need to
115  * expand __LINE__ with one indirection before doing the actual concatenation. */
116 # define ATOMIC_ASSERT_CONCAT_(a, b) a##b
117 # define ATOMIC_ASSERT_CONCAT(a, b) ATOMIC_ASSERT_CONCAT_(a, b)
118 /* These can't be used after statements in c89. */
119 # if defined(__COUNTER__) /* MSVC */
120 # define ATOMIC_STATIC_ASSERT(a, msg) \
121  ; \
122  enum { ATOMIC_ASSERT_CONCAT(static_assert_, __COUNTER__) = 1 / (int)(!!(a)) };
123 # else /* older gcc, clang... */
124 /* This can't be used twice on the same line so ensure if using in headers
125  * that the headers are not included twice (by wrapping in #ifndef...#endif)
126  * Note it doesn't cause an issue when used on same line of separate modules
127  * compiled with gcc -combine -fwhole-program. */
128 # define ATOMIC_STATIC_ASSERT(a, msg) \
129  ; \
130  enum { ATOMIC_ASSERT_CONCAT(assert_line_, __LINE__) = 1 / (int)(!!(a)) };
131 # endif
132 #endif
133 
134 #endif /* __ATOMIC_OPS_UTILS_H__ */