Skip to content

Commit deca9ad

Browse files
authored
Merge pull request #680 from sanpeqf/feat-array
Feat array
2 parents 5cfa669 + 62b4830 commit deca9ad

5 files changed

Lines changed: 162 additions & 41 deletions

File tree

include/bfdev/array.h

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ BFDEV_BEGIN_DECLS
2424
* is typically useful when buffering I/O or processing data.
2525
*/
2626

27-
#ifndef BFDEV_ARRAY_MSIZE
28-
# define BFDEV_ARRAY_MSIZE 32
27+
#ifndef BFDEV_ARRAY_MINSIZE
28+
# define BFDEV_ARRAY_MINSIZE 32
2929
#endif
3030

3131
typedef struct bfdev_array bfdev_array_t;
@@ -34,6 +34,7 @@ struct bfdev_array {
3434
const bfdev_alloc_t *alloc;
3535
unsigned long capacity;
3636
unsigned long index;
37+
unsigned long seek;
3738
bfdev_size_t cells;
3839
void *data;
3940
};
@@ -87,6 +88,26 @@ bfdev_array_index(const bfdev_array_t *array)
8788
return array->index;
8889
}
8990

91+
static inline unsigned long
92+
bfdev_array_tell(const bfdev_array_t *array)
93+
{
94+
return array->seek;
95+
}
96+
97+
/**
98+
* bfdev_array_offset() - get elements offset in array.
99+
* @array: the array object.
100+
* @index: elements index.
101+
*
102+
* Return the address offset of the object indexed
103+
* by @index in the array.
104+
*/
105+
static inline bfdev_uintptr_t
106+
bfdev_array_offset(const bfdev_array_t *array, unsigned long index)
107+
{
108+
return array->cells * index;
109+
}
110+
90111
/**
91112
* bfdev_array_size() - get total size in array.
92113
* @array: the array object.
@@ -97,21 +118,20 @@ bfdev_array_index(const bfdev_array_t *array)
97118
static inline bfdev_size_t
98119
bfdev_array_size(const bfdev_array_t *array)
99120
{
100-
return array->cells * array->index;
121+
return bfdev_array_offset(array, array->index);
101122
}
102123

103124
/**
104-
* bfdev_array_offset() - get elements offset in array.
125+
* bfdev_array_remain() - get remain size in array.
105126
* @array: the array object.
106-
* @index: elements index.
107127
*
108-
* Return the address offset of the object indexed
109-
* by @index in the array.
128+
* Returns the remain size of elements stored in
129+
* the array container.
110130
*/
111-
static inline bfdev_uintptr_t
112-
bfdev_array_offset(const bfdev_array_t *array, unsigned long index)
131+
static inline bfdev_size_t
132+
bfdev_array_remain(const bfdev_array_t *array)
113133
{
114-
return array->cells * index;
134+
return bfdev_array_offset(array, array->index - array->seek);
115135
}
116136

117137
/**
@@ -125,10 +145,36 @@ bfdev_array_offset(const bfdev_array_t *array, unsigned long index)
125145
static inline void *
126146
bfdev_array_data(const bfdev_array_t *array, unsigned long index)
127147
{
128-
if (bfdev_unlikely(index >= array->index))
148+
void *data;
149+
150+
if (bfdev_unlikely(array->seek + index >= array->index))
129151
return BFDEV_NULL;
130152

131-
return array->data + bfdev_array_offset(array, index);
153+
data = array->data + bfdev_array_offset(array, array->seek);
154+
data += bfdev_array_offset(array, index);
155+
156+
return data;
157+
}
158+
159+
/**
160+
* bfdev_array_seek() - seek elements in the array.
161+
* @array: the array object.
162+
* @seek: the number of element to seek.
163+
*
164+
* Set the current position of the array to @seek.
165+
* The next call to bfdev_array_data() will start
166+
* from this position.
167+
*
168+
* Return 0 on success or a negative error code on failure.
169+
*/
170+
static inline int
171+
bfdev_array_seek(bfdev_array_t *array, unsigned long seek)
172+
{
173+
if (bfdev_unlikely(seek > array->index))
174+
return -BFDEV_EOVERFLOW;
175+
array->seek = seek;
176+
177+
return -BFDEV_ENOERR;
132178
}
133179

134180
/**
@@ -152,17 +198,6 @@ bfdev_array_pop(bfdev_array_t *array, unsigned long num);
152198
extern void *
153199
bfdev_array_peek(const bfdev_array_t *array, unsigned long num);
154200

155-
/**
156-
* bfdev_array_append() - append elements into the array.
157-
* @array: the array object.
158-
* @data: the elements to append.
159-
* @num: the number of element to append.
160-
*
161-
* Return 0 on success or a negative error code on failure.
162-
*/
163-
extern int
164-
bfdev_array_append(bfdev_array_t *array, const void *data, unsigned long num);
165-
166201
/**
167202
* bfdev_array_remove() - remove elements from the array.
168203
* @array: the array object.

include/bfdev/template/array.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* SPDX-License-Identifier: LGPL-3.0-or-later */
2+
/*
3+
* Copyright(c) 2025 John Sanpe <sanpeqf@gmail.com>
4+
*/
5+
6+
#ifndef _BFDEV_TEMPLATE_ARRAY_H_
7+
#define _BFDEV_TEMPLATE_ARRAY_H_
8+
9+
#include <bfdev/config.h>
10+
#include <bfdev/array.h>
11+
12+
BFDEV_BEGIN_DECLS
13+
14+
/**
15+
* bfdev_array_append() - append elements into the array.
16+
* @array: the array object.
17+
* @data: the elements to append.
18+
* @num: the number of element to append.
19+
*
20+
* Return 0 on success or a negative error code on failure.
21+
*/
22+
extern int
23+
bfdev_array_append(bfdev_array_t *array, const void *data, unsigned long num);
24+
25+
/**
26+
* bfdev_array_append_array() - append array into the array.
27+
* @array: the array object.
28+
* @append: the array to append.
29+
*
30+
* Return 0 on success or a negative error code on failure.
31+
*/
32+
extern int
33+
bfdev_array_append_array(bfdev_array_t *array, const bfdev_array_t *append);
34+
35+
/**
36+
* bfdev_array_append_cstr() - append cstr into the array.
37+
* @array: the array object.
38+
* @append: the cstr to append.
39+
*
40+
* Return 0 on success or a negative error code on failure.
41+
*/
42+
extern int
43+
bfdev_array_append_cstr(bfdev_array_t *array, const char *append);
44+
45+
BFDEV_END_DECLS
46+
47+
#endif /* _BFDEV_TEMPLATE_ARRAY_H_ */

src/array.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ array_reqsize(bfdev_array_t *array, unsigned long count)
1515
unsigned long request;
1616
bfdev_size_t size;
1717

18-
request = bfdev_max(BFDEV_ARRAY_MSIZE, count);
18+
request = bfdev_max(BFDEV_ARRAY_MINSIZE, count);
1919
size = bfdev_pow2_roundup(request * array->cells);
2020

2121
return size;
@@ -104,23 +104,6 @@ bfdev_array_peek(const bfdev_array_t *array, unsigned long num)
104104
{
105105
return array_peek(array, num, BFDEV_NULL);
106106
}
107-
108-
export int
109-
bfdev_array_append(bfdev_array_t *array, const void *data, unsigned long num)
110-
{
111-
bfdev_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-
bfdev_memcpy(buff, data, size);
120-
121-
return -BFDEV_ENOERR;
122-
}
123-
124107
export int
125108
bfdev_array_remove(bfdev_array_t *array, unsigned long index, unsigned long num)
126109
{

template/array.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* SPDX-License-Identifier: LGPL-3.0-or-later */
2+
/*
3+
* Copyright(c) 2023 ffashion <helloworldffashion@gmail.com>
4+
* Copyright(c) 2023 John Sanpe <sanpeqf@gmail.com>
5+
*/
6+
7+
#include <base.h>
8+
#include <bfdev/template/array.h>
9+
#include <export.h>
10+
11+
export int
12+
bfdev_array_append(bfdev_array_t *array, const void *data, unsigned long num)
13+
{
14+
bfdev_size_t size;
15+
void *buff;
16+
17+
buff = bfdev_array_push(array, num);
18+
if (bfdev_unlikely(!buff))
19+
return -BFDEV_ENOMEM;
20+
21+
size = bfdev_array_offset(array, num);
22+
bfdev_memcpy(buff, data, size);
23+
24+
return -BFDEV_ENOERR;
25+
}
26+
27+
export int
28+
bfdev_array_append_array(bfdev_array_t *array, const bfdev_array_t *append)
29+
{
30+
int retval;
31+
32+
if (array->cells != append->cells)
33+
return -BFDEV_EPROTO;
34+
35+
retval = bfdev_array_append(array, append->data, bfdev_array_size(append));
36+
if (bfdev_unlikely(retval))
37+
return retval;
38+
39+
return -BFDEV_ENOERR;
40+
}
41+
42+
export int
43+
bfdev_array_append_cstr(bfdev_array_t *array, const char *append)
44+
{
45+
int retval;
46+
47+
if (array->cells != sizeof(*append))
48+
return -BFDEV_EPROTO;
49+
50+
retval = bfdev_array_append(array, append, bfdev_strlen(append));
51+
if (bfdev_unlikely(retval))
52+
return retval;
53+
54+
return -BFDEV_ENOERR;
55+
}

template/build.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55

66
set(BFDEV_SOURCE
77
${BFDEV_SOURCE}
8+
${CMAKE_CURRENT_LIST_DIR}/array.c
89
${CMAKE_CURRENT_LIST_DIR}/btree.c
910
)

0 commit comments

Comments
 (0)