Skip to content

Commit f2f6b52

Browse files
authored
Merge pull request #683 from sanpeqf/feat-buff
Feat buff
2 parents deca9ad + 68a3eb5 commit f2f6b52

File tree

7 files changed

+130
-0
lines changed

7 files changed

+130
-0
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_subdirectory(base64)
1313
add_subdirectory(bfdev)
1414
add_subdirectory(bloom)
1515
add_subdirectory(btree)
16+
add_subdirectory(buff)
1617
add_subdirectory(cache)
1718
add_subdirectory(circle)
1819
add_subdirectory(crc)

examples/buff/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-License-Identifier: GPL-2.0-or-later
2+
/buff-string

examples/buff/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-License-Identifier: GPL-2.0-or-later
2+
#
3+
# Copyright(c) 2025 ffashion <helloworldffashion@gmail.com>
4+
#
5+
6+
add_executable(buff-string string.c)
7+
target_link_libraries(buff-string bfdev)
8+
add_test(buff-string buff-string)
9+
10+
if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev")
11+
install(FILES
12+
string.c
13+
DESTINATION
14+
${CMAKE_INSTALL_DOCDIR}/examples/buff
15+
)
16+
17+
install(TARGETS
18+
buff-string
19+
DESTINATION
20+
${CMAKE_INSTALL_DOCDIR}/bin
21+
)
22+
endif()

examples/buff/string.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* Copyright(c) 2025 John Sanpe <sanpeqf@gmail.com>
4+
*/
5+
6+
#define MODULE_NAME "string-simple"
7+
#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt
8+
9+
#include <stdio.h>
10+
#include <bfdev/buff.h>
11+
12+
int
13+
main(int argc, const char *argv[])
14+
{
15+
BFDEV_DEFINE_STRING(test, "Hello World!\n");
16+
fwrite(test.data, test.len, 1, stdout);
17+
return 0;
18+
}

include/bfdev/buff.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* SPDX-License-Identifier: LGPL-3.0-or-later */
2+
/*
3+
* Copyright(c) 2024 John Sanpe <sanpeqf@gmail.com>
4+
*/
5+
6+
#ifndef _BFDEV_BUFF_H_
7+
#define _BFDEV_BUFF_H_
8+
9+
#include <bfdev/config.h>
10+
#include <bfdev/types.h>
11+
#include <bfdev/string.h>
12+
13+
BFDEV_BEGIN_DECLS
14+
15+
typedef struct bfdev_buff bfdev_buff_t;
16+
17+
struct bfdev_buff {
18+
bfdev_size_t len;
19+
void *data;
20+
};
21+
22+
#define BFDEV_BUFF_STATIC(LEN, DATA) { \
23+
.len = (LEN), .data = (void *)(DATA), \
24+
}
25+
26+
#define BFDEV_BUFF_INIT(len, data) \
27+
(bfdev_buff_t) BFDEV_BUFF_STATIC(len, data)
28+
29+
#define BFDEV_DEFINE_BUFF(name, len, data) \
30+
bfdev_buff_t name = BFDEV_BUFF_INIT(len, data)
31+
32+
#define BFDEV_DEFINE_STRING(name, str) \
33+
BFDEV_DEFINE_BUFF(name, sizeof(str) - 1, str)
34+
35+
static inline void
36+
bfdev_buff_init(bfdev_buff_t *buff, bfdev_size_t len, void *data)
37+
{
38+
*buff = BFDEV_BUFF_INIT(len, data);
39+
}
40+
41+
static inline void
42+
bfdev_string_init(bfdev_buff_t *buff, const char *str)
43+
{
44+
*buff = BFDEV_BUFF_INIT(bfdev_strlen(str), str);
45+
}
46+
47+
static inline void *
48+
bfdev_buff_data(bfdev_buff_t *buff)
49+
{
50+
return buff->data;
51+
}
52+
53+
static inline bfdev_size_t
54+
bfdev_buff_len(bfdev_buff_t *buff)
55+
{
56+
return buff->len;
57+
}
58+
59+
BFDEV_END_DECLS
60+
61+
#endif /* _BFDEV_BUFF_H_ */

include/bfdev/template/array.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <bfdev/config.h>
1010
#include <bfdev/array.h>
11+
#include <bfdev/buff.h>
1112

1213
BFDEV_BEGIN_DECLS
1314

@@ -32,6 +33,16 @@ bfdev_array_append(bfdev_array_t *array, const void *data, unsigned long num);
3233
extern int
3334
bfdev_array_append_array(bfdev_array_t *array, const bfdev_array_t *append);
3435

36+
/**
37+
* bfdev_array_append_buff() - append buff into the array.
38+
* @array: the array object.
39+
* @append: the buff to append.
40+
*
41+
* Return 0 on success or a negative error code on failure.
42+
*/
43+
extern int
44+
bfdev_array_append_buff(bfdev_array_t *array, bfdev_buff_t *append);
45+
3546
/**
3647
* bfdev_array_append_cstr() - append cstr into the array.
3748
* @array: the array object.

template/array.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ bfdev_array_append_array(bfdev_array_t *array, const bfdev_array_t *append)
3939
return -BFDEV_ENOERR;
4040
}
4141

42+
export int
43+
bfdev_array_append_buff(bfdev_array_t *array, bfdev_buff_t *append)
44+
{
45+
int retval;
46+
47+
if (array->cells != BFDEV_BYTES_PER_U8)
48+
return -BFDEV_EPROTO;
49+
50+
retval = bfdev_array_append(array, append->data, append->len);
51+
if (bfdev_unlikely(retval))
52+
return retval;
53+
54+
return -BFDEV_ENOERR;
55+
}
56+
4257
export int
4358
bfdev_array_append_cstr(bfdev_array_t *array, const char *append)
4459
{

0 commit comments

Comments
 (0)