Pioneer
Loading...
Searching...
No Matches
macros.h
Go to the documentation of this file.
1// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#pragma once
5
6#include <cstddef>
7
8#ifndef __GNUC__
9#define __attribute(x)
10#endif /* __GNUC__ */
11
12// GCC warns when a function marked __attribute((noreturn)) actually returns a value
13// but other compilers which don't see the noreturn attribute of course require that
14// a function with a non-void return type should return something.
15#ifndef __GNUC__
16#define RETURN_ZERO_NONGNU_ONLY return 0;
17#else
18#define RETURN_ZERO_NONGNU_ONLY
19#endif
20
21// align x to a. taken from the Linux kernel
22#define ALIGN(x, a) __ALIGN_MASK(x, (a - 1))
23#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
24
25// This file stores most utility macros used across the whole codebase that
26// do not depend on large header includes.
27
28#ifdef NDEBUG
29#define PiVerify(x) ((void)(x))
30#else
31#include <cassert>
32#define PiVerify(x) assert(x)
33#endif
34
35// from StackOverflow: http://stackoverflow.com/a/1500517/52251
36// Q: "Compile time sizeof_array without using a macro"
37template <typename T, size_t N>
38char (&COUNTOF_Helper(T (&array)[N]))[N];
39#define COUNTOF(array) (sizeof(COUNTOF_Helper(array)))
40
41// Helper to implement stack-based variable-length arrays in a crossplatform way
42// Avoids a heap allocation for std::vector in "hot" code
43
44#ifdef _MSC_VER
45#include <malloc.h>
46#define stackalloc(T, n) reinterpret_cast<T *>(_alloca(sizeof(T) * n))
47#else
48#include <alloca.h>
49#define stackalloc(T, n) reinterpret_cast<T *>(alloca(sizeof(T) * n))
50#endif
char(& COUNTOF_Helper(T(&array)[N]))[N]