@@ -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
3131typedef 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)
97118static inline bfdev_size_t
98119bfdev_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)
125145static inline void *
126146bfdev_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);
152198extern void *
153199bfdev_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.
0 commit comments