Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion include/bfdev/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <bfdev/config.h>
#include <bfdev/types.h>
#include <bfdev/stddef.h>
#include <bfdev/string.h>
#include <bfdev/allocator.h>

BFDEV_BEGIN_DECLS
Expand Down Expand Up @@ -131,7 +132,7 @@ bfdev_array_data(const bfdev_array_t *array, unsigned long index)
/**
* bfdev_array_push() - push elements into the array.
* @array: the array object.
* @num: the data length to push.
* @num: the number of element to push.
*
* Creates a number of new elements on the array and
* returns a pointer to the first of these elements.
Expand All @@ -149,10 +150,23 @@ bfdev_array_pop(bfdev_array_t *array, unsigned long num);
extern void *
bfdev_array_peek(const bfdev_array_t *array, unsigned long num);

/**
* bfdev_array_append() - append memory into the array.
* @array: the array object.
* @data: the data to append.
* @num: the number of element to append.
*
* Return 0 on success or a negative error code on failure.
*/
extern int
bfdev_array_append(bfdev_array_t *array, const void *data, unsigned long num);

/**
* bfdev_array_resize() - directly set the number of elements in array.
* @array: the array object.
* @num: the number required resize.
*
* Return 0 on success or a negative error code on failure.
*/
extern int
bfdev_array_resize(bfdev_array_t *array, unsigned long num);
Expand All @@ -165,6 +179,8 @@ bfdev_array_resize(bfdev_array_t *array, unsigned long num);
* Ensure that the buffer has space allocated for at least
* @num bytes. If the current buffer is too small, it will
* be reallocated, possibly to a larger size than requested.
*
* Return 0 on success or a negative error code on failure.
*/
extern int
bfdev_array_reserve(bfdev_array_t *array, unsigned long num);
Expand Down
23 changes: 19 additions & 4 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ array_apply(bfdev_array_t *array, unsigned long count)
}

static inline void *
array_peek(const bfdev_array_t *array, unsigned long num,
unsigned long *indexp)
array_peek(const bfdev_array_t *array, unsigned long num, unsigned long *idxp)
{
unsigned long index;
uintptr_t offset;
Expand All @@ -65,8 +64,8 @@ array_peek(const bfdev_array_t *array, unsigned long num,
return NULL;

offset = bfdev_array_offset(array, index);
if (indexp)
*indexp = index;
if (idxp)
*idxp = index;

return array->data + offset;
}
Expand Down Expand Up @@ -106,6 +105,22 @@ bfdev_array_peek(const bfdev_array_t *array, unsigned long num)
return array_peek(array, num, NULL);
}

export int
bfdev_array_append(bfdev_array_t *array, const void *data, unsigned long num)
{
size_t size;
void *buff;

buff = bfdev_array_push(array, num);
if (bfdev_unlikely(!buff))
return -BFDEV_ENOMEM;

size = bfdev_array_offset(array, num);
bfport_memcpy(buff, data, size);

return -BFDEV_ENOERR;
}

export int
bfdev_array_resize(bfdev_array_t *array, unsigned long num)
{
Expand Down
Loading