Debian Codegraph @

lib/util/agxbuf.h

raw
1 /**
2 * @file
3 * @ingroup cgraph_utils
4 * @brief Dynamically expanding string buffers
5 *
6 * Incrementally constructing a string in C is not something easily available
7 * out-of-the-box. You either need to statically know the size of the string
8 * upfront or rely on non-standard APIs like `asprintf` and `open_memstream`.
9 *
10 * This header implements a self-contained, reasonably efficient alternative.
11 * You can think of this as similar to C++’s `std::ostringstream` or Python’s
12 * `io.StringIO`.
13 *
14 * `agxbuf` includes Short String Optimization (SSO), a technique to pack small
15 * strings into the bytes of the structure itself rather than an out-of-line
16 * heap buffer. The point of this is to save memory and/or runtime by avoiding
17 * allocator calls. In contrast to most other SSO implementations (e.g. those in
18 * the `std::string` implementations of many C++ standard libraries), `agxbuf`
19 * biases heavily towards saving memory over runtime performance. That is, the
20 * maximum length of string is eligible for SSO, at the expense of more runtime
21 * code branches. The thinking here (that should periodically be reconsidered)
22 * is that expensive Graphviz runs typically hit memory limits before runtime
23 * limits, and thus would benefit more from saving memory than running faster.
24 */
25 /*************************************************************************
26 * Copyright (c) 2011 AT&T Intellectual Property
27 * All rights reserved. This program and the accompanying materials
28 * are made available under the terms of the Eclipse Public License v1.0
29 * which accompanies this distribution, and is available at
30 * https://www.eclipse.org/legal/epl-v10.html
31 *
32 * Contributors: Details at https://graphviz.org
33 *************************************************************************/
34
35 #pragma once
36
37 #include <assert.h>
38 #include <limits.h>
39 #include <stdarg.h>
40 #include <stdbool.h>
41 #include <stddef.h>
42 #include <stdint.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <util/alloc.h>
46 #include <util/unused.h>
47
48 /// a description of where a buffer is located
49 typedef enum {
50 AGXBUF_INLINE_SIZE_0 = 0,
51 AGXBUF_ON_HEAP = 255, ///< buffer is dynamically allocated
52 /// other values mean an inline buffer with size N
53 } agxbuf_loc_t;
54
55 /// extensible buffer
56 ///
57 /// Malloc'ed memory is never released until \p agxbdisown or \p agxbfree is
58 /// called.
59 ///
60 /// This has the following layout assuming x86-64.
61 ///
62 /// located
63 /// ↓
64 /// ┌───────────────┬───────────────┬───────────────┬─────────────┬─┐
65 /// │ buf │ size │ capacity │ padding │ │
66 /// ├───────────────┴───────────────┴───────────────┴─────────────┼─┤
67 /// │ store │ │
68 /// └─────────────────────────────────────────────────────────────┴─┘
69 /// 0 8 16 24 32
70 ///
71 /// \p buf, \p size, and \p capacity are in use when \p located is
72 /// \p AGXBUF_ON_HEAP. \p store is in use when \p located is <
73 /// \p AGXBUF_ON_HEAP.
74 typedef struct {
75 union {
76 struct {
77 char *buf; ///< start of buffer
78 size_t size; ///< number of characters in the buffer
79 size_t capacity; ///< available bytes in the buffer
80 char padding[sizeof(size_t) - 1]; ///< unused; for alignment
81 unsigned char
82 located; ///< where does the backing memory for this buffer live?
83 };
84 char store[sizeof(char *) + sizeof(size_t) * 3 -
85 1]; ///< inline storage used when \p located is
86 ///< < \p AGXBUF_ON_HEAP
87 };
88 } agxbuf;
89
90 static inline bool agxbuf_is_inline(const agxbuf *xb) {
91 assert((xb->located == AGXBUF_ON_HEAP || xb->located <= sizeof(xb->store)) &&
92 "corrupted agxbuf type");
93 return xb->located < AGXBUF_ON_HEAP;
94 }
95
96 /// free any malloced resources
97 static inline void agxbfree(agxbuf *xb) {
98 if (xb->located == AGXBUF_ON_HEAP)
99 free(xb->buf);
100 }
101
102 /// return pointer to beginning of buffer
103 static inline char *agxbstart(agxbuf *xb) {
104 return agxbuf_is_inline(xb) ? xb->store : xb->buf;
105 }
106
107 /// return number of characters currently stored
108 static inline size_t agxblen(const agxbuf *xb) {
109 if (agxbuf_is_inline(xb)) {
110 return xb->located - AGXBUF_INLINE_SIZE_0;
111 }
112 return xb->size;
113 }
114
115 /// get the capacity of the backing memory of a buffer
116 ///
117 /// In contrast to \p agxblen, this is the total number of usable bytes in the
118 /// backing store, not the total number of currently stored bytes.
119 ///
120 /// \param xb Buffer to operate on
121 /// \return Number of usable bytes in the backing store
122 static inline size_t agxbsizeof(const agxbuf *xb) {
123 if (agxbuf_is_inline(xb)) {
124 return sizeof(xb->store);
125 }
126 return xb->capacity;
127 }
128
129 /// removes last character added, if any
130 static inline int agxbpop(agxbuf *xb) {
131
132 size_t len = agxblen(xb);
133 if (len == 0) {
134 return -1;
135 }
136
137 if (agxbuf_is_inline(xb)) {
138 assert(xb->located > AGXBUF_INLINE_SIZE_0);
139 int c = xb->store[len - 1];
140 --xb->located;
141 return c;
142 }
143
144 int c = xb->buf[xb->size - 1];
145 --xb->size;
146 return c;
147 }
148
149 /// expand buffer to hold at least ssz more bytes
150 static inline void agxbmore(agxbuf *xb, size_t ssz) {
151 size_t cnt = 0; // current no. of characters in buffer
152 size_t size = 0; // current buffer size
153 size_t nsize = 0; // new buffer size
154 char *nbuf; // new buffer
155
156 size = agxbsizeof(xb);
157 nsize = size == 0 ? BUFSIZ : (2 * size);
158 if (size + ssz > nsize)
159 nsize = size + ssz;
160 cnt = agxblen(xb);
161
162 if (xb->located == AGXBUF_ON_HEAP) {
163 nbuf = (char *)gv_recalloc(xb->buf, size, nsize, sizeof(char));
164 } else {
165 nbuf = (char *)gv_calloc(nsize, sizeof(char));
166 memcpy(nbuf, xb->store, cnt);
167 xb->size = cnt;
168 }
169 xb->buf = nbuf;
170 xb->capacity = nsize;
171 xb->located = AGXBUF_ON_HEAP;
172 }
173
174 /// next position for writing
175 static inline char *agxbnext(agxbuf *xb) {
176 size_t len = agxblen(xb);
177 return agxbuf_is_inline(xb) ? &xb->store[len] : &xb->buf[len];
178 }
179
180 /// vprintf-style output to an agxbuf
181 static inline int vagxbprint(agxbuf *xb, const char *fmt, va_list ap) {
182 size_t size;
183 int result;
184
185 // determine how many bytes we need to print
186 {
187 va_list ap2;
188 int rc;
189 va_copy(ap2, ap);
190 rc = vsnprintf(NULL, 0, fmt, ap2);
191 va_end(ap2);
192 if (rc < 0) {
193 return rc;
194 }
195 size = (size_t)rc + 1; // account for NUL terminator
196 }
197
198 // should we use double buffering?
199 bool use_stage = false;
200
201 // do we need to expand the buffer?
202 {
203 size_t unused_space = agxbsizeof(xb) - agxblen(xb);
204 if (unused_space < size) {
205 size_t extra = size - unused_space;
206 if (agxbuf_is_inline(xb) && extra == 1) {
207 // The content is currently stored inline, but this print will push it
208 // over into being heap-allocated by a single byte. This last byte is a
209 // '\0' that `vsnprintf` unavoidably writes but we do not need. So lets
210 // avoid this by printing to an intermediate, larger buffer, and then
211 // copying the content minus the trailing '\0' to the final destination.
212 use_stage = true;
213 } else {
214 agxbmore(xb, extra);
215 }
216 }
217 }
218
219 // a buffer one byte larger than inline storage to fit the trailing '\0'
220 char stage[sizeof(xb->store) + 1] = {0};
221 assert(!use_stage || size <= sizeof(stage));
222
223 // we can now safely print into the buffer
224 char *dst = use_stage ? stage : agxbnext(xb);
225 result = vsnprintf(dst, size, fmt, ap);
226 assert(result == (int)(size - 1) || result < 0);
227 if (result > 0) {
228 if (agxbuf_is_inline(xb)) {
229 assert(result <= (int)UCHAR_MAX);
230 if (use_stage) {
231 memcpy(agxbnext(xb), stage, (size_t)result);
232 }
233 xb->located += (unsigned char)result;
234 assert(agxblen(xb) <= sizeof(xb->store) && "agxbuf corruption");
235 } else {
236 assert(!use_stage);
237 xb->size += (size_t)result;
238 }
239 }
240
241 return result;
242 }
243
244 /* support for extra API misuse warnings if available */
245 #ifdef __GNUC__
246 #define PRINTF_LIKE(index, first) __attribute__((format(printf, index, first)))
247 #else
248 #define PRINTF_LIKE(index, first) /* nothing */
249 #endif
250
251 /// Printf-style output to an agxbuf
252 static inline PRINTF_LIKE(2, 3) int agxbprint(agxbuf *xb, const char *fmt,
253 ...) {
254 va_list ap;
255 int result;
256
257 va_start(ap, fmt);
258
259 result = vagxbprint(xb, fmt, ap);
260
261 va_end(ap);
262 return result;
263 }
264
265 #undef PRINTF_LIKE
266
267 /// append string s of length ssz into xb
268 static inline size_t agxbput_n(agxbuf *xb, const char *s, size_t ssz) {
269 if (ssz == 0) {
270 return 0;
271 }
272 if (ssz > agxbsizeof(xb) - agxblen(xb))
273 agxbmore(xb, ssz);
274 size_t len = agxblen(xb);
275 if (agxbuf_is_inline(xb)) {
276 memcpy(&xb->store[len], s, ssz);
277 assert(ssz <= UCHAR_MAX);
278 xb->located += (unsigned char)ssz;
279 assert(agxblen(xb) <= sizeof(xb->store) && "agxbuf corruption");
280 } else {
281 memcpy(&xb->buf[len], s, ssz);
282 xb->size += ssz;
283 }
284 return ssz;
285 }
286
287 /// append string s into xb
288 static inline size_t agxbput(agxbuf *xb, const char *s) {
289 size_t ssz = strlen(s);
290
291 return agxbput_n(xb, s, ssz);
292 }
293
294 /// add character to buffer
295 static inline int agxbputc(agxbuf *xb, char c) {
296 if (agxblen(xb) >= agxbsizeof(xb)) {
297 agxbmore(xb, 1);
298 }
299 size_t len = agxblen(xb);
300 if (agxbuf_is_inline(xb)) {
301 xb->store[len] = c;
302 ++xb->located;
303 assert(agxblen(xb) <= sizeof(xb->store) && "agxbuf corruption");
304 } else {
305 xb->buf[len] = c;
306 ++xb->size;
307 }
308 return 0;
309 }
310
311 /// resets pointer to data
312 static inline void agxbclear(agxbuf *xb) {
313 if (agxbuf_is_inline(xb)) {
314 xb->located = AGXBUF_INLINE_SIZE_0;
315 } else {
316 xb->size = 0;
317 }
318 }
319
320 /* Null-terminates buffer; resets and returns pointer to data. The buffer is
321 * still associated with the agxbuf and will be overwritten on the next, e.g.,
322 * agxbput. If you want to retrieve and disassociate the buffer, use agxbdisown
323 * instead.
324 */
325 static inline WUR char *agxbuse(agxbuf *xb) {
326 if (!agxbuf_is_inline(xb) || agxblen(xb) != sizeof(xb->store)) {
327 (void)agxbputc(xb, '\0');
328 } else {
329 // we can skip explicitly null-terminating the buffer because `agxbclear`
330 // resets the `xb->located` byte such that it naturally forms a terminator
331 assert(AGXBUF_INLINE_SIZE_0 == '\0');
332 }
333
334 agxbclear(xb);
335 return agxbstart(xb);
336 }
337
338 /* Disassociate the backing buffer from this agxbuf and return it. The buffer is
339 * NUL terminated before being returned. If the agxbuf is using stack memory,
340 * this will first copy the data to a new heap buffer to then return. If you
341 * want to temporarily access the string in the buffer, but have it overwritten
342 * and reused the next time, e.g., agxbput is called, use agxbuse instead of
343 * agxbdisown.
344 */
345 static inline char *agxbdisown(agxbuf *xb) {
346 char *buf;
347
348 if (agxbuf_is_inline(xb)) {
349 // the string lives in `store`, so we need to copy its contents to heap
350 // memory
351 buf = gv_strndup(xb->store, agxblen(xb));
352 } else {
353 // the buffer is already dynamically allocated, so terminate it and then
354 // take it as-is
355 agxbputc(xb, '\0');
356 buf = xb->buf;
357 }
358
359 // reset xb to a state where it is usable
360 memset(xb, 0, sizeof(*xb));
361
362 return buf;
363 }
364
365 /** trim extraneous trailing information from a printed floating point value
366 *
367 * tl;dr:
368 * - “42.00” → “42”
369 * - “42.01” → “42.01”
370 * - “42.10” → “42.1”
371 * - “-0.0” → “0”
372 *
373 * Printing a \p double or \p float via, for example,
374 * \p agxbprint("%.02f", 42.003) can result in output like “42.00”. If this data
375 * is destined for something that does generalized floating point
376 * parsing/decoding (e.g. SVG viewers) the “.00” is unnecessary. “42” would be
377 * interpreted identically. This function can be called after such a
378 * \p agxbprint to normalize data.
379 *
380 * \param xb Buffer to operate on
381 */
382 static inline void agxbuf_trim_zeros(agxbuf *xb) {
383
384 // find last period
385 char *start = agxbstart(xb);
386 size_t period;
387 for (period = agxblen(xb) - 1;; --period) {
388 if (period == SIZE_MAX) {
389 // we searched the entire string and did not find a period
390 return;
391 }
392 if (start[period] == '.') {
393 break;
394 }
395 }
396
397 // truncate any “0”s that provide no information
398 for (size_t follower = agxblen(xb) - 1;; --follower) {
399 if (follower == period || start[follower] == '0') {
400 // truncate this character
401 if (agxbuf_is_inline(xb)) {
402 assert(xb->located > AGXBUF_INLINE_SIZE_0);
403 --xb->located;
404 } else {
405 --xb->size;
406 }
407 if (follower == period) {
408 break;
409 }
410 } else {
411 return;
412 }
413 }
414
415 // is the remainder we have left not “-0”?
416 const size_t len = agxblen(xb);
417 if (len < 2 || start[len - 2] != '-' || start[len - 1] != '0') {
418 return;
419 }
420
421 // turn “-0” into “0”
422 start[len - 2] = '0';
423 if (agxbuf_is_inline(xb)) {
424 assert(xb->located > AGXBUF_INLINE_SIZE_0);
425 --xb->located;
426 } else {
427 --xb->size;
428 }
429 }