Merge branch 'hotfix/open-fix'
[reservoir_sample] / reservoir.h
1 #ifndef RESERVOIR_H_MZSA8WJ3
2 #define RESERVOIR_H_MZSA8WJ3
3
4 #include <stdlib.h>
5
6 #include "buf.h"
7
8 #ifndef NDEBUG
9 #include "notify.h"
10 #endif /* NDEBUG */
11
12 /* A pool of buf_t */
13
14 typedef struct reservoir_ {
15 size_t reservoir_sz; /* allocated slots */
16 size_t reservoir_used; /* slots filled */
17 unsigned long reservoir_tally; /* total number of items remembered */
18 buf_t reservoir[];
19 } *reservoir_t;
20
21 #ifndef NDEBUG
22 #define D_RESERVOIR(__r__) do {\
23 size_t i;\
24 NOTIFY_DEBUG("reservoir:%p sz: %zu used:%zu tally:%zu", (__r__), (__r__)->reservoir_sz, (__r__)->reservoir_used, (__r__)->reservoir_tally);\
25 for (i = 0; i < (__r__)->reservoir_sz; i++) {\
26 D_BUF("reservoir[%zu] ", (__r__)->reservoir[i], i);\
27 }\
28 } while (0)
29 #else /* NDEBUG */
30 #define D_RESERVOIR(...)
31 #endif /* NDEBUG */
32
33 /* reservoir_new
34 Allocate and return a new reservoir capable of holding #sz bufs.
35 */
36 reservoir_t reservoir_new(size_t sz);
37
38 /* reservoir_grow
39 Increase the storage capacity of the reservoir pointed to by #preservoir
40 by #growby bufs.
41 */
42 int reservoir_grow(reservoir_t *preservoir, size_t growby);
43
44 /* reservoir_remember
45 Remember #buf, forgetting another buf at random if the reservoir
46 pointed to by #preservoir is already full to capacity.
47 */
48 void reservoir_remember(reservoir_t reservoir, buf_t buf);
49
50 /* reservoir_write
51 Write the contents of the bufs within #reservoir to #fd, each with a
52 trailing #delim.
53 */
54 int reservoir_write(int fd, reservoir_t reservoir, char delim);
55
56 /* reservoir_write_meta
57 Write metadata of #reservoir and #samples to fd.
58 */
59 int reservoir_write_meta(int fd, reservoir_t reservoir, unsigned long samples, char delim);
60
61 #endif /* RESERVOIR_H_MZSA8WJ3 */