diff options
Diffstat (limited to 'include/git2')
-rw-r--r-- | include/git2/blob.h | 10 | ||||
-rw-r--r-- | include/git2/buffer.h | 96 | ||||
-rw-r--r-- | include/git2/filter.h | 23 | ||||
-rw-r--r-- | include/git2/sys/filter.h | 18 |
4 files changed, 79 insertions, 68 deletions
diff --git a/include/git2/blob.h b/include/git2/blob.h index dcb815b2f..dcab4fbe0 100644 --- a/include/git2/blob.h +++ b/include/git2/blob.h @@ -104,17 +104,17 @@ GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob); * CRLF filtering or other types of changes depending on the file * attributes set for the blob and the content detected in it. * - * The output is written into a `git_buffer` which the caller must free - * when done (via `git_buffer_free`). + * The output is written into a `git_buf` which the caller must free + * when done (via `git_buf_free`). * * If no filters need to be applied, then the `out` buffer will just be * populated with a pointer to the raw content of the blob. In that case, * be careful to *not* free the blob until done with the buffer. To keep - * the data detached from the blob, call `git_buffer_resize` on the buffer + * the data detached from the blob, call `git_buf_grow` on the buffer * with a `want_size` of 0 and the buffer will be reallocated to be * detached from the blob. * - * @param out The git_buffer to be filled in + * @param out The git_buf to be filled in * @param blob Pointer to the blob * @param as_path Path used for file attribute lookups, etc. * @param check_for_binary_data Should this test if blob content contains @@ -122,7 +122,7 @@ GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob); * @return 0 on success or an error code */ GIT_EXTERN(int) git_blob_filtered_content( - git_buffer *out, + git_buf *out, git_blob *blob, const char *as_path, int check_for_binary_data); diff --git a/include/git2/buffer.h b/include/git2/buffer.h index cb80e48f7..ae8681f13 100644 --- a/include/git2/buffer.h +++ b/include/git2/buffer.h @@ -4,8 +4,8 @@ * This file is part of libgit2, distributed under the GNU GPL v2 with * a Linking Exception. For full terms see the included COPYING file. */ -#ifndef INCLUDE_git_buffer_h__ -#define INCLUDE_git_buffer_h__ +#ifndef INCLUDE_git_buf_h__ +#define INCLUDE_git_buf_h__ #include "common.h" @@ -25,59 +25,69 @@ GIT_BEGIN_DECL * caller and have the caller take responsibility for freeing that memory. * This can be awkward if the caller does not have easy access to the same * allocation functions that libgit2 is using. In those cases, libgit2 - * will instead fill in a `git_buffer` and the caller can use - * `git_buffer_free()` to release it when they are done. - * - * * `ptr` refers to the start of the allocated memory. - * * `size` contains the size of the data in `ptr` that is actually used. - * * `available` refers to the known total amount of allocated memory. It - * may be larger than the `size` actually in use. - * - * In a few cases, for uniformity and simplicity, an API may populate a - * `git_buffer` with data that should *not* be freed (i.e. the lifetime of - * the data buffer is actually tied to another libgit2 object). These - * cases will be clearly documented in the APIs that use the `git_buffer`. - * In those cases, the `available` field will be set to zero even though - * the `ptr` and `size` will be valid. + * will fill in a `git_buf` and the caller can use `git_buf_free()` to + * release it when they are done. + * + * A `git_buf` may also be used for the caller to pass in a reference to + * a block of memory they hold. In this case, libgit2 will not resize or + * free the memory, but will read from it as needed. + * + * A `git_buf` is a public structure with three fields: + * + * - `ptr` points to the start of the allocated memory. If it is NULL, + * then the `git_buf` is considered empty and libgit2 will feel free + * to overwrite it with new data. + * + * - `size` holds the size (in bytes) of the data that is actually used. + * + * - `asize` holds the known total amount of allocated memory if the `ptr` + * was allocated by libgit2. It may be larger than `size`. If `ptr` + * was not allocated by libgit2 and should not be resized and/or freed, + * then `asize` will be set to zero. + * + * Some APIs may occasionally do something slightly unusual with a buffer, + * such as setting `ptr` to a value that was passed in by the user. In + * those cases, the behavior will be clearly documented by the API. */ -typedef struct git_buffer { +typedef struct { char *ptr; - size_t size; - size_t available; -} git_buffer; + size_t asize, size; +} git_buf; /** - * Use to initialize buffer structure when git_buffer is on stack - */ -#define GIT_BUFFER_INIT { NULL, 0, 0 } - -/** - * Free the memory referred to by the git_buffer. + * Free the memory referred to by the git_buf. * - * Note that this does not free the `git_buffer` itself, just the memory - * pointed to by `buffer->ptr`. If that memory was not allocated by - * libgit2 itself, be careful with using this function because it could - * cause problems. + * Note that this does not free the `git_buf` itself, just the memory + * pointed to by `buffer->ptr`. This will not free the memory if it looks + * like it was not allocated internally, but it will clear the buffer back + * to the empty state. * - * @param buffer The buffer with allocated memory + * @param buffer The buffer to deallocate */ -GIT_EXTERN(void) git_buffer_free(git_buffer *buffer); +GIT_EXTERN(void) git_buf_free(git_buf *buffer); /** * Resize the buffer allocation to make more space. * - * This will update `buffer->available` with the new size (which will be - * at least `want_size` and may be larger). This may or may not change - * `buffer->ptr` depending on whether there is an existing allocation and - * whether that allocation can be increased in place. + * This will attempt to grow the buffer to accomodate the target size. + * + * If the buffer refers to memory that was not allocated by libgit2 (i.e. + * the `asize` field is zero), then `ptr` will be replaced with a newly + * allocated block of data. Be careful so that memory allocated by the + * caller is not lost. As a special variant, if you pass `target_size` as + * 0 and the memory is not allocated by libgit2, this will allocate a new + * buffer of size `size` and copy the external data into it. + * + * Currently, this will never shrink a buffer, only expand it. * - * Currently, this will never shrink the buffer, only expand it. + * If the allocation fails, this will return an error and the buffer will be + * marked as invalid for future operations, invaliding the contents. * * @param buffer The buffer to be resized; may or may not be allocated yet - * @param want_size The desired available size - * @return 0 on success, negative error code on allocation failure + * @param target_size The desired available size + * @return 0 on success, -1 on allocation failure */ -GIT_EXTERN(int) git_buffer_resize(git_buffer *buffer, size_t want_size); +GIT_EXTERN(int) git_buf_grow(git_buf *buffer, size_t target_size); /** * Set buffer to a copy of some raw data. @@ -85,10 +95,10 @@ GIT_EXTERN(int) git_buffer_resize(git_buffer *buffer, size_t want_size); * @param buffer The buffer to set * @param data The data to copy into the buffer * @param datalen The length of the data to copy into the buffer - * @return 0 on success, negative error code on allocation failure + * @return 0 on success, -1 on allocation failure */ -GIT_EXTERN(int) git_buffer_copy( - git_buffer *buffer, const void *data, size_t datalen); +GIT_EXTERN(int) git_buf_set( + git_buf *buffer, const void *data, size_t datalen); GIT_END_DECL diff --git a/include/git2/filter.h b/include/git2/filter.h index 649ed97cf..f96b6766b 100644 --- a/include/git2/filter.h +++ b/include/git2/filter.h @@ -88,16 +88,17 @@ GIT_EXTERN(int) git_filter_list_load( /** * Apply filter list to a data buffer. * - * See `git2/buffer.h` for background on `git_buffer` objects. + * See `git2/buffer.h` for background on `git_buf` objects. * - * If the `in` buffer refers to data managed by libgit2 - * (i.e. `in->available` is not zero), then it will be overwritten when - * applying the filters. If not, then it will be left untouched. + * If the `in` buffer holds data allocated by libgit2 (i.e. `in->asize` is + * not zero), then it will be overwritten when applying the filters. If + * not, then it will be left untouched. * * If there are no filters to apply (or `filters` is NULL), then the `out` - * buffer will reference the `in` buffer data (with `available` set to - * zero) instead of allocating data. This keeps allocations to a minimum, - * but it means you have to be careful about freeing the `in` data. + * buffer will reference the `in` buffer data (with `asize` set to zero) + * instead of allocating data. This keeps allocations to a minimum, but + * it means you have to be careful about freeing the `in` data since `out` + * may be pointing to it! * * @param out Buffer to store the result of the filtering * @param filters A loaded git_filter_list (or NULL) @@ -105,15 +106,15 @@ GIT_EXTERN(int) git_filter_list_load( * @return 0 on success, an error code otherwise */ GIT_EXTERN(int) git_filter_list_apply_to_data( - git_buffer *out, + git_buf *out, git_filter_list *filters, - git_buffer *in); + git_buf *in); /** * Apply filter list to the contents of a file on disk */ GIT_EXTERN(int) git_filter_list_apply_to_file( - git_buffer *out, + git_buf *out, git_filter_list *filters, git_repository *repo, const char *path); @@ -122,7 +123,7 @@ GIT_EXTERN(int) git_filter_list_apply_to_file( * Apply filter list to the contents of a blob */ GIT_EXTERN(int) git_filter_list_apply_to_blob( - git_buffer *out, + git_buf *out, git_filter_list *filters, git_blob *blob); diff --git a/include/git2/sys/filter.h b/include/git2/sys/filter.h index c35fe55f6..36e97fe91 100644 --- a/include/git2/sys/filter.h +++ b/include/git2/sys/filter.h @@ -4,8 +4,8 @@ * This file is part of libgit2, distributed under the GNU GPL v2 with * a Linking Exception. For full terms see the included COPYING file. */ -#ifndef INCLUDE_sys_git_config_backend_h__ -#define INCLUDE_sys_git_config_backend_h__ +#ifndef INCLUDE_sys_git_filter_h__ +#define INCLUDE_sys_git_filter_h__ #include "git2/filter.h" @@ -117,19 +117,19 @@ typedef void (*git_filter_shutdown_fn)(git_filter *self); * Callback to decide if a given source needs this filter */ typedef int (*git_filter_check_fn)( - git_filter *self, - void **payload, /* points to NULL ptr on entry, may be set */ + git_filter *self, + void **payload, /* points to NULL ptr on entry, may be set */ const git_filter_source *src, - const char **attr_values); + const char **attr_values); /** * Callback to actually perform the data filtering */ typedef int (*git_filter_apply_fn)( - git_filter *self, - void **payload, /* may be read and/or set */ - git_buffer *to, - const git_buffer *from, + git_filter *self, + void **payload, /* may be read and/or set */ + git_buf *to, + const git_buf *from, const git_filter_source *src); /** |