22 #ifndef LELY_UTIL_MEMBUF_H_
23 #define LELY_UTIL_MEMBUF_H_
31 #ifndef LELY_UTIL_MEMBUF_INLINE
32 #define LELY_UTIL_MEMBUF_INLINE static inline
68 struct membuf *buf,
void *ptr,
size_t size);
113 struct membuf *buf, ptrdiff_t offset);
145 struct membuf *buf,
const void *ptr,
size_t size);
150 LELY_UTIL_MEMBUF_INLINE
void
154 assert(ptr || !size);
156 buf->
begin = buf->
cur = (
char *)ptr;
160 LELY_UTIL_MEMBUF_INLINE
void *
168 LELY_UTIL_MEMBUF_INLINE
void
176 LELY_UTIL_MEMBUF_INLINE
size_t
184 LELY_UTIL_MEMBUF_INLINE
size_t
189 return buf->
end - buf->
cur;
192 LELY_UTIL_MEMBUF_INLINE ptrdiff_t
197 char *cur = buf->
cur + offset;
198 if (cur - buf->
begin < 0) {
200 offset = cur - buf->
cur;
201 }
else if (buf->
end - cur < 0) {
203 offset = cur - buf->
cur;
210 LELY_UTIL_MEMBUF_INLINE
void *
216 void *cur = buf->
cur;
221 LELY_UTIL_MEMBUF_INLINE
size_t
225 assert(ptr || !size);
229 memcpy(cur, ptr, size);
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.
void * membuf_begin(const struct membuf *buf)
Returns a pointer to the first byte in a memory buffer.
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.
void membuf_fini(struct membuf *buf)
Finalizes a memory buffer.
size_t membuf_capacity(const struct membuf *buf)
Returns the number of unused bytes remaining in a memory buffer.
void membuf_flush(struct membuf *buf, size_t size)
Flushes size bytes from the beginning of a memory buffer.
void membuf_init(struct membuf *buf, void *ptr, size_t size)
Initializes a memory buffer.
size_t membuf_write(struct membuf *buf, const void *ptr, size_t size)
Writes data to a memory buffer.
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...
void membuf_clear(struct membuf *buf)
Clears a memory buffer.
size_t membuf_size(const struct membuf *buf)
Returns the total number of bytes written to a memory buffer.
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....
char * end
A pointer to one past the last byte in the buffer.
char * begin
A pointer to the first byte in the buffer.
char * cur
A pointer to one past the last byte written to the buffer.