quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

DYNBUF.md (3763B)


      1 <!--
      2 Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
      3 
      4 SPDX-License-Identifier: curl
      5 -->
      6 
      7 # dynbuf
      8 
      9 This is the internal module for creating and handling "dynamic buffers". This
     10 means buffers that can be appended to, dynamically and grow to adapt.
     11 
     12 There is always a terminating zero put at the end of the dynamic buffer.
     13 
     14 The `struct dynbuf` is used to hold data for each instance of a dynamic
     15 buffer. The members of that struct **MUST NOT** be accessed or modified
     16 without using the dedicated dynbuf API.
     17 
     18 ## `curlx_dyn_init`
     19 
     20 ```c
     21 void curlx_dyn_init(struct dynbuf *s, size_t toobig);
     22 ```
     23 
     24 This initializes a struct to use for dynbuf and it cannot fail. The `toobig`
     25 value **must** be set to the maximum size we allow this buffer instance to
     26 grow to. The functions below return `CURLE_OUT_OF_MEMORY` when hitting this
     27 limit.
     28 
     29 ## `curlx_dyn_free`
     30 
     31 ```c
     32 void curlx_dyn_free(struct dynbuf *s);
     33 ```
     34 
     35 Free the associated memory and clean up. After a free, the `dynbuf` struct can
     36 be reused to start appending new data to.
     37 
     38 ## `curlx_dyn_addn`
     39 
     40 ```c
     41 CURLcode curlx_dyn_addn(struct dynbuf *s, const void *mem, size_t len);
     42 ```
     43 
     44 Append arbitrary data of a given length to the end of the buffer.
     45 
     46 If this function fails it calls `curlx_dyn_free` on `dynbuf`.
     47 
     48 ## `curlx_dyn_add`
     49 
     50 ```c
     51 CURLcode curlx_dyn_add(struct dynbuf *s, const char *str);
     52 ```
     53 
     54 Append a C string to the end of the buffer.
     55 
     56 If this function fails it calls `curlx_dyn_free` on `dynbuf`.
     57 
     58 ## `curlx_dyn_addf`
     59 
     60 ```c
     61 CURLcode curlx_dyn_addf(struct dynbuf *s, const char *fmt, ...);
     62 ```
     63 
     64 Append a `printf()`-style string to the end of the buffer.
     65 
     66 If this function fails it calls `curlx_dyn_free` on `dynbuf`.
     67 
     68 ## `curlx_dyn_vaddf`
     69 
     70 ```c
     71 CURLcode curlx_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap);
     72 ```
     73 
     74 Append a `vprintf()`-style string to the end of the buffer.
     75 
     76 If this function fails it calls `curlx_dyn_free` on `dynbuf`.
     77 
     78 ## `curlx_dyn_reset`
     79 
     80 ```c
     81 void curlx_dyn_reset(struct dynbuf *s);
     82 ```
     83 
     84 Reset the buffer length, but leave the allocation.
     85 
     86 ## `curlx_dyn_tail`
     87 
     88 ```c
     89 CURLcode curlx_dyn_tail(struct dynbuf *s, size_t length);
     90 ```
     91 
     92 Keep `length` bytes of the buffer tail (the last `length` bytes of the
     93 buffer). The rest of the buffer is dropped. The specified `length` must not be
     94 larger than the buffer length. To instead keep the leading part, see
     95 `curlx_dyn_setlen()`.
     96 
     97 ## `curlx_dyn_ptr`
     98 
     99 ```c
    100 char *curlx_dyn_ptr(const struct dynbuf *s);
    101 ```
    102 
    103 Returns a `char *` to the buffer if it has a length, otherwise may return
    104 NULL. Since the buffer may be reallocated, this pointer should not be trusted
    105 or used anymore after the next buffer manipulation call.
    106 
    107 ## `curlx_dyn_uptr`
    108 
    109 ```c
    110 unsigned char *curlx_dyn_uptr(const struct dynbuf *s);
    111 ```
    112 
    113 Returns an `unsigned char *` to the buffer if it has a length, otherwise may
    114 return NULL. Since the buffer may be reallocated, this pointer should not be
    115 trusted or used anymore after the next buffer manipulation call.
    116 
    117 ## `curlx_dyn_len`
    118 
    119 ```c
    120 size_t curlx_dyn_len(const struct dynbuf *s);
    121 ```
    122 
    123 Returns the length of the buffer in bytes. Does not include the terminating
    124 zero byte.
    125 
    126 ## `curlx_dyn_setlen`
    127 
    128 ```c
    129 CURLcode curlx_dyn_setlen(struct dynbuf *s, size_t len);
    130 ```
    131 
    132 Sets the new shorter length of the buffer in number of bytes. Keeps the
    133 leftmost set number of bytes, discards the rest. To instead keep the tail part
    134 of the buffer, see `curlx_dyn_tail()`.
    135 
    136 ## `curlx_dyn_take`
    137 
    138 ```c
    139 char *curlx_dyn_take(struct dynbuf *s, size_t *plen);
    140 ```
    141 
    142 Transfers ownership of the internal buffer to the caller. The dynbuf
    143 resets to its initial state. The returned pointer may be `NULL` if the
    144 dynbuf never allocated memory. The returned length is the amount of
    145 data written to the buffer. The actual allocated memory might be larger.