Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4memberdata.cpp
Go to the documentation of this file.
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// Qt-Security score:significant
4
6#include <private/qv4mm_p.h>
7#include "qv4value_p.h"
8
9using namespace QV4;
10
12
13static size_t nextPowerOfTwo(size_t s)
14{
15 --s;
16 s |= s >> 1;
17 s |= s >> 2;
18 s |= s >> 4;
19 s |= s >> 8;
20 s |= s >> 16;
21#if (QT_POINTER_SIZE == 8)
22 s |= s >> 32;
23#endif
24 ++s;
25 return s;
26}
27
28Heap::MemberData *MemberData::allocate(ExecutionEngine *e, uint n, Heap::MemberData *old)
29{
30 Q_ASSERT(!old || old->values.size <= n);
31 if (!n)
32 n = 4;
33
34 size_t alloc = MemoryManager::align(sizeof(Heap::MemberData) + (n - 1)*sizeof(Value));
35 // round up to next power of two to avoid quadratic behaviour for very large objects
36 alloc = nextPowerOfTwo(alloc);
37
38 // The above code can overflow in a number of interesting ways. All of those are unsigned,
39 // and therefore defined behavior. Still, apply some sane bounds.
40 const size_t intMax = std::numeric_limits<int>::max();
41 if (alloc > intMax)
42 alloc = intMax;
43
44 Heap::MemberData *m;
45 if (old) {
46 const size_t oldSize = sizeof(Heap::MemberData) + (old->values.size - 1) * sizeof(Value);
47 if (oldSize > alloc)
48 alloc = oldSize;
49 m = e->memoryManager->allocManaged<MemberData>(alloc);
50 // no write barrier required here, as m gets marked later when member data is set
51 memcpy(m, old, oldSize);
52 } else {
53 m = e->memoryManager->allocManaged<MemberData>(alloc);
54 m->init();
55 }
56
57 m->values.alloc = static_cast<uint>((alloc - sizeof(Heap::MemberData) + sizeof(Value))/sizeof(Value));
58 m->values.size = m->values.alloc;
59 return m;
60}
DEFINE_MANAGED_VTABLE(MemberData)
static size_t nextPowerOfTwo(size_t s)