Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2022 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #include <algorithm>
21 :
22 : #include "IpcLogging.h"
23 : #include "SimpleBufferPool.h"
24 :
25 67 : SimpleBufferPool::SimpleBufferPool() : m_staticBuf(nullptr), m_staticBufSize(0)
26 : {
27 : // allocate a 64kb 'static' buffer and split it up into smaller individual buffers
28 67 : m_staticBufSize = 64 * 1024;
29 67 : m_staticBuf = new uint8_t[m_staticBufSize];
30 :
31 67 : uint8_t *p = m_staticBuf;
32 603 : for (int i = 0; i < 8; i++)
33 : {
34 536 : m_staticBufMetaData.emplace(p, BufInfo{256, true});
35 536 : p += 256;
36 : }
37 469 : for (int i = 0; i < 6; i++)
38 : {
39 402 : m_staticBufMetaData.emplace(p, BufInfo{1024, true});
40 402 : p += 1024;
41 : }
42 201 : for (int i = 0; i < 2; i++)
43 : {
44 134 : m_staticBufMetaData.emplace(p, BufInfo{4096, true});
45 134 : p += 4096;
46 : }
47 134 : for (int i = 0; i < 1; i++)
48 : {
49 67 : m_staticBufMetaData.emplace(p, BufInfo{16384, true});
50 67 : p += 16384;
51 : }
52 134 : for (int i = 0; i < 1; i++)
53 : {
54 67 : m_staticBufMetaData.emplace(p, BufInfo{32768, true});
55 67 : p += 32768;
56 : }
57 : }
58 :
59 67 : SimpleBufferPool::~SimpleBufferPool()
60 : {
61 67 : delete[] m_staticBuf;
62 : }
63 :
64 49 : void *SimpleBufferPool::allocateImpl(size_t bytes)
65 : {
66 : // try and find a free static buffer that is big enough
67 : {
68 49 : std::lock_guard<std::mutex> locker(m_staticBufLock);
69 :
70 49 : auto entry = std::find_if(m_staticBufMetaData.begin(), m_staticBufMetaData.end(), [&](const auto &metadata)
71 49 : { return (metadata.second.free && (metadata.second.size >= bytes)); });
72 49 : if (m_staticBufMetaData.end() != entry)
73 : {
74 49 : entry->second.free = false;
75 49 : return entry->first;
76 : }
77 : }
78 :
79 : // RIALTO_IPC_LOG_DEBUG("no static buffers for alloc of size %zu", bytes);
80 :
81 : // failed, so revert to dynamic allocation
82 0 : return malloc(bytes);
83 : }
84 :
85 49 : void SimpleBufferPool::deallocate(void *p)
86 : {
87 : // if the pointer is not within our static area then assume it was dynamically
88 : // allocated, in which case just free it
89 49 : if ((p < m_staticBuf) || (p >= (m_staticBuf + m_staticBufSize)))
90 : {
91 0 : free(p);
92 0 : return;
93 : }
94 :
95 49 : std::lock_guard<std::mutex> locker(m_staticBufLock);
96 :
97 : // else find the meta-data on the buffer pointer and mark as freed
98 49 : auto it = m_staticBufMetaData.find(p);
99 49 : if (it == m_staticBufMetaData.end())
100 : {
101 0 : RIALTO_IPC_LOG_FATAL("trying to free an unknown buffer from the pool!");
102 0 : return;
103 : }
104 :
105 49 : it->second.free = true;
106 : }
|