|
| 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_ */ |
0 commit comments