1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Stream/packet buffer API
3 * Copyright (c) 2014-2015 Timo Teräs
12 #include <sys/types.h>
16 PREDECL_DLIST(zbuf_queue
);
19 struct zbuf_queue_item queue_entry
;
20 unsigned allocated
: 1;
26 DECLARE_DLIST(zbuf_queue
, struct zbuf
, queue_entry
);
29 struct zbuf_queue_head queue_head
;
32 struct zbuf
*zbuf_alloc(size_t size
);
33 void zbuf_init(struct zbuf
*zb
, void *buf
, size_t len
, size_t datalen
);
34 void zbuf_free(struct zbuf
*zb
);
36 static inline size_t zbuf_size(struct zbuf
*zb
)
38 return zb
->end
- zb
->buf
;
41 static inline size_t zbuf_used(struct zbuf
*zb
)
43 return zb
->tail
- zb
->head
;
46 static inline size_t zbuf_tailroom(struct zbuf
*zb
)
48 return zb
->end
- zb
->tail
;
51 static inline size_t zbuf_headroom(struct zbuf
*zb
)
53 return zb
->head
- zb
->buf
;
56 void zbuf_reset(struct zbuf
*zb
);
57 void zbuf_reset_head(struct zbuf
*zb
, void *ptr
);
58 ssize_t
zbuf_read(struct zbuf
*zb
, int fd
, size_t maxlen
);
59 ssize_t
zbuf_write(struct zbuf
*zb
, int fd
);
60 ssize_t
zbuf_recv(struct zbuf
*zb
, int fd
);
61 ssize_t
zbuf_send(struct zbuf
*zb
, int fd
);
63 static inline void zbuf_set_rerror(struct zbuf
*zb
)
69 static inline void zbuf_set_werror(struct zbuf
*zb
)
75 static inline void *__zbuf_pull(struct zbuf
*zb
, size_t size
, int error
)
77 void *head
= zb
->head
;
78 if (size
> zbuf_used(zb
)) {
87 #define zbuf_pull(zb, type) ((type *)__zbuf_pull(zb, sizeof(type), 1))
88 #define zbuf_pulln(zb, sz) (__zbuf_pull(zb, sz, 1))
89 #define zbuf_may_pull(zb, type) ((type *)__zbuf_pull(zb, sizeof(type), 0))
90 #define zbuf_may_pulln(zb, sz) (__zbuf_pull(zb, sz, 0))
92 void *zbuf_may_pull_until(struct zbuf
*zb
, const char *sep
, struct zbuf
*msg
);
94 static inline void zbuf_get(struct zbuf
*zb
, void *dst
, size_t len
)
96 void *src
= zbuf_pulln(zb
, len
);
98 memcpy(dst
, src
, len
);
101 static inline uint8_t zbuf_get8(struct zbuf
*zb
)
103 uint8_t *src
= zbuf_pull(zb
, uint8_t);
109 static inline uint32_t zbuf_get32(struct zbuf
*zb
)
113 } __attribute__((packed
));
115 struct unaligned32
*v
= zbuf_pull(zb
, struct unaligned32
);
121 static inline uint16_t zbuf_get_be16(struct zbuf
*zb
)
125 } __attribute__((packed
));
127 struct unaligned16
*v
= zbuf_pull(zb
, struct unaligned16
);
129 return be16toh(v
->value
);
133 static inline uint32_t zbuf_get_be32(struct zbuf
*zb
)
135 return be32toh(zbuf_get32(zb
));
138 static inline void *__zbuf_push(struct zbuf
*zb
, size_t size
, int error
)
140 void *tail
= zb
->tail
;
141 if (size
> zbuf_tailroom(zb
)) {
150 #define zbuf_push(zb, type) ((type *)__zbuf_push(zb, sizeof(type), 1))
151 #define zbuf_pushn(zb, sz) (__zbuf_push(zb, sz, 1))
152 #define zbuf_may_push(zb, type) ((type *)__zbuf_may_push(zb, sizeof(type), 0))
153 #define zbuf_may_pushn(zb, sz) (__zbuf_push(zb, sz, 0))
155 static inline void zbuf_put(struct zbuf
*zb
, const void *src
, size_t len
)
157 void *dst
= zbuf_pushn(zb
, len
);
159 memcpy(dst
, src
, len
);
162 static inline void zbuf_put8(struct zbuf
*zb
, uint8_t val
)
164 uint8_t *dst
= zbuf_push(zb
, uint8_t);
169 static inline void zbuf_put_be16(struct zbuf
*zb
, uint16_t val
)
173 } __attribute__((packed
));
175 struct unaligned16
*v
= zbuf_push(zb
, struct unaligned16
);
177 v
->value
= htobe16(val
);
180 static inline void zbuf_put_be32(struct zbuf
*zb
, uint32_t val
)
184 } __attribute__((packed
));
186 struct unaligned32
*v
= zbuf_push(zb
, struct unaligned32
);
188 v
->value
= htobe32(val
);
191 void zbuf_copy(struct zbuf
*zb
, struct zbuf
*src
, size_t len
);
192 void zbuf_copy_peek(struct zbuf
*zdst
, struct zbuf
*zsrc
, size_t len
);
194 void zbufq_init(struct zbuf_queue
*);
195 void zbufq_reset(struct zbuf_queue
*);
196 void zbufq_queue(struct zbuf_queue
*, struct zbuf
*);
197 int zbufq_write(struct zbuf_queue
*, int);