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
35 #define LELY_MEMBUF_SIZE 16
37 #endif
38 
39 void
40 membuf_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 
51 size_t
52 membuf_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 
87 void
88 membuf_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 }
LELY_MEMBUF_SIZE
#define LELY_MEMBUF_SIZE
The initial size (in bytes) of a memory buffer.
Definition: membuf.c:36
membuf_reserve
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
MIN
#define MIN(a, b)
Returns the minimum of a and b.
Definition: util.h:57
membuf
A memory buffer.
Definition: membuf.h:36
ERRNUM_NOMEM
@ ERRNUM_NOMEM
Not enough space.
Definition: errnum.h:172
errno2c
int errno2c(int errnum)
Transforms a standard C error number to a native error code.
Definition: errnum.c:46
util.h
membuf_size
size_t membuf_size(const struct membuf *buf)
Returns the total number of bytes written to a memory buffer.
Definition: membuf.h:177
membuf_fini
void membuf_fini(struct membuf *buf)
Finalizes a memory buffer.
Definition: membuf.c:40
membuf::end
char * end
A pointer to one past the last byte in the buffer.
Definition: membuf.h:42
set_errnum
void set_errnum(errnum_t errnum)
Sets the current (thread-specific) platform-independent error number to errnum.
Definition: errnum.h:424
set_errc
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition: errnum.c:944
errnum.h
membuf.h
membuf_flush
void membuf_flush(struct membuf *buf, size_t size)
Flushes size bytes from the beginning of a memory buffer.
Definition: membuf.c:88
membuf_capacity
size_t membuf_capacity(const struct membuf *buf)
Returns the number of unused bytes remaining in a memory buffer.
Definition: membuf.h:185
membuf::cur
char * cur
A pointer to one past the last byte written to the buffer.
Definition: membuf.h:38
membuf::begin
char * begin
A pointer to the first byte in the buffer.
Definition: membuf.h:40
stdlib.h