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
|
#ifndef INCLUDE_buffer_h__
#define INCLUDE_buffer_h__
#include <stdbool.h>
#include <stddef.h>
#include <stdarg.h>
#include <sys/types.h>
typedef struct {
unsigned char *ptr;
int asize, size;
} gh_buf;
extern unsigned char gh_buf__initbuf[];
extern unsigned char gh_buf__oom[];
#define GH_BUF_INIT { gh_buf__initbuf, 0, 0 }
/**
* Initialize a gh_buf structure.
*
* For the cases where GH_BUF_INIT cannot be used to do static
* initialization.
*/
extern void gh_buf_init(gh_buf *buf, int initial_size);
/**
* Attempt to grow the buffer to hold at least `target_size` bytes.
*
* If the allocation fails, this will return an error. If mark_oom is true,
* this will mark the buffer as invalid for future operations; if false,
* existing buffer content will be preserved, but calling code must handle
* that buffer was not expanded.
*/
extern int gh_buf_try_grow(gh_buf *buf, int target_size, bool mark_oom);
/**
* Grow the buffer to hold at least `target_size` bytes.
*
* If the allocation fails, this will return an error and the buffer will be
* marked as invalid for future operations, invaliding contents.
*
* @return 0 on success or -1 on failure
*/
static inline int gh_buf_grow(gh_buf *buf, int target_size)
{
return gh_buf_try_grow(buf, target_size, true);
}
extern void gh_buf_free(gh_buf *buf);
extern void gh_buf_swap(gh_buf *buf_a, gh_buf *buf_b);
/**
* Test if there have been any reallocation failures with this gh_buf.
*
* Any function that writes to a gh_buf can fail due to memory allocation
* issues. If one fails, the gh_buf will be marked with an OOM error and
* further calls to modify the buffer will fail. Check gh_buf_oom() at the
* end of your sequence and it will be true if you ran out of memory at any
* point with that buffer.
*
* @return false if no error, true if allocation error
*/
static inline bool gh_buf_oom(const gh_buf *buf)
{
return (buf->ptr == gh_buf__oom);
}
static inline size_t gh_buf_len(const gh_buf *buf)
{
return buf->size;
}
extern int gh_buf_cmp(const gh_buf *a, const gh_buf *b);
extern void gh_buf_attach(gh_buf *buf, unsigned char *ptr, int asize);
extern unsigned char *gh_buf_detach(gh_buf *buf);
extern void gh_buf_copy_cstr(char *data, int datasize, const gh_buf *buf);
static inline const char *gh_buf_cstr(const gh_buf *buf)
{
return (char *)buf->ptr;
}
#define gh_buf_at(buf, n) ((buf)->ptr[n])
/*
* Functions below that return int value error codes will return 0 on
* success or -1 on failure (which generally means an allocation failed).
* Using a gh_buf where the allocation has failed with result in -1 from
* all further calls using that buffer. As a result, you can ignore the
* return code of these functions and call them in a series then just call
* gh_buf_oom at the end.
*/
extern int gh_buf_set(gh_buf *buf, const unsigned char *data, int len);
extern int gh_buf_sets(gh_buf *buf, const char *string);
extern int gh_buf_putc(gh_buf *buf, int c);
extern int gh_buf_put(gh_buf *buf, const unsigned char *data, int len);
extern int gh_buf_puts(gh_buf *buf, const char *string);
extern int gh_buf_printf(gh_buf *buf, const char *format, ...)
__attribute__((format (printf, 2, 3)));
extern int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap);
extern void gh_buf_clear(gh_buf *buf);
int gh_buf_strchr(const gh_buf *buf, int c, int pos);
int gh_buf_strrchr(const gh_buf *buf, int c, int pos);
void gh_buf_truncate(gh_buf *buf, int len);
void gh_buf_ltruncate(gh_buf *buf, int len);
void gh_buf_trim(gh_buf *buf);
#endif
|