|
3 | 3 | * |
4 | 4 | * Sometimes libgit2 wants to return an allocated data buffer to the |
5 | 5 | * caller and have the caller take responsibility for freeing that memory. |
6 | | - * This can be awkward if the caller does not have easy access to the same |
7 | | - * allocation functions that libgit2 is using. In those cases, libgit2 |
8 | | - * will fill in a `git_buf` and the caller can use `git_buf_dispose()` to |
9 | | - * release it when they are done. |
| 6 | + * To make ownership clear in these cases, libgit2 uses `git_buf` to |
| 7 | + * return this data. Callers should use `git_buf_dispose()` to release |
| 8 | + * the memory when they are done. |
10 | 9 | * |
11 | | - * A `git_buf` may also be used for the caller to pass in a reference to |
12 | | - * a block of memory they hold. In this case, libgit2 will not resize or |
13 | | - * free the memory, but will read from it as needed. |
14 | | - * |
15 | | - * Some APIs may occasionally do something slightly unusual with a buffer, |
16 | | - * such as setting `ptr` to a value that was passed in by the user. In |
17 | | - * those cases, the behavior will be clearly documented by the API. |
| 10 | + * A `git_buf` contains a pointer to a NUL-terminated C string, and |
| 11 | + * the length of the string (not including the NUL terminator). |
18 | 12 | *) |
19 | 13 |
|
20 | 14 | type |
21 | 15 | git_buf = record |
22 | 16 | (** |
23 | | - * The buffer contents. |
24 | | - * |
25 | | - * `ptr` points to the start of the allocated memory. If it is NULL, |
26 | | - * then the `git_buf` is considered empty and libgit2 will feel free |
27 | | - * to overwrite it with new data. |
| 17 | + * The buffer contents. `ptr` points to the start of the buffer |
| 18 | + * being returned. The buffer's length (in bytes) is specified |
| 19 | + * by the `size` member of the structure, and contains a NUL |
| 20 | + * terminator at position `(size + 1)`. |
28 | 21 | *) |
29 | 22 | ptr: PAnsiChar; |
30 | 23 | (** |
31 | | - * `asize` holds the known total amount of allocated memory if the `ptr` |
32 | | - * was allocated by libgit2. It may be larger than `size`. If `ptr` |
33 | | - * was not allocated by libgit2 and should not be resized and/or freed, |
34 | | - * then `asize` will be set to zero. |
| 24 | + * This field is reserved and unused. |
35 | 25 | *) |
36 | | - asize: size_t; |
| 26 | + reserved: size_t; |
37 | 27 | (** |
38 | | - * `size` holds the size (in bytes) of the data that is actually used. |
| 28 | + * The length (in bytes) of the buffer pointed to by `ptr`, |
| 29 | + * not including a NUL terminator. |
39 | 30 | *) |
40 | 31 | size: size_t; |
41 | 32 | end; |
42 | 33 |
|
43 | 34 | (** |
44 | | - * Static initializer for git_buf from static buffer |
| 35 | + * Use to initialize a `git_buf` before passing it to a function that |
| 36 | + * will populate it. |
45 | 37 | *) |
46 | 38 |
|
47 | | - //#define GIT_BUF_INIT_CONST(STR,LEN) { (char *)(STR), 0, (size_t)(LEN) } |
| 39 | + //#define GIT_BUF_INIT { NULL, 0, 0 } |
48 | 40 |
|
49 | 41 | (** |
50 | 42 | * Free the memory referred to by the git_buf. |
51 | 43 | * |
52 | 44 | * Note that this does not free the `git_buf` itself, just the memory |
53 | | - * pointed to by `buffer->ptr`. This will not free the memory if it looks |
54 | | - * like it was not allocated internally, but it will clear the buffer back |
55 | | - * to the empty state. |
| 45 | + * pointed to by `buffer->ptr`. |
56 | 46 | * |
57 | 47 | * @param buffer The buffer to deallocate |
58 | 48 | *) |
59 | 49 |
|
60 | 50 | type |
61 | 51 | Pgit_buf = ^git_buf; |
62 | | -procedure git_buf_dispose(buffer: Pgit_buf); cdecl; external libgit2_dll; |
63 | | - |
64 | | -(** |
65 | | - * Resize the buffer allocation to make more space. |
66 | | - * |
67 | | - * This will attempt to grow the buffer to accommodate the target size. |
68 | | - * |
69 | | - * If the buffer refers to memory that was not allocated by libgit2 (i.e. |
70 | | - * the `asize` field is zero), then `ptr` will be replaced with a newly |
71 | | - * allocated block of data. Be careful so that memory allocated by the |
72 | | - * caller is not lost. As a special variant, if you pass `target_size` as |
73 | | - * 0 and the memory is not allocated by libgit2, this will allocate a new |
74 | | - * buffer of size `size` and copy the external data into it. |
75 | | - * |
76 | | - * Currently, this will never shrink a buffer, only expand it. |
77 | | - * |
78 | | - * If the allocation fails, this will return an error and the buffer will be |
79 | | - * marked as invalid for future operations, invaliding the contents. |
80 | | - * |
81 | | - * @param buffer The buffer to be resized; may or may not be allocated yet |
82 | | - * @param target_size The desired available size |
83 | | - * @return 0 on success, -1 on allocation failure |
84 | | - *) |
85 | | - |
86 | | -function git_buf_grow(buffer: Pgit_buf; target_size: size_t): Integer; cdecl; external libgit2_dll; |
87 | | - |
88 | | -(** |
89 | | - * Set buffer to a copy of some raw data. |
90 | | - * |
91 | | - * @param buffer The buffer to set |
92 | | - * @param data The data to copy into the buffer |
93 | | - * @param datalen The length of the data to copy into the buffer |
94 | | - * @return 0 on success, -1 on allocation failure |
95 | | - *) |
96 | | - |
97 | | -function git_buf_set(buffer: Pgit_buf; data: Pointer; datalen: size_t): Integer; cdecl; external libgit2_dll; |
98 | | - |
99 | | -(** |
100 | | -* Check quickly if buffer looks like it contains binary data |
101 | | -* |
102 | | -* @param buf Buffer to check |
103 | | -* @return 1 if buffer looks like non-text data |
104 | | - *) |
105 | | - |
106 | | -function git_buf_is_binary(buf: Pgit_buf): Integer; cdecl; external libgit2_dll; |
107 | | - |
108 | | -(** |
109 | | -* Check quickly if buffer contains a NUL byte |
110 | | -* |
111 | | -* @param buf Buffer to check |
112 | | -* @return 1 if buffer contains a NUL byte |
113 | | - *) |
114 | | - |
115 | | -function git_buf_contains_nul(buf: Pgit_buf): Integer; cdecl; external libgit2_dll; |
116 | 52 |
|
| 53 | +procedure git_buf_dispose(buffer: Pgit_buf); cdecl; external libgit2_dll; |
117 | 54 |
|
118 | 55 |
|
0 commit comments