diff options
author | Daniel Stenberg <daniel@haxx.se> | 2020-05-02 17:04:08 +0200 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2020-05-04 10:40:39 +0200 |
commit | ed35d6590e72c23c568af1e3b8ac6e4e2d883888 (patch) | |
tree | 57555732f4f452bf84c3c7296581485be064a853 /docs | |
parent | 00c2e8da9a9555ce6171e3f7ddc5e43fc6f9bb4b (diff) |
dynbuf: introduce internal generic dynamic buffer functions
A common set of functions instead of many separate implementations for
creating buffers that can grow when appending data to them. Existing
functionality has been ported over.
In my early basic testing, the total number of allocations seem at
roughly the same amount as before, possibly a few less.
See docs/DYNBUF.md for a description of the API.
Closes #5300
Diffstat (limited to 'docs')
-rw-r--r-- | docs/DYNBUF.md | 80 | ||||
-rw-r--r-- | docs/Makefile.am | 1 |
2 files changed, 81 insertions, 0 deletions
diff --git a/docs/DYNBUF.md b/docs/DYNBUF.md new file mode 100644 index 000000000..ff9009de3 --- /dev/null +++ b/docs/DYNBUF.md @@ -0,0 +1,80 @@ +# dynbuf + +This is the internal module for creating and handling "dynamic buffers". This +means buffers that can be appended to, dynamically and grow in size to adapt. + +There will always be a terminating zero put at the end of the dynamic buffer. + +The `struct dynbuf` is used to hold data for each instance of a dynamic +buffer. The members of that struct **MUST NOT** be accessed or modified +without using the dedicated dynbuf API. + +## init + + void Curl_dyn_init(struct dynbuf *s, size_t toobig); + +This inits a struct to use for dynbuf and it can't fail. The `toobig` value +**must** be set to the maximum size we allow this buffer instance to grow to. +The functions below will return `CURLE_OUT_OF_MEMORY` when hitting this limit. + +## free + + void Curl_dyn_free(struct dynbuf *s); + +Free the associated memory and clean up. After a free, the `dynbuf` struct can +be re-used to start appending new data to. + +## addn + + CURLcode Curl_dyn_addn(struct dynbuf *s, const void *mem, size_t len); + +Append arbitrary data of a given length to the end of the buffer. + +## add + + CURLcode Curl_dyn_add(struct dynbuf *s, const char *str); + +Append a C string to the end of the buffer. + +## addf + + CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...); + +Append a `printf()`-style string to the end of the buffer. + +## reset + + void Curl_dyn_reset(struct dynbuf *s); + +Reset the buffer length, but leave the allocation. + +## tail + + CURLcode Curl_dyn_trail(struct dynbuf *s, size_t length) + +Keep `length` bytes of the buffer tail (the last `length` bytes of the +buffer). The rest of the buffer is dropped. The specified `length` must not be +larger than the buffer length. (**This function is currently not provided**.) + +## ptr + + char *Curl_dyn_ptr(const struct dynbuf *s); + +Returns a `char *` to the buffer. Since the buffer may be reallocated, this +pointer should not be trusted or used anymore after the next buffer +manipulation call. + +## uptr + + unsigned char *Curl_dyn_uptr(const struct dynbuf *s); + +Returns an `unsigned char *` to the buffer. Since the buffer may be +reallocated, this pointer should not be trusted or used anymore after the next +buffer manipulation call. + +## len + + size_t Curl_dyn_len(const struct dynbuf *s); + +Returns the length of the buffer in bytes. Does not include the terminating +zero byte. diff --git a/docs/Makefile.am b/docs/Makefile.am index 860be45f2..51bcf16c6 100644 --- a/docs/Makefile.am +++ b/docs/Makefile.am @@ -54,6 +54,7 @@ EXTRA_DIST = \ CONTRIBUTE.md \ CURL-DISABLE.md \ DEPRECATE.md \ + DYNBUF.md \ ESNI.md \ EXPERIMENTAL.md \ FAQ \ |