Sun, 16 Apr 2023 21:35:08 +0200
add convenience functions for allocating a buffer on the heap
src/buffer.c | file | annotate | diff | comparison | revisions | |
src/cx/buffer.h | file | annotate | diff | comparison | revisions | |
tests/test_buffer.cpp | file | annotate | diff | comparison | revisions |
1.1 --- a/src/buffer.c Sun Apr 16 21:09:25 2023 +0200 1.2 +++ b/src/buffer.c Sun Apr 16 21:35:08 2023 +0200 1.3 @@ -70,6 +70,29 @@ 1.4 } 1.5 } 1.6 1.7 +CxBuffer *cxBufferCreate( 1.8 + void *space, 1.9 + size_t capacity, 1.10 + CxAllocator const *allocator, 1.11 + int flags 1.12 +) { 1.13 + CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer)); 1.14 + if (buf == NULL) return NULL; 1.15 + if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) { 1.16 + return buf; 1.17 + } else { 1.18 + cxFree(allocator, buf); 1.19 + return NULL; 1.20 + } 1.21 +} 1.22 + 1.23 +void cxBufferFree(CxBuffer *buffer) { 1.24 + if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) { 1.25 + cxFree(buffer->allocator, buffer->bytes); 1.26 + } 1.27 + cxFree(buffer->allocator, buffer); 1.28 +} 1.29 + 1.30 int cxBufferSeek( 1.31 CxBuffer *buffer, 1.32 off_t offset,
2.1 --- a/src/cx/buffer.h Sun Apr 16 21:09:25 2023 +0200 2.2 +++ b/src/cx/buffer.h Sun Apr 16 21:35:08 2023 +0200 2.3 @@ -164,16 +164,52 @@ 2.4 ); 2.5 2.6 /** 2.7 + * Allocates and initializes a fresh buffer. 2.8 + * 2.9 + * \note You may provide \c NULL as argument for \p space. 2.10 + * Then this function will allocate the space and enforce 2.11 + * the #CX_BUFFER_FREE_CONTENTS flag. 2.12 + * 2.13 + * @param space pointer to the memory area, or \c NULL to allocate 2.14 + * new memory 2.15 + * @param capacity the capacity of the buffer 2.16 + * @param allocator the allocator to use for allocating the structure and the automatic 2.17 + * memory management within the buffer. If \c NULL, the default heap allocator will be used. 2.18 + * @param flags buffer features (see cx_buffer_s.flags) 2.19 + * @return a pointer to the buffer on success, \c NULL if a required allocation failed 2.20 + */ 2.21 +CxBuffer *cxBufferCreate( 2.22 + void *space, 2.23 + size_t capacity, 2.24 + CxAllocator const *allocator, 2.25 + int flags 2.26 +); 2.27 + 2.28 +/** 2.29 * Destroys the buffer contents. 2.30 * 2.31 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled. 2.32 + * If you want to free the memory of the entire buffer, use cxBufferFree(). 2.33 * 2.34 * @param buffer the buffer which contents shall be destroyed 2.35 + * @see cxBufferInit() 2.36 */ 2.37 __attribute__((__nonnull__)) 2.38 void cxBufferDestroy(CxBuffer *buffer); 2.39 2.40 /** 2.41 + * Deallocates the buffer. 2.42 + * 2.43 + * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys 2.44 + * the contents. If you \em only want to destroy the contents, use cxBufferDestroy(). 2.45 + * 2.46 + * @param buffer the buffer to deallocate 2.47 + * @see cxBufferCreate() 2.48 + */ 2.49 +__attribute__((__nonnull__)) 2.50 +void cxBufferFree(CxBuffer *buffer); 2.51 + 2.52 +/** 2.53 * Shifts the contents of the buffer by the given offset. 2.54 * 2.55 * If the offset is positive, the contents are shifted to the right.
3.1 --- a/tests/test_buffer.cpp Sun Apr 16 21:09:25 2023 +0200 3.2 +++ b/tests/test_buffer.cpp Sun Apr 16 21:35:08 2023 +0200 3.3 @@ -127,6 +127,24 @@ 3.4 EXPECT_TRUE(alloc.verify()); 3.5 } 3.6 3.7 +TEST(BufferInit, OnHeap) { 3.8 + CxTestingAllocator alloc; 3.9 + CxBuffer *buf; 3.10 + void *space = cxMalloc(&alloc, 16); 3.11 + buf = cxBufferCreate(space, 16, &alloc, CX_BUFFER_FREE_CONTENTS); 3.12 + EXPECT_NE(buf, nullptr); 3.13 + expect_default_flush_config(buf); 3.14 + EXPECT_EQ(buf->space, space); 3.15 + EXPECT_EQ(buf->flags & CX_BUFFER_AUTO_EXTEND, 0); 3.16 + EXPECT_EQ(buf->flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS); 3.17 + EXPECT_EQ(buf->pos, 0); 3.18 + EXPECT_EQ(buf->size, 0); 3.19 + EXPECT_EQ(buf->capacity, 16); 3.20 + EXPECT_EQ(buf->allocator, &alloc); 3.21 + cxBufferFree(buf); 3.22 + EXPECT_TRUE(alloc.verify()); 3.23 +} 3.24 + 3.25 class BufferShiftFixture : public ::testing::Test { 3.26 protected: 3.27 void SetUp() override {