Debian Codegraph @

lib/usual/mbuf.c

raw symbols SCIP
1
2 /*
3 * Safe and easy access to memory buffer.
4 */
5
6 #include <usual/mbuf.h>
7
8 bool mbuf_make_room(struct MBuf *buf, unsigned len)
9 {
10 unsigned new_alloc = buf->alloc_len;
11 void *ptr;
12
13 /* is it a dynamic buffer */
14 if (buf->reader || buf->fixed)
15 return false;
16
17 /* maybe there is enough room already */
18 if (buf->write_pos + len <= buf->alloc_len)
19 return true;
20
21 if (new_alloc == 0)
22 new_alloc = 128;
23
24 /* calc new alloc size */
25 while (new_alloc < buf->write_pos + len)
26 new_alloc *= 2;
27
28 /* realloc */
29 ptr = realloc(buf->data, new_alloc);
30 if (!ptr)
31 return false;
32 buf->data = ptr;
33 buf->alloc_len = new_alloc;
34 return true;
35 }