Make FS use new allocator API

This commit is contained in:
elasota
2021-04-28 23:15:46 -04:00
parent a2d374f650
commit 7442b92dd3
8 changed files with 522 additions and 69 deletions

View File

@@ -103,6 +103,8 @@
<ClInclude Include="..\GpCommon\GpDisplayDriverTickStatus.h" />
<ClInclude Include="..\GpCommon\GpFileCreationDisposition.h" />
<ClInclude Include="..\GpCommon\GpInputDriverProperties.h" />
<ClInclude Include="..\GpCommon\GpString.h" />
<ClInclude Include="..\GpCommon\GpVector.h" />
<ClInclude Include="..\GpCommon\IGpCursor.h" />
<ClInclude Include="..\GpCommon\IGpAudioChannelCallbacks.h" />
<ClInclude Include="..\GpCommon\IGpDisplayDriverSurface.h" />

View File

@@ -93,6 +93,12 @@
<ClInclude Include="GpBWCursor_Win32.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\GpCommon\GpVector.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\GpCommon\GpString.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="ConvertedResources\Large128.ico">

View File

@@ -7,7 +7,7 @@
#include "IGpAllocator.h"
#include "IGpDirectoryCursor.h"
#include <string>
#include <Shlwapi.h>
#include <ShlObj.h>
#include <commdlg.h>
@@ -105,48 +105,69 @@ GpDirectoryCursor_Win32::~GpDirectoryCursor_Win32()
FindClose(m_handle);
}
GpFileSystem_Win32::GpFileSystem_Win32()
: m_alloc(GpAllocator_C::GetInstance())
GpFileSystem_Win32::GpFileSystem_Win32(IGpAllocator *alloc)
: m_alloc(alloc)
, m_prefsDir(alloc)
, m_scoresDir(alloc)
, m_packagedDir(alloc)
, m_housesDir(alloc)
, m_logsDir(alloc)
, m_userHousesDir(alloc)
, m_userSavesDir(alloc)
, m_resourcesDir(alloc)
{
// GP TODO: This shouldn't be static init since it allocates memory
m_executablePath[0] = 0;
}
void GpFileSystem_Win32::Destroy()
{
IGpAllocator *alloc = m_alloc;
this->~GpFileSystem_Win32();
alloc->Release(this);
}
bool GpFileSystem_Win32::Init()
{
PWSTR docsPath;
if (!FAILED(SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &docsPath)))
{
try
{
m_prefsDir = docsPath;
}
catch(...)
if (FAILED(SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &docsPath)))
return false;
if (!m_prefsDir.Set(docsPath))
{
CoTaskMemFree(docsPath);
throw;
return false;
}
m_prefsDir.append(L"\\" GP_APPLICATION_NAME_W);
CoTaskMemFree(docsPath);
m_userHousesDir = m_prefsDir + L"\\Houses";
m_userSavesDir = m_prefsDir + L"\\SavedGames";
m_scoresDir = m_prefsDir + L"\\Scores";
m_logsDir = m_prefsDir + L"\\Logs";
m_fontCacheDir = m_prefsDir + L"\\FontCache";
if (!m_prefsDir.Append(L"\\" GP_APPLICATION_NAME_W))
return false;
CreateDirectoryW(m_prefsDir.c_str(), nullptr);
CreateDirectoryW(m_scoresDir.c_str(), nullptr);
CreateDirectoryW(m_userHousesDir.c_str(), nullptr);
CreateDirectoryW(m_userSavesDir.c_str(), nullptr);
CreateDirectoryW(m_logsDir.c_str(), nullptr);
CreateDirectoryW(m_fontCacheDir.c_str(), nullptr);
if (!m_userHousesDir.Set(m_prefsDir) || !m_userHousesDir.Append(L"\\Houses"))
return false;
m_prefsDir.append(L"\\");
m_scoresDir.append(L"\\");
m_userHousesDir.append(L"\\");
m_userSavesDir.append(L"\\");
m_logsDir.append(L"\\");
m_fontCacheDir.append(L"\\");
m_resourcesDir.append(L"\\");
}
if (!m_userSavesDir.Set(m_prefsDir) || !m_userSavesDir.Append(L"\\SavedGames"))
return false;
if (!m_scoresDir.Set(m_prefsDir) || !m_scoresDir.Append(L"\\Scores"))
return false;
if (!m_logsDir.Set(m_prefsDir) || !m_logsDir.Append(L"\\Logs"))
return false;
CreateDirectoryW(m_prefsDir.Buffer(), nullptr);
CreateDirectoryW(m_scoresDir.Buffer(), nullptr);
CreateDirectoryW(m_userHousesDir.Buffer(), nullptr);
CreateDirectoryW(m_userSavesDir.Buffer(), nullptr);
CreateDirectoryW(m_logsDir.Buffer(), nullptr);
if (!m_prefsDir.Append(L"\\") ||
!m_scoresDir.Append(L"\\") ||
!m_userHousesDir.Append(L"\\") ||
!m_userSavesDir.Append(L"\\") ||
!m_logsDir.Append(L"\\") ||
!m_resourcesDir.Append(L"\\"))
return false;
DWORD modulePathSize = GetModuleFileNameW(nullptr, m_executablePath, MAX_PATH);
if (modulePathSize == MAX_PATH || modulePathSize == 0)
@@ -182,12 +203,19 @@ GpFileSystem_Win32::GpFileSystem_Win32()
currentPathLength--;
}
if (currentPathLength > 0)
{
m_packagedDir = std::wstring(m_executablePath) + L"Packaged\\";
m_housesDir = std::wstring(m_executablePath) + L"Packaged\\Houses\\";
m_resourcesDir = std::wstring(m_executablePath) + L"Resources\\";
}
if (currentPathLength == 0)
return false;
if (!m_packagedDir.Set(m_executablePath) || !m_packagedDir.Append(L"Packaged\\"))
return false;
if (!m_housesDir.Set(m_executablePath) || !m_housesDir.Append(L"Packaged\\Houses\\"))
return false;
if (!m_resourcesDir.Set(m_executablePath) || !m_resourcesDir.Append(L"Resources\\"))
return false;
return true;
}
bool GpFileSystem_Win32::FileExists(PortabilityLayer::VirtualDirectory_t virtualDirectory, const char *path)
@@ -428,9 +456,27 @@ const wchar_t *GpFileSystem_Win32::GetBasePath() const
return m_executablePath;
}
GpFileSystem_Win32 *GpFileSystem_Win32::CreateInstance(IGpAllocator *alloc)
{
void *storage = alloc->Alloc(sizeof(GpFileSystem_Win32));
if (!storage)
return nullptr;
GpFileSystem_Win32 *fs = new (storage) GpFileSystem_Win32(alloc);
if (!fs->Init())
{
fs->Destroy();
return nullptr;
}
ms_instance = fs;
return fs;
}
GpFileSystem_Win32 *GpFileSystem_Win32::GetInstance()
{
return &ms_instance;
return ms_instance;
}
bool GpFileSystem_Win32::ResolvePath(PortabilityLayer::VirtualDirectory_t virtualDirectory, char const* const* paths, size_t numPaths, wchar_t *outPath)
@@ -440,28 +486,28 @@ bool GpFileSystem_Win32::ResolvePath(PortabilityLayer::VirtualDirectory_t virtua
switch (virtualDirectory)
{
case PortabilityLayer::VirtualDirectories::kApplicationData:
baseDir = m_packagedDir.c_str();
baseDir = m_packagedDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kGameData:
baseDir = m_housesDir.c_str();
baseDir = m_housesDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kUserData:
baseDir = m_userHousesDir.c_str();
baseDir = m_userHousesDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kUserSaves:
baseDir = m_userSavesDir.c_str();
baseDir = m_userSavesDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kPrefs:
baseDir = m_prefsDir.c_str();
baseDir = m_prefsDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kFonts:
baseDir = m_resourcesDir.c_str();
baseDir = m_resourcesDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kHighScores:
baseDir = m_scoresDir.c_str();
baseDir = m_scoresDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kLogs:
baseDir = m_logsDir.c_str();
baseDir = m_logsDir.Buffer();
break;
default:
return false;
@@ -507,4 +553,4 @@ bool GpFileSystem_Win32::ResolvePath(PortabilityLayer::VirtualDirectory_t virtua
return true;
}
GpFileSystem_Win32 GpFileSystem_Win32::ms_instance;
GpFileSystem_Win32 *GpFileSystem_Win32::ms_instance;

View File

@@ -4,13 +4,14 @@
#include "GpCoreDefs.h"
#include "GpWindows.h"
#include <string>
#include "GpString.h"
class GpFileSystem_Win32 final : public IGpFileSystem
{
public:
GpFileSystem_Win32();
explicit GpFileSystem_Win32(IGpAllocator *alloc);
void Destroy();
bool FileExists(PortabilityLayer::VirtualDirectory_t virtualDirectory, const char *path) override;
bool FileLocked(PortabilityLayer::VirtualDirectory_t virtualDirectory, const char *path, bool &exists) override;
@@ -25,23 +26,25 @@ public:
const wchar_t *GetBasePath() const;
static GpFileSystem_Win32 *CreateInstance(IGpAllocator *alloc);
static GpFileSystem_Win32 *GetInstance();
private:
bool Init();
bool ResolvePath(PortabilityLayer::VirtualDirectory_t virtualDirectory, char const* const* paths, size_t numPaths, wchar_t *outPath);
std::wstring m_prefsDir;
std::wstring m_scoresDir;
std::wstring m_packagedDir;
std::wstring m_housesDir;
std::wstring m_logsDir;
std::wstring m_userHousesDir;
std::wstring m_userSavesDir;
std::wstring m_resourcesDir;
std::wstring m_fontCacheDir;
GpWString m_prefsDir;
GpWString m_scoresDir;
GpWString m_packagedDir;
GpWString m_housesDir;
GpWString m_logsDir;
GpWString m_userHousesDir;
GpWString m_userSavesDir;
GpWString m_resourcesDir;
wchar_t m_executablePath[MAX_PATH];
IGpAllocator *m_alloc;
static GpFileSystem_Win32 ms_instance;
static GpFileSystem_Win32 *ms_instance;
};

View File

@@ -404,6 +404,13 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
int nArgs;
LPWSTR *cmdLineArgs = CommandLineToArgvW(cmdLine, &nArgs);
IGpAllocator *alloc = GpAllocator_C::GetInstance();
// Init file system first since logging may depend on it
GpFileSystem_Win32 *fs = GpFileSystem_Win32::CreateInstance(alloc);
if (!fs)
return -1;
for (int i = 1; i < nArgs; i++)
{
if (!wcscmp(cmdLineArgs[i], L"-diagnostics"))
@@ -411,7 +418,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
}
IGpLogDriver *logger = GpLogDriver_Win32::GetInstance();
IGpAllocator *alloc = GpAllocator_C::GetInstance();
IGpSystemServices *sysServices = GpSystemServices_Win32::GetInstance();
GpDriverCollection *drivers = GpAppInterface_Get()->PL_GetDriverCollection();
@@ -469,5 +475,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
LocalFree(cmdLineArgs);
fs->Destroy();
return returnCode;
}

View File

@@ -37,6 +37,12 @@ IGpInputDriver *GpDriver_CreateInputDriver_SDL2_Gamepad(const GpInputDriverPrope
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
IGpAllocator *alloc = GpAllocator_C::GetInstance();
GpFileSystem_Win32 *fs = GpFileSystem_Win32::CreateInstance(alloc);
if (!fs)
return -1;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) < 0)
return -1;
@@ -57,7 +63,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
IGpLogDriver *logger = GpLogDriver_Win32::GetInstance();
GpDriverCollection *drivers = GpAppInterface_Get()->PL_GetDriverCollection();
drivers->SetDriver<GpDriverIDs::kFileSystem>(GpFileSystem_Win32::GetInstance());
drivers->SetDriver<GpDriverIDs::kFileSystem>(fs);
drivers->SetDriver<GpDriverIDs::kSystemServices>(GpSystemServices_Win32::GetInstance());
drivers->SetDriver<GpDriverIDs::kLog>(GpLogDriver_Win32::GetInstance());
drivers->SetDriver<GpDriverIDs::kAlloc>(GpAllocator_C::GetInstance());
@@ -88,7 +94,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
g_gpGlobalConfig.m_osGlobals = &g_gpWindowsGlobals;
g_gpGlobalConfig.m_logger = logger;
g_gpGlobalConfig.m_systemServices = GpSystemServices_Win32::GetInstance();
g_gpGlobalConfig.m_allocator = GpAllocator_C::GetInstance();
g_gpGlobalConfig.m_allocator = alloc;
GpDisplayDriverFactory::RegisterDisplayDriverFactory(EGpDisplayDriverType_SDL_GL2, GpDriver_CreateDisplayDriver_SDL_GL2);
GpAudioDriverFactory::RegisterAudioDriverFactory(EGpAudioDriverType_SDL2, GpDriver_CreateAudioDriver_SDL);
@@ -105,5 +111,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
LocalFree(cmdLineArgs);
fs->Destroy();
return returnCode;
}

150
GpCommon/GpString.h Normal file
View File

@@ -0,0 +1,150 @@
#pragma once
#include "GpVector.h"
#include <stdint.h>
#include <stddef.h>
template<class TChar>
class GpString
{
public:
explicit GpString(IGpAllocator *alloc);
GpString(GpString<TChar> &&other);
bool Set(const TChar *str);
bool Set(const TChar *str, size_t len);
bool Set(const GpString<TChar> &other);
bool Append(const TChar *str);
bool Append(const TChar *str, size_t len);
bool Append(const GpString<TChar> &other);
TChar *Buffer();
const TChar *Buffer() const ;
private:
static const size_t kStaticSize = 32;
GpVector<TChar, kStaticSize> m_chars;
size_t m_length;
};
typedef GpString<char> GpCString;
typedef GpString<wchar_t> GpWString;
#include <new>
#include <string.h>
template<class TChar>
GpString<TChar>::GpString(IGpAllocator *alloc)
: m_chars(alloc)
, m_length(0)
{
(void)m_chars.Resize(1);
m_chars[0] = static_cast<TChar>(0);
}
template<class TChar>
GpString<TChar>::GpString(GpString<TChar> &&other)
: m_chars(static_cast<GpVector<TChar, kStaticSize>&&>(other.m_chars))
, m_length(other.m_length)
{
other.m_length = 0;
}
template<class TChar>
bool GpString<TChar>::Set(const TChar *str)
{
size_t len = 0;
while (str[len] != static_cast<TChar>(0))
len++;
return this->Set(str, len);
}
template<class TChar>
bool GpString<TChar>::Set(const TChar *str, size_t len)
{
if (!m_chars.ResizeNoConstruct(len + 1))
return false;
TChar *chars = m_chars.Buffer();
memcpy(chars, str, sizeof(TChar) * len);
chars[len] = static_cast<TChar>(0);
m_length = len;
return true;
}
template<class TChar>
bool GpString<TChar>::Set(const GpString<TChar> &other)
{
if (&other == this)
return true;
return this->Set(other.Buffer(), other.m_length);
}
template<class TChar>
bool GpString<TChar>::Append(const TChar *str)
{
size_t len = 0;
while (str[len] != static_cast<TChar>(0))
len++;
return this->Append(str, len);
}
template<class TChar>
bool GpString<TChar>::Append(const TChar *str, size_t len)
{
// This is a special path in case we're appending the string to itself and the resize relocates the buffer
if (len == 0)
return true;
assert(str != this->Buffer());
if (!m_chars.ResizeNoConstruct(m_length + len + 1))
return false;
memcpy(m_chars.Buffer() + m_length, str, sizeof(TChar) * len);
m_chars[m_length + len] = static_cast<TChar>(0);
m_length += len;
return true;
}
template<class TChar>
bool GpString<TChar>::Append(const GpString<TChar> &other)
{
// This is a special path in case we're appending the string to itself and the resize relocates the buffer
if (other.m_length == 0)
return true;
if (!m_chars.ResizeNoConstruct(m_length + other.m_length))
return false;
memcpy(m_chars.Buffer() + m_length, other.Buffer(), sizeof(TChar) * other.m_length);
m_chars[m_length + other.m_length] = static_cast<TChar>(0);
m_length += other.m_length;
return true;
}
template<class TChar>
TChar *GpString<TChar>::Buffer()
{
return m_chars.Buffer();
}
template<class TChar>
const TChar *GpString<TChar>::Buffer() const
{
return m_chars.Buffer();
}

230
GpCommon/GpVector.h Normal file
View File

@@ -0,0 +1,230 @@
#pragma once
#include "CoreDefs.h"
#include <stdint.h>
struct IGpAllocator;
template<class T, size_t TStaticSize>
class GpVectorStaticData
{
protected:
GP_ALIGNED(GP_SYSTEM_MEMORY_ALIGNMENT) uint8_t m_staticElementData[sizeof(T) * TStaticSize];
T *GetStaticElements();
const T *GetStaticElements() const;
};
template<class T>
class GpVectorStaticData<T, 0>
{
protected:
T *GetStaticElements();
const T *GetStaticElements() const;
};
template<class T, size_t TStaticSize = 0>
class GpVector : public GpVectorStaticData<T, TStaticSize>
{
public:
explicit GpVector(IGpAllocator *alloc);
GpVector(GpVector<T, TStaticSize> &&other);
~GpVector();
T &operator[](size_t index);
const T &operator[](size_t index) const;
bool Resize(size_t newSize);
bool ResizeNoConstruct(size_t newSize);
T *Buffer();
const T *Buffer() const;
const size_t Count() const;
private:
GpVector(const GpVector<T, TStaticSize> &other) GP_DELETED;
static const size_t kStaticSize = TStaticSize;
T *m_elements;
size_t m_capacity;
size_t m_count;
IGpAllocator *m_alloc;
};
#include <new>
#include <cassert>
#include "IGpAllocator.h"
template<class T, size_t TStaticSize>
T *GpVectorStaticData<T, TStaticSize>::GetStaticElements()
{
return reinterpret_cast<T*>(this->m_staticElementData);
}
template<class T, size_t TStaticSize>
const T *GpVectorStaticData<T, TStaticSize>::GetStaticElements() const
{
return reinterpret_cast<const T*>(this->m_staticElementData);
}
template<class T>
T *GpVectorStaticData<T, 0>::GetStaticElements()
{
return nullptr;
}
template<class T>
const T *GpVectorStaticData<T, 0>::GetStaticElements() const
{
return nullptr;
}
///////////////////////////////////////////////////////////////////////////////
// GpVector
template<class T, size_t TStaticSize>
GpVector<T, TStaticSize>::GpVector(IGpAllocator *alloc)
: m_elements(this->GetStaticElements())
, m_capacity(TStaticSize)
, m_count(0)
, m_alloc(alloc)
{
}
template<class T, size_t TStaticSize>
GpVector<T, TStaticSize>::GpVector(GpVector<T, TStaticSize> &&other)
: m_elements(other.m_elements)
, m_capacity(other.m_capacity)
, m_count(other.m_count)
, m_alloc(other.m_alloc)
{
if (m_capacity <= TStaticSize)
{
const size_t count = m_count;
T *elements = this->GetStaticElements();
const T *srcElements = other.m_elements;
m_elements = elements;
for (size_t i = 0; i < count; i++)
new (m_elements + i) T(static_cast<T&&>(other.m_elements));
}
other.m_count = 0;
other.m_capacity = 0;
other.m_elements = nullptr;
}
template<class T, size_t TStaticSize>
GpVector<T, TStaticSize>::~GpVector()
{
T *elements = m_elements;
size_t remaining = m_count;
while (remaining > 0)
{
remaining--;
elements[remaining].~T();
}
if (m_capacity > TStaticSize)
{
m_alloc->Release(m_elements);
}
}
template<class T, size_t TStaticSize>
T &GpVector<T, TStaticSize>::operator[](size_t index)
{
assert(index <= m_count);
return m_elements[index];
}
template<class T, size_t TStaticSize>
const T &GpVector<T, TStaticSize>::operator[](size_t index) const
{
assert(index <= m_count);
return m_elements[index];
}
template<class T, size_t TStaticSize>
bool GpVector<T, TStaticSize>::Resize(size_t newSize)
{
const size_t oldCount = m_count;
if (!ResizeNoConstruct(newSize))
return false;
for (size_t i = oldCount; i < newSize; i++)
new (m_elements + i) T();
return true;
}
template<class T, size_t TStaticSize>
bool GpVector<T, TStaticSize>::ResizeNoConstruct(size_t newSize)
{
T *elements = m_elements;
if (newSize <= m_count)
{
size_t count = m_count;
while (count > newSize)
{
count--;
m_elements[count].~T();
}
m_count = count;
return true;
}
if (newSize <= m_capacity)
{
m_count = newSize;
return true;
}
size_t newCapacity = newSize;
assert(newCapacity > kStaticSize);
T *newElements = static_cast<T*>(m_alloc->Alloc(newCapacity * sizeof(T)));
if (!newElements)
return false;
const size_t oldCount = m_count;
for (size_t i = 0; i < oldCount; i++)
new (newElements + i) T(static_cast<T&&>(elements[i]));
for (size_t i = 0; i < oldCount; i++)
elements[oldCount - 1 - i].~T();
if (m_capacity > kStaticSize)
m_alloc->Release(m_elements);
m_elements = newElements;
m_capacity = newCapacity;
m_count = newSize;
return true;
}
template<class T, size_t TStaticSize>
const size_t GpVector<T, TStaticSize>::Count() const
{
return m_count;
}
template<class T, size_t TStaticSize>
T *GpVector<T, TStaticSize>::Buffer()
{
return m_elements;
}
template<class T, size_t TStaticSize>
const T *GpVector<T, TStaticSize>::Buffer() const
{
return m_elements;
}