2024-03-31 17:52:28 +08:00
|
|
|
<!--
|
|
|
|
Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
|
|
|
|
|
|
|
SPDX-License-Identifier: curl
|
|
|
|
-->
|
|
|
|
|
2021-03-23 09:02:18 +08:00
|
|
|
# bufref
|
|
|
|
|
|
|
|
This is an internal module for handling buffer references. A referenced
|
|
|
|
buffer is associated with its destructor function that is implicitly called
|
|
|
|
when the reference is invalidated. Once referenced, a buffer cannot be
|
|
|
|
reallocated.
|
|
|
|
|
|
|
|
A data length is stored within the reference for binary data handling
|
2022-01-27 09:12:50 +08:00
|
|
|
purposes; it is not used by the bufref API.
|
2021-03-23 09:02:18 +08:00
|
|
|
|
|
|
|
The `struct bufref` is used to hold data referencing a buffer. The members of
|
|
|
|
that structure **MUST NOT** be accessed or modified without using the dedicated
|
|
|
|
bufref API.
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `init`
|
2021-03-23 09:02:18 +08:00
|
|
|
|
|
|
|
```c
|
|
|
|
void Curl_bufref_init(struct bufref *br);
|
|
|
|
```
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
Initializes a `bufref` structure. This function **MUST** be called before any
|
2021-03-23 09:02:18 +08:00
|
|
|
other operation is performed on the structure.
|
|
|
|
|
|
|
|
Upon completion, the referenced buffer is `NULL` and length is zero.
|
|
|
|
|
|
|
|
This function may also be called to bypass referenced buffer destruction while
|
|
|
|
invalidating the current reference.
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `free`
|
2021-03-23 09:02:18 +08:00
|
|
|
|
|
|
|
```c
|
|
|
|
void Curl_bufref_free(struct bufref *br);
|
|
|
|
```
|
|
|
|
|
|
|
|
Destroys the previously referenced buffer using its destructor and
|
2022-09-21 05:30:19 +08:00
|
|
|
reinitializes the structure for a possible subsequent reuse.
|
2021-03-23 09:02:18 +08:00
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `set`
|
2021-03-23 09:02:18 +08:00
|
|
|
|
|
|
|
```c
|
|
|
|
void Curl_bufref_set(struct bufref *br, const void *buffer, size_t length,
|
|
|
|
void (*destructor)(void *));
|
|
|
|
```
|
|
|
|
|
2021-05-31 18:47:15 +08:00
|
|
|
Releases the previously referenced buffer, then assigns the new `buffer` to
|
2022-01-27 09:12:50 +08:00
|
|
|
the structure, associated with its `destructor` function. The latter can be
|
2024-02-27 14:48:10 +08:00
|
|
|
specified as `NULL`: this is the case when the referenced buffer is static.
|
2021-03-23 09:02:18 +08:00
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
if `buffer` is NULL, `length` must be zero.
|
2021-03-23 09:02:18 +08:00
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `memdup`
|
2021-03-23 09:02:18 +08:00
|
|
|
|
|
|
|
```c
|
|
|
|
CURLcode Curl_bufref_memdup(struct bufref *br, const void *data, size_t length);
|
|
|
|
```
|
|
|
|
|
2021-05-31 18:47:15 +08:00
|
|
|
Releases the previously referenced buffer, then duplicates the `length`-byte
|
2021-07-18 08:45:38 +08:00
|
|
|
`data` into a buffer allocated via `malloc()` and references the latter
|
2021-03-23 09:02:18 +08:00
|
|
|
associated with destructor `curl_free()`.
|
|
|
|
|
2022-09-17 23:32:21 +08:00
|
|
|
An additional trailing byte is allocated and set to zero as a possible string
|
|
|
|
null-terminator; it is not counted in the stored length.
|
2021-03-23 09:02:18 +08:00
|
|
|
|
|
|
|
Returns `CURLE_OK` if successful, else `CURLE_OUT_OF_MEMORY`.
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `ptr`
|
2021-03-23 09:02:18 +08:00
|
|
|
|
|
|
|
```c
|
|
|
|
const unsigned char *Curl_bufref_ptr(const struct bufref *br);
|
|
|
|
```
|
|
|
|
|
2021-05-31 18:47:15 +08:00
|
|
|
Returns a `const unsigned char *` to the referenced buffer.
|
2021-03-23 09:02:18 +08:00
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `len`
|
2021-03-23 09:02:18 +08:00
|
|
|
|
|
|
|
```c
|
|
|
|
size_t Curl_bufref_len(const struct bufref *br);
|
|
|
|
```
|
|
|
|
|
|
|
|
Returns the stored length of the referenced buffer.
|