Recast: move common definitions to separate header

Moved to separate header so they can be used everywhere.
This commit is contained in:
Kawe Mazidjatari 2024-09-03 11:08:23 +02:00
parent 98454cceab
commit 1e92548624
9 changed files with 60 additions and 30 deletions

View File

@ -315,14 +315,14 @@ bool FileIO::isReading() const
return m_mode == 2;
}
bool FileIO::write(const void* ptr, const size_t size)
bool FileIO::write(const void* ptr, const rdSizeType size)
{
if (!m_fp || m_mode != 1) return false;
fwrite(ptr, size, 1, m_fp);
return true;
}
bool FileIO::read(void* ptr, const size_t size)
bool FileIO::read(void* ptr, const rdSizeType size)
{
if (!m_fp || m_mode != 2) return false;
size_t readLen = fread(ptr, size, 1, m_fp);

View File

@ -90,8 +90,8 @@ public:
bool openForRead(const char* path);
virtual bool isWriting() const;
virtual bool isReading() const;
virtual bool write(const void* ptr, const size_t size);
virtual bool read(void* ptr, const size_t size);
virtual bool write(const void* ptr, const rdSizeType size);
virtual bool read(void* ptr, const rdSizeType size);
private:
// Explicitly disabled copy constructor and copy assignment operator.
FileIO(const FileIO&);

View File

@ -19,6 +19,7 @@ add_sources( SOURCE_GROUP "Include"
"Shared/Include/SharedAlloc.h"
"Shared/Include/SharedAssert.h"
"Shared/Include/SharedCommon.h"
"Shared/Include/SharedDefs.h"
"Shared/Include/SharedMath.h"
)

View File

@ -24,8 +24,8 @@ struct duFileIO
virtual ~duFileIO() = 0;
virtual bool isWriting() const = 0;
virtual bool isReading() const = 0;
virtual bool write(const void* ptr, const size_t size) = 0;
virtual bool read(void* ptr, const size_t size) = 0;
virtual bool write(const void* ptr, const rdSizeType size) = 0;
virtual bool read(void* ptr, const rdSizeType size) = 0;
};
#endif // FILE_IO_H

View File

@ -82,7 +82,7 @@ struct dtTileCacheAlloc
virtual void reset() {}
virtual void* alloc(const size_t size)
virtual void* alloc(const rdSizeType size)
{
return rdAlloc(size, RD_ALLOC_TEMP);
}

View File

@ -19,6 +19,7 @@
#ifndef RECASTDETOURALLOC_H
#define RECASTDETOURALLOC_H
#include "SharedDefs.h"
#include "SharedAssert.h"
/// Provides hint values to the memory allocator on how long the
@ -34,7 +35,7 @@ enum rdAllocHint
// @param[in] rdAllocHint A hint to the allocator on how long the memory is expected to be in use.
// @return A pointer to the beginning of the allocated memory block, or null if the allocation failed.
/// @see rdAllocSetCustom
typedef void* (rdAllocFunc)(size_t size, rdAllocHint hint);
typedef void* (rdAllocFunc)(rdSizeType size, rdAllocHint hint);
/// A memory deallocation function.
/// @param[in] ptr A pointer to a memory block previously allocated using #rdAllocFunc.
@ -55,7 +56,7 @@ void rdAllocSetCustom(rdAllocFunc *allocFunc, rdFreeFunc *freeFunc);
/// @return A pointer to the beginning of the allocated memory block, or null if the allocation failed.
///
/// @see rdFree, rdAllocSetCustom
void* rdAlloc(size_t size, rdAllocHint hint);
void* rdAlloc(rdSizeType size, rdAllocHint hint);
/// Deallocates a memory block. If @p ptr is NULL, this does nothing.
///
@ -74,21 +75,6 @@ struct rdNewTag {};
inline void* operator new(size_t, const rdNewTag&, void* p) { return p; }
inline void operator delete(void*, const rdNewTag&, void*) {}
/// Signed to avoid warnings when comparing to int loop indexes, and common error with comparing to zero.
/// MSVC2010 has a bug where ssize_t is unsigned (!!!).
typedef intptr_t rdSizeType;
#define RD_SIZE_MAX INTPTR_MAX
/// Macros to hint to the compiler about the likeliest branch. Please add a benchmark that demonstrates a performance
/// improvement before introducing use cases.
#if defined(__GNUC__) || defined(__clang__)
#define rdLikely(x) __builtin_expect((x), true)
#define rdUnlikely(x) __builtin_expect((x), false)
#else
#define rdLikely(x) (x)
#define rdUnlikely(x) (x)
#endif
/// Variable-sized storage type. Mimics the interface of std::vector<T> with some notable differences:
/// * Uses rdAlloc()/rdFree() to handle storage.
/// * No support for a custom allocator.

View File

@ -19,10 +19,9 @@
#ifndef RECASTDETOURCOMMON_H
#define RECASTDETOURCOMMON_H
#include "Shared/Include/SharedDefs.h"
#include "Shared/Include/SharedMath.h"
#define rdForceInline __forceinline
/// The total number of bits in an bit cell integer.
static const int RD_BITS_PER_BIT_CELL = 32;
@ -615,7 +614,7 @@ void rdRandomPointInConvexPoly(const float* pts, const int npts, float* areas,
const float s, const float t, float* out);
template<typename TypeToRetrieveAs>
TypeToRetrieveAs* rdGetThenAdvanceBufferPointer(const unsigned char*& buffer, const size_t distanceToAdvance)
TypeToRetrieveAs* rdGetThenAdvanceBufferPointer(const unsigned char*& buffer, const rdSizeType distanceToAdvance)
{
TypeToRetrieveAs* returnPointer = reinterpret_cast<TypeToRetrieveAs*>(buffer);
buffer += distanceToAdvance;
@ -623,7 +622,7 @@ TypeToRetrieveAs* rdGetThenAdvanceBufferPointer(const unsigned char*& buffer, co
}
template<typename TypeToRetrieveAs>
TypeToRetrieveAs* rdGetThenAdvanceBufferPointer(unsigned char*& buffer, const size_t distanceToAdvance)
TypeToRetrieveAs* rdGetThenAdvanceBufferPointer(unsigned char*& buffer, const rdSizeType distanceToAdvance)
{
TypeToRetrieveAs* returnPointer = reinterpret_cast<TypeToRetrieveAs*>(buffer);
buffer += distanceToAdvance;

View File

@ -0,0 +1,44 @@
//
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
#ifndef RECASTDETOURDEFS_H
#define RECASTDETOURDEFS_H
/// Signed to avoid warnings when comparing to int loop indexes, and common error with comparing to zero.
/// MSVC2010 has a bug where ssize_t is unsigned (!!!).
typedef intptr_t rdSizeType;
#define RD_SIZE_MAX INTPTR_MAX
/// Macros to hint to the compiler about the likeliest branch. Please add a benchmark that demonstrates a performance
/// improvement before introducing use cases.
#if defined(__GNUC__) || defined(__clang__)
#define rdLikely(x) __builtin_expect((x), true)
#define rdUnlikely(x) __builtin_expect((x), false)
#else
#define rdLikely(x) (x)
#define rdUnlikely(x) (x)
#endif
/// Macros to forcefully inline a function.
#if defined(__GNUC__) || defined(__clang__)
#define rdForceInline static inline
#else
#define rdForceInline __forceinline
#endif
#endif // RECASTDETOURDEFS_H

View File

@ -18,7 +18,7 @@
#include "Shared/Include/SharedAlloc.h"
static void *rdAllocDefault(size_t size, rdAllocHint)
static void *rdAllocDefault(rdSizeType size, rdAllocHint)
{
return malloc(size);
}
@ -37,7 +37,7 @@ void rdAllocSetCustom(rdAllocFunc *allocFunc, rdFreeFunc *freeFunc)
sFreeFunc = freeFunc ? freeFunc : rdFreeDefault;
}
void* rdAlloc(size_t size, rdAllocHint hint)
void* rdAlloc(rdSizeType size, rdAllocHint hint)
{
return sAllocFunc(size, hint);
}