mirror of
https://github.com/bkaradzic/bx.git
synced 2026-02-17 20:52:37 +01:00
Cleanup.
This commit is contained in:
@@ -15,40 +15,34 @@
|
||||
#include "config.h"
|
||||
#include "macros.h"
|
||||
|
||||
namespace bx
|
||||
{
|
||||
// http://cnicholson.net/2011/01/stupid-c-tricks-a-better-sizeof_array/
|
||||
template<typename T, size_t N> char (&COUNTOF_REQUIRES_ARRAY_ARGUMENT(const T(&)[N]) )[N];
|
||||
///
|
||||
#define BX_COUNTOF(_x) sizeof(bx::COUNTOF_REQUIRES_ARRAY_ARGUMENT(_x) )
|
||||
|
||||
// Template for avoiding MSVC: C4127: conditional expression is constant
|
||||
template<bool>
|
||||
inline bool isEnabled()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline bool isEnabled<false>()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
#define BX_ENABLED(_x) bx::isEnabled<!!(_x)>()
|
||||
|
||||
inline bool ignoreC4127(bool _x)
|
||||
{
|
||||
return _x;
|
||||
}
|
||||
///
|
||||
#define BX_IGNORE_C4127(_x) bx::ignoreC4127(!!(_x) )
|
||||
|
||||
template<typename Ty>
|
||||
inline void xchg(Ty& _a, Ty& _b)
|
||||
{
|
||||
Ty tmp = _a; _a = _b; _b = tmp;
|
||||
}
|
||||
///
|
||||
#define BX_ENABLED(_x) bx::isEnabled<!!(_x)>()
|
||||
|
||||
namespace bx
|
||||
{
|
||||
/// Template for avoiding MSVC: C4127: conditional expression is constant
|
||||
template<bool>
|
||||
bool isEnabled();
|
||||
|
||||
///
|
||||
void* memCopy(void* _dst, const void* _src, size_t _numBytes);
|
||||
bool ignoreC4127(bool _x);
|
||||
|
||||
///
|
||||
template<typename Ty>
|
||||
void xchg(Ty& _a, Ty& _b);
|
||||
|
||||
// http://cnicholson.net/2011/01/stupid-c-tricks-a-better-sizeof_array/
|
||||
template<typename T, size_t N>
|
||||
char (&COUNTOF_REQUIRES_ARRAY_ARGUMENT(const T(&)[N]) )[N];
|
||||
|
||||
///
|
||||
void memCopy(void* _dst, const void* _src, size_t _numBytes);
|
||||
|
||||
///
|
||||
void memCopy(void* _dst, const void* _src, uint32_t _size, uint32_t _num, uint32_t _srcPitch, uint32_t _dstPitch);
|
||||
@@ -60,11 +54,13 @@ namespace bx
|
||||
void scatter(void* _dst, const void* _src, uint32_t _size, uint32_t _num, uint32_t _dstPitch);
|
||||
|
||||
///
|
||||
void* memMove(void* _dst, const void* _src, size_t _numBytes);
|
||||
void memMove(void* _dst, const void* _src, size_t _numBytes);
|
||||
|
||||
///
|
||||
void* memSet(void* _dst, uint8_t _ch, size_t _numBytes);
|
||||
void memSet(void* _dst, uint8_t _ch, size_t _numBytes);
|
||||
|
||||
} // namespace bx
|
||||
|
||||
#include "bx.inl"
|
||||
|
||||
#endif // BX_H_HEADER_GUARD
|
||||
|
||||
55
src/crt.cpp
55
src/crt.cpp
@@ -13,7 +13,18 @@
|
||||
|
||||
namespace bx
|
||||
{
|
||||
void* memCopyRef(void* _dst, const void* _src, size_t _numBytes)
|
||||
void xchg(void* _a, void* _b, size_t _numBytes)
|
||||
{
|
||||
uint8_t* lhs = (uint8_t*)_a;
|
||||
uint8_t* rhs = (uint8_t*)_b;
|
||||
const uint8_t* end = rhs + _numBytes;
|
||||
while (rhs != end)
|
||||
{
|
||||
xchg(*lhs++, *rhs++);
|
||||
}
|
||||
}
|
||||
|
||||
void memCopyRef(void* _dst, const void* _src, size_t _numBytes)
|
||||
{
|
||||
uint8_t* dst = (uint8_t*)_dst;
|
||||
const uint8_t* end = dst + _numBytes;
|
||||
@@ -22,16 +33,14 @@ namespace bx
|
||||
{
|
||||
*dst++ = *src++;
|
||||
}
|
||||
|
||||
return _dst;
|
||||
}
|
||||
|
||||
void* memCopy(void* _dst, const void* _src, size_t _numBytes)
|
||||
void memCopy(void* _dst, const void* _src, size_t _numBytes)
|
||||
{
|
||||
#if BX_CRT_NONE
|
||||
return memCopyRef(_dst, _src, _numBytes);
|
||||
memCopyRef(_dst, _src, _numBytes);
|
||||
#else
|
||||
return ::memcpy(_dst, _src, _numBytes);
|
||||
::memcpy(_dst, _src, _numBytes);
|
||||
#endif // BX_CRT_NONE
|
||||
}
|
||||
|
||||
@@ -60,7 +69,7 @@ namespace bx
|
||||
memCopy(_dst, _src, _size, _num, _size, _dstPitch);
|
||||
}
|
||||
|
||||
void* memMoveRef(void* _dst, const void* _src, size_t _numBytes)
|
||||
void memMoveRef(void* _dst, const void* _src, size_t _numBytes)
|
||||
{
|
||||
uint8_t* dst = (uint8_t*)_dst;
|
||||
const uint8_t* src = (const uint8_t*)_src;
|
||||
@@ -68,33 +77,32 @@ namespace bx
|
||||
if (_numBytes == 0
|
||||
|| dst == src)
|
||||
{
|
||||
return dst;
|
||||
return;
|
||||
}
|
||||
|
||||
// if (src+_numBytes <= dst || end <= src)
|
||||
if (dst < src)
|
||||
{
|
||||
return memCopy(_dst, _src, _numBytes);
|
||||
memCopy(_dst, _src, _numBytes);
|
||||
return;
|
||||
}
|
||||
|
||||
for (intptr_t ii = _numBytes-1; ii >= 0; --ii)
|
||||
{
|
||||
dst[ii] = src[ii];
|
||||
}
|
||||
|
||||
return _dst;
|
||||
}
|
||||
|
||||
void* memMove(void* _dst, const void* _src, size_t _numBytes)
|
||||
void memMove(void* _dst, const void* _src, size_t _numBytes)
|
||||
{
|
||||
#if BX_CRT_NONE
|
||||
return memMoveRef(_dst, _src, _numBytes);
|
||||
memMoveRef(_dst, _src, _numBytes);
|
||||
#else
|
||||
return ::memmove(_dst, _src, _numBytes);
|
||||
::memmove(_dst, _src, _numBytes);
|
||||
#endif // BX_CRT_NONE
|
||||
}
|
||||
|
||||
void* memSetRef(void* _dst, uint8_t _ch, size_t _numBytes)
|
||||
void memSetRef(void* _dst, uint8_t _ch, size_t _numBytes)
|
||||
{
|
||||
uint8_t* dst = (uint8_t*)_dst;
|
||||
const uint8_t* end = dst + _numBytes;
|
||||
@@ -102,16 +110,14 @@ namespace bx
|
||||
{
|
||||
*dst++ = char(_ch);
|
||||
}
|
||||
|
||||
return _dst;
|
||||
}
|
||||
|
||||
void* memSet(void* _dst, uint8_t _ch, size_t _numBytes)
|
||||
void memSet(void* _dst, uint8_t _ch, size_t _numBytes)
|
||||
{
|
||||
#if BX_CRT_NONE
|
||||
return memSetRef(_dst, _ch, _numBytes);
|
||||
memSetRef(_dst, _ch, _numBytes);
|
||||
#else
|
||||
return ::memset(_dst, _ch, _numBytes);
|
||||
::memset(_dst, _ch, _numBytes);
|
||||
#endif // BX_CRT_NONE
|
||||
}
|
||||
|
||||
@@ -396,16 +402,19 @@ namespace bx
|
||||
#if BX_CRT_NONE
|
||||
extern "C" void* memcpy(void* _dst, const void* _src, size_t _numBytes)
|
||||
{
|
||||
return bx::memCopy(_dst, _src, _numBytes);
|
||||
bx::memCopy(_dst, _src, _numBytes);
|
||||
return _dst;
|
||||
}
|
||||
|
||||
extern "C" void* memmove(void* _dst, const void* _src, size_t _numBytes)
|
||||
{
|
||||
return bx::memMove(_dst, _src, _numBytes);
|
||||
bx::memMove(_dst, _src, _numBytes);
|
||||
return _dst;
|
||||
}
|
||||
|
||||
extern "C" void* memset(void* _dst, int _ch, size_t _numBytes)
|
||||
{
|
||||
return bx::memSet(_dst, uint8_t(_ch), _numBytes);
|
||||
bx::memSet(_dst, uint8_t(_ch), _numBytes);
|
||||
return _dst;
|
||||
}
|
||||
#endif // BX_CRT_NONE
|
||||
|
||||
@@ -10,10 +10,10 @@ TEST_CASE("memSet", "")
|
||||
{
|
||||
char temp[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
|
||||
|
||||
REQUIRE(temp == bx::memSet(temp, 0, 0) );
|
||||
bx::memSet(temp, 0, 0);
|
||||
REQUIRE(temp[0] == 1);
|
||||
|
||||
REQUIRE(temp == bx::memSet(temp, 0, 5) );
|
||||
bx::memSet(temp, 0, 5);
|
||||
REQUIRE(temp[0] == 0);
|
||||
REQUIRE(temp[1] == 0);
|
||||
REQUIRE(temp[2] == 0);
|
||||
@@ -24,17 +24,21 @@ TEST_CASE("memSet", "")
|
||||
|
||||
TEST_CASE("memMove", "")
|
||||
{
|
||||
const char* orignal = "xxxxabvgd";
|
||||
char str[] = { 'x', 'x', 'x', 'x', 'a', 'b', 'v', 'g', 'd' };
|
||||
|
||||
REQUIRE(bx::memMove(&str[4], &str[4], 0) );
|
||||
REQUIRE(bx::memMove(&str[4], &str[4], 5) );
|
||||
bx::memMove(&str[4], &str[4], 0);
|
||||
REQUIRE(0 == bx::strncmp(str, orignal) );
|
||||
|
||||
REQUIRE(&str[0] == bx::memMove(str, &str[4], 5) );
|
||||
REQUIRE( 0 == bx::strncmp(str, "abvgd", 5) );
|
||||
bx::memMove(&str[4], &str[4], 5);
|
||||
REQUIRE(0 == bx::strncmp(str, orignal) );
|
||||
|
||||
REQUIRE(&str[4] == bx::memMove(&str[4], str, 5) );
|
||||
REQUIRE( str[4] == 'a' );
|
||||
bx::memMove(str, &str[4], 5);
|
||||
REQUIRE(0 == bx::strncmp(str, "abvgd", 5) );
|
||||
|
||||
bx::memMove(&str[4], str, 5);
|
||||
REQUIRE(str[4] == 'a' );
|
||||
|
||||
bx::memSet(str, 'x', 4);
|
||||
REQUIRE(0 == bx::strncmp(str, "xxxxabvgd", 9) );
|
||||
REQUIRE(0 == bx::strncmp(str, orignal) );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user