Skip to content

Commit 2333fd7

Browse files
spot tons of issues with ICPUBuffer, opened an issue
1 parent 180a4d2 commit 2333fd7

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

include/nbl/asset/ICPUBuffer.h

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,23 @@ namespace nbl::asset
2727
class ICPUBuffer : public asset::IBuffer, public asset::IAsset
2828
{
2929
protected:
30-
virtual ~ICPUBuffer()
31-
{
32-
this->convertToDummyObject();
33-
}
34-
3530
//! Non-allocating constructor for CCustormAllocatorCPUBuffer derivative
3631
ICPUBuffer(size_t sizeInBytes, void* dat) : asset::IBuffer({ dat ? sizeInBytes : 0,EUF_TRANSFER_DST_BIT }), data(dat) {}
32+
3733
public:
3834
//! Constructor. TODO: remove, alloc can fail, should be a static create method instead!
3935
/** @param sizeInBytes Size in bytes. If `dat` argument is present, it denotes size of data pointed by `dat`, otherwise - size of data to be allocated.
4036
*/
4137
ICPUBuffer(size_t sizeInBytes) : asset::IBuffer({0,EUF_TRANSFER_DST_BIT})
4238
{
4339
data = _NBL_ALIGNED_MALLOC(sizeInBytes,_NBL_SIMD_ALIGNMENT);
44-
if (!data)
40+
if (!data) // FIXME: cannot fail like that, need factory `create` methods
4541
return;
4642

4743
m_creationParams.size = sizeInBytes;
4844
}
4945

50-
core::smart_refctd_ptr<IAsset> clone(uint32_t = ~0u) const override
46+
core::smart_refctd_ptr<IAsset> clone(uint32_t = ~0u) const override final
5147
{
5248
auto cp = core::make_smart_refctd_ptr<ICPUBuffer>(m_creationParams.size);
5349
clone_common(cp.get());
@@ -56,33 +52,29 @@ class ICPUBuffer : public asset::IBuffer, public asset::IAsset
5652
return cp;
5753
}
5854

59-
virtual void convertToDummyObject(uint32_t referenceLevelsBelowToConvert = 0u) override
55+
void convertToDummyObject(uint32_t referenceLevelsBelowToConvert = 0u) override final
6056
{
6157
if (!canBeConvertedToDummy())
6258
return;
6359
convertToDummyObject_common(referenceLevelsBelowToConvert);
64-
65-
if (data)
66-
_NBL_ALIGNED_FREE(data);
67-
data = nullptr;
68-
m_creationParams.size = 0ull;
60+
freeData();
6961
isDummyObjectForCacheAliasing = true;
7062
}
7163

7264
_NBL_STATIC_INLINE_CONSTEXPR auto AssetType = ET_BUFFER;
73-
inline IAsset::E_TYPE getAssetType() const override { return AssetType; }
65+
inline IAsset::E_TYPE getAssetType() const override final { return AssetType; }
7466

75-
virtual size_t conservativeSizeEstimate() const override { return getSize(); }
67+
size_t conservativeSizeEstimate() const override final { return getSize(); }
7668

7769
//! Returns pointer to data.
78-
virtual const void* getPointer() const {return data;}
79-
virtual void* getPointer()
70+
const void* getPointer() const {return data;}
71+
void* getPointer()
8072
{
8173
assert(!isImmutable_debug());
8274
return data;
8375
}
8476

85-
bool canBeRestoredFrom(const IAsset* _other) const override
77+
bool canBeRestoredFrom(const IAsset* _other) const override final
8678
{
8779
auto* other = static_cast<const ICPUBuffer*>(_other);
8880
if (m_creationParams.size != other->m_creationParams.size)
@@ -108,14 +100,27 @@ class ICPUBuffer : public asset::IBuffer, public asset::IAsset
108100
}
109101

110102
protected:
111-
void restoreFromDummy_impl(IAsset* _other, uint32_t _levelsBelow) override
103+
void restoreFromDummy_impl(IAsset* _other, uint32_t _levelsBelow) override final
112104
{
113105
auto* other = static_cast<ICPUBuffer*>(_other);
114106

107+
// NO THIS IS A NIGHTMARE!
108+
// FIXME: ONLY SWAP FOR COMPATIBLE ALLOCATORS! OTHERWISE MEMCPY!
115109
if (willBeRestoredFrom(_other))
116110
std::swap(data, other->data);
117111
}
118112

113+
// REMEMBER TO CALL FROM DTOR!
114+
// TODO: idea, make the `ICPUBuffer` an ADT, and use the default allocator CCPUBuffer instead for consistency
115+
// TODO: idea make a macro for overriding all `delete` operators of a class to enforce a finalizer that runs in reverse order to destructors (to allow polymorphic cleanups)
116+
virtual void freeData()
117+
{
118+
if (data)
119+
_NBL_ALIGNED_FREE(data);
120+
data = nullptr;
121+
m_creationParams.size = 0ull;
122+
}
123+
119124
void* data;
120125
};
121126

@@ -140,42 +145,37 @@ using CDummyCPUBuffer = CCustomAllocatorCPUBuffer<core::null_allocator<uint8_t>,
140145
*/
141146

142147
template<typename Allocator>
143-
class CCustomAllocatorCPUBuffer<Allocator, true> : public ICPUBuffer
148+
class CCustomAllocatorCPUBuffer<Allocator,true> : public ICPUBuffer
144149
{
145150
static_assert(sizeof(typename Allocator::value_type) == 1u, "Allocator::value_type must be of size 1");
146151
protected:
147152
Allocator m_allocator;
148153

149-
virtual ~CCustomAllocatorCPUBuffer()
154+
virtual ~CCustomAllocatorCPUBuffer() final
150155
{
151-
this->convertToDummyObject();
156+
freeData();
152157
}
153-
154-
public:
155-
CCustomAllocatorCPUBuffer(size_t sizeInBytes, void* dat, core::adopt_memory_t, Allocator&& alctr = Allocator()) : ICPUBuffer(sizeInBytes, dat), m_allocator(std::move(alctr))
156-
{
157-
}
158-
159-
virtual void convertToDummyObject(uint32_t referenceLevelsBelowToConvert = 0u) override
158+
inline void freeData() override
160159
{
161-
if (isDummyObjectForCacheAliasing)
162-
return;
163-
convertToDummyObject_common(referenceLevelsBelowToConvert);
164-
if (!canBeConvertedToDummy())
165-
return;
166-
167160
if (ICPUBuffer::data)
168161
m_allocator.deallocate(reinterpret_cast<typename Allocator::pointer>(ICPUBuffer::data), ICPUBuffer::m_creationParams.size);
169162
ICPUBuffer::data = nullptr; // so that ICPUBuffer won't try deallocating
170163
}
164+
165+
public:
166+
CCustomAllocatorCPUBuffer(size_t sizeInBytes, void* dat, core::adopt_memory_t, Allocator&& alctr = Allocator()) : ICPUBuffer(sizeInBytes,dat), m_allocator(std::move(alctr))
167+
{
168+
}
171169
};
172170

173171
template<typename Allocator>
174172
class CCustomAllocatorCPUBuffer<Allocator, false> : public CCustomAllocatorCPUBuffer<Allocator, true>
175173
{
176174
using Base = CCustomAllocatorCPUBuffer<Allocator, true>;
175+
177176
protected:
178177
virtual ~CCustomAllocatorCPUBuffer() = default;
178+
inline void freeData() override {}
179179

180180
public:
181181
using Base::Base;

0 commit comments

Comments
 (0)