Skip to content

Commit fe1644e

Browse files
authored
Merge pull request #663 from sanpeqf/feat-array
feat array: added append apis
2 parents 0769b09 + 3a0269c commit fe1644e

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

include/bfdev/array.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <bfdev/config.h>
1111
#include <bfdev/types.h>
1212
#include <bfdev/stddef.h>
13+
#include <bfdev/string.h>
1314
#include <bfdev/allocator.h>
1415

1516
BFDEV_BEGIN_DECLS
@@ -131,7 +132,7 @@ bfdev_array_data(const bfdev_array_t *array, unsigned long index)
131132
/**
132133
* bfdev_array_push() - push elements into the array.
133134
* @array: the array object.
134-
* @num: the data length to push.
135+
* @num: the number of element to push.
135136
*
136137
* Creates a number of new elements on the array and
137138
* returns a pointer to the first of these elements.
@@ -149,10 +150,23 @@ bfdev_array_pop(bfdev_array_t *array, unsigned long num);
149150
extern void *
150151
bfdev_array_peek(const bfdev_array_t *array, unsigned long num);
151152

153+
/**
154+
* bfdev_array_append() - append memory into the array.
155+
* @array: the array object.
156+
* @data: the data to append.
157+
* @num: the number of element to append.
158+
*
159+
* Return 0 on success or a negative error code on failure.
160+
*/
161+
extern int
162+
bfdev_array_append(bfdev_array_t *array, const void *data, unsigned long num);
163+
152164
/**
153165
* bfdev_array_resize() - directly set the number of elements in array.
154166
* @array: the array object.
155167
* @num: the number required resize.
168+
*
169+
* Return 0 on success or a negative error code on failure.
156170
*/
157171
extern int
158172
bfdev_array_resize(bfdev_array_t *array, unsigned long num);
@@ -165,6 +179,8 @@ bfdev_array_resize(bfdev_array_t *array, unsigned long num);
165179
* Ensure that the buffer has space allocated for at least
166180
* @num bytes. If the current buffer is too small, it will
167181
* be reallocated, possibly to a larger size than requested.
182+
*
183+
* Return 0 on success or a negative error code on failure.
168184
*/
169185
extern int
170186
bfdev_array_reserve(bfdev_array_t *array, unsigned long num);

src/array.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ array_apply(bfdev_array_t *array, unsigned long count)
5353
}
5454

5555
static inline void *
56-
array_peek(const bfdev_array_t *array, unsigned long num,
57-
unsigned long *indexp)
56+
array_peek(const bfdev_array_t *array, unsigned long num, unsigned long *idxp)
5857
{
5958
unsigned long index;
6059
uintptr_t offset;
@@ -65,8 +64,8 @@ array_peek(const bfdev_array_t *array, unsigned long num,
6564
return NULL;
6665

6766
offset = bfdev_array_offset(array, index);
68-
if (indexp)
69-
*indexp = index;
67+
if (idxp)
68+
*idxp = index;
7069

7170
return array->data + offset;
7271
}
@@ -106,6 +105,22 @@ bfdev_array_peek(const bfdev_array_t *array, unsigned long num)
106105
return array_peek(array, num, NULL);
107106
}
108107

108+
export int
109+
bfdev_array_append(bfdev_array_t *array, const void *data, unsigned long num)
110+
{
111+
size_t size;
112+
void *buff;
113+
114+
buff = bfdev_array_push(array, num);
115+
if (bfdev_unlikely(!buff))
116+
return -BFDEV_ENOMEM;
117+
118+
size = bfdev_array_offset(array, num);
119+
bfport_memcpy(buff, data, size);
120+
121+
return -BFDEV_ENOERR;
122+
}
123+
109124
export int
110125
bfdev_array_resize(bfdev_array_t *array, unsigned long num)
111126
{

0 commit comments

Comments
 (0)