]> git.ozlabs.org Git - ccan/blob - ccan/membuf/membuf.h
membuf: new module for linear memory buffers.
[ccan] / ccan / membuf / membuf.h
1 /* MIT (BSD) license - see LICENSE file for details */
2 #ifndef CCAN_MEMBUF_H
3 #define CCAN_MEMBUF_H
4 #include "config.h"
5 #include <assert.h>
6 #include <ccan/tcon/tcon.h>
7
8 /**
9  * struct membuf - representation of a memory buffer.
10  *
11  * It's exposed here to allow you to embed it and so we can inline the
12  * trivial functions.
13  */
14 struct membuf {
15         /* These are the cursors into buf elements */
16         size_t start;
17         size_t end;
18
19         /* Number of elements in buf */
20         size_t max_elems;
21         /* The buffer; at this low-level, untyped. */
22         char *elems;
23
24         void *(*expandfn)(struct membuf *, void *elems, size_t max_elems);
25 };
26
27 /**
28  * MEMBUF - declare a type-specific membuf
29  * @membertype: type for this buffer's values.
30  *
31  * You use this to create your own typed membuf.
32  *
33  * Example:
34  *      MEMBUF(int *) intp_membuf;
35  *      printf("Address of our int * membuf = %p\n", &intp_membuf);
36  */
37 #define MEMBUF(membertype)                                      \
38         TCON_WRAP(struct membuf, membertype canary)
39
40 /**
41  * membuf_init - initialize a type-specfic membuf.
42  * @mb: the MEMBUF() declared membuf.
43  * @elems: the initial buffer, if any.
44  * @max_elems: the initial space @elems, in number of elements.
45  * @expandfn: the function to enlarge buf (eg. membuf_realloc).
46  *
47  * Example:
48  *      membuf_init(&intp_membuf, NULL, 0, membuf_realloc);
49  */
50 #define membuf_init(mb, elems, num, expandfn)                           \
51         membuf_init_(tcon_unwrap(tcon_check_ptr((mb), canary, (elems))), \
52                      (elems), (num), tcon_sizeof((mb), canary), (expandfn))
53
54 void membuf_init_(struct membuf *mb,
55                   void *elems, size_t max_elems, size_t elemsize,
56                   void *(*expandfn)(struct membuf *, void *, size_t));
57
58 /**
59  * membuf_realloc - simple membuf helper to do realloc().
60  *
61  * Assumes initial buffer was NULL, or malloc().
62  */
63 void *membuf_realloc(struct membuf *mb, void *rawelems, size_t newsize);
64
65 /**
66  * membuf_num_elems - number of populated elements in the membuf.
67  * @mb: the MEMBUF() declared membuf.
68  */
69 #define membuf_num_elems(mb) membuf_num_elems_(tcon_unwrap(mb))
70
71 static inline size_t membuf_num_elems_(const struct membuf *mb)
72 {
73         return mb->end - mb->start;
74 }
75
76 /**
77  * membuf_elems - pointer to the populated elements in the membuf.
78  * @mb: the MEMBUF() declared membuf.
79  */
80 #define membuf_elems(mb)                                                \
81         tcon_cast_ptr(mb, canary,                                       \
82                       membuf_elems_(tcon_unwrap(mb), tcon_sizeof((mb), canary)))
83
84 static inline void *membuf_elems_(const struct membuf *mb, size_t elemsize)
85 {
86         return mb->elems + mb->start * elemsize;
87 }
88
89 /**
90  * membuf_consume - we've used up this many membuf_elems.
91  * @mb: the MEMBUF() declared membuf.
92  * @num: the number of elems.
93  *
94  * Returns a pointer to the old start of membuf, so you can mark consumed
95  * and actually process in a single call.
96  */
97 #define membuf_consume(mb, num)                                         \
98         tcon_cast_ptr(mb, canary,                                       \
99                       membuf_consume_(tcon_unwrap(mb), (num),           \
100                                       tcon_sizeof((mb), canary)))
101
102 static inline void *membuf_consume_(struct membuf *mb,
103                                     size_t num, size_t elemsize)
104 {
105         void *old_start = membuf_elems_(mb, elemsize);
106         assert(num <= membuf_num_elems_(mb));
107         mb->start += num;
108
109         return old_start;
110 }
111
112 /**
113  * membuf_num_space - number of unpopulated elements at end of the membuf.
114  * @mb: the MEMBUF() declared membuf.
115  */
116 #define membuf_num_space(mb) membuf_num_space_(tcon_unwrap(mb))
117
118 static inline size_t membuf_num_space_(const struct membuf *mb)
119 {
120         return mb->max_elems - mb->end;
121 }
122
123 /**
124  * membuf_space - pointer to the unpopulated elements at end of membuf.
125  * @mb: the MEMBUF() declared membuf.
126  */
127 #define membuf_space(mb)                                                \
128         tcon_cast_ptr(mb, canary,                                       \
129                       membuf_space_(tcon_unwrap(mb), tcon_sizeof((mb), canary)))
130
131 static inline void *membuf_space_(struct membuf *mb, size_t elemsize)
132 {
133         return mb->elems + mb->end * elemsize;
134 }
135
136 /**
137  * membuf_prepare_space - internal routine to make sure we've got space.
138  * @mb: the MEMBUF() declared membuf.
139  * @num_extra: the minimum number of elements of space we need
140  *
141  * Usually you wouldn't call this yourself; see membuf_add() below.  But
142  * you might use this if you need to know about moves within mb->elements
143  * so you can adjust your own pointers/offsets.
144  *
145  * It returns the offset *in bytes* between the old locations and the new.
146  * This is because it may not be a whole number of elements, in the case
147  * of realloc!
148  *
149  * If you want to check for expandfn failure (which sets errno to
150  * ENOMEM), you can check if membuf_num_space() is < num_extra which will
151  * never otherwise happen.
152  */
153 #define membuf_prepare_space(mb, num_extra)                     \
154         membuf_prepare_space_(tcon_unwrap(mb),                  \
155                               (num_extra),                      \
156                               tcon_sizeof((mb), canary))
157
158 size_t membuf_prepare_space_(struct membuf *mb,
159                              size_t num_extra, size_t elemsize);
160
161 /**
162  * membuf_add - add to the end of the membuf.
163  * @mb: the MEMBUF() declared membuf.
164  * @num: the number of elements (must be that much space available!).
165  *
166  * Returns the pointer to the space just added, in case you want to
167  * populate it afterwards.
168  *
169  * Note that this may invalidate existing buf pointers!  If you want to
170  * avoid that, call membuf_prepare_space(mb, num) first.
171  */
172 #define membuf_add(mb, num)                                             \
173         tcon_cast_ptr(mb, canary,                                       \
174                       membuf_add_(tcon_unwrap(mb), (num),               \
175                                   tcon_sizeof((mb), canary)))
176
177 static inline void *membuf_add_(struct membuf *mb, size_t num, size_t elemsize)
178 {
179         void *oldend;
180         membuf_prepare_space_(mb, num, elemsize);
181
182         oldend = membuf_space_(mb, elemsize);
183         /* We assume expandfn succeeded. */
184         assert(num <= membuf_num_space_(mb));
185         mb->end += num;
186
187         return oldend;
188 }
189
190 /**
191  * membuf_cleanup - reset membuf, return elems array for freeing.
192  * @mb: the MEMBUF() declared membuf.
193  *
194  * The mb will be empty after this, and crash if you try to expand it.
195  * You can membuf_init() it again, however.
196  *
197  * Example:
198  *      free(membuf_cleanup(&intp_membuf));
199  */
200 #define membuf_cleanup(mb) membuf_cleanup_(tcon_unwrap(mb))
201
202 static inline void *membuf_cleanup_(struct membuf *mb)
203 {
204         mb->start = mb->end = mb->max_elems = 0;
205         mb->expandfn = NULL;
206
207         return mb->elems;
208 }
209 #endif /* CCAN_MEMBUF_H */