LCOV - code coverage report
Current view: top level - ipc/common/source - SimpleBufferPool.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 87.5 % 40 35
Test Date: 2025-02-18 13:13:53 Functions: 100.0 % 5 5

            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           53 : SimpleBufferPool::SimpleBufferPool() : m_staticBuf(nullptr), m_staticBufSize(0)
      26              : {
      27              :     // allocate a 64kb 'static' buffer and split it up into smaller individual buffers
      28           53 :     m_staticBufSize = 64 * 1024;
      29           53 :     m_staticBuf = new uint8_t[m_staticBufSize];
      30              : 
      31           53 :     uint8_t *p = m_staticBuf;
      32          477 :     for (int i = 0; i < 8; i++)
      33              :     {
      34          424 :         m_staticBufMetaData.emplace(p, BufInfo{256, true});
      35          424 :         p += 256;
      36              :     }
      37          371 :     for (int i = 0; i < 6; i++)
      38              :     {
      39          318 :         m_staticBufMetaData.emplace(p, BufInfo{1024, true});
      40          318 :         p += 1024;
      41              :     }
      42          159 :     for (int i = 0; i < 2; i++)
      43              :     {
      44          106 :         m_staticBufMetaData.emplace(p, BufInfo{4096, true});
      45          106 :         p += 4096;
      46              :     }
      47          106 :     for (int i = 0; i < 1; i++)
      48              :     {
      49           53 :         m_staticBufMetaData.emplace(p, BufInfo{16384, true});
      50           53 :         p += 16384;
      51              :     }
      52          106 :     for (int i = 0; i < 1; i++)
      53              :     {
      54           53 :         m_staticBufMetaData.emplace(p, BufInfo{32768, true});
      55           53 :         p += 32768;
      56              :     }
      57              : }
      58              : 
      59           53 : SimpleBufferPool::~SimpleBufferPool()
      60              : {
      61           53 :     delete[] m_staticBuf;
      62              : }
      63              : 
      64           33 : void *SimpleBufferPool::allocateImpl(size_t bytes)
      65              : {
      66              :     // try and find a free static buffer that is big enough
      67              :     {
      68           33 :         std::lock_guard<std::mutex> locker(m_staticBufLock);
      69              : 
      70           33 :         auto entry = std::find_if(m_staticBufMetaData.begin(), m_staticBufMetaData.end(),
      71           33 :                                   [&](const auto &metadata)
      72           33 :                                   { return (metadata.second.free && (metadata.second.size >= bytes)); });
      73           33 :         if (m_staticBufMetaData.end() != entry)
      74              :         {
      75           33 :             entry->second.free = false;
      76           33 :             return entry->first;
      77              :         }
      78              :     }
      79              : 
      80              :     // RIALTO_IPC_LOG_DEBUG("no static buffers for alloc of size %zu", bytes);
      81              : 
      82              :     // failed, so revert to dynamic allocation
      83            0 :     return malloc(bytes);
      84              : }
      85              : 
      86           33 : void SimpleBufferPool::deallocate(void *p)
      87              : {
      88              :     // if the pointer is not within our static area then assume it was dynamically
      89              :     // allocated, in which case just free it
      90           33 :     if ((p < m_staticBuf) || (p >= (m_staticBuf + m_staticBufSize)))
      91              :     {
      92            0 :         free(p);
      93            0 :         return;
      94              :     }
      95              : 
      96           33 :     std::lock_guard<std::mutex> locker(m_staticBufLock);
      97              : 
      98              :     // else find the meta-data on the buffer pointer and mark as freed
      99           33 :     auto it = m_staticBufMetaData.find(p);
     100           33 :     if (it == m_staticBufMetaData.end())
     101              :     {
     102            0 :         RIALTO_IPC_LOG_FATAL("trying to free an unknown buffer from the pool!");
     103            0 :         return;
     104              :     }
     105              : 
     106           33 :     it->second.free = true;
     107              : }
        

Generated by: LCOV version 2.0-1