Skip to content
This repository was archived by the owner on Jul 18, 2023. It is now read-only.

Commit 585623d

Browse files
authored
Aligned Blob (#16)
fix#15
1 parent d239d87 commit 585623d

File tree

4 files changed

+68
-15
lines changed

4 files changed

+68
-15
lines changed

Core/Blob.h

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,65 @@ namespace Snowing
1414
size_t size_;
1515

1616
bool owner_;
17+
bool aligned_;
1718

1819
inline Blob(std::uint8_t* bytes, size_t size, bool owner) noexcept :
1920
bytes_{ bytes },
2021
size_{ size },
21-
owner_{ owner }
22+
owner_{ owner },
23+
aligned_{ false }
2224
{}
2325

2426
public:
2527
inline Blob():
2628
bytes_{ nullptr },
2729
size_{ 0 },
28-
owner_{ false }
30+
owner_{ false },
31+
aligned_{ false }
2932
{}
3033

3134
explicit inline Blob(size_t size) :
3235
bytes_{ new std::uint8_t[size] },
3336
size_{ size },
34-
owner_{ true }
37+
owner_{ true },
38+
aligned_{ false }
3539
{
3640
assert(size);
3741
}
3842

43+
inline Blob(size_t size, size_t alignment):
44+
size_{ size },
45+
owner_{ true },
46+
aligned_{ true }
47+
{
48+
size_t offset, shift, alignedAddress;
49+
void* allocation;
50+
void** preamble;
51+
52+
offset = alignment - 1 + sizeof(void*);
53+
54+
allocation = static_cast<void*>(new uint8_t[size + static_cast<uint32_t>(offset)]);
55+
56+
alignedAddress = reinterpret_cast<size_t>(allocation) + sizeof(void*);
57+
58+
shift = alignedAddress % alignment;
59+
60+
if (shift)
61+
{
62+
alignedAddress += (alignment - shift);
63+
}
64+
65+
preamble = reinterpret_cast<void**>(alignedAddress);
66+
preamble[-1] = allocation;
67+
68+
bytes_ = reinterpret_cast<uint8_t*>(alignedAddress);
69+
}
70+
3971
inline Blob(std::vector<uint8_t>* blob,bool copyAndOwner = true):
4072
bytes_{ blob->data() },
4173
size_{ blob->size() },
42-
owner_{ copyAndOwner }
74+
owner_{ copyAndOwner },
75+
aligned_{ false }
4376
{
4477
if (owner_)
4578
{
@@ -50,8 +83,17 @@ namespace Snowing
5083

5184
inline ~Blob() noexcept
5285
{
53-
if(owner_)
54-
delete[] bytes_;
86+
if (owner_)
87+
{
88+
if (!aligned_)
89+
delete[] bytes_;
90+
else
91+
{
92+
void** preamble = reinterpret_cast<void**>(bytes_);
93+
delete [] reinterpret_cast<uint8_t*>(preamble[-1]);
94+
}
95+
}
96+
5597
}
5698

5799
[[nodiscard]]
@@ -66,11 +108,13 @@ namespace Snowing
66108
inline Blob(Blob&& b) noexcept :
67109
bytes_{ b.bytes_ },
68110
size_{ b.size_ },
69-
owner_{ b.owner_ }
111+
owner_{ b.owner_ },
112+
aligned_{ b.aligned_ }
70113
{
71114
b.size_ = 0;
72115
b.bytes_ = nullptr;
73116
b.owner_ = false;
117+
b.aligned_ = false;
74118
}
75119

76120
Blob& operator= (const Blob&) = delete;
@@ -84,10 +128,14 @@ namespace Snowing
84128
bytes_ = b.bytes_;
85129
size_ = b.size_;
86130
owner_ = b.owner_;
131+
aligned_ = b.aligned_;
87132

88133
b.bytes_ = nullptr;
89134
b.size_ = 0;
90135
b.owner_ = false;
136+
b.aligned_ = false;
137+
138+
return *this;
91139
}
92140

93141
template <typename TPointer>

WindowsImpl/AssetLoader.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using namespace Snowing;
55

6-
Blob Snowing::PlatformImpls::WindowsImpl::ReadFile(const char * fileName)
6+
Blob Snowing::PlatformImpls::WindowsImpl::ReadFile(const char * fileName,size_t alignment)
77
{
88
std::ifstream in{ fileName,std::ios::binary };
99

@@ -21,7 +21,12 @@ Blob Snowing::PlatformImpls::WindowsImpl::ReadFile(const char * fileName)
2121
in.seekg(0, std::ios::end);
2222
const auto size = static_cast<size_t>(in.tellg());
2323

24-
Blob b{ size };
24+
Blob b;
25+
26+
if (alignment)
27+
b = Blob{ size,alignment };
28+
else
29+
b = Blob{ size };
2530

2631
in.seekg(0, std::ios::beg);
2732
in.read(b.Get<char*>(), size);
@@ -30,12 +35,12 @@ Blob Snowing::PlatformImpls::WindowsImpl::ReadFile(const char * fileName)
3035
return b;
3136
}
3237

33-
Blob Snowing::PlatformImpls::WindowsImpl::ReadFile(const wchar_t * fileName)
38+
Blob Snowing::PlatformImpls::WindowsImpl::ReadFile(const wchar_t * fileName, size_t alignment)
3439
{
3540
char fileNameA[64];
3641
char *pFileNameA = fileNameA;
3742
while (*fileName)
3843
*pFileNameA++ = static_cast<char>(*fileName++);
3944
*pFileNameA = 0;
40-
return Snowing::PlatformImpls::WindowsImpl::ReadFile(fileNameA);
45+
return Snowing::PlatformImpls::WindowsImpl::ReadFile(fileNameA, alignment);
4146
}

WindowsImpl/AssetLoader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
namespace Snowing::PlatformImpls::WindowsImpl
55
{
66
[[nodiscard]]
7-
Blob ReadFile(const char* fileName);
7+
Blob ReadFile(const char* fileName,size_t alignment = 0);
88

99
[[nodiscard]]
10-
Blob ReadFile(const wchar_t* fileName);
10+
Blob ReadFile(const wchar_t* fileName,size_t alignment = 0);
1111
}

WindowsImpl/PlatformImpls.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ namespace Snowing
2323
#ifndef PUBLISH_MODE
2424
template <typename TChar>
2525
[[nodiscard]]
26-
inline Blob LoadAsset(const TChar* name)
26+
inline Blob LoadAsset(const TChar* name, size_t alignment = 0)
2727
{
28-
return Snowing::PlatformImpls::WindowsImpl::ReadFile(name);
28+
return Snowing::PlatformImpls::WindowsImpl::ReadFile(name, alignment);
2929
}
3030
#endif
3131
}

0 commit comments

Comments
 (0)