… | |
… | |
55 | chunk *first, *last; |
55 | chunk *first, *last; |
56 | |
56 | |
57 | void reserve (int size); |
57 | void reserve (int size); |
58 | void init (int initial); // allocate sinitial chunk |
58 | void init (int initial); // allocate sinitial chunk |
59 | void free (chunk *&chain); // free chain of chunks |
59 | void free (chunk *&chain); // free chain of chunks |
60 | char *_linearise (); |
60 | char *_linearise (int extra = 0); |
61 | void finalise (); |
61 | void finalise (); |
62 | |
62 | |
63 | public: |
63 | public: |
64 | |
64 | |
65 | // initial - the size of the initial chunk to be allocated |
65 | // initial - the size of the initial chunk to be allocated |
… | |
… | |
89 | return first->next ? _linearise () : first->data; |
89 | return first->next ? _linearise () : first->data; |
90 | } |
90 | } |
91 | |
91 | |
92 | int room () const { return end - ptr; } |
92 | int room () const { return end - ptr; } |
93 | |
93 | |
|
|
94 | // make sure we have "size" extra room |
94 | char *force (int size) |
95 | char *force (int size) |
95 | { |
96 | { |
96 | if (expect_false (ptr + size >= end)) |
97 | if (expect_false (ptr + size >= end)) |
97 | reserve (size); |
98 | reserve (size); |
98 | |
99 | |
99 | return ptr; |
100 | return ptr; |
100 | } |
101 | } |
101 | |
102 | |
|
|
103 | // allocate size bytes and return pointer to them (caller must force()) |
102 | char *falloc (int size) |
104 | char *falloc (int size) |
103 | { |
105 | { |
104 | char *res = ptr; |
106 | char *res = ptr; |
105 | ptr += size; |
107 | ptr += size; |
106 | return res; |
108 | return res; |
107 | } |
109 | } |
108 | |
110 | |
|
|
111 | // allocate size bytes and return pointer to them |
109 | char *alloc (int size) |
112 | char *alloc (int size) |
110 | { |
113 | { |
111 | force (size); |
114 | force (size); |
112 | return falloc (size); |
115 | return falloc (size); |
113 | } |
116 | } |
… | |
… | |
137 | |
140 | |
138 | void add (shstr_tmp s) |
141 | void add (shstr_tmp s) |
139 | { |
142 | { |
140 | add (s.s, s.length ()); |
143 | add (s.s, s.length ()); |
141 | } |
144 | } |
|
|
145 | |
|
|
146 | // rather inefficient |
|
|
147 | void splice (int offset, int olen, const char *s, int slen); |
|
|
148 | void splice (int offset, int olen) { splice (offset, olen, "", 0); } |
142 | |
149 | |
143 | //TODO |
150 | //TODO |
144 | //void add_destructive (dynbuf &buf); |
151 | //void add_destructive (dynbuf &buf); |
145 | |
152 | |
146 | dynbuf &operator << (char c) { add (c); return *this; } |
153 | dynbuf &operator << (char c) { add (c); return *this; } |
… | |
… | |
180 | void vprintf (const char *format, va_list ap); |
187 | void vprintf (const char *format, va_list ap); |
181 | |
188 | |
182 | void add_abilities (const char *name, uint32 abilities); |
189 | void add_abilities (const char *name, uint32 abilities); |
183 | void add_paths (const char *name, uint32 paths); |
190 | void add_paths (const char *name, uint32 paths); |
184 | |
191 | |
|
|
192 | // we need to redefine, to keep the API :/ |
|
|
193 | using dynbuf::splice; |
|
|
194 | void splice (int offset, int olen, const char *s) |
|
|
195 | { |
|
|
196 | dynbuf::splice (offset, olen, s, strlen (s)); |
|
|
197 | } |
|
|
198 | |
185 | // returns the string, linearised and with trailing \0 |
199 | // returns the string, linearised and with trailing \0 |
186 | operator char *(); |
200 | operator char *(); |
187 | }; |
201 | }; |
188 | |
202 | |
189 | #endif |
203 | #endif |