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
glslmemorypool.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
7#include <cstring>
8#include <cassert>
9#include <cstdlib>
10
11QT_BEGIN_NAMESPACE
12
13using namespace GLSL;
14
15MemoryPool::MemoryPool()
16 : _blocks(nullptr),
17 _allocatedBlocks(0),
18 _blockCount(-1),
19 _ptr(nullptr),
20 _end(nullptr)
21{ }
22
23MemoryPool::~MemoryPool()
24{
25 if (_blocks) {
26 for (int i = 0; i < _allocatedBlocks; ++i) {
27 if (char *b = _blocks[i])
28 std::free(b);
29 }
30
31 std::free(_blocks);
32 }
33}
34
35void MemoryPool::reset()
36{
37 _blockCount = -1;
38 _ptr = _end = nullptr;
39}
40
41void *MemoryPool::allocate_helper(size_t size)
42{
43 assert(size < BLOCK_SIZE);
44
45 if (++_blockCount == _allocatedBlocks) {
46 if (! _allocatedBlocks)
47 _allocatedBlocks = DEFAULT_BLOCK_COUNT;
48 else
49 _allocatedBlocks *= 2;
50
51 _blocks = (char **) realloc(_blocks, sizeof(char *) * _allocatedBlocks);
52
53 for (int index = _blockCount; index < _allocatedBlocks; ++index)
54 _blocks[index] = nullptr;
55 }
56
57 char *&block = _blocks[_blockCount];
58
59 if (! block)
60 block = (char *) std::malloc(BLOCK_SIZE);
61
62 _ptr = block;
63 _end = _ptr + BLOCK_SIZE;
64
65 void *addr = _ptr;
66 _ptr += size;
67 return addr;
68}
69
70Managed::Managed()
71{ }
72
73Managed::~Managed()
74{ }
75
76void *Managed::operator new(size_t size, MemoryPool *pool)
77{ return pool->allocate(size); }
78
79void Managed::operator delete(void *)
80{ }
81
82void Managed::operator delete(void *, MemoryPool *)
83{ }
84
85QT_END_NAMESPACE
#define assert