Matter SDK Coverage Report
Current view: top level - lib/support - ScopedBuffer.h (source / functions) Coverage Total Hit
Test: SHA:da00cfb13e586707ec8d44ae49325a696d4a8855 Lines: 83.5 % 85 71
Test Date: 2025-08-16 07:11:35 Functions: 86.6 % 97 84

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2020 Project CHIP Authors
       4              :  *    All rights reserved.
       5              :  *
       6              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       7              :  *    you may not use this file except in compliance with the License.
       8              :  *    You may obtain a copy of the License at
       9              :  *
      10              :  *        http://www.apache.org/licenses/LICENSE-2.0
      11              :  *
      12              :  *    Unless required by applicable law or agreed to in writing, software
      13              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      14              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      15              :  *    See the License for the specific language governing permissions and
      16              :  *    limitations under the License.
      17              :  */
      18              : 
      19              : /**
      20              :  *    @file
      21              :  *      Defines scoped auto-free buffers for CHIP.
      22              :  *
      23              :  */
      24              : 
      25              : #pragma once
      26              : 
      27              : #include <lib/support/CHIPMem.h>
      28              : #include <lib/support/CodeUtils.h>
      29              : #include <lib/support/Span.h>
      30              : 
      31              : #include <type_traits>
      32              : #include <utility>
      33              : 
      34              : namespace chip {
      35              : namespace Platform {
      36              : 
      37              : namespace Impl {
      38              : 
      39              : /**
      40              :  * Represents a memory buffer that is auto-freed in the destructor.
      41              :  *
      42              :  * This class uses void* underneath on purpose (rather than a unique_ptr like
      43              :  * 'Type') and uses  templated type on Ptr(). This is to avoid template explosion
      44              :  * when the buffers are used for different types - only one implementation of
      45              :  * the class will be stored in flash.
      46              :  */
      47              : template <class Impl>
      48              : class ScopedMemoryBufferBase
      49              : {
      50              : public:
      51        22922 :     ScopedMemoryBufferBase() {}
      52              :     ScopedMemoryBufferBase(const ScopedMemoryBufferBase &)                   = delete;
      53              :     ScopedMemoryBufferBase & operator=(const ScopedMemoryBufferBase & other) = delete;
      54              : 
      55              :     ScopedMemoryBufferBase(ScopedMemoryBufferBase && other) { *this = std::move(other); }
      56              : 
      57         7342 :     ScopedMemoryBufferBase & operator=(ScopedMemoryBufferBase && other)
      58              :     {
      59         7342 :         if (this != &other)
      60              :         {
      61         7342 :             mBuffer       = other.mBuffer;
      62         7342 :             other.mBuffer = nullptr;
      63              :         }
      64         7342 :         return *this;
      65              :     }
      66              : 
      67        22922 :     ~ScopedMemoryBufferBase() { Free(); }
      68              : 
      69              :     /** Check if a buffer is valid */
      70         1299 :     explicit operator bool() const { return mBuffer != nullptr; }
      71         7191 :     bool operator!() const { return mBuffer == nullptr; }
      72            2 :     inline bool IsNull() const { return mBuffer == nullptr; }
      73              : 
      74              :     /** Release memory used */
      75        49968 :     void Free()
      76              :     {
      77        49968 :         if (mBuffer == nullptr)
      78              :         {
      79        36788 :             return;
      80              :         }
      81        13180 :         Impl::MemoryFree(mBuffer);
      82        13180 :         mBuffer = nullptr;
      83              :     }
      84              : 
      85              : protected:
      86        29856 :     void * Ptr() { return mBuffer; }
      87         9217 :     const void * Ptr() const { return mBuffer; }
      88              : 
      89              :     /**
      90              :      * Releases the underlying buffer.
      91              :      *
      92              :      * The buffer stops being managed and will not be auto-freed.
      93              :      */
      94            1 :     CHECK_RETURN_VALUE void * Release()
      95              :     {
      96            1 :         void * buffer = mBuffer;
      97            1 :         mBuffer       = nullptr;
      98            1 :         return buffer;
      99              :     }
     100              : 
     101         8700 :     void Alloc(size_t size)
     102              :     {
     103         8700 :         Free();
     104         8700 :         mBuffer = Impl::MemoryAlloc(size);
     105         8700 :     }
     106              : 
     107         4481 :     void Calloc(size_t elementCount, size_t elementSize)
     108              :     {
     109         4481 :         Free();
     110         4481 :         mBuffer = Impl::MemoryCalloc(elementCount, elementSize);
     111         4481 :     }
     112              : 
     113              : private:
     114              :     void * mBuffer = nullptr;
     115              : };
     116              : 
     117              : /**
     118              :  * Helper class that forwards memory management tasks to Platform::Memory* calls.
     119              :  */
     120              : class PlatformMemoryManagement
     121              : {
     122              : public:
     123        13176 :     static void MemoryFree(void * p) { chip::Platform::MemoryFree(p); }
     124         8695 :     static void * MemoryAlloc(size_t size) { return chip::Platform::MemoryAlloc(size); }
     125         4480 :     static void * MemoryCalloc(size_t num, size_t size) { return chip::Platform::MemoryCalloc(num, size); }
     126              : };
     127              : 
     128              : } // namespace Impl
     129              : 
     130              : /**
     131              :  * Represents a memory buffer allocated using chip::Platform::Memory*Alloc
     132              :  * methods.
     133              :  *
     134              :  * Use for RAII to auto-free after use.
     135              :  *
     136              :  * For a single element RAII with dtor, use Platform::UniquePtr<>
     137              :  */
     138              : template <typename T, class MemoryManagement = Impl::PlatformMemoryManagement>
     139              : class ScopedMemoryBuffer : public Impl::ScopedMemoryBufferBase<MemoryManagement>
     140              : {
     141              :     friend class Impl::ScopedMemoryBufferBase<MemoryManagement>;
     142              : 
     143              : public:
     144              :     using Base = Impl::ScopedMemoryBufferBase<MemoryManagement>;
     145              : 
     146              :     static_assert(std::is_trivially_destructible<T>::value, "Destructors won't get run");
     147              : 
     148        29856 :     T * Get() { return static_cast<T *>(Base::Ptr()); }
     149          712 :     T & operator[](size_t index) { return Get()[index]; }
     150              : 
     151         9217 :     const T * Get() const { return static_cast<const T *>(Base::Ptr()); }
     152          100 :     const T & operator[](size_t index) const { return Get()[index]; }
     153              : 
     154              :     /**
     155              :      * Releases the underlying buffer.
     156              :      *
     157              :      * The buffer stops being managed and will not be auto-freed.
     158              :      */
     159            1 :     CHECK_RETURN_VALUE T * Release() { return static_cast<T *>(Base::Release()); }
     160              : 
     161         4481 :     ScopedMemoryBuffer & Calloc(size_t elementCount)
     162              :     {
     163         4481 :         Base::Calloc(elementCount, sizeof(T));
     164         4481 :         ExecuteConstructors(elementCount);
     165         4481 :         return *this;
     166              :     }
     167              : 
     168         8700 :     ScopedMemoryBuffer & Alloc(size_t elementCount)
     169              :     {
     170         8700 :         Base::Alloc(elementCount * sizeof(T));
     171         8700 :         ExecuteConstructors(elementCount);
     172         8700 :         return *this;
     173              :     }
     174              : 
     175              : private:
     176              :     template <typename U = T, std::enable_if_t<std::is_trivial<U>::value, int> = 0>
     177        13181 :     void ExecuteConstructors(size_t elementCount)
     178              :     {
     179              :         // Do nothing if our type is trivial.  In particular, if we are a buffer
     180              :         // of integers, we should not go zero-initializing them here: either
     181              :         // caller wants that and called Calloc(), or it doesn't and we shouldn't
     182              :         // do it.
     183        13181 :     }
     184              : 
     185              :     template <typename U = T, std::enable_if_t<!std::is_trivial<U>::value, int> = 0>
     186            0 :     void ExecuteConstructors(size_t elementCount)
     187              :     {
     188            0 :         T * elementPtr = Get();
     189            0 :         if (elementPtr == nullptr)
     190              :         {
     191              :             // Alloc failed, don't bother.
     192            0 :             return;
     193              :         }
     194            0 :         for (size_t i = 0; i < elementCount; ++i)
     195              :         {
     196            0 :             new (&elementPtr[i]) T();
     197              :         }
     198              :     }
     199              : };
     200              : 
     201              : /**
     202              :  * Represents a memory buffer with buffer size allocated using chip::Platform::Memory*Alloc
     203              :  * methods.
     204              :  *
     205              :  * Use for RAII to auto-free after use.
     206              :  */
     207              : template <typename T>
     208              : class ScopedMemoryBufferWithSize : public ScopedMemoryBuffer<T>
     209              : {
     210              : public:
     211         8984 :     ScopedMemoryBufferWithSize() {}
     212          208 :     ScopedMemoryBufferWithSize(ScopedMemoryBufferWithSize && other) { *this = std::move(other); }
     213              : 
     214         2624 :     ScopedMemoryBufferWithSize & operator=(ScopedMemoryBufferWithSize && other)
     215              :     {
     216         2624 :         if (this != &other)
     217              :         {
     218         2624 :             mCount       = other.mCount;
     219         2624 :             other.mCount = 0;
     220              :         }
     221         2624 :         ScopedMemoryBuffer<T>::operator=(std::move(other));
     222         2624 :         return *this;
     223              :     }
     224              : 
     225              :     // return the size as count of elements
     226         6171 :     inline size_t AllocatedSize() const { return mCount; }
     227              : 
     228            3 :     chip::Span<T> Span() { return chip::Span<T>(this->Get(), AllocatedSize()); }
     229              :     chip::Span<const T> Span() const { return chip::Span<T>(this->Get(), AllocatedSize()); }
     230              : 
     231        13788 :     void Free()
     232              :     {
     233        13788 :         mCount = 0;
     234        13788 :         ScopedMemoryBuffer<T>::Free();
     235        13788 :     }
     236              : 
     237              :     /**
     238              :      * Releases the underlying buffer.
     239              :      *
     240              :      * The buffer stops being managed and will not be auto-freed.
     241              :      */
     242              :     CHECK_RETURN_VALUE T * Release()
     243              :     {
     244              :         T * buffer = ScopedMemoryBuffer<T>::Release();
     245              :         mCount     = 0;
     246              :         return buffer;
     247              :     }
     248              : 
     249          263 :     ScopedMemoryBufferWithSize & Calloc(size_t elementCount)
     250              :     {
     251          263 :         ScopedMemoryBuffer<T>::Calloc(elementCount);
     252          263 :         if (this->Get() != nullptr)
     253              :         {
     254          263 :             mCount = elementCount;
     255              :         }
     256          263 :         return *this;
     257              :     }
     258              : 
     259         2833 :     ScopedMemoryBufferWithSize & Alloc(size_t elementCount)
     260              :     {
     261         2833 :         ScopedMemoryBuffer<T>::Alloc(elementCount);
     262         2833 :         if (this->Get() != nullptr)
     263              :         {
     264         2833 :             mCount = elementCount;
     265              :         }
     266         2833 :         return *this;
     267              :     }
     268              : 
     269              :     // Allow creating ScopedMemoryBufferWithSize from Span, so we
     270              :     // don't have to reinvent it in various places.
     271              :     template <class U, typename = std::enable_if_t<sizeof(U) == sizeof(T) && std::is_convertible_v<U *, T *>>>
     272            0 :     ScopedMemoryBufferWithSize & CopyFromSpan(const chip::Span<const U> & span)
     273              :     {
     274              :         static_assert(std::is_trivially_copyable_v<U>, "Span<const U> must be trivially copyable");
     275              : 
     276            0 :         if (span.size() == 0)
     277              :         {
     278            0 :             Free();
     279            0 :             return *this;
     280              :         }
     281              : 
     282            0 :         ScopedMemoryBufferWithSize<T>::Alloc(span.size());
     283              : 
     284            0 :         if (AllocatedSize() > 0)
     285              :         {
     286            0 :             memcpy(ScopedMemoryBuffer<T>::Get(), span.data(), AllocatedSize());
     287              :         }
     288              : 
     289            0 :         return *this;
     290              :     }
     291              : 
     292              : private:
     293              :     size_t mCount = 0;
     294              : };
     295              : 
     296              : } // namespace Platform
     297              : } // namespace chip
        

Generated by: LCOV version 2.0-1