Lely core libraries 1.9.2
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 <stddef.h>
28#include <string.h>
29
30#ifndef LELY_UTIL_MEMBUF_INLINE
31#define LELY_UTIL_MEMBUF_INLINE static inline
32#endif
33
35struct membuf {
37 char *cur;
39 char *begin;
41 char *end;
42};
43
45#define MEMBUF_INIT \
46 { \
47 NULL, NULL, NULL \
48 }
49
50#ifdef __cplusplus
51extern "C" {
52#endif
53
55LELY_UTIL_MEMBUF_INLINE void membuf_init(struct membuf *buf);
56
58void membuf_fini(struct membuf *buf);
59
61LELY_UTIL_MEMBUF_INLINE void *membuf_begin(const struct membuf *buf);
62
64LELY_UTIL_MEMBUF_INLINE void membuf_clear(struct membuf *buf);
65
67LELY_UTIL_MEMBUF_INLINE size_t membuf_size(const struct membuf *buf);
68
70LELY_UTIL_MEMBUF_INLINE size_t membuf_capacity(const struct membuf *buf);
71
85size_t membuf_reserve(struct membuf *buf, size_t size);
86
99LELY_UTIL_MEMBUF_INLINE ptrdiff_t membuf_seek(
100 struct membuf *buf, ptrdiff_t offset);
101
118LELY_UTIL_MEMBUF_INLINE void *membuf_alloc(struct membuf *buf, size_t *size);
119
131LELY_UTIL_MEMBUF_INLINE size_t membuf_write(
132 struct membuf *buf, const void *ptr, size_t size);
133
135void membuf_flush(struct membuf *buf, size_t size);
136
137inline void
138membuf_init(struct membuf *buf)
139{
140 buf->begin = buf->end = buf->cur = NULL;
141}
142
143inline void *
144membuf_begin(const struct membuf *buf)
145{
146 return buf->begin;
147}
148
149inline void
151{
152 buf->cur = buf->begin;
153}
154
155inline size_t
156membuf_size(const struct membuf *buf)
157{
158 return buf->cur - buf->begin;
159}
160
161inline size_t
162membuf_capacity(const struct membuf *buf)
163{
164 return buf->end - buf->cur;
165}
166
167inline ptrdiff_t
168membuf_seek(struct membuf *buf, ptrdiff_t offset)
169{
170 char *cur = buf->cur + offset;
171 if (cur - buf->begin < 0) {
172 cur = buf->begin;
173 offset = cur - buf->cur;
174 } else if (buf->end - cur < 0) {
175 cur = buf->end;
176 offset = cur - buf->cur;
177 }
178 buf->cur = cur;
179
180 return offset;
181}
182
183inline void *
184membuf_alloc(struct membuf *buf, size_t *size)
185{
186 void *cur = buf->cur;
187 *size = membuf_seek(buf, *size);
188 return cur;
189}
190
191inline size_t
192membuf_write(struct membuf *buf, const void *ptr, size_t size)
193{
194 void *cur = membuf_alloc(buf, &size);
195 memcpy(cur, ptr, size);
196 return size;
197}
198
199#ifdef __cplusplus
200}
201#endif
202
203#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:168
void * membuf_begin(const struct membuf *buf)
Returns a pointer to the first byte in a memory buffer.
Definition: membuf.h:144
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:46
void membuf_fini(struct membuf *buf)
Finalizes a memory buffer.
Definition: membuf.c:38
size_t membuf_capacity(const struct membuf *buf)
Returns the number of unused bytes remaining in a memory buffer.
Definition: membuf.h:162
void membuf_flush(struct membuf *buf, size_t size)
Flushes size bytes from the beginning of a memory buffer.
Definition: membuf.c:75
size_t membuf_write(struct membuf *buf, const void *ptr, size_t size)
Writes data to a memory buffer.
Definition: membuf.h:192
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:184
void membuf_clear(struct membuf *buf)
Clears a memory buffer.
Definition: membuf.h:150
void membuf_init(struct membuf *buf)
Initializes a memory buffer.
Definition: membuf.h:138
size_t membuf_size(const struct membuf *buf)
Returns the total number of bytes written to a memory buffer.
Definition: membuf.h:156
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:35
char * end
A pointer to one past the last byte in the buffer.
Definition: membuf.h:41
char * begin
A pointer to the first byte in the buffer.
Definition: membuf.h:39
char * cur
A pointer to one past the last byte written to the buffer.
Definition: membuf.h:37