From a65cc26d607bc43ea25b6fb6731e0c3c97f0917f Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 1 May 2025 20:08:52 +0800 Subject: [PATCH 1/2] feat array: added seek api Signed-off-by: John Sanpe --- include/bfdev/array.h | 66 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/include/bfdev/array.h b/include/bfdev/array.h index 38d74f55..bef83e98 100644 --- a/include/bfdev/array.h +++ b/include/bfdev/array.h @@ -34,6 +34,7 @@ struct bfdev_array { const bfdev_alloc_t *alloc; unsigned long capacity; unsigned long index; + unsigned long seek; bfdev_size_t cells; void *data; }; @@ -87,6 +88,26 @@ bfdev_array_index(const bfdev_array_t *array) return array->index; } +static inline unsigned long +bfdev_array_tell(const bfdev_array_t *array) +{ + return array->seek; +} + +/** + * bfdev_array_offset() - get elements offset in array. + * @array: the array object. + * @index: elements index. + * + * Return the address offset of the object indexed + * by @index in the array. + */ +static inline bfdev_uintptr_t +bfdev_array_offset(const bfdev_array_t *array, unsigned long index) +{ + return array->cells * index; +} + /** * bfdev_array_size() - get total size in array. * @array: the array object. @@ -97,21 +118,20 @@ bfdev_array_index(const bfdev_array_t *array) static inline bfdev_size_t bfdev_array_size(const bfdev_array_t *array) { - return array->cells * array->index; + return bfdev_array_offset(array, array->index); } /** - * bfdev_array_offset() - get elements offset in array. + * bfdev_array_remain() - get remain size in array. * @array: the array object. - * @index: elements index. * - * Return the address offset of the object indexed - * by @index in the array. + * Returns the remain size of elements stored in + * the array container. */ -static inline bfdev_uintptr_t -bfdev_array_offset(const bfdev_array_t *array, unsigned long index) +static inline bfdev_size_t +bfdev_array_remain(const bfdev_array_t *array) { - return array->cells * index; + return bfdev_array_offset(array, array->index - array->seek); } /** @@ -125,10 +145,36 @@ bfdev_array_offset(const bfdev_array_t *array, unsigned long index) static inline void * bfdev_array_data(const bfdev_array_t *array, unsigned long index) { - if (bfdev_unlikely(index >= array->index)) + void *data; + + if (bfdev_unlikely(array->seek + index >= array->index)) return BFDEV_NULL; - return array->data + bfdev_array_offset(array, index); + data = array->data + bfdev_array_offset(array, array->seek); + data += bfdev_array_offset(array, index); + + return data; +} + +/** + * bfdev_array_seek() - seek elements in the array. + * @array: the array object. + * @seek: the number of element to seek. + * + * Set the current position of the array to @seek. + * The next call to bfdev_array_data() will start + * from this position. + * + * Return 0 on success or a negative error code on failure. + */ +static inline int +bfdev_array_seek(bfdev_array_t *array, unsigned long seek) +{ + if (bfdev_unlikely(seek > array->index)) + return -BFDEV_EOVERFLOW; + array->seek = seek; + + return -BFDEV_ENOERR; } /** From 62b483083435396699adb76d2875c637babaa604 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 1 May 2025 20:24:03 +0800 Subject: [PATCH 2/2] refactor array: move append apis to template Signed-off-by: John Sanpe --- include/bfdev/array.h | 15 ++-------- include/bfdev/template/array.h | 47 +++++++++++++++++++++++++++++ src/array.c | 19 +----------- template/array.c | 55 ++++++++++++++++++++++++++++++++++ template/build.cmake | 1 + 5 files changed, 106 insertions(+), 31 deletions(-) create mode 100644 include/bfdev/template/array.h create mode 100644 template/array.c diff --git a/include/bfdev/array.h b/include/bfdev/array.h index bef83e98..d2395ec0 100644 --- a/include/bfdev/array.h +++ b/include/bfdev/array.h @@ -24,8 +24,8 @@ BFDEV_BEGIN_DECLS * is typically useful when buffering I/O or processing data. */ -#ifndef BFDEV_ARRAY_MSIZE -# define BFDEV_ARRAY_MSIZE 32 +#ifndef BFDEV_ARRAY_MINSIZE +# define BFDEV_ARRAY_MINSIZE 32 #endif typedef struct bfdev_array bfdev_array_t; @@ -198,17 +198,6 @@ 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 elements into the array. - * @array: the array object. - * @data: the elements 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_remove() - remove elements from the array. * @array: the array object. diff --git a/include/bfdev/template/array.h b/include/bfdev/template/array.h new file mode 100644 index 00000000..96e5537c --- /dev/null +++ b/include/bfdev/template/array.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * Copyright(c) 2025 John Sanpe + */ + +#ifndef _BFDEV_TEMPLATE_ARRAY_H_ +#define _BFDEV_TEMPLATE_ARRAY_H_ + +#include +#include + +BFDEV_BEGIN_DECLS + +/** + * bfdev_array_append() - append elements into the array. + * @array: the array object. + * @data: the elements 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_append_array() - append array into the array. + * @array: the array object. + * @append: the array to append. + * + * Return 0 on success or a negative error code on failure. + */ +extern int +bfdev_array_append_array(bfdev_array_t *array, const bfdev_array_t *append); + +/** + * bfdev_array_append_cstr() - append cstr into the array. + * @array: the array object. + * @append: the cstr to append. + * + * Return 0 on success or a negative error code on failure. + */ +extern int +bfdev_array_append_cstr(bfdev_array_t *array, const char *append); + +BFDEV_END_DECLS + +#endif /* _BFDEV_TEMPLATE_ARRAY_H_ */ diff --git a/src/array.c b/src/array.c index 3ce50de9..d46a6c99 100644 --- a/src/array.c +++ b/src/array.c @@ -15,7 +15,7 @@ array_reqsize(bfdev_array_t *array, unsigned long count) unsigned long request; bfdev_size_t size; - request = bfdev_max(BFDEV_ARRAY_MSIZE, count); + request = bfdev_max(BFDEV_ARRAY_MINSIZE, count); size = bfdev_pow2_roundup(request * array->cells); return size; @@ -104,23 +104,6 @@ bfdev_array_peek(const bfdev_array_t *array, unsigned long num) { return array_peek(array, num, BFDEV_NULL); } - -export int -bfdev_array_append(bfdev_array_t *array, const void *data, unsigned long num) -{ - bfdev_size_t size; - void *buff; - - buff = bfdev_array_push(array, num); - if (bfdev_unlikely(!buff)) - return -BFDEV_ENOMEM; - - size = bfdev_array_offset(array, num); - bfdev_memcpy(buff, data, size); - - return -BFDEV_ENOERR; -} - export int bfdev_array_remove(bfdev_array_t *array, unsigned long index, unsigned long num) { diff --git a/template/array.c b/template/array.c new file mode 100644 index 00000000..742fc0b0 --- /dev/null +++ b/template/array.c @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * Copyright(c) 2023 ffashion + * Copyright(c) 2023 John Sanpe + */ + +#include +#include +#include + +export int +bfdev_array_append(bfdev_array_t *array, const void *data, unsigned long num) +{ + bfdev_size_t size; + void *buff; + + buff = bfdev_array_push(array, num); + if (bfdev_unlikely(!buff)) + return -BFDEV_ENOMEM; + + size = bfdev_array_offset(array, num); + bfdev_memcpy(buff, data, size); + + return -BFDEV_ENOERR; +} + +export int +bfdev_array_append_array(bfdev_array_t *array, const bfdev_array_t *append) +{ + int retval; + + if (array->cells != append->cells) + return -BFDEV_EPROTO; + + retval = bfdev_array_append(array, append->data, bfdev_array_size(append)); + if (bfdev_unlikely(retval)) + return retval; + + return -BFDEV_ENOERR; +} + +export int +bfdev_array_append_cstr(bfdev_array_t *array, const char *append) +{ + int retval; + + if (array->cells != sizeof(*append)) + return -BFDEV_EPROTO; + + retval = bfdev_array_append(array, append, bfdev_strlen(append)); + if (bfdev_unlikely(retval)) + return retval; + + return -BFDEV_ENOERR; +} diff --git a/template/build.cmake b/template/build.cmake index c56570d3..2cc81b4c 100644 --- a/template/build.cmake +++ b/template/build.cmake @@ -5,5 +5,6 @@ set(BFDEV_SOURCE ${BFDEV_SOURCE} + ${CMAKE_CURRENT_LIST_DIR}/array.c ${CMAKE_CURRENT_LIST_DIR}/btree.c )