This commit is contained in:
Doyle T 2018-07-03 23:02:36 +10:00
commit 49577e2c27
4 changed files with 38 additions and 50 deletions

View File

@ -24,7 +24,7 @@ void DqnFixedString_Test()
{ {
DqnFixedString<512> str = DQN_SLICE("hello world"); DqnFixedString<512> str = DQN_SLICE("hello world");
DQN_ASSERT(str.Sprintf("hello %s", "sailor")); DQN_ASSERT(str.Sprintf("hello %s", "sailor"));
DQN_ASSERT(DqnStr_Cmp(str.str, "hello sailor") == 0); DQN_ASSERTM(DqnStr_Cmp(str.str, "hello sailor") == 0, "Result: %s", str.str);
Log(Status::Ok, "Sprintf"); Log(Status::Ok, "Sprintf");
} }

View File

@ -136,7 +136,6 @@ void LogHeader(char const *funcName)
} }
#include "DqnFixedString.cpp" #include "DqnFixedString.cpp"
#include "DqnOS.cpp"
void HandmadeMathVerifyMat4(DqnMat4 dqnMat, hmm_mat4 hmmMat) void HandmadeMathVerifyMat4(DqnMat4 dqnMat, hmm_mat4 hmmMat)
{ {
@ -1180,7 +1179,7 @@ void DqnArray_TestInternal(DqnMemAPI *const memAPI)
DqnArray<DqnV2> array(memAPI); DqnArray<DqnV2> array(memAPI);
if (1) if (1)
{ {
DQN_ASSERT(array.Reserve(1)); array.Reserve(1);
DQN_ASSERT(array.max >= 1); DQN_ASSERT(array.max >= 1);
DQN_ASSERT(array.count == 0); DQN_ASSERT(array.count == 0);
@ -1313,7 +1312,7 @@ void DqnArray_TestInternal(DqnMemAPI *const memAPI)
if (1) if (1)
{ {
DQN_ASSERT(array.Reserve(1)); array.Reserve(1);
DQN_ASSERT(array.max >= 1); DQN_ASSERT(array.max >= 1);
DQN_ASSERT(array.count == 0); DQN_ASSERT(array.count == 0);
Log(Status::Ok, "Empty array"); Log(Status::Ok, "Empty array");
@ -1327,7 +1326,7 @@ void DqnArray_TestInternal(DqnMemAPI *const memAPI)
DqnV2 c = DqnV2(5, 6); DqnV2 c = DqnV2(5, 6);
DqnV2 d = DqnV2(7, 8); DqnV2 d = DqnV2(7, 8);
DQN_ASSERT(array.Reserve(16)); array.Reserve(16);
DQN_ASSERT(array.max >= 16); DQN_ASSERT(array.max >= 16);
DQN_ASSERT(array.count == 0); DQN_ASSERT(array.count == 0);
@ -1376,7 +1375,7 @@ void DqnArray_TestInternal(DqnMemAPI *const memAPI)
DqnV2 c = DqnV2(5, 6); DqnV2 c = DqnV2(5, 6);
DqnV2 d = DqnV2(7, 8); DqnV2 d = DqnV2(7, 8);
DQN_ASSERT(array.Reserve(16)); array.Reserve(16);
DQN_ASSERT(array.Push(a)); DQN_ASSERT(array.Push(a));
DQN_ASSERT(array.Push(b)); DQN_ASSERT(array.Push(b));
@ -1605,7 +1604,7 @@ void DqnArray_Test()
if (1) if (1)
{ {
DqnArray<char> array = {}; DqnArray<char> array = {};
DQN_ASSERT(array.Reserve(1)); array.Reserve(1);
DqnArray_TestRealDataInternal(&array); DqnArray_TestRealDataInternal(&array);
} }
@ -1617,7 +1616,7 @@ void DqnArray_Test()
{ {
auto memGuard0 = stack.TempRegionGuard(); auto memGuard0 = stack.TempRegionGuard();
DqnArray<char> array(&stack.myHeadAPI); DqnArray<char> array(&stack.myHeadAPI);
DQN_ASSERT(array.Reserve(1)); array.Reserve(1);
DqnArray_TestRealDataInternal(&array); DqnArray_TestRealDataInternal(&array);
} }
@ -1626,7 +1625,7 @@ void DqnArray_Test()
{ {
auto memGuard0 = stack.TempRegionGuard(); auto memGuard0 = stack.TempRegionGuard();
DqnArray<char> array(&stack.myHeadAPI); DqnArray<char> array(&stack.myHeadAPI);
DQN_ASSERT(array.Reserve(128)); array.Reserve(128);
stack.Push(1024); stack.Push(1024);
DqnArray_TestRealDataInternal(&array); DqnArray_TestRealDataInternal(&array);
} }
@ -2898,7 +2897,7 @@ int main(void)
DqnFixedString_Test(); DqnFixedString_Test();
#ifdef DQN_PLATFORM_HEADER #ifdef DQN_PLATFORM_HEADER
DqnOS_Test(); // DqnOS_Test();
DqnFile_Test(); DqnFile_Test();
DqnTimer_Test(); DqnTimer_Test();
DqnJobQueue_Test(); DqnJobQueue_Test();

63
dqn.h
View File

@ -1147,9 +1147,9 @@ struct DqnArray
void Free () { if (data) { memAPI->Free(data); } *this = {}; } void Free () { if (data) { memAPI->Free(data); } *this = {}; }
T *Front () { DQN_ASSERT(count > 0); return data + 0; } T *Front () { DQN_ASSERT(count > 0); return data + 0; }
T *Back () { DQN_ASSERT(count > 0); return data + (count - 1); } T *Back () { DQN_ASSERT(count > 0); return data + (count - 1); }
bool Resize (isize newCount) { if (newCount > max) Reserve(GrowCapacity_(newCount)); count = newCount; } void Resize (isize newCount) { if (newCount > max) Reserve(GrowCapacity_(newCount)); count = newCount; }
bool Resize (isize newCount, T const *v) { if (newCount > max) Reserve(GrowCapacity_(newCount)); if (newCount > count) for (isize n = count; n < newCount; n++) data[n] = *v; count = newCount; } void Resize (isize newCount, T const *v) { if (newCount > max) Reserve(GrowCapacity_(newCount)); if (newCount > count) for (isize n = count; n < newCount; n++) data[n] = *v; count = newCount; }
bool Reserve (isize newMax); void Reserve (isize newMax);
T *Push (T const &v) { return Insert(count, &v, 1); } T *Push (T const &v) { return Insert(count, &v, 1); }
T *Push (T const *v, isize numItems = 1) { return Insert(count, v, numItems); } T *Push (T const *v, isize numItems = 1) { return Insert(count, v, numItems); }
void Pop () { if (count > 0) count--; } void Pop () { if (count > 0) count--; }
@ -1173,10 +1173,7 @@ template<typename T> T *DqnArray<T>::Insert(isize index, T const *v, isize numIt
index = DQN_MIN(DQN_MAX(index, 0), count); index = DQN_MIN(DQN_MAX(index, 0), count);
isize const newCount = count + numItems; isize const newCount = count + numItems;
if (newCount >= max && !Reserve(GrowCapacity_(newCount))) if (newCount >= max) Reserve(GrowCapacity_(newCount));
{
return nullptr;
}
T *src = data + index; T *src = data + index;
T *dest = src + numItems; T *dest = src + numItems;
@ -1198,24 +1195,27 @@ template <typename T> void DqnArray<T>::EraseStable(isize index)
count--; count--;
} }
template <typename T> bool DqnArray<T>::Reserve(isize newMax) template <typename T> void DqnArray<T>::Reserve(isize newMax)
{ {
if (newMax <= max) return true; // TODO(doyle): Super hard asserts
if (newMax <= max) return;
if (data) if (data)
{ {
T *newData = (T *)memAPI->Realloc(data, max * sizeof(T), newMax * sizeof(T)); T *newData = (T *)memAPI->Realloc(data, max * sizeof(T), newMax * sizeof(T));
DQN_ASSERT(newData);
if (newData) if (newData)
{ {
data = newData; data = newData;
max = newMax; max = newMax;
} }
return newData;
} }
else
{
data = (T *)memAPI->Alloc(newMax * sizeof(T)); data = (T *)memAPI->Alloc(newMax * sizeof(T));
max = newMax; max = newMax;
return data; DQN_ASSERT(data);
}
} }
#if 0 #if 0
@ -3622,7 +3622,7 @@ void DqnAllocatorMetadata::Init(bool boundsGuard)
this->boundsGuardSize = sizeof(HEAD_GUARD_VALUE); this->boundsGuardSize = sizeof(HEAD_GUARD_VALUE);
LOCAL_PERSIST DqnMemAPI heap = DqnMemAPI::HeapAllocator(); LOCAL_PERSIST DqnMemAPI heap = DqnMemAPI::HeapAllocator();
this->allocations.memAPI = &heap; this->allocations.memAPI = &heap;
DQN_ASSERT(this->allocations.Reserve(128)); this->allocations.Reserve(128);
} }
else else
{ {
@ -8057,13 +8057,8 @@ DQN_FILE__LIST_DIR(DqnFile__PlatformListDir)
#endif // DQN__WIN32_PLATFORM #endif // DQN__WIN32_PLATFORM
#ifdef DQN__UNIX_PLATFORM #ifdef DQN__UNIX_PLATFORM
FILE_SCOPE bool DqnFile__UnixGetFileSizeWithStat(FILE *file, char const *path, usize *size) FILE_SCOPE bool DqnFile__UnixGetFileSize(char const *path, usize *size)
{ {
if (!file)
{
return false;
}
struct stat fileStat = {}; struct stat fileStat = {};
if (stat(path, &fileStat)) if (stat(path, &fileStat))
{ {
@ -8077,6 +8072,12 @@ FILE_SCOPE bool DqnFile__UnixGetFileSizeWithStat(FILE *file, char const *path, u
// But there can also be zero-byte files, we can't be sure. So manual check by counting bytes // But there can also be zero-byte files, we can't be sure. So manual check by counting bytes
if (*size == 0) if (*size == 0)
{ {
FILE *file = fopen(path, "r");
if (!file)
{
return false;
}
u64 fileSizeInBytes = 0; u64 fileSizeInBytes = 0;
char c = fgetc(file); char c = fgetc(file);
@ -8142,16 +8143,15 @@ DqnFile__UnixOpen(char const *path, DqnFile *file, u32 flags, DqnFile::Action ac
// TODO(doyle): Use open syscall // TODO(doyle): Use open syscall
// TODO(doyle): Query errno // TODO(doyle): Query errno
file->handle = reinterpret_cast<void *>(fopen(path, mode)); if (!DqnFile__UnixGetFileSize(path, &file->size))
if (!DqnFile__UnixGetFileSize(reinterpret_cast<FILE *>(file->handle), path, &file->size))
{ {
// TODO(doyle): Logging // TODO(doyle): Logging
fclose(handle);
return false; return false;
} }
file->handle = reinterpret_cast<void *>(fopen(path, mode));
file->flags = flags; file->flags = flags;
return true; return file->handle;
} }
DQN_FILE__LIST_DIR(DqnFile__PlatformListDir) DQN_FILE__LIST_DIR(DqnFile__PlatformListDir)
@ -8210,7 +8210,7 @@ DQN_FILE__LIST_DIR(DqnFile__PlatformListDir)
*numFiles = currNumFiles; *numFiles = currNumFiles;
while (dirFile) while (dirFile)
{ {
DqnStr_Copy(list[listIndex++], dirFile->d_name, DQN_ARRAY_COUNT(dirFile->d_name)); DqnMem_Copy(list[listIndex++], dirFile->d_name, DQN_ARRAY_COUNT(dirFile->d_name));
dirFile = readdir(dirHandle); dirFile = readdir(dirHandle);
} }
closedir(dirHandle); closedir(dirHandle);
@ -8470,18 +8470,7 @@ bool DqnFile::GetFileSize(char const *path, usize *size)
#elif defined(DQN__UNIX_PLATFORM) #elif defined(DQN__UNIX_PLATFORM)
// TODO(doyle): Error logging // TODO(doyle): Error logging
if (!DqnFile__UnixGetFileSizeWithStat(path, size)) return false; if (!DqnFile__UnixGetFileSize(path, size)) return false;
// NOTE: 0 size can occur in some instances where files are generated on demand,
// i.e. /proc/cpuinfo
if (*size == 0)
{
// If stat fails, then do a manual byte count
FILE *handle = fopen(path, "r");
*size = DqnFile__UnixGetFileSizeManual(handle, false);
fclose(handle);
}
return true; return true;
#endif #endif
} }

View File

@ -1,3 +1,3 @@
all: dqn_unit_test.cpp all: DqnUnitTest.cpp
mkdir -p bin mkdir -p bin
g++ -std=c++14 -o bin/dqn_unit_test dqn_unit_test.cpp -lm -Wall -pthread -ggdb g++ -std=c++14 -o bin/DqnUnitTest DqnUnitTest.cpp -lm -Wall -pthread -ggdb