blob: 3f4f465f39606bfbaaa35d42dce239fe5820d51b [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
Andrew Scullcdfcccc2018-10-05 20:58:37 +01009// This file defines several macros, based on the current compiler. This allows
10// use of compiler-specific features in a way that remains portable.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010011//
12//===----------------------------------------------------------------------===//
13
Andrew Scullcdfcccc2018-10-05 20:58:37 +010014#ifndef LLVM_SUPPORT_COMPILER_H
15#define LLVM_SUPPORT_COMPILER_H
16
17#include "llvm/Config/llvm-config.h"
18
19#include <new>
20#include <stddef.h>
21
22#if defined(_MSC_VER)
23#include <sal.h>
24#endif
25
26#ifndef __has_feature
27# define __has_feature(x) 0
28#endif
29
30#ifndef __has_extension
31# define __has_extension(x) 0
32#endif
33
34#ifndef __has_attribute
35# define __has_attribute(x) 0
36#endif
37
38#ifndef __has_cpp_attribute
39# define __has_cpp_attribute(x) 0
40#endif
41
42#ifndef __has_builtin
43# define __has_builtin(x) 0
44#endif
45
46/// \macro LLVM_GNUC_PREREQ
47/// Extend the default __GNUC_PREREQ even if glibc's features.h isn't
48/// available.
49#ifndef LLVM_GNUC_PREREQ
50# if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
51# define LLVM_GNUC_PREREQ(maj, min, patch) \
52 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
53 ((maj) << 20) + ((min) << 10) + (patch))
54# elif defined(__GNUC__) && defined(__GNUC_MINOR__)
55# define LLVM_GNUC_PREREQ(maj, min, patch) \
56 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
57# else
58# define LLVM_GNUC_PREREQ(maj, min, patch) 0
59# endif
60#endif
61
62/// \macro LLVM_MSC_PREREQ
63/// Is the compiler MSVC of at least the specified version?
64/// The common \param version values to check for are:
65/// * 1900: Microsoft Visual Studio 2015 / 14.0
66#ifdef _MSC_VER
67#define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
68
69// We require at least MSVC 2015.
70#if !LLVM_MSC_PREREQ(1900)
71#error LLVM requires at least MSVC 2015.
72#endif
73
74#else
75#define LLVM_MSC_PREREQ(version) 0
76#endif
77
78/// Does the compiler support ref-qualifiers for *this?
79///
80/// Sadly, this is separate from just rvalue reference support because GCC
81/// and MSVC implemented this later than everything else.
82#if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
83#define LLVM_HAS_RVALUE_REFERENCE_THIS 1
84#else
85#define LLVM_HAS_RVALUE_REFERENCE_THIS 0
86#endif
87
88/// Expands to '&' if ref-qualifiers for *this are supported.
89///
90/// This can be used to provide lvalue/rvalue overrides of member functions.
91/// The rvalue override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
92#if LLVM_HAS_RVALUE_REFERENCE_THIS
93#define LLVM_LVALUE_FUNCTION &
94#else
95#define LLVM_LVALUE_FUNCTION
96#endif
97
98/// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
99/// into a shared library, then the class should be private to the library and
100/// not accessible from outside it. Can also be used to mark variables and
101/// functions, making them private to any shared library they are linked into.
102/// On PE/COFF targets, library visibility is the default, so this isn't needed.
103#if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
104 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32)
105#define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
106#else
107#define LLVM_LIBRARY_VISIBILITY
108#endif
109
110#if defined(__GNUC__)
111#define LLVM_PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
112#else
113#define LLVM_PREFETCH(addr, rw, locality)
114#endif
115
116#if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
117#define LLVM_ATTRIBUTE_USED __attribute__((__used__))
118#else
119#define LLVM_ATTRIBUTE_USED
120#endif
121
122/// LLVM_NODISCARD - Warn if a type or return value is discarded.
123#if __cplusplus > 201402L && __has_cpp_attribute(nodiscard)
124#define LLVM_NODISCARD [[nodiscard]]
125#elif !__cplusplus
126// Workaround for llvm.org/PR23435, since clang 3.6 and below emit a spurious
127// error when __has_cpp_attribute is given a scoped attribute in C mode.
128#define LLVM_NODISCARD
129#elif __has_cpp_attribute(clang::warn_unused_result)
130#define LLVM_NODISCARD [[clang::warn_unused_result]]
131#else
132#define LLVM_NODISCARD
133#endif
134
Andrew Scull0372a572018-11-16 15:47:06 +0000135// Indicate that a non-static, non-const C++ member function reinitializes
136// the entire object to a known state, independent of the previous state of
137// the object.
138//
139// The clang-tidy check bugprone-use-after-move recognizes this attribute as a
140// marker that a moved-from object has left the indeterminate state and can be
141// reused.
142#if __has_cpp_attribute(clang::reinitializes)
143#define LLVM_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
144#else
145#define LLVM_ATTRIBUTE_REINITIALIZES
146#endif
147
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100148// Some compilers warn about unused functions. When a function is sometimes
149// used or not depending on build settings (e.g. a function only called from
150// within "assert"), this attribute can be used to suppress such warnings.
151//
152// However, it shouldn't be used for unused *variables*, as those have a much
153// more portable solution:
154// (void)unused_var_name;
155// Prefer cast-to-void wherever it is sufficient.
156#if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
157#define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
158#else
159#define LLVM_ATTRIBUTE_UNUSED
160#endif
161
162// FIXME: Provide this for PE/COFF targets.
163#if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
164 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32))
165#define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
166#else
167#define LLVM_ATTRIBUTE_WEAK
168#endif
169
170// Prior to clang 3.2, clang did not accept any spelling of
171// __has_attribute(const), so assume it is supported.
172#if defined(__clang__) || defined(__GNUC__)
173// aka 'CONST' but following LLVM Conventions.
174#define LLVM_READNONE __attribute__((__const__))
175#else
176#define LLVM_READNONE
177#endif
178
179#if __has_attribute(pure) || defined(__GNUC__)
180// aka 'PURE' but following LLVM Conventions.
181#define LLVM_READONLY __attribute__((__pure__))
182#else
183#define LLVM_READONLY
184#endif
185
186#if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
187#define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
188#define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
189#else
190#define LLVM_LIKELY(EXPR) (EXPR)
191#define LLVM_UNLIKELY(EXPR) (EXPR)
192#endif
193
194/// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
195/// mark a method "not for inlining".
196#if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
197#define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
198#elif defined(_MSC_VER)
199#define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
200#else
201#define LLVM_ATTRIBUTE_NOINLINE
202#endif
203
204/// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
205/// so, mark a method "always inline" because it is performance sensitive. GCC
206/// 3.4 supported this but is buggy in various cases and produces unimplemented
207/// errors, just use it in GCC 4.0 and later.
208#if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
209#define LLVM_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
210#elif defined(_MSC_VER)
211#define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
212#else
213#define LLVM_ATTRIBUTE_ALWAYS_INLINE
214#endif
215
216#ifdef __GNUC__
217#define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
218#elif defined(_MSC_VER)
219#define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
220#else
221#define LLVM_ATTRIBUTE_NORETURN
222#endif
223
224#if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
225#define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
226#elif defined(_MSC_VER)
227#define LLVM_ATTRIBUTE_RETURNS_NONNULL _Ret_notnull_
228#else
229#define LLVM_ATTRIBUTE_RETURNS_NONNULL
230#endif
231
232/// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
233/// pointer that does not alias any other valid pointer.
234#ifdef __GNUC__
235#define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
236#elif defined(_MSC_VER)
237#define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
238#else
239#define LLVM_ATTRIBUTE_RETURNS_NOALIAS
240#endif
241
242/// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
243#if __cplusplus > 201402L && __has_cpp_attribute(fallthrough)
244#define LLVM_FALLTHROUGH [[fallthrough]]
245#elif __has_cpp_attribute(gnu::fallthrough)
246#define LLVM_FALLTHROUGH [[gnu::fallthrough]]
247#elif !__cplusplus
248// Workaround for llvm.org/PR23435, since clang 3.6 and below emit a spurious
249// error when __has_cpp_attribute is given a scoped attribute in C mode.
250#define LLVM_FALLTHROUGH
251#elif __has_cpp_attribute(clang::fallthrough)
252#define LLVM_FALLTHROUGH [[clang::fallthrough]]
253#else
254#define LLVM_FALLTHROUGH
255#endif
256
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100257/// LLVM_REQUIRE_CONSTANT_INITIALIZATION - Apply this to globals to ensure that
258/// they are constant initialized.
259#if __has_cpp_attribute(clang::require_constant_initialization)
260#define LLVM_REQUIRE_CONSTANT_INITIALIZATION \
261 [[clang::require_constant_initialization]]
262#else
263#define LLVM_REQUIRE_CONSTANT_INITIALIZATION
264#endif
265
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100266/// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
267/// pedantic diagnostics.
268#ifdef __GNUC__
269#define LLVM_EXTENSION __extension__
270#else
271#define LLVM_EXTENSION
272#endif
273
274// LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
275#if __has_feature(attribute_deprecated_with_message)
276# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
277 decl __attribute__((deprecated(message)))
278#elif defined(__GNUC__)
279# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
280 decl __attribute__((deprecated))
281#elif defined(_MSC_VER)
282# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
283 __declspec(deprecated(message)) decl
284#else
285# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
286 decl
287#endif
288
289/// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
290/// to an expression which states that it is undefined behavior for the
291/// compiler to reach this point. Otherwise is not defined.
292#if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
293# define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
294#elif defined(_MSC_VER)
295# define LLVM_BUILTIN_UNREACHABLE __assume(false)
296#endif
297
298/// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
299/// which causes the program to exit abnormally.
300#if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
301# define LLVM_BUILTIN_TRAP __builtin_trap()
302#elif defined(_MSC_VER)
303// The __debugbreak intrinsic is supported by MSVC, does not require forward
304// declarations involving platform-specific typedefs (unlike RaiseException),
305// results in a call to vectored exception handlers, and encodes to a short
306// instruction that still causes the trapping behavior we want.
307# define LLVM_BUILTIN_TRAP __debugbreak()
308#else
309# define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
310#endif
311
312/// LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to
313/// an expression which causes the program to break while running
314/// under a debugger.
315#if __has_builtin(__builtin_debugtrap)
316# define LLVM_BUILTIN_DEBUGTRAP __builtin_debugtrap()
317#elif defined(_MSC_VER)
318// The __debugbreak intrinsic is supported by MSVC and breaks while
319// running under the debugger, and also supports invoking a debugger
320// when the OS is configured appropriately.
321# define LLVM_BUILTIN_DEBUGTRAP __debugbreak()
322#else
323// Just continue execution when built with compilers that have no
324// support. This is a debugging aid and not intended to force the
325// program to abort if encountered.
326# define LLVM_BUILTIN_DEBUGTRAP
327#endif
328
329/// \macro LLVM_ASSUME_ALIGNED
330/// Returns a pointer with an assumed alignment.
331#if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
332# define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
333#elif defined(LLVM_BUILTIN_UNREACHABLE)
334// As of today, clang does not support __builtin_assume_aligned.
335# define LLVM_ASSUME_ALIGNED(p, a) \
336 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
337#else
338# define LLVM_ASSUME_ALIGNED(p, a) (p)
339#endif
340
341/// \macro LLVM_ALIGNAS
342/// Used to specify a minimum alignment for a structure or variable.
343#if __GNUC__ && !__has_feature(cxx_alignas) && !LLVM_GNUC_PREREQ(4, 8, 1)
344# define LLVM_ALIGNAS(x) __attribute__((aligned(x)))
345#else
346# define LLVM_ALIGNAS(x) alignas(x)
347#endif
348
349/// \macro LLVM_PACKED
350/// Used to specify a packed structure.
351/// LLVM_PACKED(
352/// struct A {
353/// int i;
354/// int j;
355/// int k;
356/// long long l;
357/// });
358///
359/// LLVM_PACKED_START
360/// struct B {
361/// int i;
362/// int j;
363/// int k;
364/// long long l;
365/// };
366/// LLVM_PACKED_END
367#ifdef _MSC_VER
368# define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
369# define LLVM_PACKED_START __pragma(pack(push, 1))
370# define LLVM_PACKED_END __pragma(pack(pop))
371#else
372# define LLVM_PACKED(d) d __attribute__((packed))
373# define LLVM_PACKED_START _Pragma("pack(push, 1)")
374# define LLVM_PACKED_END _Pragma("pack(pop)")
375#endif
376
377/// \macro LLVM_PTR_SIZE
378/// A constant integer equivalent to the value of sizeof(void*).
379/// Generally used in combination with LLVM_ALIGNAS or when doing computation in
380/// the preprocessor.
381#ifdef __SIZEOF_POINTER__
382# define LLVM_PTR_SIZE __SIZEOF_POINTER__
383#elif defined(_WIN64)
384# define LLVM_PTR_SIZE 8
385#elif defined(_WIN32)
386# define LLVM_PTR_SIZE 4
387#elif defined(_MSC_VER)
388# error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
389#else
390# define LLVM_PTR_SIZE sizeof(void *)
391#endif
392
393/// \macro LLVM_MEMORY_SANITIZER_BUILD
394/// Whether LLVM itself is built with MemorySanitizer instrumentation.
395#if __has_feature(memory_sanitizer)
396# define LLVM_MEMORY_SANITIZER_BUILD 1
397# include <sanitizer/msan_interface.h>
398#else
399# define LLVM_MEMORY_SANITIZER_BUILD 0
400# define __msan_allocated_memory(p, size)
401# define __msan_unpoison(p, size)
402#endif
403
404/// \macro LLVM_ADDRESS_SANITIZER_BUILD
405/// Whether LLVM itself is built with AddressSanitizer instrumentation.
406#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
407# define LLVM_ADDRESS_SANITIZER_BUILD 1
408# include <sanitizer/asan_interface.h>
409#else
410# define LLVM_ADDRESS_SANITIZER_BUILD 0
411# define __asan_poison_memory_region(p, size)
412# define __asan_unpoison_memory_region(p, size)
413#endif
414
415/// \macro LLVM_THREAD_SANITIZER_BUILD
416/// Whether LLVM itself is built with ThreadSanitizer instrumentation.
417#if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
418# define LLVM_THREAD_SANITIZER_BUILD 1
419#else
420# define LLVM_THREAD_SANITIZER_BUILD 0
421#endif
422
423#if LLVM_THREAD_SANITIZER_BUILD
424// Thread Sanitizer is a tool that finds races in code.
425// See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
426// tsan detects these exact functions by name.
427#ifdef __cplusplus
428extern "C" {
429#endif
430void AnnotateHappensAfter(const char *file, int line, const volatile void *cv);
431void AnnotateHappensBefore(const char *file, int line, const volatile void *cv);
432void AnnotateIgnoreWritesBegin(const char *file, int line);
433void AnnotateIgnoreWritesEnd(const char *file, int line);
434#ifdef __cplusplus
435}
436#endif
437
438// This marker is used to define a happens-before arc. The race detector will
439// infer an arc from the begin to the end when they share the same pointer
440// argument.
441# define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv)
442
443// This marker defines the destination of a happens-before arc.
444# define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv)
445
446// Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
447# define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
448
449// Resume checking for racy writes.
450# define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
451#else
452# define TsanHappensBefore(cv)
453# define TsanHappensAfter(cv)
454# define TsanIgnoreWritesBegin()
455# define TsanIgnoreWritesEnd()
456#endif
457
458/// \macro LLVM_NO_SANITIZE
459/// Disable a particular sanitizer for a function.
460#if __has_attribute(no_sanitize)
461#define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND)))
462#else
463#define LLVM_NO_SANITIZE(KIND)
464#endif
465
466/// Mark debug helper function definitions like dump() that should not be
467/// stripped from debug builds.
468/// Note that you should also surround dump() functions with
469/// `#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)` so they do always
470/// get stripped in release builds.
471// FIXME: Move this to a private config.h as it's not usable in public headers.
472#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
473#define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
474#else
475#define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
476#endif
477
478/// \macro LLVM_PRETTY_FUNCTION
479/// Gets a user-friendly looking function signature for the current scope
480/// using the best available method on each platform. The exact format of the
481/// resulting string is implementation specific and non-portable, so this should
482/// only be used, for example, for logging or diagnostics.
483#if defined(_MSC_VER)
484#define LLVM_PRETTY_FUNCTION __FUNCSIG__
485#elif defined(__GNUC__) || defined(__clang__)
486#define LLVM_PRETTY_FUNCTION __PRETTY_FUNCTION__
487#else
488#define LLVM_PRETTY_FUNCTION __func__
489#endif
490
491/// \macro LLVM_THREAD_LOCAL
492/// A thread-local storage specifier which can be used with globals,
493/// extern globals, and static globals.
494///
495/// This is essentially an extremely restricted analog to C++11's thread_local
496/// support, and uses that when available. However, it falls back on
497/// platform-specific or vendor-provided extensions when necessary. These
498/// extensions don't support many of the C++11 thread_local's features. You
499/// should only use this for PODs that you can statically initialize to
500/// some constant value. In almost all circumstances this is most appropriate
501/// for use with a pointer, integer, or small aggregation of pointers and
502/// integers.
503#if LLVM_ENABLE_THREADS
504#if __has_feature(cxx_thread_local)
505#define LLVM_THREAD_LOCAL thread_local
506#elif defined(_MSC_VER)
507// MSVC supports this with a __declspec.
508#define LLVM_THREAD_LOCAL __declspec(thread)
509#else
510// Clang, GCC, and other compatible compilers used __thread prior to C++11 and
511// we only need the restricted functionality that provides.
512#define LLVM_THREAD_LOCAL __thread
513#endif
514#else // !LLVM_ENABLE_THREADS
515// If threading is disabled entirely, this compiles to nothing and you get
516// a normal global variable.
517#define LLVM_THREAD_LOCAL
518#endif
519
520/// \macro LLVM_ENABLE_EXCEPTIONS
521/// Whether LLVM is built with exception support.
522#if __has_feature(cxx_exceptions)
523#define LLVM_ENABLE_EXCEPTIONS 1
524#elif defined(__GNUC__) && defined(__EXCEPTIONS)
525#define LLVM_ENABLE_EXCEPTIONS 1
526#elif defined(_MSC_VER) && defined(_CPPUNWIND)
527#define LLVM_ENABLE_EXCEPTIONS 1
528#endif
529
530namespace llvm {
531
532/// Allocate a buffer of memory with the given size and alignment.
533///
534/// When the compiler supports aligned operator new, this will use it to to
535/// handle even over-aligned allocations.
536///
537/// However, this doesn't make any attempt to leverage the fancier techniques
538/// like posix_memalign due to portability. It is mostly intended to allow
539/// compatibility with platforms that, after aligned allocation was added, use
540/// reduced default alignment.
541inline void *allocate_buffer(size_t Size, size_t Alignment) {
542 return ::operator new(Size
Andrew Scull0372a572018-11-16 15:47:06 +0000543#ifdef __cpp_aligned_new
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100544 ,
545 std::align_val_t(Alignment)
546#endif
547 );
548}
549
550/// Deallocate a buffer of memory with the given size and alignment.
551///
552/// If supported, this will used the sized delete operator. Also if supported,
553/// this will pass the alignment to the delete operator.
554///
555/// The pointer must have been allocated with the corresponding new operator,
556/// most likely using the above helper.
557inline void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment) {
558 ::operator delete(Ptr
Andrew Scull0372a572018-11-16 15:47:06 +0000559#ifdef __cpp_sized_deallocation
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100560 ,
561 Size
562#endif
Andrew Scull0372a572018-11-16 15:47:06 +0000563#ifdef __cpp_aligned_new
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100564 ,
565 std::align_val_t(Alignment)
566#endif
567 );
568}
569
570} // End namespace llvm
571
572#endif