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 | #if QT_CONFIG(alloca_h) |
21 | # include <alloca.h> |
22 | #elif QT_CONFIG(alloca_malloc_h) |
23 | # include <malloc.h> |
24 | // This does not matter unless compiling in strict standard mode. |
25 | # ifdef Q_CC_MSVC |
26 | # define alloca _alloca |
27 | # endif |
28 | #else |
29 | # include <stdlib.h> |
30 | #endif |
31 | |
32 | // Define Q_ALLOCA_VAR macro to be used instead of #ifdeffing |
33 | // the occurrences of alloca() in case it's not supported. |
34 | // Q_ALLOCA_DECLARE and Q_ALLOCA_ASSIGN macros separate |
35 | // memory allocation from the declaration and RAII. |
36 | #define Q_ALLOCA_VAR(type, name, size) \ |
37 | Q_ALLOCA_DECLARE(type, name); \ |
38 | Q_ALLOCA_ASSIGN(type, name, size) |
39 | |
40 | #if QT_CONFIG(alloca) |
41 | |
42 | #define Q_ALLOCA_DECLARE(type, name) \ |
43 | type *name = 0 |
44 | |
45 | #define Q_ALLOCA_ASSIGN(type, name, size) \ |
46 | name = static_cast<type*>(alloca(size)) |
47 | |
48 | #else |
49 | QT_BEGIN_NAMESPACE |
50 | class Qt_AllocaWrapper |
51 | { |
52 | public: |
53 | Qt_AllocaWrapper() { m_data = 0; } |
54 | ~Qt_AllocaWrapper() { free(m_data); } |
55 | void *data() { return m_data; } |
56 | void allocate(int size) { m_data = malloc(size); memset(m_data, 0, size); } |
57 | private: |
58 | void *m_data; |
59 | }; |
60 | QT_END_NAMESPACE |
61 | |
62 | #define Q_ALLOCA_DECLARE(type, name) \ |
63 | Qt_AllocaWrapper _qt_alloca_##name; \ |
64 | type *name = nullptr |
65 | |
66 | #define Q_ALLOCA_ASSIGN(type, name, size) \ |
67 | do { \ |
68 | _qt_alloca_##name.allocate(size); \ |
69 | name = static_cast<type*>(_qt_alloca_##name.data()); \ |
70 | } while (false) |
71 | |
72 | #endif |
73 | |
74 | #endif |
75 | |