Mon, 18 Dec 2023 15:13:26 +0100
add cxBufferReset() - resolves #338
CHANGELOG | file | annotate | diff | comparison | revisions | |
src/buffer.c | file | annotate | diff | comparison | revisions | |
src/cx/buffer.h | file | annotate | diff | comparison | revisions | |
tests/CMakeLists.txt | file | annotate | diff | comparison | revisions | |
tests/test_buffer.cpp | file | annotate | diff | comparison | revisions |
1.1 --- a/CHANGELOG Mon Dec 18 14:25:10 2023 +0100 1.2 +++ b/CHANGELOG Mon Dec 18 15:13:26 2023 +0100 1.3 @@ -1,7 +1,8 @@ 1.4 Version 3.1 - tbd. 1.5 ------------------------ 1.6 -* fix wrong link from UCX 2 documentation to UCX 3 documentation 1.7 -* remove CMake, except for tests at the moment, in favor of uwproj 1.8 + * adds cxBufferReset() 1.9 + * fixes wrong link from UCX 2 documentation to UCX 3 documentation 1.10 + * removes CMake, except for tests at the moment, in favor of uwproj 1.11 1.12 Version 3.0 - 2023-07-09 1.13 ------------------------
2.1 --- a/src/buffer.c Mon Dec 18 14:25:10 2023 +0100 2.2 +++ b/src/buffer.c Mon Dec 18 15:13:26 2023 +0100 2.3 @@ -135,6 +135,11 @@ 2.4 buffer->pos = 0; 2.5 } 2.6 2.7 +void cxBufferReset(CxBuffer *buffer) { 2.8 + buffer->size = 0; 2.9 + buffer->pos = 0; 2.10 +} 2.11 + 2.12 int cxBufferEof(CxBuffer const *buffer) { 2.13 return buffer->pos >= buffer->size; 2.14 }
3.1 --- a/src/cx/buffer.h Mon Dec 18 14:25:10 2023 +0100 3.2 +++ b/src/cx/buffer.h Mon Dec 18 15:13:26 2023 +0100 3.3 @@ -311,13 +311,27 @@ 3.4 * Clears the buffer by resetting the position and deleting the data. 3.5 * 3.6 * The data is deleted by zeroing it with a call to memset(). 3.7 + * If you do not need that, you can use the faster cxBufferReset(). 3.8 * 3.9 * @param buffer the buffer to be cleared 3.10 + * @see cxBufferReset() 3.11 */ 3.12 __attribute__((__nonnull__)) 3.13 void cxBufferClear(CxBuffer *buffer); 3.14 3.15 /** 3.16 + * Resets the buffer by resetting the position and size to zero. 3.17 + * 3.18 + * The data in the buffer is not deleted. If you need a safe 3.19 + * reset of the buffer, use cxBufferClear(). 3.20 + * 3.21 + * @param buffer the buffer to be cleared 3.22 + * @see cxBufferClear() 3.23 + */ 3.24 +__attribute__((__nonnull__)) 3.25 +void cxBufferReset(CxBuffer *buffer); 3.26 + 3.27 +/** 3.28 * Tests, if the buffer position has exceeded the buffer size. 3.29 * 3.30 * @param buffer the buffer to test
4.1 --- a/tests/CMakeLists.txt Mon Dec 18 14:25:10 2023 +0100 4.2 +++ b/tests/CMakeLists.txt Mon Dec 18 15:13:26 2023 +0100 4.3 @@ -36,5 +36,6 @@ 4.4 selftest.cpp 4.5 util_allocator.cpp 4.6 ) 4.7 +target_include_directories(ucxtest PRIVATE ${CMAKE_SOURCE_DIR}/../src) 4.8 target_link_libraries(ucxtest ${CMAKE_BINARY_DIR}/../libucx_static${STLIB_EXT} gtest_main) 4.9 gtest_discover_tests(ucxtest)
5.1 --- a/tests/test_buffer.cpp Mon Dec 18 14:25:10 2023 +0100 5.2 +++ b/tests/test_buffer.cpp Mon Dec 18 15:13:26 2023 +0100 5.3 @@ -344,6 +344,20 @@ 5.4 cxBufferDestroy(&buf); 5.5 } 5.6 5.7 +TEST(BufferReset, Test) { 5.8 + char space[16]; 5.9 + strcpy(space, "reset test"); 5.10 + CxBuffer buf; 5.11 + cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 5.12 + buf.size = 5; 5.13 + buf.pos = 3; 5.14 + cxBufferReset(&buf); 5.15 + EXPECT_EQ(memcmp(space, "reset test", 10), 0); 5.16 + EXPECT_EQ(buf.size, 0); 5.17 + EXPECT_EQ(buf.pos, 0); 5.18 + cxBufferDestroy(&buf); 5.19 +} 5.20 + 5.21 class BufferWrite : public ::testing::Test { 5.22 protected: 5.23 CxBuffer buf{}, target{};