Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
UseTab: Never
IndentWidth: 2
Language: Cpp
DerivePointerAlignment: false
PointerAlignment: Right
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
IndentCaseLabels: true
AttributeMacros: [UD_PRINTF_FORMAT_FUNC(1)]
ColumnLimit: 0
IndentPPDirectives: AfterHash
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AlignConsecutiveMacros: true
Cpp11BracedListStyle: false
2 changes: 2 additions & 0 deletions 3rdParty/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DisableFormat: true
SortIncludes: false
26 changes: 14 additions & 12 deletions Include/udAsyncJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
// with the caller able to either poll or wait for completion at a later time
//

#include "udPlatformUtil.h"
#include "udResult.h"
#include "udThread.h"
#include "udPlatformUtil.h"

// A simple interface to allow function calls to be easily made optionally background calls with one additional parameter

Expand All @@ -20,8 +20,8 @@ struct udAsyncJob;
// A helper for pausing ASync jobs, ensure all fields are zero to be initialised into non-paused state
struct udAsyncPause
{
udSemaphore *volatile pSema; // Created locked by initiator of pause, released and destroyed by handler of pause when incremented to release pause
udResult errorCausingPause; // If an error condition (eg disk full) initiated a pause, the error code is here
udSemaphore *volatile pSema; // Created locked by initiator of pause, released and destroyed by handler of pause when incremented to release pause
udResult errorCausingPause; // If an error condition (eg disk full) initiated a pause, the error code is here
enum Context
{
EC_None,
Expand Down Expand Up @@ -69,14 +69,16 @@ const char *udAsyncPause_GetErrorContextString(udAsyncPause::Context errorContex
// Some helper macros for boiler-plate code generation, each macro corresponds to number of parameters before pAsyncJob
// For these macros to work, udAsyncJob *pAsyncJob must be the LAST PARAMETER of the function

#define UDASYNC_CALL(funcCall) if (pAsyncJob) { \
udThreadStart udajStartFunc = [=](void *) -> unsigned int \
{ \
udAsyncJob_SetResult(pAsyncJob, funcCall); \
return 0; \
}; \
udAsyncJob_SetPending(pAsyncJob); \
return udThread_Create(nullptr, udajStartFunc, nullptr, udTCF_None, __FUNC_NAME__); \
}
#define UDASYNC_CALL(funcCall) \
if (pAsyncJob) \
{ \
udThreadStart udajStartFunc = [=](void *) -> unsigned int \
{ \
udAsyncJob_SetResult(pAsyncJob, funcCall); \
return 0; \
}; \
udAsyncJob_SetPending(pAsyncJob); \
return udThread_Create(nullptr, udajStartFunc, nullptr, udTCF_None, __FUNC_NAME__); \
}

#endif // UDASYNCJOB_H
74 changes: 57 additions & 17 deletions Include/udCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,99 @@
//

#include "udNew.h"
#include <utility>
#include <type_traits>
#include <utility>

template<typename T>
template <typename T>
struct udAbstractCallback;

template<typename Result, typename ...Args>
template <typename Result, typename... Args>
struct udAbstractCallback<Result(Args...)>
{
virtual Result call(Args... args) const = 0;
virtual ~udAbstractCallback() = default;
};

template<typename T, typename U>
template <typename T, typename U>
struct udConcreteCallback;

template<typename T, typename Result, typename ...Args>
template <typename T, typename Result, typename... Args>
struct udConcreteCallback<T, Result(Args...)> : udAbstractCallback<Result(Args...)>
{
T callback;
explicit udConcreteCallback(T &&callback) : callback(std::move(callback)) {}
Result call(Args... args) const override { return callback(args...); }
};

template<typename T>
template <typename T>
struct udCallback;

template<typename Result, typename ...Args>
template <typename Result, typename... Args>
struct udCallback<Result(Args...)>
{
uint8_t buffer[256];
udAbstractCallback<Result(Args...)> *pPtr;

udCallback() noexcept : buffer(), pPtr(nullptr) { }
udCallback(std::nullptr_t) noexcept : buffer(), pPtr(nullptr) { }
udCallback() noexcept : buffer(), pPtr(nullptr) {}
udCallback(std::nullptr_t) noexcept : buffer(), pPtr(nullptr) {}

udCallback(const udCallback &other) { memcpy(buffer, other.buffer, sizeof(buffer)); if (other.pPtr) pPtr = (decltype(pPtr))buffer; else pPtr = nullptr; };
udCallback(udCallback &&other) noexcept { memmove(buffer, other.buffer, sizeof(buffer)); if (other.pPtr) pPtr = (decltype(pPtr))buffer; else pPtr = nullptr; other.pPtr = nullptr; }
udCallback(const udCallback &other)
{
memcpy(buffer, other.buffer, sizeof(buffer));
if (other.pPtr)
pPtr = (decltype(pPtr))buffer;
else
pPtr = nullptr;
};
udCallback(udCallback &&other) noexcept
{
memmove(buffer, other.buffer, sizeof(buffer));
if (other.pPtr)
pPtr = (decltype(pPtr))buffer;
else
pPtr = nullptr;
other.pPtr = nullptr;
}

template<typename T>
template <typename T>
udCallback(T callback)
{
UDCOMPILEASSERT(sizeof(T) <= sizeof(buffer), "Provided function is larger than buffer!");
using udCallbackT = udConcreteCallback<T, Result(Args...)>;
pPtr = new (buffer) udCallbackT(std::move(callback));
}

~udCallback() { if (pPtr) pPtr->~udAbstractCallback(); }
~udCallback()
{
if (pPtr)
pPtr->~udAbstractCallback();
}

udCallback &operator=(const udCallback &other) { memcpy(buffer, other.buffer, sizeof(buffer)); if (other.pPtr) pPtr = (decltype(pPtr))buffer; else pPtr = nullptr; return *this; }
udCallback &operator=(udCallback &&other) noexcept { memmove(buffer, other.buffer, sizeof(buffer)); if (other.pPtr) pPtr = (decltype(pPtr))buffer; else pPtr = nullptr; other.pPtr = nullptr; return *this; }
udCallback &operator=(std::nullptr_t) noexcept { pPtr = nullptr; return *this; }
template<typename T>
udCallback &operator=(const udCallback &other)
{
memcpy(buffer, other.buffer, sizeof(buffer));
if (other.pPtr)
pPtr = (decltype(pPtr))buffer;
else
pPtr = nullptr;
return *this;
}
udCallback &operator=(udCallback &&other) noexcept
{
memmove(buffer, other.buffer, sizeof(buffer));
if (other.pPtr)
pPtr = (decltype(pPtr))buffer;
else
pPtr = nullptr;
other.pPtr = nullptr;
return *this;
}
udCallback &operator=(std::nullptr_t) noexcept
{
pPtr = nullptr;
return *this;
}
template <typename T>
udCallback &operator=(T callback)
{
UDCOMPILEASSERT(sizeof(T) <= sizeof(buffer), "Provided function is larger than buffer!");
Expand Down
44 changes: 23 additions & 21 deletions Include/udChunkedArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ struct udChunkedArrayIterator
using iterator_category = std::random_access_iterator_tag;
using difference_type = ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = T&;
using pointer = T *;
using reference = T &;

udChunkedArrayIterator(T **ppChunks, size_t inset, size_t chunkElementCount, size_t chunkElementCountShift, size_t chunkElementCountMask, size_t startInd);

Expand Down Expand Up @@ -53,7 +53,6 @@ struct udChunkedArrayIterator
bool operator==(const udChunkedArrayIterator<T> &rhs) const;
};


template <typename T>
struct udChunkedArray
{
Expand All @@ -68,20 +67,20 @@ struct udChunkedArray
size_t FindIndex(const T &element, size_t compareLen = sizeof(T)) const; // Linear search for matching element (first compareLen bytes compared)
void SetElement(size_t index, const T &data);

udResult PushBack(const T &v); // Push a copy of v to the back of the array, can fail if memory allocation fails
udResult PushBack(T **ppElement, bool zeroMemory = true); // Get pointer to new element at back of the array, can fail if memory allocation fails when growing array
T *PushBack(); // Get pointer to new zeroed element at back of the array, or NULL on failure
udResult PushBack(const T &v); // Push a copy of v to the back of the array, can fail if memory allocation fails
udResult PushBack(T **ppElement, bool zeroMemory = true); // Get pointer to new element at back of the array, can fail if memory allocation fails when growing array
T *PushBack(); // Get pointer to new zeroed element at back of the array, or NULL on failure

udResult PushFront(const T &v); // Push a copy of v to the front of the array, can fail if memory allocation fails
udResult PushFront(T **ppElement, bool zeroMemory = true); // Get pointer to new element at front of the array, can fail if memory allocation fails when growing array
T *PushFront(); // Get pointer to new zeroed element at front of the array, or NULL on failure
udResult PushFront(const T &v); // Push a copy of v to the front of the array, can fail if memory allocation fails
udResult PushFront(T **ppElement, bool zeroMemory = true); // Get pointer to new element at front of the array, can fail if memory allocation fails when growing array
T *PushFront(); // Get pointer to new zeroed element at front of the array, or NULL on failure

udResult Insert(size_t index, const T *pData = nullptr); // Insert the element at index, pushing and moving all elements after to make space.
udResult Insert(size_t index, const T *pData = nullptr); // Insert the element at index, pushing and moving all elements after to make space.

bool PopBack(T *pData = nullptr); // Returns false if no element to pop
bool PopFront(T *pData = nullptr); // Returns false if no element to pop
void RemoveAt(size_t index); // Remove the element at index, moving all elements after to fill the gap.
void RemoveSwapLast(size_t index); // Remove the element at index, swapping with the last element to ensure array is contiguous
bool PopBack(T *pData = nullptr); // Returns false if no element to pop
bool PopFront(T *pData = nullptr); // Returns false if no element to pop
void RemoveAt(size_t index); // Remove the element at index, moving all elements after to fill the gap.
void RemoveSwapLast(size_t index); // Remove the element at index, swapping with the last element to ensure array is contiguous

udResult ToArray(T *pArray, size_t arrayLength, size_t startIndex = 0, size_t count = 0) const; // Copy elements to an array supplied by caller
udResult ToArray(T **ppArray, size_t startIndex = 0, size_t count = 0) const; // Copy elements to an array allocated and returned to caller
Expand All @@ -101,7 +100,10 @@ struct udChunkedArray
iterator begin();
iterator end();

enum { ptrArrayInc = 32 };
enum
{
ptrArrayInc = 32
};

T **ppChunks;
size_t ptrArraySize;
Expand Down Expand Up @@ -148,7 +150,7 @@ inline udChunkedArrayIterator<T> &udChunkedArrayIterator<T>::operator+=(const di
//index within the chunk adjusting for block size
difference_type newInd = (rawInd < 0 ? (difference_type)chunkElementCount : 0) + rawInd % (difference_type)chunkElementCount;
// change in chunk index
difference_type chunkChange = (rawInd < 0 ? -1 : 0) + rawInd / (difference_type)chunkElementCount;
difference_type chunkChange = (rawInd < 0 ? -1 : 0) + rawInd / (difference_type)chunkElementCount;

ppCurrChunk += chunkChange;
currChunkElementIndex = newInd;
Expand Down Expand Up @@ -205,7 +207,7 @@ bool udChunkedArrayIterator<T>::operator!=(const udChunkedArrayIterator<T> &rhs)
return !(*this == rhs);
}

template<typename T>
template <typename T>
typename udChunkedArrayIterator<T>::reference udChunkedArrayIterator<T>::operator[](const size_t &a) const
{
return *(*this + a);
Expand Down Expand Up @@ -474,7 +476,7 @@ inline udResult udChunkedArray<T>::PushBack(T **ppElement, bool zeroMemory)

*ppElement = ppChunks[chunkIndex] + (newIndex & chunkElementCountMask);
if (zeroMemory)
memset((void*)*ppElement, 0, sizeof(T));
memset((void *)*ppElement, 0, sizeof(T));

++length;
return udR_Success;
Expand Down Expand Up @@ -563,7 +565,7 @@ inline udResult udChunkedArray<T>::PushFront(T **ppElement, bool zeroMemory)

*ppElement = ppChunks[0] + inset;
if (zeroMemory)
memset((void*)*ppElement, 0, sizeof(T));
memset((void *)*ppElement, 0, sizeof(T));

return udR_Success;
}
Expand Down Expand Up @@ -783,7 +785,7 @@ inline udResult udChunkedArray<T>::Insert(size_t index, const T *pData)
if (result != udR_Success)
return result;

for (size_t dst = length - 1; dst > index; )
for (size_t dst = length - 1; dst > index;)
{
size_t src = dst - 1;
size_t srcBackRunLen = GetElementRunLength(src, true);
Expand Down Expand Up @@ -837,7 +839,7 @@ inline typename udChunkedArray<T>::iterator udChunkedArray<T>::end()
return udChunkedArrayIterator<T>(this->ppChunks, this->inset, this->chunkElementCount, this->chunkElementCountShift, this->chunkElementCountMask, this->length);
}

template<typename T>
template <typename T>
udChunkedArrayIterator<T>::udChunkedArrayIterator(T **ppChunks, size_t inset, size_t chunkElementCount, size_t chunkElementCountShift, size_t chunkElementCountMask, size_t startInd)
{
this->ppCurrChunk = &ppChunks[(inset + startInd) >> chunkElementCountShift];
Expand Down
4 changes: 2 additions & 2 deletions Include/udCompression.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

enum udCompressionType
{
udCT_None, // No compression (performs a udMemDup)
udCT_RawDeflate, // Raw deflate compression
udCT_None, // No compression (performs a udMemDup)
udCT_RawDeflate, // Raw deflate compression
udCT_ZlibDeflate, // Deflate compression with zlib header and footer
udCT_GzipDeflate, // Deflate compression with gzip header and footer

Expand Down
13 changes: 5 additions & 8 deletions Include/udCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ enum udCryptoCipherKeyLength
enum udCryptoPaddingMode
{
udCPM_None,
udCPM_PKCS7, // PKCS5 is identical but was only defined for 8-byte block ciphers
udCPM_PKCS7, // PKCS5 is identical but was only defined for 8-byte block ciphers
};

enum udCryptoChainMode
{
udCCM_None, // Sentinal meaning no chaining mode has been set yet
udCCM_CBC, // Sequential access, requires IV unique to each call to encrypt
udCCM_CTR, // Random access, requires a nonce unique to file
udCCM_None, // Sentinal meaning no chaining mode has been set yet
udCCM_CBC, // Sequential access, requires IV unique to each call to encrypt
udCCM_CTR, // Random access, requires a nonce unique to file
};

struct udCryptoCipherContext;
Expand All @@ -79,7 +79,6 @@ udResult udCryptoCipher_Destroy(udCryptoCipherContext **ppCtx);
// Internal self-test
udResult udCryptoCipher_SelfTest(udCryptoCiphers cipher);


// **** Hash algorithms ****

enum udCryptoHashes
Expand All @@ -101,7 +100,6 @@ enum udCryptoHashLength
udCHL_MaxHashLength = udCHL_SHA512Length
};


struct udCryptoHashContext;

// Initialise a hash
Expand All @@ -125,7 +123,6 @@ udResult udCryptoHash_HMAC(udCryptoHashes hash, const char *pKeyBase64, const vo
// Internal self-test
udResult udCryptoHash_SelfTest(udCryptoHashes hash);


// **** Key derivation/exchange functions, generated keys are encoded into a string ****

struct udCryptoDHMContext;
Expand Down Expand Up @@ -180,7 +177,7 @@ enum udCryptoSigType

enum udCryptoSigPadScheme
{
udCSPS_Deterministic // A deterministic signature that doesn't require entropy. For RSA PKCS #1.5
udCSPS_Deterministic // A deterministic signature that doesn't require entropy. For RSA PKCS #1.5
};

// Generate a random key-pair
Expand Down
Loading