1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QV4_ALLOCA_H |
5 | #define QV4_ALLOCA_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtCore/private/qglobal_p.h> |
19 | |
20 | #include <stdlib.h> |
21 | #if __has_include(<alloca.h>) |
22 | # include <alloca.h> |
23 | #endif |
24 | #if __has_include(<malloc.h>) |
25 | # include <malloc.h> |
26 | #endif |
27 | |
28 | #ifdef Q_CC_MSVC |
29 | // This does not matter unless compiling in strict standard mode. |
30 | # define alloca _alloca |
31 | #endif |
32 | |
33 | // Define Q_ALLOCA_VAR macro to be used instead of #ifdeffing |
34 | // the occurrences of alloca() in case it's not supported. |
35 | // Q_ALLOCA_DECLARE and Q_ALLOCA_ASSIGN macros separate |
36 | // memory allocation from the declaration and RAII. |
37 | #define Q_ALLOCA_VAR(type, name, size) \ |
38 | Q_ALLOCA_DECLARE(type, name); \ |
39 | Q_ALLOCA_ASSIGN(type, name, size) |
40 | |
41 | #ifdef alloca |
42 | |
43 | #define Q_ALLOCA_DECLARE(type, name) \ |
44 | type *name = 0 |
45 | |
46 | #define Q_ALLOCA_ASSIGN(type, name, size) \ |
47 | name = static_cast<type*>(alloca(size)) |
48 | |
49 | #else |
50 | # include <memory> |
51 | |
52 | #define Q_ALLOCA_DECLARE(type, name) \ |
53 | std::unique_ptr<char[]> _qt_alloca_##name; \ |
54 | type *name = nullptr |
55 | |
56 | #define Q_ALLOCA_ASSIGN(type, name, size) \ |
57 | do { \ |
58 | _qt_alloca_##name.reset(new char[size]); \ |
59 | name = reinterpret_cast<type*>(_qt_alloca_##name.get()); \ |
60 | } while (false) |
61 | |
62 | #endif |
63 | |
64 | #endif |
65 | |