Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DataBlobImpl: added option to provide custom memory allocator
Browse files Browse the repository at this point in the history
TheMostDiligent committed Nov 3, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 5196c52 commit bcd80f6
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
@@ -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"

@@ -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;
@@ -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
17 changes: 14 additions & 3 deletions Common/src/DataBlobImpl.cpp
Original file line number Diff line number Diff line change
@@ -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)
@@ -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)
{

0 comments on commit bcd80f6

Please sign in to comment.