summaryrefslogtreecommitdiff
path: root/usual/mbuf.c
blob: d9f3514db38a4ca353330e4ae1868fe8e99a8171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

/*
 * Safe and easy access to memory buffer.
 */

#include <usual/mbuf.h>

bool mbuf_make_room(struct MBuf *buf, unsigned len)
{
	unsigned new_alloc = buf->alloc_len;
	void *ptr;

	/* is it a dynamic buffer */
	if (buf->reader || buf->fixed)
		return false;

	/* maybe there is enough room already */
	if (buf->write_pos + len <= buf->alloc_len)
		return true;

	if (new_alloc == 0)
		new_alloc = 128;

	/* calc new alloc size */
	while (new_alloc < buf->write_pos + len)
		new_alloc *= 2;

	/* realloc */
	ptr = realloc(buf->data, new_alloc);
	if (!ptr)
		return false;
	buf->data = ptr;
	buf->alloc_len = new_alloc;
	return true;
}