Skip to content

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo committed Feb 22, 2024
2 parents be70313 + 676b355 commit 45b192e
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.26)
project(naughty-buffers LANGUAGES C VERSION 1.1.1)
project(naughty-buffers LANGUAGES C VERSION 1.2.0)

include(GNUInstallDirs)
include(GenerateExportHeader)
Expand Down
43 changes: 43 additions & 0 deletions include/naughty-buffers/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,49 @@ NAUGHTY_BUFFERS_EXPORT void * nb_back(struct nb_buffer * buffer);
*/
NAUGHTY_BUFFERS_EXPORT enum NB_ASSIGN_RESULT nb_assign(struct nb_buffer * buffer, size_t index, void * data);


/**
* @brief Copies `block_count` blocks from `data` to the buffer starting at `index`.
*
* If `index` or `index + block_count` is larger than the buffer capacity the buffer data will be reallocated to accommodate the new data.
* Uninitialized data will be present in the blocks preceding the new data if they were not present before.
*
* Caller is expected to ensure that `data` points at the first block to be copied from and that it has
* at least `block_count * block_size` bytes.
*
* @param buffer A pointer to a ::nb_buffer struct
* @param index The block index to assign the data to
* @param data A pointer to the data to be copied in the buffer at the specified index.
* @param block_count Amount of blocks to copy from `data` into the buffer
* @return NB_ASSIGN_OK if assignment was successful or NB_ASSIGN_OUT_OF_MEMORY if out of memory
* @ingroup buffer
*
* **Example**
* @code
int main(void) {
struct nb_buffer buffer;
nb_init(&buffer, sizeof(int));
int value[3] = { 20, 21, 22 };
int * read_value;
nb_assign_many(&buffer, 20, value, 3);
read_value = (int*) nb_at(&buffer, 20);
assert(*read_value = 20);
read_value = (int*) nb_at(&buffer, 21);
assert(*read_value = 21);
read_value = (int*) nb_at(&buffer, 22);
assert(*read_value = 22);
nb_release(&buffer);
return 0;
}
* @endcode
*/
NAUGHTY_BUFFERS_EXPORT enum NB_ASSIGN_RESULT nb_assign_many(struct nb_buffer * buffer, size_t index, void * data, size_t block_count);

/**
* @brief Inserts `data` to the block at index `index` moving all blocks past the index forward one position.
*
Expand Down
12 changes: 8 additions & 4 deletions src/naughty-buffers/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,18 @@ void nb_release(struct nb_buffer * buffer) {
}

enum NB_ASSIGN_RESULT nb_assign(struct nb_buffer * buffer, size_t index, void * data) {
if (index >= buffer->block_capacity) {
uint8_t grow_success = nb_grow(buffer, index + 1);
return nb_assign_many(buffer, index, data, 1);
}

enum NB_ASSIGN_RESULT nb_assign_many(struct nb_buffer * buffer, size_t index, void * data, size_t block_count) {
if (index + block_count >= buffer->block_capacity) {
uint8_t grow_success = nb_grow(buffer, index + block_count);
if (!grow_success) return NB_ASSIGN_OUT_OF_MEMORY;
}
uint8_t * buffer_data = buffer->data;
void * block_data = buffer_data + (index * buffer->block_size);
ctx_copy(buffer, block_data, data, buffer->block_size);
if (index >= buffer->block_count) buffer->block_count = index + 1;
ctx_copy(buffer, block_data, data, buffer->block_size * block_count);
if (index + block_count >= buffer->block_count) buffer->block_count = index + block_count;
return NB_ASSIGN_OK;
}

Expand Down
1 change: 1 addition & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ endmacro()
nb_test(test-memory-calls memory.c)
nb_test(test-push push.c)
nb_test(test-assign assign.c)
nb_test(test-assign-many assign-many.c)
nb_test(test-insert insert.c)
nb_test(test-remove remove.c)
nb_test(test-sort sort.c)
Expand Down
89 changes: 89 additions & 0 deletions src/tests/assign-many.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "naughty-buffers/buffer.h"
#include <assert.h>

void assign_many_increases_count_correctly() {
struct nb_buffer buffer;
uint32_t value[] = { 0, 1, 2 };
nb_init(&buffer, sizeof(uint32_t));
assert(buffer.block_count == 0);
nb_assign_many(&buffer, 0, &value, 3);
assert(buffer.block_count == 3);
nb_assign_many(&buffer, 3, &value, 3);
assert(buffer.block_count == 6);
nb_assign_many(&buffer, 3, &value, 6);
assert(buffer.block_count == 9);
nb_assign_many(&buffer, 18, &value, 3);
assert(buffer.block_count == 21);
nb_release(&buffer);
}

void assign_many_stores_the_right_values() {
struct nb_buffer buffer;
uint32_t value_1[] = { 0, 1, 2 };
uint32_t * read_value = NULL;

nb_init(&buffer, sizeof(uint32_t));

nb_assign_many(&buffer, 0, &value_1, 3);
for (size_t i = 0; i < 3; i++) {
read_value = nb_at(&buffer, i);
assert(*read_value == value_1[i]);
}

uint32_t value_2[] = { 3, 4, 5 };
nb_assign_many(&buffer, 0, &value_2, 3);
for (size_t i = 0; i < 3; i++) {
read_value = nb_at(&buffer, i);
assert(*read_value == value_2[i]);
}

uint32_t value_3[] = { 33333, 44444, 55555 };
nb_assign_many(&buffer, 0, value_3, 3);
for (size_t i = 0; i < 3; i++) {
read_value = nb_at(&buffer, i);
assert(*read_value == value_3[i]);
}

nb_release(&buffer);
}

void assign_many_store_values_not_addresses() {
struct nb_buffer buffer;
uint32_t value[] = { 0, 1, 2, 3 };
uint32_t * read_value = NULL;

nb_init(&buffer, sizeof(uint32_t));

nb_assign_many(&buffer, 0, &value, 4);
for (size_t i = 0; i < 4; i++) {
read_value = nb_at(&buffer, i);
assert(read_value != &value[i]);
}

nb_release(&buffer);
}

void assign_many_properly_stretches_the_buffer() {
struct nb_buffer buffer;
uint32_t value[] = { 0, 1, 2 };

nb_init(&buffer, sizeof(uint32_t));

nb_assign_many(&buffer, 33, &value, 3);
assert(buffer.block_count == 36);
assert(buffer.block_capacity >= 35);


nb_assign_many(&buffer, 63, &value, 3);
assert(buffer.block_count == 66);
assert(buffer.block_capacity >= 67);

nb_release(&buffer);
}

int main(void) {
assign_many_increases_count_correctly();
assign_many_stores_the_right_values();
assign_many_store_values_not_addresses();
assign_many_properly_stretches_the_buffer();
}

0 comments on commit 45b192e

Please sign in to comment.