Bumped version to 1.3
[reservoir_sample] / buf.h
1 #ifndef BUF_H_B87OZOFU
2 #define BUF_H_B87OZOFU
3
4 #include <stdlib.h>
5
6 #ifndef NDEBUG
7 #include "notify.h"
8 #endif /* NDEBUG */
9
10 /* A simple sliding-window byte buffer. */
11
12 typedef struct buf_ {
13 size_t buf_sz;
14 size_t buf_start;
15 size_t buf_used;
16 unsigned char buf[];
17 } *buf_t;
18 #define BUF_ROOM(__b__) ( (__b__)->buf_sz - ( (__b__)->buf_start + (__b__)->buf_used ) )
19
20 #ifndef NDEBUG
21 /* For debugging, dump a buffer. */
22 #define D_BUF(__pre__,__b__,...) do {\
23 if ( (__b__) == NULL )\
24 NOTIFY_DEBUG(__pre__ "buf:%p", ## __VA_ARGS__, (__b__));\
25 else {\
26 NOTIFY_DEBUG(__pre__ "buf:%p sz:%zu start:%zu used:%zu free:%zu '%.*s'",\
27 ## __VA_ARGS__,\
28 (__b__),\
29 (__b__)->buf_sz,\
30 (__b__)->buf_start,\
31 (__b__)->buf_used,\
32 BUF_ROOM((__b__)),\
33 (int)(__b__)->buf_used, (__b__)->buf + (__b__)->buf_start);\
34 assert( (__b__)->buf_sz >= (__b__)->buf_start + (__b__)->buf_used );\
35 }\
36 } while (0)
37 #else /* NDEBUG */
38 #define D_BUF(...)
39 #endif /* NDEBUG */
40
41 /* buf_new
42 Allocate and return a new buf_t capable of holding #sz bytes.
43 */
44 buf_t buf_new(size_t sz);
45
46 /* buf_del
47 Deallocate the buf_t pointed to by #pbuf.
48 */
49 void buf_del(buf_t *pbuf);
50
51 /* buf_rebase
52 Reclaim any free space from the start of #buf, preserving active content.
53 */
54 void buf_rebase(buf_t buf);
55
56 /* buf_makeroom
57 Assures that the buf pointed to by #pbuf has at space to hold #roomfor
58 more bytes.
59 */
60 int buf_makeroom(buf_t *pbuf, size_t roomfor);
61
62 /* buf_range_dup_or_append
63 Starting at the #src_offset byte of #src, appends the following #n
64 bytes to the buffer pointed to by #dstp, which will be re/allocated if
65 needed.
66 */
67 int buf_range_dup_or_append(buf_t src, size_t src_offset, size_t n, buf_t *pdst);
68
69 /* buf_flense
70 Starting after #src_offset characters, scan through the buffer pointed
71 to by #psrc, stopping at the first byte matching #delimiter, whereupon, if
72 #pdst is not NULL, all the bytes previous to #delimiter are appended onto
73 the buffer pointed to by *#pdst. The buffer pointed to by #psrc is then
74 trimmed to only contain the bytes following #delimiter. The delimiter byte
75 is discarded.
76 Returns the number of characters flensed from #src.
77 */
78 ssize_t buf_flense(buf_t *psrc, size_t src_offset, int delimiter, buf_t *pdst);
79
80 #endif /* BUF_H_B87OZOFU */