Lely core libraries  2.2.5
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 
35 struct 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
51 extern "C" {
52 #endif
53 
55 LELY_UTIL_MEMBUF_INLINE void membuf_init(struct membuf *buf);
56 
58 void membuf_fini(struct membuf *buf);
59 
61 LELY_UTIL_MEMBUF_INLINE void *membuf_begin(const struct membuf *buf);
62 
64 LELY_UTIL_MEMBUF_INLINE void membuf_clear(struct membuf *buf);
65 
67 LELY_UTIL_MEMBUF_INLINE size_t membuf_size(const struct membuf *buf);
68 
70 LELY_UTIL_MEMBUF_INLINE size_t membuf_capacity(const struct membuf *buf);
71 
85 size_t membuf_reserve(struct membuf *buf, size_t size);
86 
99 LELY_UTIL_MEMBUF_INLINE ptrdiff_t membuf_seek(
100  struct membuf *buf, ptrdiff_t offset);
101 
118 LELY_UTIL_MEMBUF_INLINE void *membuf_alloc(struct membuf *buf, size_t *size);
119 
131 LELY_UTIL_MEMBUF_INLINE size_t membuf_write(
132  struct membuf *buf, const void *ptr, size_t size);
133 
135 void membuf_flush(struct membuf *buf, size_t size);
136 
137 inline void
138 membuf_init(struct membuf *buf)
139 {
140  buf->begin = buf->end = buf->cur = NULL;
141 }
142 
143 inline void *
144 membuf_begin(const struct membuf *buf)
145 {
146  return buf->begin;
147 }
148 
149 inline void
150 membuf_clear(struct membuf *buf)
151 {
152  buf->cur = buf->begin;
153 }
154 
155 inline size_t
156 membuf_size(const struct membuf *buf)
157 {
158  return buf->cur - buf->begin;
159 }
160 
161 inline size_t
162 membuf_capacity(const struct membuf *buf)
163 {
164  return buf->end - buf->cur;
165 }
166 
167 inline ptrdiff_t
168 membuf_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 
183 inline void *
184 membuf_alloc(struct membuf *buf, size_t *size)
185 {
186  void *cur = buf->cur;
187  *size = membuf_seek(buf, *size);
188  return cur;
189 }
190 
191 inline size_t
192 membuf_write(struct membuf *buf, const void *ptr, size_t size)
193 {
194  void *cur = membuf_alloc(buf, &size);
195  if (size)
196  memcpy(cur, ptr, size);
197  return size;
198 }
199 
200 #ifdef __cplusplus
201 }
202 #endif
203 
204 #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