Lely core libraries  2.3.4
membuf.h
Go to the documentation of this file.
1 
22 #ifndef LELY_UTIL_MEMBUF_H_
23 #define LELY_UTIL_MEMBUF_H_
24 
25 #include <lely/util/util.h>
26 
27 #include <assert.h>
28 #include <stddef.h>
29 #include <string.h>
30 
31 #ifndef LELY_UTIL_MEMBUF_INLINE
32 #define LELY_UTIL_MEMBUF_INLINE static inline
33 #endif
34 
36 struct membuf {
38  char *cur;
40  char *begin;
42  char *end;
43 };
44 
46 #define MEMBUF_INIT \
47  { \
48  NULL, NULL, NULL \
49  }
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
67 LELY_UTIL_MEMBUF_INLINE void membuf_init(
68  struct membuf *buf, void *ptr, size_t size);
69 
71 void membuf_fini(struct membuf *buf);
72 
74 LELY_UTIL_MEMBUF_INLINE void *membuf_begin(const struct membuf *buf);
75 
77 LELY_UTIL_MEMBUF_INLINE void membuf_clear(struct membuf *buf);
78 
80 LELY_UTIL_MEMBUF_INLINE size_t membuf_size(const struct membuf *buf);
81 
83 LELY_UTIL_MEMBUF_INLINE size_t membuf_capacity(const struct membuf *buf);
84 
98 size_t membuf_reserve(struct membuf *buf, size_t size);
99 
112 LELY_UTIL_MEMBUF_INLINE ptrdiff_t membuf_seek(
113  struct membuf *buf, ptrdiff_t offset);
114 
131 LELY_UTIL_MEMBUF_INLINE void *membuf_alloc(struct membuf *buf, size_t *size);
132 
144 LELY_UTIL_MEMBUF_INLINE size_t membuf_write(
145  struct membuf *buf, const void *ptr, size_t size);
146 
148 void membuf_flush(struct membuf *buf, size_t size);
149 
150 LELY_UTIL_MEMBUF_INLINE void
151 membuf_init(struct membuf *buf, void *ptr, size_t size)
152 {
153  assert(buf);
154  assert(ptr || !size);
155 
156  buf->begin = buf->cur = (char *)ptr;
157  buf->end = buf->begin + size;
158 }
159 
160 LELY_UTIL_MEMBUF_INLINE void *
161 membuf_begin(const struct membuf *buf)
162 {
163  assert(buf);
164 
165  return buf->begin;
166 }
167 
168 LELY_UTIL_MEMBUF_INLINE void
169 membuf_clear(struct membuf *buf)
170 {
171  assert(buf);
172 
173  buf->cur = buf->begin;
174 }
175 
176 LELY_UTIL_MEMBUF_INLINE size_t
177 membuf_size(const struct membuf *buf)
178 {
179  assert(buf);
180 
181  return buf->cur - buf->begin;
182 }
183 
184 LELY_UTIL_MEMBUF_INLINE size_t
185 membuf_capacity(const struct membuf *buf)
186 {
187  assert(buf);
188 
189  return buf->end - buf->cur;
190 }
191 
192 LELY_UTIL_MEMBUF_INLINE ptrdiff_t
193 membuf_seek(struct membuf *buf, ptrdiff_t offset)
194 {
195  assert(buf);
196 
197  char *cur = buf->cur + offset;
198  if (cur - buf->begin < 0) {
199  cur = buf->begin;
200  offset = cur - buf->cur;
201  } else if (buf->end - cur < 0) {
202  cur = buf->end;
203  offset = cur - buf->cur;
204  }
205  buf->cur = cur;
206 
207  return offset;
208 }
209 
210 LELY_UTIL_MEMBUF_INLINE void *
211 membuf_alloc(struct membuf *buf, size_t *size)
212 {
213  assert(buf);
214  assert(size);
215 
216  void *cur = buf->cur;
217  *size = membuf_seek(buf, *size);
218  return cur;
219 }
220 
221 LELY_UTIL_MEMBUF_INLINE size_t
222 membuf_write(struct membuf *buf, const void *ptr, size_t size)
223 {
224  assert(buf);
225  assert(ptr || !size);
226 
227  void *cur = membuf_alloc(buf, &size);
228  if (size)
229  memcpy(cur, ptr, size);
230  return size;
231 }
232 
233 #ifdef __cplusplus
234 }
235 #endif
236 
237 #endif // !LELY_UTIL_MEMBUF_H_
This is the public header file of the utilities library.
ptrdiff_t membuf_seek(struct membuf *buf, ptrdiff_t offset)
Adjusts the position indicator of a memory buffer by offset bytes.
Definition: membuf.h:193
void * membuf_begin(const struct membuf *buf)
Returns a pointer to the first byte in a memory buffer.
Definition: membuf.h:161
size_t membuf_reserve(struct membuf *buf, size_t size)
Resizes a memory buffer, if necessary, to make room for at least an additional size bytes.
Definition: membuf.c:52
void membuf_fini(struct membuf *buf)
Finalizes a memory buffer.
Definition: membuf.c:40
size_t membuf_capacity(const struct membuf *buf)
Returns the number of unused bytes remaining in a memory buffer.
Definition: membuf.h:185
void membuf_flush(struct membuf *buf, size_t size)
Flushes size bytes from the beginning of a memory buffer.
Definition: membuf.c:88
void membuf_init(struct membuf *buf, void *ptr, size_t size)
Initializes a memory buffer.
Definition: membuf.h:151
size_t membuf_write(struct membuf *buf, const void *ptr, size_t size)
Writes data to a memory buffer.
Definition: membuf.h:222
void * membuf_alloc(struct membuf *buf, size_t *size)
Creates region of *size bytes in a memory buffer, starting at the current position indicator given by...
Definition: membuf.h:211
void membuf_clear(struct membuf *buf)
Clears a memory buffer.
Definition: membuf.h:169
size_t membuf_size(const struct membuf *buf)
Returns the total number of bytes written to a memory buffer.
Definition: membuf.h:177
This header file is part of the C11 and POSIX compatibility library; it includes <stddef....
This header file is part of the C11 and POSIX compatibility library; it includes <string....
A memory buffer.
Definition: membuf.h:36
char * end
A pointer to one past the last byte in the buffer.
Definition: membuf.h:42
char * begin
A pointer to the first byte in the buffer.
Definition: membuf.h:40
char * cur
A pointer to one past the last byte written to the buffer.
Definition: membuf.h:38