add cxBufferReset() - resolves #338

Mon, 18 Dec 2023 15:13:26 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 15:13:26 +0100
changeset 761
61d5197d612b
parent 760
52e49533e7b4
child 762
4523f6d42512

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
--- a/CHANGELOG	Mon Dec 18 14:25:10 2023 +0100
+++ b/CHANGELOG	Mon Dec 18 15:13:26 2023 +0100
@@ -1,7 +1,8 @@
 Version 3.1 - tbd.
 ------------------------
-* fix wrong link from UCX 2 documentation to UCX 3 documentation
-* remove CMake, except for tests at the moment, in favor of uwproj
+ * adds cxBufferReset()
+ * fixes wrong link from UCX 2 documentation to UCX 3 documentation
+ * removes CMake, except for tests at the moment, in favor of uwproj
 
 Version 3.0 - 2023-07-09
 ------------------------
--- a/src/buffer.c	Mon Dec 18 14:25:10 2023 +0100
+++ b/src/buffer.c	Mon Dec 18 15:13:26 2023 +0100
@@ -135,6 +135,11 @@
     buffer->pos = 0;
 }
 
+void cxBufferReset(CxBuffer *buffer) {
+    buffer->size = 0;
+    buffer->pos = 0;
+}
+
 int cxBufferEof(CxBuffer const *buffer) {
     return buffer->pos >= buffer->size;
 }
--- a/src/cx/buffer.h	Mon Dec 18 14:25:10 2023 +0100
+++ b/src/cx/buffer.h	Mon Dec 18 15:13:26 2023 +0100
@@ -311,13 +311,27 @@
  * Clears the buffer by resetting the position and deleting the data.
  *
  * The data is deleted by zeroing it with a call to memset().
+ * If you do not need that, you can use the faster cxBufferReset().
  *
  * @param buffer the buffer to be cleared
+ * @see cxBufferReset()
  */
 __attribute__((__nonnull__))
 void cxBufferClear(CxBuffer *buffer);
 
 /**
+ * Resets the buffer by resetting the position and size to zero.
+ *
+ * The data in the buffer is not deleted. If you need a safe
+ * reset of the buffer, use cxBufferClear().
+ *
+ * @param buffer the buffer to be cleared
+ * @see cxBufferClear()
+ */
+__attribute__((__nonnull__))
+void cxBufferReset(CxBuffer *buffer);
+
+/**
  * Tests, if the buffer position has exceeded the buffer size.
  *
  * @param buffer the buffer to test
--- a/tests/CMakeLists.txt	Mon Dec 18 14:25:10 2023 +0100
+++ b/tests/CMakeLists.txt	Mon Dec 18 15:13:26 2023 +0100
@@ -36,5 +36,6 @@
         selftest.cpp
         util_allocator.cpp
         )
+target_include_directories(ucxtest PRIVATE ${CMAKE_SOURCE_DIR}/../src)
 target_link_libraries(ucxtest ${CMAKE_BINARY_DIR}/../libucx_static${STLIB_EXT} gtest_main)
 gtest_discover_tests(ucxtest)
--- a/tests/test_buffer.cpp	Mon Dec 18 14:25:10 2023 +0100
+++ b/tests/test_buffer.cpp	Mon Dec 18 15:13:26 2023 +0100
@@ -344,6 +344,20 @@
     cxBufferDestroy(&buf);
 }
 
+TEST(BufferReset, Test) {
+    char space[16];
+    strcpy(space, "reset test");
+    CxBuffer buf;
+    cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
+    buf.size = 5;
+    buf.pos = 3;
+    cxBufferReset(&buf);
+    EXPECT_EQ(memcmp(space, "reset test", 10), 0);
+    EXPECT_EQ(buf.size, 0);
+    EXPECT_EQ(buf.pos, 0);
+    cxBufferDestroy(&buf);
+}
+
 class BufferWrite : public ::testing::Test {
 protected:
     CxBuffer buf{}, target{};

mercurial