… | |
… | |
2 | #define DYNBUF_H__ |
2 | #define DYNBUF_H__ |
3 | |
3 | |
4 | #include <cstring> |
4 | #include <cstring> |
5 | #include <cassert> |
5 | #include <cassert> |
6 | |
6 | |
|
|
7 | #include "util.h" |
7 | #include "shstr.h" |
8 | #include "shstr.h" |
8 | |
9 | |
9 | // this is a "buffer" that can grow fast |
10 | // this is a "buffer" that can grow fast |
10 | // and is still somewhat space-efficient. |
11 | // and is still somewhat space-efficient. |
11 | // unlike obstacks or other data structures, |
12 | // unlike obstacks or other data structures, |
… | |
… | |
28 | int _size; |
29 | int _size; |
29 | |
30 | |
30 | chunk *first, *last; |
31 | chunk *first, *last; |
31 | |
32 | |
32 | void _reserve (int size); |
33 | void _reserve (int size); |
|
|
34 | void _clear (); |
33 | void clear (); |
35 | void clear (); |
34 | void finish (); |
36 | void finish (); |
35 | |
37 | |
36 | public: |
38 | public: |
37 | |
39 | |
… | |
… | |
46 | |
48 | |
47 | int room () const { return end - ptr; } |
49 | int room () const { return end - ptr; } |
48 | |
50 | |
49 | char *force (int size) |
51 | char *force (int size) |
50 | { |
52 | { |
51 | if (ptr + size >= end) |
53 | if (expect_false (ptr + size >= end)) |
52 | _reserve (size); |
54 | _reserve (size); |
53 | |
55 | |
54 | return ptr; |
56 | return ptr; |
55 | } |
57 | } |
56 | |
58 | |
57 | char *alloc (int size) |
59 | char *alloc (int size) |
58 | { |
60 | { |
59 | char *res = force (size); |
61 | char *res = force (size); |
60 | ptr += size; |
62 | ptr += size; |
61 | return res; |
63 | return res; |
62 | } |
64 | } |
63 | |
65 | |
64 | void fadd (char c) { *ptr++ = c; } |
66 | void fadd (char c) { *ptr++ = c; } |
65 | void fadd (unsigned char c) { fadd (char (c)); } |
67 | void fadd (unsigned char c) { fadd (char (c)); } |