Skip to content

Commit

Permalink
DataBlobImpl: added option to use custom memory allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Nov 4, 2024
1 parent dbfcb85 commit 24b29cd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
9 changes: 7 additions & 2 deletions Common/interface/DataBlobImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "../../Primitives/interface/BasicTypes.h"
#include "../../Primitives/interface/DataBlob.h"
#include "../../Primitives/interface/MemoryAllocator.h"
#include "STDAllocator.hpp"
#include "RefCntAutoPtr.hpp"
#include "ObjectBase.hpp"

Expand All @@ -47,6 +48,7 @@ class DataBlobImpl final : public Diligent::ObjectBase<IDataBlob>
typedef ObjectBase<IDataBlob> TBase;

static RefCntAutoPtr<DataBlobImpl> Create(size_t InitialSize = 0, const void* pData = nullptr);
static RefCntAutoPtr<DataBlobImpl> Create(IMemoryAllocator* pAllocator, size_t InitialSize = 0, const void* pData = nullptr);
static RefCntAutoPtr<DataBlobImpl> MakeCopy(const IDataBlob* pDataBlob);

~DataBlobImpl() override;
Expand Down Expand Up @@ -81,10 +83,13 @@ class DataBlobImpl final : public Diligent::ObjectBase<IDataBlob>
template <typename AllocatorType, typename ObjectType>
friend class MakeNewRCObj;

explicit DataBlobImpl(IReferenceCounters* pRefCounters, size_t InitialSize = 0, const void* pData = nullptr);
DataBlobImpl(IReferenceCounters* pRefCounters,
IMemoryAllocator& Allocator,
size_t InitialSize = 0,
const void* pData = nullptr);

private:
std::vector<Uint8> m_DataBuff;
std::vector<Uint8, STDAllocatorRawMem<Uint8>> m_DataBuff;
};

class DataBlobAllocatorAdapter final : public IMemoryAllocator
Expand Down
17 changes: 14 additions & 3 deletions Common/src/DataBlobImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,23 @@

#include "pch.h"
#include "DataBlobImpl.hpp"
#include "DefaultRawMemoryAllocator.hpp"

#include <cstring>

namespace Diligent
{

RefCntAutoPtr<DataBlobImpl> DataBlobImpl::Create(IMemoryAllocator* pAllocator, size_t InitialSize, const void* pData)
{
if (pAllocator == nullptr)
pAllocator = &DefaultRawMemoryAllocator::GetAllocator();
return RefCntAutoPtr<DataBlobImpl>{MakeNewRCObj<DataBlobImpl>()(*pAllocator, InitialSize, pData)};
}

RefCntAutoPtr<DataBlobImpl> DataBlobImpl::Create(size_t InitialSize, const void* pData)
{
return RefCntAutoPtr<DataBlobImpl>{MakeNewRCObj<DataBlobImpl>()(InitialSize, pData)};
return Create(nullptr, InitialSize, pData);
}

RefCntAutoPtr<DataBlobImpl> DataBlobImpl::MakeCopy(const IDataBlob* pDataBlob)
Expand All @@ -46,9 +54,12 @@ RefCntAutoPtr<DataBlobImpl> DataBlobImpl::MakeCopy(const IDataBlob* pDataBlob)
return Create(pDataBlob->GetSize(), pDataBlob->GetConstDataPtr());
}

DataBlobImpl::DataBlobImpl(IReferenceCounters* pRefCounters, size_t InitialSize, const void* pData) :
DataBlobImpl::DataBlobImpl(IReferenceCounters* pRefCounters,
IMemoryAllocator& Allocator,
size_t InitialSize,
const void* pData) :
TBase{pRefCounters},
m_DataBuff(InitialSize)
m_DataBuff{InitialSize, Uint8{}, STD_ALLOCATOR_RAW_MEM(Uint8, Allocator, "Allocator for vector<Uint8>")}
{
if (!m_DataBuff.empty() && pData != nullptr)
{
Expand Down

0 comments on commit 24b29cd

Please sign in to comment.