LCOV - code coverage report
Current view: top level - include/internal - cgrowbuf.h (source / functions) Coverage Total Hit
Test: c_collections 2.0.0 Lines: 100.0 % 16 16
Test Date: 2026-11-15 18:57:05 Functions: 100.0 % 3 3
Legend: Lines: hit not hit

            Line data    Source code
       1              : /*
       2              :  * MIT License
       3              :  *
       4              :  * Copyright (c) 2026 - A bunch of nerds
       5              :  *
       6              :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       7              :  * of this software and associated documentation files (the "Software"), to
       8              :  * deal in the Software without restriction, including without limitation the
       9              :  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
      10              :  * sell copies of the Software, and to permit persons to whom the Software is
      11              :  * furnished to do so, subject to the following conditions:
      12              :  *
      13              :  * The above copyright notice and this permission notice shall be included in
      14              :  * all copies or substantial portions of the Software.
      15              :  *
      16              :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      17              :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      18              :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      19              :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      20              :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      21              :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      22              :  * DEALINGS IN THE SOFTWARE.
      23              :  */
      24              : 
      25              : // INTERNAL ONLY. A growable byte buffer with a latching out-of-memory flag,
      26              : // for code that builds text piece by piece (the serializers, the record
      27              : // builder of the logger, text that a parse collects). Once an allocation fails
      28              : // every later operation on the buffer does nothing, so the caller checks for
      29              : // an error once, at the end of the whole build. The header isn't installed and
      30              : // its symbols aren't exported.
      31              : #ifndef CCOL_CGROWBUF_H
      32              : #define CCOL_CGROWBUF_H
      33              : 
      34              : #include <common.h>
      35              : // C23 has bool, true and false as keywords.
      36              : #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 202311L
      37              : #include <stdbool.h>
      38              : #endif
      39              : #include <stddef.h>
      40              : #include <stdint.h>
      41              : #include <string.h>
      42              : 
      43              : // The buffer. When an allocation fails, or a size is too large to represent,
      44              : // oom latches to true. After the build the caller reads buf, len and oom
      45              : // directly; buf is NUL-terminated but can be NULL when oom is set. cap and
      46              : // m_procs are internal bookkeeping.
      47              : typedef struct {
      48              :   char *buf;
      49              :   size_t len;
      50              :   size_t cap;
      51              :   bool oom;
      52              :   ccol_memmgmt_procs *m_procs;
      53              : } ccol_growbuf;
      54              : 
      55              : // Sets b up with a 256-byte store, so a builder can start appending. Every
      56              : // later growth allocates through mp. A failed allocation sets oom.
      57              : void ccol_growbuf_init(ccol_growbuf *b, ccol_memmgmt_procs *mp);
      58              : 
      59              : // Like ccol_growbuf_init(), for a caller that knows roughly how much it will
      60              : // append, so it doesn't pay for growth steps: The store holds at least hint + 1
      61              : // bytes (never less than 64). A hint of SIZE_MAX can't be represented, so it
      62              : // sets oom.
      63              : void ccol_growbuf_init_hint(ccol_growbuf *b, ccol_memmgmt_procs *mp,
      64              :                             size_t hint);
      65              : 
      66              : // Appends n bytes of data to b, growing the buffer when needed. It's the out of
      67              : // line half of ccol_growbuf_append(), which calls it for every case that the
      68              : // inline path doesn't handle. data may be NULL when n is 0. Does nothing once
      69              : // oom is set, and sets oom when the new size can't be represented or the
      70              : // allocation fails.
      71              : void ccol_growbuf_append_slow(ccol_growbuf *b, const char *data, size_t n);
      72              : 
      73              : // Appends n bytes of data to b; it's the append that every builder calls. The
      74              : // common case (n bytes that fit in the room already allocated) runs inline and
      75              : // costs no call. Growth, an empty append and the oom state go to
      76              : // ccol_growbuf_append_slow(). The n <= PTRDIFF_MAX bound changes nothing at run
      77              : // time; it tells the fortify checks that the memcpy never sees a larger
      78              : // length.
      79      3207502 : static inline void ccol_growbuf_append(ccol_growbuf *b, const char *data,
      80              :                                        size_t n) {
      81      9621711 :   if (__builtin_expect(
      82      3207502 :           !b->oom && n != 0 && n <= (size_t)PTRDIFF_MAX && n < b->cap - b->len,
      83              :           1)) {
      84      3205668 :     memcpy(b->buf + b->len, data, n);
      85      3205668 :     b->len += n;
      86      3205668 :     b->buf[b->len] = '\0';
      87      3205668 :     return;
      88              :   }
      89         1834 :   ccol_growbuf_append_slow(b, data, n);
      90              : }
      91              : 
      92              : // Appends the byte c to b, for builders that emit punctuation one byte at a
      93              : // time.
      94      2641718 : static inline void ccol_growbuf_append_c(ccol_growbuf *b, char c) {
      95      2641718 :   ccol_growbuf_append(b, &c, 1);
      96              : }
      97              : 
      98              : // Appends the NUL-terminated string s to b, for builders that emit literals. A
      99              : // NULL s is an empty append, so a caller can pass along the buf field of
     100              : // another buffer that ran out of memory.
     101        57709 : static inline void ccol_growbuf_append_cstr(ccol_growbuf *b, const char *s) {
     102        57709 :   if (!s) return;
     103        57709 :   ccol_growbuf_append(b, s, strlen(s));
     104              : }
     105              : 
     106              : // Frees the store of b (not b itself), for a builder that drops its result or
     107              : // fails. Safe on a buffer in the oom state.
     108        26248 : static inline void ccol_growbuf_destroy(ccol_growbuf *b) {
     109        26248 :   ccol__mem_free(b->m_procs, b->buf);
     110        26248 : }
     111              : 
     112              : #endif
        

Generated by: LCOV version 2.0-1