diff options
Diffstat (limited to 'include/git2')
-rw-r--r-- | include/git2/blob.h | 1 | ||||
-rw-r--r-- | include/git2/buffer.h | 134 | ||||
-rw-r--r-- | include/git2/deprecated.h | 27 | ||||
-rw-r--r-- | include/git2/describe.h | 2 | ||||
-rw-r--r-- | include/git2/filter.h | 2 | ||||
-rw-r--r-- | include/git2/mailmap.h | 1 | ||||
-rw-r--r-- | include/git2/message.h | 1 | ||||
-rw-r--r-- | include/git2/object.h | 2 | ||||
-rw-r--r-- | include/git2/refspec.h | 2 | ||||
-rw-r--r-- | include/git2/repository.h | 1 | ||||
-rw-r--r-- | include/git2/sys/mempack.h | 2 | ||||
-rw-r--r-- | include/git2/types.h | 1 | ||||
-rw-r--r-- | include/git2/userbuf.h | 47 | ||||
-rw-r--r-- | include/git2/worktree.h | 1 |
14 files changed, 64 insertions, 160 deletions
diff --git a/include/git2/blob.h b/include/git2/blob.h index 325ec44c5..1cf39eb1d 100644 --- a/include/git2/blob.h +++ b/include/git2/blob.h @@ -11,7 +11,6 @@ #include "types.h" #include "oid.h" #include "object.h" -#include "buffer.h" #include "userbuf.h" /** diff --git a/include/git2/buffer.h b/include/git2/buffer.h deleted file mode 100644 index 926f1332d..000000000 --- a/include/git2/buffer.h +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * 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_buf_h__ -#define INCLUDE_git_buf_h__ - -#include "common.h" - -/** - * @file git2/buffer.h - * @brief Buffer export structure - * - * @ingroup Git - * @{ - */ -GIT_BEGIN_DECL - -/** - * A data buffer for exporting data from libgit2 - * - * Sometimes libgit2 wants to return an allocated data buffer to the - * 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 fill in a `git_buf` and the caller can use `git_buf_dispose()` 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. - * - * 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 { - /** - * The buffer contents. - * - * `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. - */ - char *ptr; - - /** - * `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. - */ - size_t asize; - - /** - * `size` holds the size (in bytes) of the data that is actually used. - */ - size_t size; -} git_buf; - -/** - * Static initializer for git_buf from static buffer - */ -#define GIT_BUF_INIT_CONST(STR,LEN) { (char *)(STR), 0, (size_t)(LEN) } - -/** - * Free the memory referred to by the git_buf. - * - * 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 to deallocate - */ -GIT_EXTERN(void) git_buf_dispose(git_buf *buffer); - -/** - * Resize the buffer allocation to make more space. - * - * This will attempt to grow the buffer to accommodate 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. - * - * 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 target_size The desired available size - * @return 0 on success, -1 on allocation failure - */ -GIT_EXTERN(int) git_buf_grow(git_buf *buffer, size_t target_size); - -/** - * Set buffer to a copy of some raw data. - * - * @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, -1 on allocation failure - */ -GIT_EXTERN(int) git_buf_set( - git_buf *buffer, const void *data, size_t datalen); - -/** -* Check quickly if buffer looks like it contains binary data -* -* @param buf Buffer to check -* @return 1 if buffer looks like non-text data -*/ -GIT_EXTERN(int) git_buf_is_binary(const git_buf *buf); - -/** -* Check quickly if buffer contains a NUL byte -* -* @param buf Buffer to check -* @return 1 if buffer contains a NUL byte -*/ -GIT_EXTERN(int) git_buf_contains_nul(const git_buf *buf); - -GIT_END_DECL - -/** @} */ - -#endif diff --git a/include/git2/deprecated.h b/include/git2/deprecated.h index 7be6bb258..9fd4489a9 100644 --- a/include/git2/deprecated.h +++ b/include/git2/deprecated.h @@ -11,7 +11,6 @@ #include "config.h" #include "common.h" #include "blame.h" -#include "buffer.h" #include "checkout.h" #include "cherrypick.h" #include "clone.h" @@ -123,17 +122,23 @@ GIT_EXTERN(int) git_blob_filtered_content( */ /**@{*/ -/** - * Free the memory referred to by the git_buf. This is an alias of - * `git_buf_dispose` and is preserved for backward compatibility. - * - * This function is deprecated, but there is no plan to remove this - * function at this time. - * - * @deprecated Use git_buf_dispose - * @see git_buf_dispose +/* + * You may hard-deprecate only the git_buf compatibility layer by + * setting GIT_DEPRECATE_BUF. */ -GIT_EXTERN(void) git_buf_free(git_buf *buffer); +#ifndef GIT_DEPRECATE_BUF + +typedef git_userbuf git_buf; + +#define GIT_BUF_INIT_CONST(str, len) GIT_USERBUF_CONST(str, len) +#define git_buf_set(buf, data, datalen) git_userbuf_set(buf, data, datalen) +#define git_buf_grow(buf, size) git_userbuf_grow(buf, size) +#define git_buf_is_binary(b) git_userbuf_is_binary(b) +#define git_buf_contains_nul(b) git_userbuf_contains_nul(b) +#define git_buf_dispose(b) git_userbuf_dispose(b) +#define git_buf_free(b) git_userbuf_dispose(b) + +#endif /* GIT_DEPRECATE_BUF */ /**@}*/ diff --git a/include/git2/describe.h b/include/git2/describe.h index d53d9e81f..bca14aa59 100644 --- a/include/git2/describe.h +++ b/include/git2/describe.h @@ -9,7 +9,7 @@ #include "common.h" #include "types.h" -#include "buffer.h" +#include "userbuf.h" /** * @file git2/describe.h diff --git a/include/git2/filter.h b/include/git2/filter.h index 01a8e86a8..383c23fdc 100644 --- a/include/git2/filter.h +++ b/include/git2/filter.h @@ -10,7 +10,7 @@ #include "common.h" #include "types.h" #include "oid.h" -#include "buffer.h" +#include "userbuf.h" /** * @file git2/filter.h diff --git a/include/git2/mailmap.h b/include/git2/mailmap.h index 7c3f60fcc..4ac972957 100644 --- a/include/git2/mailmap.h +++ b/include/git2/mailmap.h @@ -9,7 +9,6 @@ #include "common.h" #include "types.h" -#include "buffer.h" /** * @file git2/mailmap.h diff --git a/include/git2/message.h b/include/git2/message.h index 98078fe02..aa6e0c72c 100644 --- a/include/git2/message.h +++ b/include/git2/message.h @@ -8,7 +8,6 @@ #define INCLUDE_git_message_h__ #include "common.h" -#include "buffer.h" #include "userbuf.h" /** diff --git a/include/git2/object.h b/include/git2/object.h index 9f9131bbe..f8fdb2271 100644 --- a/include/git2/object.h +++ b/include/git2/object.h @@ -10,7 +10,7 @@ #include "common.h" #include "types.h" #include "oid.h" -#include "buffer.h" +#include "userbuf.h" /** * @file git2/object.h diff --git a/include/git2/refspec.h b/include/git2/refspec.h index 5676c1ab9..6ff4558c7 100644 --- a/include/git2/refspec.h +++ b/include/git2/refspec.h @@ -10,7 +10,7 @@ #include "common.h" #include "types.h" #include "net.h" -#include "buffer.h" +#include "userbuf.h" /** * @file git2/refspec.h diff --git a/include/git2/repository.h b/include/git2/repository.h index 8b61cd5b7..3265b2e6b 100644 --- a/include/git2/repository.h +++ b/include/git2/repository.h @@ -10,7 +10,6 @@ #include "common.h" #include "types.h" #include "oid.h" -#include "buffer.h" #include "userbuf.h" /** diff --git a/include/git2/sys/mempack.h b/include/git2/sys/mempack.h index e869589a1..70d3725c6 100644 --- a/include/git2/sys/mempack.h +++ b/include/git2/sys/mempack.h @@ -11,7 +11,7 @@ #include "git2/types.h" #include "git2/oid.h" #include "git2/odb.h" -#include "git2/buffer.h" +#include "git2/userbuf.h" /** * @file git2/sys/mempack.h diff --git a/include/git2/types.h b/include/git2/types.h index ade0c7d32..e9d7a6ce6 100644 --- a/include/git2/types.h +++ b/include/git2/types.h @@ -66,7 +66,6 @@ typedef int64_t git_time_t; /**< time in seconds from epoch */ /** The maximum size of an object */ typedef uint64_t git_object_size_t; -#include "buffer.h" #include "oid.h" /** Basic type (loose or packed) of any Git object. */ diff --git a/include/git2/userbuf.h b/include/git2/userbuf.h index 4fe8ccf47..2e498914a 100644 --- a/include/git2/userbuf.h +++ b/include/git2/userbuf.h @@ -28,9 +28,9 @@ GIT_BEGIN_DECL * A data buffer for exporting data from libgit2. * * Sometimes libgit2 wants to return an allocated data buffer to the - * caller and have the caller take responsibility for freeing that memory. - * This structure should be freed with `git_userbuf_dispose` when you - * have finished with it. + * caller and allow the caller take responsibility for its lifecycle. + * This requires the caller to free the memory with `git_userbuf_dispose` + * when they have finished using it. */ typedef struct { /** @@ -54,6 +54,22 @@ typedef struct { } git_userbuf; /** + * Check quickly if buffer looks like it contains binary data + * + * @param buf Buffer to check + * @return 1 if buffer looks like non-text data + */ +GIT_EXTERN(int) git_userbuf_is_binary(const git_userbuf *buf); + + /** + * Check quickly if buffer contains a NUL byte + * + * @param buf Buffer to check + * @return 1 if buffer contains a NUL byte + */ +GIT_EXTERN(int) git_userbuf_contains_nul(const git_userbuf *buf); + +/** * Places the given data in the buffer. This is necessary for some * callback functions that take user data. If there is already data in * the buffer, you should call `git_userbuf_dispose` before setting the @@ -63,7 +79,30 @@ typedef struct { * @param ptr The data to place in the buffer * @param len The length of the data in bytes */ -GIT_EXTERN(int) git_userbuf_set(git_userbuf *buf, const char *ptr, size_t len); +GIT_EXTERN(int) git_userbuf_set(git_userbuf *buf, const void *ptr, size_t len); + +/** + * Resize the buffer allocation to make more space. + * + * This will attempt to grow the buffer to accommodate 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. + * + * 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 target_size The desired available size + * @return 0 on success, -1 on allocation failure + */ +GIT_EXTERN(int) git_userbuf_grow(git_userbuf *buffer, size_t target_size); /** * Free the memory referred to by the git_userbuf. diff --git a/include/git2/worktree.h b/include/git2/worktree.h index bda916d96..bd7420c74 100644 --- a/include/git2/worktree.h +++ b/include/git2/worktree.h @@ -8,7 +8,6 @@ #define INCLUDE_git_worktree_h__ #include "common.h" -#include "buffer.h" #include "userbuf.h" #include "types.h" #include "strarray.h" |