Blender V4.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 adapted from jemalloc.
42 * Modifications Copyright (C) 2016 Blender Foundation
43 */
44
49#ifndef __ATOMIC_OPS_UTILS_H__
50#define __ATOMIC_OPS_UTILS_H__
51
52#include <limits.h>
53#include <stddef.h>
54#include <stdint.h>
55#include <stdlib.h>
56
57#include <assert.h>
58
59/* little macro so inline keyword works */
60#if defined(_MSC_VER)
61# define ATOMIC_INLINE static __forceinline
62#else
63# define ATOMIC_INLINE static inline __attribute__((always_inline))
64#endif
65
66#ifdef __GNUC__
67# define _ATOMIC_LIKELY(x) __builtin_expect(!!(x), 1)
68# define _ATOMIC_UNLIKELY(x) __builtin_expect(!!(x), 0)
69# define _ATOMIC_MAYBE_UNUSED __attribute__((unused))
70#else
71# define _ATOMIC_LIKELY(x) (x)
72# define _ATOMIC_UNLIKELY(x) (x)
73# define _ATOMIC_MAYBE_UNUSED
74#endif
75
76#if defined(__SIZEOF_POINTER__)
77# define LG_SIZEOF_PTR __SIZEOF_POINTER__
78#elif defined(UINTPTR_MAX)
79# if (UINTPTR_MAX == 0xFFFFFFFF)
80# define LG_SIZEOF_PTR 4
81# elif (UINTPTR_MAX == 0xFFFFFFFFFFFFFFFF)
82# define LG_SIZEOF_PTR 8
83# endif
84#elif defined(__WORDSIZE) /* Fallback for older glibc and cpp */
85# if (__WORDSIZE == 32)
86# define LG_SIZEOF_PTR 4
87# elif (__WORDSIZE == 64)
88# define LG_SIZEOF_PTR 8
89# endif
90#endif
91
92#ifndef LG_SIZEOF_PTR
93# error "Cannot find pointer size"
94#endif
95
96#if (UINT_MAX == 0xFFFFFFFF)
97# define LG_SIZEOF_INT 4
98#elif (UINT_MAX == 0xFFFFFFFFFFFFFFFF)
99# define LG_SIZEOF_INT 8
100#else
101# error "Cannot find int size"
102#endif
103
104/* Copied from BLI_utils... */
105/* C++ can't use _Static_assert, expects static_assert() but c++0x only,
106 * Coverity also errors out. */
107#if (!defined(__cplusplus)) && (!defined(__COVERITY__)) && (defined(__GNUC__)) /* GCC only. */
108# define ATOMIC_STATIC_ASSERT(a, msg) __extension__ _Static_assert(a, msg);
109#else
110/* Code adapted from http://www.pixelbeat.org/programming/gcc/static_assert.html */
111/* Note we need the two concats below because arguments to ## are not expanded, so we need to
112 * expand __LINE__ with one indirection before doing the actual concatenation. */
113# define ATOMIC_ASSERT_CONCAT_(a, b) a##b
114# define ATOMIC_ASSERT_CONCAT(a, b) ATOMIC_ASSERT_CONCAT_(a, b)
115/* These can't be used after statements in c89. */
116# if defined(__COUNTER__) /* MSVC */
117# define ATOMIC_STATIC_ASSERT(a, msg) \
118 ; \
119 enum { ATOMIC_ASSERT_CONCAT(static_assert_, __COUNTER__) = 1 / (int)(!!(a)) };
120# else /* older gcc, clang... */
121/* This can't be used twice on the same line so ensure if using in headers
122 * that the headers are not included twice (by wrapping in #ifndef...#endif)
123 * Note it doesn't cause an issue when used on same line of separate modules
124 * compiled with gcc -combine -fwhole-program. */
125# define ATOMIC_STATIC_ASSERT(a, msg) \
126 ; \
127 enum { ATOMIC_ASSERT_CONCAT(assert_line_, __LINE__) = 1 / (int)(!!(a)) };
128# endif
129#endif
130
131#endif /* __ATOMIC_OPS_UTILS_H__ */