xref: /aosp_15_r20/external/curl/docs/internals/BUFREF.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker<!--
2*6236dae4SAndroid Build Coastguard WorkerCopyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard Worker
4*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
5*6236dae4SAndroid Build Coastguard Worker-->
6*6236dae4SAndroid Build Coastguard Worker
7*6236dae4SAndroid Build Coastguard Worker# bufref
8*6236dae4SAndroid Build Coastguard Worker
9*6236dae4SAndroid Build Coastguard WorkerThis is an internal module for handling buffer references. A referenced
10*6236dae4SAndroid Build Coastguard Workerbuffer is associated with its destructor function that is implicitly called
11*6236dae4SAndroid Build Coastguard Workerwhen the reference is invalidated. Once referenced, a buffer cannot be
12*6236dae4SAndroid Build Coastguard Workerreallocated.
13*6236dae4SAndroid Build Coastguard Worker
14*6236dae4SAndroid Build Coastguard WorkerA data length is stored within the reference for binary data handling
15*6236dae4SAndroid Build Coastguard Workerpurposes; it is not used by the bufref API.
16*6236dae4SAndroid Build Coastguard Worker
17*6236dae4SAndroid Build Coastguard WorkerThe `struct bufref` is used to hold data referencing a buffer. The members of
18*6236dae4SAndroid Build Coastguard Workerthat structure **MUST NOT** be accessed or modified without using the dedicated
19*6236dae4SAndroid Build Coastguard Workerbufref API.
20*6236dae4SAndroid Build Coastguard Worker
21*6236dae4SAndroid Build Coastguard Worker## `init`
22*6236dae4SAndroid Build Coastguard Worker
23*6236dae4SAndroid Build Coastguard Worker```c
24*6236dae4SAndroid Build Coastguard Workervoid Curl_bufref_init(struct bufref *br);
25*6236dae4SAndroid Build Coastguard Worker```
26*6236dae4SAndroid Build Coastguard Worker
27*6236dae4SAndroid Build Coastguard WorkerInitializes a `bufref` structure. This function **MUST** be called before any
28*6236dae4SAndroid Build Coastguard Workerother operation is performed on the structure.
29*6236dae4SAndroid Build Coastguard Worker
30*6236dae4SAndroid Build Coastguard WorkerUpon completion, the referenced buffer is `NULL` and length is zero.
31*6236dae4SAndroid Build Coastguard Worker
32*6236dae4SAndroid Build Coastguard WorkerThis function may also be called to bypass referenced buffer destruction while
33*6236dae4SAndroid Build Coastguard Workerinvalidating the current reference.
34*6236dae4SAndroid Build Coastguard Worker
35*6236dae4SAndroid Build Coastguard Worker## `free`
36*6236dae4SAndroid Build Coastguard Worker
37*6236dae4SAndroid Build Coastguard Worker```c
38*6236dae4SAndroid Build Coastguard Workervoid Curl_bufref_free(struct bufref *br);
39*6236dae4SAndroid Build Coastguard Worker```
40*6236dae4SAndroid Build Coastguard Worker
41*6236dae4SAndroid Build Coastguard WorkerDestroys the previously referenced buffer using its destructor and
42*6236dae4SAndroid Build Coastguard Workerreinitializes the structure for a possible subsequent reuse.
43*6236dae4SAndroid Build Coastguard Worker
44*6236dae4SAndroid Build Coastguard Worker## `set`
45*6236dae4SAndroid Build Coastguard Worker
46*6236dae4SAndroid Build Coastguard Worker```c
47*6236dae4SAndroid Build Coastguard Workervoid Curl_bufref_set(struct bufref *br, const void *buffer, size_t length,
48*6236dae4SAndroid Build Coastguard Worker                     void (*destructor)(void *));
49*6236dae4SAndroid Build Coastguard Worker```
50*6236dae4SAndroid Build Coastguard Worker
51*6236dae4SAndroid Build Coastguard WorkerReleases the previously referenced buffer, then assigns the new `buffer` to
52*6236dae4SAndroid Build Coastguard Workerthe structure, associated with its `destructor` function. The latter can be
53*6236dae4SAndroid Build Coastguard Workerspecified as `NULL`: this is the case when the referenced buffer is static.
54*6236dae4SAndroid Build Coastguard Worker
55*6236dae4SAndroid Build Coastguard Workerif `buffer` is NULL, `length` must be zero.
56*6236dae4SAndroid Build Coastguard Worker
57*6236dae4SAndroid Build Coastguard Worker## `memdup`
58*6236dae4SAndroid Build Coastguard Worker
59*6236dae4SAndroid Build Coastguard Worker```c
60*6236dae4SAndroid Build Coastguard WorkerCURLcode Curl_bufref_memdup(struct bufref *br, const void *data, size_t length);
61*6236dae4SAndroid Build Coastguard Worker```
62*6236dae4SAndroid Build Coastguard Worker
63*6236dae4SAndroid Build Coastguard WorkerReleases the previously referenced buffer, then duplicates the `length`-byte
64*6236dae4SAndroid Build Coastguard Worker`data` into a buffer allocated via `malloc()` and references the latter
65*6236dae4SAndroid Build Coastguard Workerassociated with destructor `curl_free()`.
66*6236dae4SAndroid Build Coastguard Worker
67*6236dae4SAndroid Build Coastguard WorkerAn additional trailing byte is allocated and set to zero as a possible string
68*6236dae4SAndroid Build Coastguard Workernull-terminator; it is not counted in the stored length.
69*6236dae4SAndroid Build Coastguard Worker
70*6236dae4SAndroid Build Coastguard WorkerReturns `CURLE_OK` if successful, else `CURLE_OUT_OF_MEMORY`.
71*6236dae4SAndroid Build Coastguard Worker
72*6236dae4SAndroid Build Coastguard Worker## `ptr`
73*6236dae4SAndroid Build Coastguard Worker
74*6236dae4SAndroid Build Coastguard Worker```c
75*6236dae4SAndroid Build Coastguard Workerconst unsigned char *Curl_bufref_ptr(const struct bufref *br);
76*6236dae4SAndroid Build Coastguard Worker```
77*6236dae4SAndroid Build Coastguard Worker
78*6236dae4SAndroid Build Coastguard WorkerReturns a `const unsigned char *` to the referenced buffer.
79*6236dae4SAndroid Build Coastguard Worker
80*6236dae4SAndroid Build Coastguard Worker## `len`
81*6236dae4SAndroid Build Coastguard Worker
82*6236dae4SAndroid Build Coastguard Worker```c
83*6236dae4SAndroid Build Coastguard Workersize_t Curl_bufref_len(const struct bufref *br);
84*6236dae4SAndroid Build Coastguard Worker```
85*6236dae4SAndroid Build Coastguard Worker
86*6236dae4SAndroid Build Coastguard WorkerReturns the stored length of the referenced buffer.
87