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 <tlsf.h>
9
11
12namespace QtMultimediaPrivate {
13
14QTlsfMemoryResource::QTlsfMemoryResource(std::size_t preallocatedBytes, memory_resource *upstream):
15 m_allocationSize {
16 preallocatedBytes + 8192, // over-allocate by 8k for tlsf header
17 },
18 m_upstream{
19 upstream,
20 }
21{
22 m_buffer = reinterpret_cast<std::byte *>(operator new(m_allocationSize, poolAlignment));
23
24 m_tlsf = QtPrivate::tlsf_create_with_pool(m_buffer, m_allocationSize);
25 Q_ASSERT(m_tlsf);
26}
27
28QTlsfMemoryResource::~QTlsfMemoryResource()
29{
30 QtPrivate::tlsf_destroy(m_tlsf);
31 ::operator delete(m_buffer, poolAlignment);
32}
33
34void *QTlsfMemoryResource::do_allocate(size_t bytes, size_t alignment)
35{
36 void *ret = QtPrivate::tlsf_memalign(m_tlsf, alignment, bytes);
37 if (Q_UNLIKELY(ret == nullptr))
38 ret = m_upstream->allocate(bytes, alignment);
39
40 return ret;
41}
42
43void QTlsfMemoryResource::do_deallocate(void *p, size_t bytes, size_t alignment)
44{
45 if (Q_LIKELY(p >= m_buffer && p < m_buffer + m_allocationSize))
46 QtPrivate::tlsf_free(m_tlsf, p);
47 else
48 m_upstream->deallocate(p, bytes, alignment);
49}
50
51bool QTlsfMemoryResource::do_is_equal(const memory_resource &other) const noexcept
52{
53 return this == &other;
54}
55
56} // namespace QtMultimediaPrivate
57
58QT_END_NAMESPACE
Combined button and popup list for selecting options.
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
Q_MULTIMEDIA_EXPORT QTlsfMemoryResource(std::size_t preallocatedBytes, pmr::memory_resource *upstream=pmr::get_default_resource())