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
qmemory_resource_tlsf.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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
5
6#include <QtCore/qassert.h>
7
8#include <memory_resource>
9
10#include <tlsf.h>
11
12QT_BEGIN_NAMESPACE
13
14namespace QtMultimediaPrivate {
15
16QTlsfMemoryResource::QTlsfMemoryResource(std::size_t preallocatedBytes, memory_resource *upstream):
17 m_allocationSize {
18 preallocatedBytes + 8192, // over-allocate by 8k for tlsf header
19 },
20 m_upstream{
21 upstream,
22 }
23{
24 m_buffer = reinterpret_cast<std::byte *>(operator new(m_allocationSize, poolAlignment));
25
26 m_tlsf = QtPrivate::tlsf_create_with_pool(m_buffer, m_allocationSize);
27 Q_ASSERT(m_tlsf);
28}
29
30QTlsfMemoryResource::~QTlsfMemoryResource()
31{
32 QtPrivate::tlsf_destroy(m_tlsf);
33 ::operator delete(m_buffer, poolAlignment);
34}
35
36void *QTlsfMemoryResource::do_allocate(size_t bytes, size_t alignment)
37{
38 void *ret = QtPrivate::tlsf_memalign(m_tlsf, alignment, bytes);
39 if (Q_UNLIKELY(ret == nullptr))
40 ret = m_upstream->allocate(bytes, alignment);
41
42 return ret;
43}
44
45void QTlsfMemoryResource::do_deallocate(void *p, size_t bytes, size_t alignment)
46{
47 if (Q_LIKELY(p >= m_buffer && p < m_buffer + m_allocationSize))
48 QtPrivate::tlsf_free(m_tlsf, p);
49 else
50 m_upstream->deallocate(p, bytes, alignment);
51}
52
53bool QTlsfMemoryResource::do_is_equal(const memory_resource &other) const noexcept
54{
55 return this == &other;
56}
57
58} // namespace QtMultimediaPrivate
59
60QT_END_NAMESPACE
void do_deallocate(void *p, size_t bytes, size_t alignment) override
void * do_allocate(size_t bytes, size_t alignment) override
bool do_is_equal(const memory_resource &other) const noexcept override