| 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 QV4EXECUTABLEALLOCATOR_H |
| 5 | #define QV4EXECUTABLEALLOCATOR_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 <QMultiMap> |
| 19 | #include <QHash> |
| 20 | #include <QVector> |
| 21 | #include <QByteArray> |
| 22 | #include <QMutex> |
| 23 | |
| 24 | #include <QtQml/private/qtqmlglobal_p.h> |
| 25 | |
| 26 | namespace WTF { |
| 27 | class PageAllocation; |
| 28 | } |
| 29 | |
| 30 | QT_BEGIN_NAMESPACE |
| 31 | |
| 32 | namespace QV4 { |
| 33 | |
| 34 | class Q_QML_AUTOTEST_EXPORT ExecutableAllocator |
| 35 | { |
| 36 | public: |
| 37 | struct ChunkOfPages; |
| 38 | struct Allocation; |
| 39 | |
| 40 | ExecutableAllocator(); |
| 41 | ~ExecutableAllocator(); |
| 42 | |
| 43 | Allocation *allocate(size_t size); |
| 44 | void free(Allocation *allocation); |
| 45 | |
| 46 | struct Allocation |
| 47 | { |
| 48 | Allocation() |
| 49 | : size(0) |
| 50 | , free(true) |
| 51 | {} |
| 52 | |
| 53 | void *memoryStart() const; |
| 54 | size_t memorySize() const { return size; } |
| 55 | |
| 56 | void *exceptionHandlerStart() const; |
| 57 | size_t exceptionHandlerSize() const; |
| 58 | |
| 59 | void *codeStart() const; |
| 60 | |
| 61 | void invalidate() { addr = 0; } |
| 62 | bool isValid() const { return addr != 0; } |
| 63 | void deallocate(ExecutableAllocator *allocator); |
| 64 | |
| 65 | private: |
| 66 | ~Allocation() {} |
| 67 | |
| 68 | friend class ExecutableAllocator; |
| 69 | |
| 70 | Allocation *split(size_t dividingSize); |
| 71 | bool mergeNext(ExecutableAllocator *allocator); |
| 72 | bool mergePrevious(ExecutableAllocator *allocator); |
| 73 | |
| 74 | quintptr addr = 0; |
| 75 | uint size : 31; // More than 2GB of function code? nah :) |
| 76 | uint free : 1; |
| 77 | Allocation *next = nullptr; |
| 78 | Allocation *prev = nullptr; |
| 79 | }; |
| 80 | |
| 81 | // for debugging / unit-testing |
| 82 | int freeAllocationCount() const { return freeAllocations.size(); } |
| 83 | int chunkCount() const { return chunks.size(); } |
| 84 | |
| 85 | struct ChunkOfPages |
| 86 | { |
| 87 | ChunkOfPages() |
| 88 | |
| 89 | {} |
| 90 | ~ChunkOfPages(); |
| 91 | |
| 92 | WTF::PageAllocation *pages = nullptr; |
| 93 | Allocation *firstAllocation = nullptr; |
| 94 | |
| 95 | bool contains(Allocation *alloc) const; |
| 96 | }; |
| 97 | |
| 98 | ChunkOfPages *chunkForAllocation(Allocation *allocation) const; |
| 99 | |
| 100 | private: |
| 101 | QMultiMap<size_t, Allocation*> freeAllocations; |
| 102 | QMap<quintptr, ChunkOfPages*> chunks; |
| 103 | mutable QMutex mutex; |
| 104 | }; |
| 105 | |
| 106 | } |
| 107 | |
| 108 | QT_END_NAMESPACE |
| 109 | |
| 110 | #endif // QV4EXECUTABLEALLOCATOR_H |
| 111 | |