forked from chmod222/cNBT
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuffer.h
99 lines (81 loc) · 2.51 KB
/
buffer.h
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
/*
* -----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Lukas Niederbremer <[email protected]> and Clark Gaebel <[email protected]>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If we meet some day, and you think this stuff is worth
* it, you can buy us a beer in return.
* -----------------------------------------------------------------------------
*/
#ifndef NBT_BUFFER_H
#define NBT_BUFFER_H
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#define INITIAL_SIZE 1024
/*
* A buffer is 'unlimited' storage for raw data. As long as buffer_append is
* used to add data, it will automatically resize to make room. To read the
* data, just access `data' directly.
*/
struct buffer {
char* data; /* You can access the buffer's raw bytes through this pointer */
size_t len; /* Only accesses in the interval [data, data + len) are defined */
size_t cap; /* Internal use. The allocated size of the buffer. */
};
/*
* Initialize a buffer with this macro.
*
* Usage:
* struct buffer b = BUFFER_INIT;
* OR
* struct buffer b;
* b = BUFFER_INIT;
*/
#define BUFFER_INIT (struct buffer) { NULL, 0, 0 }
/*
* Ensures there's enough room in the buffer for at least `reserved_amount'
* bytes. Returns non-zero on failure. If such a failure occurs, further
* usage of the buffer results in undefined behavior.
*/
static inline int buffer_reserve(struct buffer* b, size_t reserved_amount)
{
if(b->cap >= reserved_amount) return 0;
while(b->cap < reserved_amount)
b->cap *= 2;
char* temp = realloc(b->data, b->cap);
if(temp == NULL)
return buffer_free(b), 1;
b->data = temp;
return 0;
}
/*
* Copies `n' bytes from `data' into the buffer. Returns non-zero if an
* out-of-memory failure occured. If such a failure occurs, further usage of the
* buffer results in undefined behavior.
*/
static inline int buffer_append(struct buffer* b, const void* data, size_t n)
{
/* lazy initialization */
if(b->data == NULL)
{
b->cap = INITIAL_SIZE;
b->data = malloc(b->cap);
if(b->data == NULL) return 1;
}
if(buffer_reserve(b, b->len + n)) return 1;
/* copy data in */
memcpy(b->data + b->len, data, n);
b->len += n;
return 0;
}
/* Frees all memory associated with the buffer */
static inline void buffer_free(struct buffer* b)
{
free(b->data);
b->data = NULL;
b->len = 0;
b->cap = 0;
}
#undef INITIAL_SIZE
#endif