diff options
author | Russell Belfer <rb@github.com> | 2013-09-11 22:00:36 -0700 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2013-09-17 09:31:45 -0700 |
commit | a9f51e430fef49b3299ec33c11a4e6623e3f58cc (patch) | |
tree | a3162a8bcf71628f0750d5560923e7783be38eca /src/buffer.h | |
parent | 4b11f25a4fbb6952284e037a70e2d61fde841ab6 (diff) | |
download | libgit2-a9f51e430fef49b3299ec33c11a4e6623e3f58cc.tar.gz |
Merge git_buf and git_buffer
This makes the git_buf struct that was used internally into an
externally available structure and eliminates the git_buffer.
As part of that, some of the special cases that arose with the
externally used git_buffer were blended into the git_buf, such as
being careful about git_buf objects that may have a NULL ptr and
allowing for bufs with a valid ptr and size but zero asize as a
way of referring to externally owned data.
Diffstat (limited to 'src/buffer.h')
-rw-r--r-- | src/buffer.h | 65 |
1 files changed, 17 insertions, 48 deletions
diff --git a/src/buffer.h b/src/buffer.h index e07f29131..4ca9d4d94 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -12,16 +12,23 @@ #include "git2/buffer.h" #include <stdarg.h> -typedef struct { - char *ptr; - size_t asize, size; -} git_buf; +/* typedef struct { + * char *ptr; + * size_t asize, size; + * } git_buf; + */ extern char git_buf__initbuf[]; extern char git_buf__oom[]; +/* Use to initialize buffer structure when git_buf is on stack */ #define GIT_BUF_INIT { git_buf__initbuf, 0, 0 } +GIT_INLINE(bool) git_buf_is_allocated(const git_buf *buf) +{ + return (buf->ptr != NULL && buf->asize > 0); +} + /** * Initialize a git_buf structure. * @@ -33,27 +40,16 @@ extern void git_buf_init(git_buf *buf, size_t initial_size); /** * Attempt to grow the buffer to hold at least `target_size` bytes. * - * If the allocation fails, this will return an error. If mark_oom is true, + * If the allocation fails, this will return an error. If `mark_oom` is true, * this will mark the buffer as invalid for future operations; if false, * existing buffer content will be preserved, but calling code must handle - * that buffer was not expanded. - */ -extern int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom); - -/** - * Grow the buffer to hold at least `target_size` bytes. - * - * If the allocation fails, this will return an error and the buffer will be - * marked as invalid for future operations, invaliding contents. - * - * @return 0 on success or -1 on failure + * that buffer was not expanded. If `preserve_external` is true, then any + * existing data pointed to be `ptr` even if `asize` is zero will be copied + * into the newly allocated buffer. */ -GIT_INLINE(int) git_buf_grow(git_buf *buf, size_t target_size) -{ - return git_buf_try_grow(buf, target_size, true); -} +extern int git_buf_try_grow( + git_buf *buf, size_t target_size, bool mark_oom, bool preserve_external); -extern void git_buf_free(git_buf *buf); extern void git_buf_swap(git_buf *buf_a, git_buf *buf_b); extern char *git_buf_detach(git_buf *buf); extern void git_buf_attach(git_buf *buf, char *ptr, size_t asize); @@ -82,7 +78,6 @@ GIT_INLINE(bool) git_buf_oom(const git_buf *buf) * return code of these functions and call them in a series then just call * git_buf_oom at the end. */ -int git_buf_set(git_buf *buf, const char *data, size_t len); int git_buf_sets(git_buf *buf, const char *string); int git_buf_putc(git_buf *buf, char c); int git_buf_put(git_buf *buf, const char *data, size_t len); @@ -175,30 +170,4 @@ int git_buf_splice( const char *data, size_t nb_to_insert); - -GIT_INLINE(bool) git_buffer_is_allocated(const git_buffer *buffer) -{ - return (buffer->ptr != NULL && buffer->available > 0); -} - -#define GIT_BUF_FROM_BUFFER(buffer) \ - { (buffer)->ptr, (buffer)->available, (buffer)->size } - -GIT_INLINE(void) git_buf_from_buffer(git_buf *buf, const git_buffer *buffer) -{ - buf->ptr = buffer->ptr; - buf->size = buffer->size; - buf->asize = buffer->available; -} - -#define GIT_BUFFER_FROM_BUF(buf) \ - { (buf)->ptr, (buf)->size, (buf)->asize } - -GIT_INLINE(void) git_buffer_from_buf(git_buffer *buffer, const git_buf *buf) -{ - buffer->ptr = buf->ptr; - buffer->size = buf->size; - buffer->available = buf->asize; -} - #endif |