Lely core libraries 2.3.4
membuf.c
Go to the documentation of this file.
1
24#include "util.h"
25#include <lely/util/errnum.h>
26#define LELY_UTIL_MEMBUF_INLINE extern inline
27#include <lely/util/membuf.h>
28
29#include <assert.h>
30#if !LELY_NO_MALLOC
31#include <stdlib.h>
32#endif
33
34#ifndef LELY_MEMBUF_SIZE
36#define LELY_MEMBUF_SIZE 16
37#endif
38
39void
40membuf_fini(struct membuf *buf)
41{
42 assert(buf);
43
44#if LELY_NO_MALLOC
45 (void)buf;
46#else
47 free(buf->begin);
48#endif
49}
50
51size_t
52membuf_reserve(struct membuf *buf, size_t size)
53{
54 assert(buf);
55
56 size_t capacity = membuf_capacity(buf);
57 if (size <= capacity)
58 return capacity;
59
60#if LELY_NO_MALLOC
62 return 0;
63#else
64 // The required size equals the size of the data already in the buffer,
65 // plus the data to be added. To limit the number of allocations, we
66 // keep doubling the size of the buffer until it is large enough.
67 size_t buf_size = LELY_MEMBUF_SIZE;
68 while (buf_size < membuf_size(buf) + size)
69 buf_size *= 2;
70
71 char *begin = realloc(buf->begin, buf_size);
72 if (!begin) {
73#if !LELY_NO_ERRNO
74 set_errc(errno2c(errno));
75#endif
76 return 0;
77 }
78
79 buf->cur = begin + membuf_size(buf);
80 buf->begin = begin;
81 buf->end = buf->begin + buf_size;
82
83 return membuf_capacity(buf);
84#endif // LELY_NO_MALLOC
85}
86
87void
88membuf_flush(struct membuf *buf, size_t size)
89{
90 assert(buf);
91
92 size = MIN(size, membuf_size(buf));
93 if (size)
94 memmove(buf->begin, buf->begin + size, membuf_size(buf) - size);
95 buf->cur -= size;
96}
This header file is part of the utilities library; it contains the native and platform-independent er...
@ ERRNUM_NOMEM
Not enough space.
Definition errnum.h:172
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition errnum.c:944
int errno2c(int errnum)
Transforms a standard C error number to a native error code.
Definition errnum.c:46
void set_errnum(errnum_t errnum)
Sets the current (thread-specific) platform-independent error number to errnum.
Definition errnum.h:424
#define MIN(a, b)
Returns the minimum of a and b.
Definition util.h:57
#define LELY_MEMBUF_SIZE
The initial size (in bytes) of a memory buffer.
Definition membuf.c:36
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
void membuf_flush(struct membuf *buf, size_t size)
Flushes size bytes from the beginning of a memory buffer.
Definition membuf.c:88
This header file is part of the utilities library; it contains the memory buffer declarations.
size_t membuf_capacity(const struct membuf *buf)
Returns the number of unused bytes remaining in a memory buffer.
Definition membuf.h:185
size_t membuf_size(const struct membuf *buf)
Returns the total number of bytes written to a memory buffer.
Definition membuf.h:177
This is the internal header file of the utilities library.
This header file is part of the C11 and POSIX compatibility library; it includes <stdlib....
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