forked from NLnetLabs/nsd
-
Notifications
You must be signed in to change notification settings - Fork 3
/
buffer.c
131 lines (110 loc) · 2.65 KB
/
buffer.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* buffer.c -- generic memory buffer .
*
* Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include "buffer.h"
static void
buffer_cleanup(void *arg)
{
buffer_type *buffer = (buffer_type *) arg;
assert(!buffer->_fixed);
free(buffer->_data);
}
buffer_type *
buffer_create(region_type *region, size_t capacity)
{
buffer_type *buffer
= (buffer_type *) region_alloc(region, sizeof(buffer_type));
if (!buffer)
return NULL;
buffer->_data = (uint8_t *) xalloc(capacity);
buffer->_position = 0;
buffer->_limit = buffer->_capacity = capacity;
buffer->_fixed = 0;
buffer_invariant(buffer);
region_add_cleanup(region, buffer_cleanup, buffer);
return buffer;
}
void
buffer_create_from(buffer_type *buffer, const void *data, size_t size)
{
assert(data);
buffer->_position = 0;
buffer->_limit = buffer->_capacity = size;
buffer->_data = (uint8_t *) data;
buffer->_fixed = 1;
buffer_invariant(buffer);
}
void
buffer_clear(buffer_type *buffer)
{
buffer_invariant(buffer);
buffer->_position = 0;
buffer->_limit = buffer->_capacity;
}
void
buffer_flip(buffer_type *buffer)
{
buffer_invariant(buffer);
buffer->_limit = buffer->_position;
buffer->_position = 0;
}
void
buffer_rewind(buffer_type *buffer)
{
buffer_invariant(buffer);
buffer->_position = 0;
}
void
buffer_set_capacity(buffer_type *buffer, size_t capacity)
{
buffer_invariant(buffer);
assert(buffer->_position <= capacity);
buffer->_data = (uint8_t *) xrealloc(buffer->_data, capacity);
buffer->_limit = buffer->_capacity = capacity;
}
void
buffer_reserve(buffer_type *buffer, size_t amount)
{
buffer_invariant(buffer);
assert(!buffer->_fixed);
if (buffer->_capacity < buffer->_position + amount) {
size_t new_capacity = buffer->_capacity * 3 / 2;
if (new_capacity < buffer->_position + amount) {
new_capacity = buffer->_position + amount;
}
buffer_set_capacity(buffer, new_capacity);
}
buffer->_limit = buffer->_capacity;
}
int
buffer_printf(buffer_type *buffer, const char *format, ...)
{
va_list args;
int written;
size_t remaining;
buffer_invariant(buffer);
assert(buffer->_limit == buffer->_capacity);
remaining = buffer_remaining(buffer);
va_start(args, format);
written = vsnprintf((char *) buffer_current(buffer), remaining,
format, args);
va_end(args);
if (written >= 0 && (size_t) written >= remaining) {
buffer_reserve(buffer, written + 1);
va_start(args, format);
written = vsnprintf((char *) buffer_current(buffer),
buffer_remaining(buffer),
format, args);
va_end(args);
}
buffer->_position += written;
return written;
}