зеркало из https://github.com/microsoft/DirectXTK.git
Code review for shared helpers
This commit is contained in:
Родитель
556caba51f
Коммит
a6e52b155a
|
@ -11,6 +11,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <malloc.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
|
||||
|
||||
|
@ -29,9 +31,10 @@ namespace DirectX
|
|||
// Allocate aligned memory.
|
||||
static void* operator new (size_t size)
|
||||
{
|
||||
const size_t alignment = __alignof(TDerived);
|
||||
const size_t alignment = alignof(TDerived);
|
||||
|
||||
static_assert(alignment > 8, "AlignedNew is only useful for types with > 8 byte alignment. Did you forget a __declspec(align) on TDerived?");
|
||||
static_assert(((alignment - 1) & alignment) == 0, "AlignedNew only works with power of two alignment");
|
||||
|
||||
void* ptr = _aligned_malloc(size, alignment);
|
||||
|
||||
|
|
|
@ -44,8 +44,16 @@ BinaryReader::BinaryReader(_In_reads_bytes_(dataSize) uint8_t const* dataBlob, s
|
|||
|
||||
|
||||
// Reads from the filesystem into memory.
|
||||
HRESULT BinaryReader::ReadEntireFile(_In_z_ wchar_t const* fileName, _Inout_ std::unique_ptr<uint8_t[]>& data, _Out_ size_t* dataSize)
|
||||
HRESULT BinaryReader::ReadEntireFile(
|
||||
_In_z_ wchar_t const* fileName,
|
||||
_Inout_ std::unique_ptr<uint8_t[]>& data,
|
||||
_Out_ size_t* dataSize)
|
||||
{
|
||||
if (!fileName || !dataSize)
|
||||
return E_INVALIDARG;
|
||||
|
||||
*dataSize = 0;
|
||||
|
||||
// Open the file.
|
||||
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
|
||||
ScopedHandle hFile(safe_handle(CreateFile2(fileName, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr)));
|
||||
|
|
|
@ -22,14 +22,20 @@ namespace DirectX
|
|||
T* result = comPtr.Get();
|
||||
|
||||
// Double-checked lock pattern.
|
||||
#ifdef _MSC_VER
|
||||
MemoryBarrier();
|
||||
#elif defined(__GNUC__)
|
||||
__sync_synchronize();
|
||||
#else
|
||||
#error Unknown memory barrier syntax
|
||||
#endif
|
||||
|
||||
if (!result)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
result = comPtr.Get();
|
||||
|
||||
|
||||
if (!result)
|
||||
{
|
||||
// Create the new object.
|
||||
|
@ -37,7 +43,11 @@ namespace DirectX
|
|||
createFunc(&result)
|
||||
);
|
||||
|
||||
#ifdef _MSC_VER
|
||||
MemoryBarrier();
|
||||
#elif defined(__GNUC__)
|
||||
__sync_synchronize();
|
||||
#endif
|
||||
|
||||
comPtr.Attach(result);
|
||||
}
|
||||
|
|
|
@ -296,6 +296,8 @@ namespace DirectX
|
|||
return E_POINTER;
|
||||
}
|
||||
|
||||
*bitSize = 0;
|
||||
|
||||
if (ddsDataSize > UINT32_MAX)
|
||||
{
|
||||
return E_FAIL;
|
||||
|
@ -328,7 +330,7 @@ namespace DirectX
|
|||
(MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC))
|
||||
{
|
||||
// Must be long enough for both headers and magic value
|
||||
if (ddsDataSize < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
|
||||
if (ddsDataSize < (sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10)))
|
||||
{
|
||||
return E_FAIL;
|
||||
}
|
||||
|
@ -360,6 +362,8 @@ namespace DirectX
|
|||
return E_POINTER;
|
||||
}
|
||||
|
||||
*bitSize = 0;
|
||||
|
||||
// open the file
|
||||
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
|
||||
ScopedHandle hFile(safe_handle(CreateFile2(fileName,
|
||||
|
@ -409,19 +413,21 @@ namespace DirectX
|
|||
}
|
||||
|
||||
// read the data in
|
||||
DWORD BytesRead = 0;
|
||||
DWORD bytesRead = 0;
|
||||
if (!ReadFile(hFile.get(),
|
||||
ddsData.get(),
|
||||
fileInfo.EndOfFile.LowPart,
|
||||
&BytesRead,
|
||||
&bytesRead,
|
||||
nullptr
|
||||
))
|
||||
{
|
||||
ddsData.reset();
|
||||
return HRESULT_FROM_WIN32(GetLastError());
|
||||
}
|
||||
|
||||
if (BytesRead < fileInfo.EndOfFile.LowPart)
|
||||
if (bytesRead < fileInfo.EndOfFile.LowPart)
|
||||
{
|
||||
ddsData.reset();
|
||||
return E_FAIL;
|
||||
}
|
||||
|
||||
|
@ -429,6 +435,7 @@ namespace DirectX
|
|||
auto dwMagicNumber = *reinterpret_cast<const uint32_t*>(ddsData.get());
|
||||
if (dwMagicNumber != DDS_MAGIC)
|
||||
{
|
||||
ddsData.reset();
|
||||
return E_FAIL;
|
||||
}
|
||||
|
||||
|
@ -438,6 +445,7 @@ namespace DirectX
|
|||
if (hdr->size != sizeof(DDS_HEADER) ||
|
||||
hdr->ddspf.size != sizeof(DDS_PIXELFORMAT))
|
||||
{
|
||||
ddsData.reset();
|
||||
return E_FAIL;
|
||||
}
|
||||
|
||||
|
@ -447,8 +455,9 @@ namespace DirectX
|
|||
(MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC))
|
||||
{
|
||||
// Must be long enough for both headers and magic value
|
||||
if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
|
||||
if (fileInfo.EndOfFile.LowPart < (sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10)))
|
||||
{
|
||||
ddsData.reset();
|
||||
return E_FAIL;
|
||||
}
|
||||
|
||||
|
|
|
@ -70,7 +70,6 @@ namespace DirectX
|
|||
#endif
|
||||
}
|
||||
|
||||
|
||||
// Helper smart-pointers
|
||||
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN10) || (defined(_XBOX_ONE) && defined(_TITLE)) || !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
|
||||
struct virtual_deleter { void operator()(void* p) noexcept { if (p) VirtualFree(p, 0, MEM_RELEASE); } };
|
||||
|
|
Загрузка…
Ссылка в новой задаче