From 17e0713b7b770f6bdc0187a762dd001d5f470ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Mon, 6 Feb 2017 16:05:58 -0800 Subject: [PATCH] Cleanup. --- include/bx/allocator.h | 1 - include/bx/allocator.inl | 2 +- include/bx/handlealloc.inl | 8 ++------ include/bx/pixelformat.inl | 36 ++++++++++++++++++------------------ include/bx/radixsort.inl | 8 ++++---- include/bx/readerwriter.h | 2 -- include/bx/readerwriter.inl | 6 +++--- include/bx/ringbuffer.h | 8 ++++---- include/bx/string.inl | 4 ++-- src/crt.cpp | 8 ++++++-- src/os.cpp | 2 +- src/string.cpp | 2 +- tests/string_test.cpp | 8 ++++---- tests/vsnprintf_test.cpp | 6 +++--- 14 files changed, 49 insertions(+), 52 deletions(-) diff --git a/include/bx/allocator.h b/include/bx/allocator.h index a1ab3e3..1eefbfc 100644 --- a/include/bx/allocator.h +++ b/include/bx/allocator.h @@ -8,7 +8,6 @@ #include "bx.h" -#include //::memmove #include #if BX_CONFIG_ALLOCATOR_DEBUG diff --git a/include/bx/allocator.inl b/include/bx/allocator.inl index 67a4a53..8a4898d 100644 --- a/include/bx/allocator.inl +++ b/include/bx/allocator.inl @@ -85,7 +85,7 @@ namespace bx } aligned = ptr + offset; - ::memmove(newAligned, aligned, _size); + memMove(newAligned, aligned, _size); uint32_t* header = (uint32_t*)newAligned - 1; *header = uint32_t(newAligned - ptr); return newAligned; diff --git a/include/bx/handlealloc.inl b/include/bx/handlealloc.inl index 04369b5..8738c3d 100644 --- a/include/bx/handlealloc.inl +++ b/include/bx/handlealloc.inl @@ -7,10 +7,6 @@ # error "Must be included from bx/handlealloc.h!" #endif // BX_HANDLE_ALLOC_H_HEADER_GUARD -#include "bx.h" -#include "allocator.h" -#include "uint32_t.h" - namespace bx { inline HandleAlloc::HandleAlloc(uint16_t _maxHandles) @@ -236,7 +232,7 @@ namespace bx template inline void HandleListT::reset() { - memset(m_links, 0xff, sizeof(m_links) ); + memSet(m_links, 0xff, sizeof(m_links) ); m_front = invalid; m_back = invalid; } @@ -496,7 +492,7 @@ namespace bx template inline void HandleHashMapT::reset() { - memset(m_handle, 0xff, sizeof(m_handle) ); + memSet(m_handle, 0xff, sizeof(m_handle) ); m_numElements = 0; } diff --git a/include/bx/pixelformat.inl b/include/bx/pixelformat.inl index be48c85..cd46892 100644 --- a/include/bx/pixelformat.inl +++ b/include/bx/pixelformat.inl @@ -641,67 +641,67 @@ namespace bx // R32I inline void packR32I(void* _dst, const float* _src) { - memcpy(_dst, _src, 4); + memCopy(_dst, _src, 4); } inline void unpackR32I(float* _dst, const void* _src) { - memcpy(_dst, _src, 4); + memCopy(_dst, _src, 4); } // R32U inline void packR32U(void* _dst, const float* _src) { - memcpy(_dst, _src, 4); + memCopy(_dst, _src, 4); } inline void unpackR32U(float* _dst, const void* _src) { - memcpy(_dst, _src, 4); + memCopy(_dst, _src, 4); } // R32F inline void packR32F(void* _dst, const float* _src) { - memcpy(_dst, _src, 4); + memCopy(_dst, _src, 4); } inline void unpackR32F(float* _dst, const void* _src) { - memcpy(_dst, _src, 4); + memCopy(_dst, _src, 4); } // RG32I inline void packRg32I(void* _dst, const float* _src) { - memcpy(_dst, _src, 8); + memCopy(_dst, _src, 8); } inline void unpackRg32I(float* _dst, const void* _src) { - memcpy(_dst, _src, 8); + memCopy(_dst, _src, 8); } // RG32U inline void packRg32U(void* _dst, const float* _src) { - memcpy(_dst, _src, 8); + memCopy(_dst, _src, 8); } inline void unpackRg32U(float* _dst, const void* _src) { - memcpy(_dst, _src, 8); + memCopy(_dst, _src, 8); } // RG32F inline void packRg32F(void* _dst, const float* _src) { - memcpy(_dst, _src, 8); + memCopy(_dst, _src, 8); } inline void unpackRg32F(float* _dst, const void* _src) { - memcpy(_dst, _src, 8); + memCopy(_dst, _src, 8); } template @@ -775,34 +775,34 @@ namespace bx // RGBA32I inline void packRgba32I(void* _dst, const float* _src) { - memcpy(_dst, _src, 16); + memCopy(_dst, _src, 16); } inline void unpackRgba32I(float* _dst, const void* _src) { - memcpy(_dst, _src, 16); + memCopy(_dst, _src, 16); } // RGBA32U inline void packRgba32U(void* _dst, const float* _src) { - memcpy(_dst, _src, 16); + memCopy(_dst, _src, 16); } inline void unpackRgba32U(float* _dst, const void* _src) { - memcpy(_dst, _src, 16); + memCopy(_dst, _src, 16); } // RGBA32F inline void packRgba32F(void* _dst, const float* _src) { - memcpy(_dst, _src, 16); + memCopy(_dst, _src, 16); } inline void unpackRgba32F(float* _dst, const void* _src) { - memcpy(_dst, _src, 16); + memCopy(_dst, _src, 16); } // R5G6B5 diff --git a/include/bx/radixsort.inl b/include/bx/radixsort.inl index 2edc5e4..582c004 100644 --- a/include/bx/radixsort.inl +++ b/include/bx/radixsort.inl @@ -70,7 +70,7 @@ done: if (0 != (pass&1) ) { // Odd number of passes needs to do copy to the destination. - memcpy(_keys, _tempKeys, _size*sizeof(uint32_t) ); + memCopy(_keys, _tempKeys, _size*sizeof(uint32_t) ); } } @@ -139,7 +139,7 @@ done: if (0 != (pass&1) ) { // Odd number of passes needs to do copy to the destination. - memcpy(_keys, _tempKeys, _size*sizeof(uint32_t) ); + memCopy(_keys, _tempKeys, _size*sizeof(uint32_t) ); for (uint32_t ii = 0; ii < _size; ++ii) { _values[ii] = _tempValues[ii]; @@ -204,7 +204,7 @@ done: if (0 != (pass&1) ) { // Odd number of passes needs to do copy to the destination. - memcpy(_keys, _tempKeys, _size*sizeof(uint64_t) ); + memCopy(_keys, _tempKeys, _size*sizeof(uint64_t) ); } } @@ -273,7 +273,7 @@ done: if (0 != (pass&1) ) { // Odd number of passes needs to do copy to the destination. - memcpy(_keys, _tempKeys, _size*sizeof(uint64_t) ); + memCopy(_keys, _tempKeys, _size*sizeof(uint64_t) ); for (uint32_t ii = 0; ii < _size; ++ii) { _values[ii] = _tempValues[ii]; diff --git a/include/bx/readerwriter.h b/include/bx/readerwriter.h index 19411a6..b7443c6 100644 --- a/include/bx/readerwriter.h +++ b/include/bx/readerwriter.h @@ -8,8 +8,6 @@ #include #include // va_list -#include -#include #include "bx.h" #include "allocator.h" diff --git a/include/bx/readerwriter.inl b/include/bx/readerwriter.inl index 9ba8517..c2eb72a 100644 --- a/include/bx/readerwriter.inl +++ b/include/bx/readerwriter.inl @@ -170,7 +170,7 @@ namespace bx int64_t remainder = m_top-m_pos; int32_t size = uint32_min(_size, uint32_t(int64_min(remainder, INT32_MAX) ) ); - memcpy(_data, &m_data[m_pos], size); + memCopy(_data, &m_data[m_pos], size); m_pos += size; if (size != _size) { @@ -242,7 +242,7 @@ namespace bx int64_t remainder = m_size-m_pos; int32_t size = uint32_min(_size, uint32_t(int64_min(remainder, INT32_MAX) ) ); - memcpy(&m_data[m_pos], _data, size); + memCopy(&m_data[m_pos], _data, size); m_pos += size; m_top = int64_max(m_top, m_pos); if (size != _size) @@ -301,7 +301,7 @@ namespace bx const uint32_t tmp1 = uint32_sels(256 - _size, 256, tmp0); const uint32_t blockSize = uint32_sels(1024 - _size, 1024, tmp1); uint8_t* temp = (uint8_t*)alloca(blockSize); - memset(temp, _byte, blockSize); + memSet(temp, _byte, blockSize); int32_t size = 0; while (0 < _size) diff --git a/include/bx/ringbuffer.h b/include/bx/ringbuffer.h index 28e7731..69aeb3a 100644 --- a/include/bx/ringbuffer.h +++ b/include/bx/ringbuffer.h @@ -222,12 +222,12 @@ namespace bx if (eof < m_read) { wrap = m_control.m_size - m_read; - memcpy(_data, from, wrap); + memCopy(_data, from, wrap); _data += wrap; from = (const char*)&m_buffer[0]; } - memcpy(_data, from, _len-wrap); + memCopy(_data, from, _len-wrap); m_read = eof; } @@ -292,12 +292,12 @@ namespace bx if (eof < m_write) { wrap = m_control.m_size - m_write; - memcpy(to, _data, wrap); + memCopy(to, _data, wrap); _data += wrap; to = (char*)&m_buffer[0]; } - memcpy(to, _data, _len-wrap); + memCopy(to, _data, _len-wrap); m_write = eof; } diff --git a/include/bx/string.inl b/include/bx/string.inl index 42f2a6a..ed60b5c 100644 --- a/include/bx/string.inl +++ b/include/bx/string.inl @@ -43,8 +43,8 @@ namespace bx { Ty str = _str; size_t startPos = 0; - const size_t fromLen = strlen(_from); - const size_t toLen = strlen(_to); + const size_t fromLen = strnlen(_from); + const size_t toLen = strnlen(_to); while ( (startPos = str.find(_from, startPos) ) != Ty::npos) { str.replace(startPos, fromLen, _to); diff --git a/src/crt.cpp b/src/crt.cpp index 468abcb..401ce00 100644 --- a/src/crt.cpp +++ b/src/crt.cpp @@ -7,6 +7,10 @@ #include #include +#if !BX_CRT_NONE +# include // memcpy, memmove, memset +#endif // !BX_CRT_NONE + namespace bx { void* memCopyRef(void* _dst, const void* _src, size_t _numBytes) @@ -70,7 +74,7 @@ namespace bx // if (src+_numBytes <= dst || end <= src) if (dst < src) { - return memcpy(_dst, _src, _numBytes); + return memCopy(_dst, _src, _numBytes); } for (intptr_t ii = _numBytes-1; ii >= 0; --ii) @@ -402,6 +406,6 @@ extern "C" void* memmove(void* _dst, const void* _src, size_t _numBytes) extern "C" void* memset(void* _dst, int _ch, size_t _numBytes) { - return bx::memSet(_dst, _ch, _numBytes); + return bx::memSet(_dst, uint8_t(_ch), _numBytes); } #endif // BX_CRT_NONE diff --git a/src/os.cpp b/src/os.cpp index 5f1d459..c3bbc70 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -237,7 +237,7 @@ namespace bx result = len != 0 && len < *_inOutSize; if (len < *_inOutSize) { - strcpy(_out, ptr); + strlncpy(_out, len, ptr); } } diff --git a/src/string.cpp b/src/string.cpp index c53a9de..a3e1ed5 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -121,7 +121,7 @@ namespace bx const size_t len = strnlen(_src, _num); const size_t max = _dstSize-1; const size_t num = (len < max ? len : max); - memcpy(_dst, _src, num); + memCopy(_dst, _src, num); _dst[num] = '\0'; return num; diff --git a/tests/string_test.cpp b/tests/string_test.cpp index bea7204..0c8955e 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -41,15 +41,15 @@ TEST_CASE("strlncpy", "") REQUIRE(num == 0); num = bx::strlncpy(dst, 3, "blah", 3); - REQUIRE(0 == strcmp(dst, "bl") ); + REQUIRE(0 == bx::strncmp(dst, "bl") ); REQUIRE(num == 2); num = bx::strlncpy(dst, sizeof(dst), "blah", 3); - REQUIRE(0 == strcmp(dst, "bla") ); + REQUIRE(0 == bx::strncmp(dst, "bla") ); REQUIRE(num == 3); num = bx::strlncpy(dst, sizeof(dst), "blah"); - REQUIRE(0 == strcmp(dst, "blah") ); + REQUIRE(0 == bx::strncmp(dst, "blah") ); REQUIRE(num == 4); } @@ -195,7 +195,7 @@ TEST_CASE("StringView", "") st.append("test", 2); REQUIRE(10 == st.getLength() ); - REQUIRE(0 == strcmp(st.getPtr(), "testtestte") ); + REQUIRE(0 == bx::strncmp(st.getPtr(), "testtestte") ); st.clear(); REQUIRE(0 == st.getLength() ); diff --git a/tests/vsnprintf_test.cpp b/tests/vsnprintf_test.cpp index 89988d1..d85fae0 100644 --- a/tests/vsnprintf_test.cpp +++ b/tests/vsnprintf_test.cpp @@ -18,12 +18,12 @@ TEST_CASE("vsnprintf truncated", "Truncated output buffer.") char buffer[7]; REQUIRE(10 == bx::snprintf(buffer, BX_COUNTOF(buffer), "Ten chars!") ); - REQUIRE(0 == strcmp(buffer, "Ten ch") ); + REQUIRE(0 == bx::strncmp(buffer, "Ten ch") ); } static bool test(const char* _expected, const char* _format, ...) { - int32_t max = (int32_t)strlen(_expected) + 1; + int32_t max = (int32_t)bx::strnlen(_expected) + 1; char* temp = (char*)alloca(max); va_list argList; @@ -33,7 +33,7 @@ static bool test(const char* _expected, const char* _format, ...) bool result = true && len == max-1 - && 0 == strcmp(_expected, temp) + && 0 == bx::strncmp(_expected, temp) ; if (!result)