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 | |
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')
-rw-r--r-- | src/blob.c | 6 | ||||
-rw-r--r-- | src/buffer.c | 93 | ||||
-rw-r--r-- | src/buffer.h | 65 | ||||
-rw-r--r-- | src/checkout.c | 9 | ||||
-rw-r--r-- | src/config_file.c | 2 | ||||
-rw-r--r-- | src/crlf.c | 42 | ||||
-rw-r--r-- | src/diff_file.c | 6 | ||||
-rw-r--r-- | src/filter.c | 48 | ||||
-rw-r--r-- | src/ident.c | 48 | ||||
-rw-r--r-- | src/odb.c | 8 | ||||
-rw-r--r-- | src/path.c | 2 | ||||
-rw-r--r-- | src/util.c | 3 |
12 files changed, 113 insertions, 219 deletions
diff --git a/src/blob.c b/src/blob.c index 97fd6f70d..e18db4dfc 100644 --- a/src/blob.c +++ b/src/blob.c @@ -111,7 +111,7 @@ static int write_file_filtered( git_filter_list *fl) { int error; - git_buffer tgt = GIT_BUFFER_INIT; + git_buf tgt = GIT_BUF_INIT; error = git_filter_list_apply_to_file(&tgt, fl, NULL, full_path); @@ -122,7 +122,7 @@ static int write_file_filtered( error = git_odb_write(oid, odb, tgt.ptr, tgt.size, GIT_OBJ_BLOB); } - git_buffer_free(&tgt); + git_buf_free(&tgt); return error; } @@ -329,7 +329,7 @@ int git_blob_is_binary(git_blob *blob) } int git_blob_filtered_content( - git_buffer *out, + git_buf *out, git_blob *blob, const char *path, int check_for_binary_data) diff --git a/src/buffer.c b/src/buffer.c index 07725b9cc..f8d47d928 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -32,7 +32,8 @@ void git_buf_init(git_buf *buf, size_t initial_size) git_buf_grow(buf, initial_size); } -int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom) +int git_buf_try_grow( + git_buf *buf, size_t target_size, bool mark_oom, bool preserve_external) { char *new_ptr; size_t new_size; @@ -40,6 +41,9 @@ int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom) if (buf->ptr == git_buf__oom) return -1; + if (!target_size) + target_size = buf->size; + if (target_size <= buf->asize) return 0; @@ -67,6 +71,9 @@ int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom) return -1; } + if (preserve_external && !buf->asize && buf->ptr != NULL && buf->size > 0) + memcpy(new_ptr, buf->ptr, min(buf->size, new_size)); + buf->asize = new_size; buf->ptr = new_ptr; @@ -78,11 +85,16 @@ int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom) return 0; } +int git_buf_grow(git_buf *buffer, size_t target_size) +{ + return git_buf_try_grow(buffer, target_size, true, true); +} + void git_buf_free(git_buf *buf) { if (!buf) return; - if (buf->ptr != git_buf__initbuf && buf->ptr != git_buf__oom) + if (buf->asize > 0 && buf->ptr != NULL && buf->ptr != git_buf__oom) git__free(buf->ptr); git_buf_init(buf, 0); @@ -91,11 +103,15 @@ void git_buf_free(git_buf *buf) void git_buf_clear(git_buf *buf) { buf->size = 0; + + if (!buf->ptr) + buf->ptr = git_buf__initbuf; + if (buf->asize > 0) buf->ptr[0] = '\0'; } -int git_buf_set(git_buf *buf, const char *data, size_t len) +int git_buf_set(git_buf *buf, const void *data, size_t len) { if (len == 0 || data == NULL) { git_buf_clear(buf); @@ -485,74 +501,3 @@ int git_buf_splice( buf->ptr[buf->size] = '\0'; return 0; } - -/* - * Public buffers API - */ - -void git_buffer_free(git_buffer *buffer) -{ - if (!buffer) - return; - - if (buffer->ptr != NULL && buffer->available > 0) - git__free(buffer->ptr); - - git__memzero(buffer, sizeof(*buffer)); -} - -static int git_buffer__resize( - git_buffer *buffer, size_t want_size, int preserve_data) -{ - int non_allocated_buffer = 0; - char *new_ptr; - - assert(buffer); - - /* check if buffer->ptr points to memory owned elsewhere */ - non_allocated_buffer = (buffer->ptr != NULL && buffer->available == 0); - - if (non_allocated_buffer && !want_size) - want_size = buffer->size; - - if (buffer->available >= want_size) - return 0; - - if (non_allocated_buffer) { - new_ptr = NULL; - if (want_size < buffer->size) - want_size = buffer->size; - } else { - new_ptr = buffer->ptr; - } - - want_size = (want_size + 7) & ~7; /* round up to multiple of 8 */ - - new_ptr = git__realloc(new_ptr, want_size); - GITERR_CHECK_ALLOC(new_ptr); - - if (non_allocated_buffer && preserve_data) - memcpy(new_ptr, buffer->ptr, buffer->size); - - buffer->ptr = new_ptr; - buffer->available = want_size; - - return 0; -} - -int git_buffer_resize(git_buffer *buffer, size_t want_size) -{ - return git_buffer__resize(buffer, want_size, true); -} - -int git_buffer_copy( - git_buffer *buffer, const void *data, size_t datalen) -{ - if (git_buffer__resize(buffer, datalen + 1, false) < 0) - return -1; - memcpy(buffer->ptr, data, datalen); - buffer->ptr[datalen] = '\0'; - buffer->size = datalen; - return 0; -} - 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 diff --git a/src/checkout.c b/src/checkout.c index 7e79c9b5e..140544c77 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -678,20 +678,19 @@ fail: static int buffer_to_file( struct stat *st, - git_buffer *buffer, + git_buf *buf, const char *path, mode_t dir_mode, int file_open_flags, mode_t file_mode) { int error; - git_buf buf = GIT_BUF_FROM_BUFFER(buffer); if ((error = git_futils_mkpath2file(path, dir_mode)) < 0) return error; if ((error = git_futils_writebuffer( - &buf, path, file_open_flags, file_mode)) < 0) + buf, path, file_open_flags, file_mode)) < 0) return error; if (st != NULL && (error = p_stat(path, st)) < 0) @@ -713,7 +712,7 @@ static int blob_content_to_file( { int error = 0; mode_t file_mode = opts->file_mode ? opts->file_mode : entry_filemode; - git_buffer out = GIT_BUFFER_INIT; + git_buf out = GIT_BUF_INIT; git_filter_list *fl = NULL; if (!opts->disable_filters && !git_blob_is_binary(blob)) @@ -731,7 +730,7 @@ static int blob_content_to_file( st->st_mode = entry_filemode; - git_buffer_free(&out); + git_buf_free(&out); } return error; diff --git a/src/config_file.c b/src/config_file.c index bd4fa7471..d0910a26c 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -293,6 +293,8 @@ static int config_iterator_new( diskfile_backend *b = (diskfile_backend *)backend; git_config_file_iter *it = git__calloc(1, sizeof(git_config_file_iter)); + GIT_UNUSED(b); + GITERR_CHECK_ALLOC(it); it->parent.backend = backend; diff --git a/src/crlf.c b/src/crlf.c index f61a870da..bde85ca06 100644 --- a/src/crlf.c +++ b/src/crlf.c @@ -119,15 +119,12 @@ static int has_cr_in_index(const git_filter_source *src) static int crlf_apply_to_odb( struct crlf_attrs *ca, - git_buffer *to, - const git_buffer *from, + git_buf *to, + const git_buf *from, const git_filter_source *src) { - const git_buf from_buf = GIT_BUF_FROM_BUFFER(from); - git_buf to_buf = GIT_BUF_FROM_BUFFER(to); - /* Empty file? Nothing to do */ - if (!git_buf_len(&from_buf)) + if (!git_buf_len(from)) return 0; /* Heuristics to see if we can skip the conversion. @@ -137,7 +134,7 @@ static int crlf_apply_to_odb( git_buf_text_stats stats; /* Check heuristics for binary vs text... */ - if (git_buf_text_gather_stats(&stats, &from_buf, false)) + if (git_buf_text_gather_stats(&stats, from, false)) return -1; /* @@ -162,13 +159,7 @@ static int crlf_apply_to_odb( } /* Actually drop the carriage returns */ - if (git_buf_text_crlf_to_lf(&to_buf, &from_buf) < 0) - return -1; - - /* Overwrite "to" buffer in case data was resized */ - git_buffer_from_buf(to, &to_buf); - - return 0; + return git_buf_text_crlf_to_lf(to, from); } static const char *line_ending(struct crlf_attrs *ca) @@ -210,14 +201,12 @@ line_ending_error: } static int crlf_apply_to_workdir( - struct crlf_attrs *ca, git_buffer *to, const git_buffer *from) + struct crlf_attrs *ca, git_buf *to, const git_buf *from) { - const git_buf from_buf = GIT_BUF_FROM_BUFFER(from); - git_buf to_buf = GIT_BUF_FROM_BUFFER(to); const char *workdir_ending = NULL; /* Empty file? Nothing to do. */ - if (git_buf_len(&from_buf) == 0) + if (git_buf_len(from) == 0) return 0; /* Determine proper line ending */ @@ -229,22 +218,19 @@ static int crlf_apply_to_workdir( if (ca->crlf_action == GIT_CRLF_GUESS && ca->auto_crlf) return GIT_ENOTFOUND; - if (git_buf_find(&from_buf, '\r') < 0) + if (git_buf_find(from, '\r') < 0) return GIT_ENOTFOUND; - if (git_buf_text_crlf_to_lf(&to_buf, &from_buf) < 0) + if (git_buf_text_crlf_to_lf(to, from) < 0) return -1; } else { /* only other supported option is lf->crlf conversion */ assert(!strcmp("\r\n", workdir_ending)); - if (git_buf_text_lf_to_crlf(&to_buf, &from_buf) < 0) + if (git_buf_text_lf_to_crlf(to, from) < 0) return -1; } - /* Overwrite "to" buffer in case data was resized */ - git_buffer_from_buf(to, &to_buf); - return 0; } @@ -297,10 +283,10 @@ static int crlf_check( } static int crlf_apply( - 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) { /* initialize payload in case `check` was bypassed */ diff --git a/src/diff_file.c b/src/diff_file.c index d02787c75..5939ee8b8 100644 --- a/src/diff_file.c +++ b/src/diff_file.c @@ -327,11 +327,11 @@ static int diff_file_content_load_workdir_file( } if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size))) { - git_buffer in = GIT_BUFFER_FROM_BUF(&raw), out = GIT_BUFFER_INIT; + git_buf out = GIT_BUF_INIT; - error = git_filter_list_apply_to_data(&out, fl, &in); + error = git_filter_list_apply_to_data(&out, fl, &raw); - git_buffer_free(&in); + git_buf_free(&raw); if (!error) { fc->map.len = out.size; diff --git a/src/filter.c b/src/filter.c index f20611471..08467a631 100644 --- a/src/filter.c +++ b/src/filter.c @@ -549,23 +549,28 @@ int git_filter_list_push( } static int filter_list_out_buffer_from_raw( - git_buffer *out, const void *ptr, size_t size) + git_buf *out, const void *ptr, size_t size) { - if (git_buffer_is_allocated(out)) - git_buffer_free(out); + if (git_buf_is_allocated(out)) + git_buf_free(out); + + if (!size) { + git_buf_init(out, 0); + } else { + out->ptr = (char *)ptr; + out->asize = 0; + out->size = size; + } - out->ptr = (char *)ptr; - out->size = size; - out->available = 0; return 0; } int git_filter_list_apply_to_data( - git_buffer *tgt, git_filter_list *fl, git_buffer *src) + git_buf *tgt, git_filter_list *fl, git_buf *src) { int error = 0; uint32_t i; - git_buffer *dbuffer[2], local = GIT_BUFFER_INIT; + git_buf *dbuffer[2], local = GIT_BUF_INIT; unsigned int si = 0; if (!fl) @@ -575,8 +580,8 @@ int git_filter_list_apply_to_data( dbuffer[1] = tgt; /* if `src` buffer is reallocable, then use it, otherwise copy it */ - if (!git_buffer_is_allocated(src)) { - if (git_buffer_copy(&local, src->ptr, src->size) < 0) + if (!git_buf_is_allocated(src)) { + if (git_buf_set(&local, src->ptr, src->size) < 0) return -1; dbuffer[0] = &local; } @@ -610,19 +615,16 @@ int git_filter_list_apply_to_data( } /* Ensure that the output ends up in dbuffer[1] (i.e. the dest) */ - if (si != 1) { - git_buffer sw = *dbuffer[1]; - *dbuffer[1] = *dbuffer[0]; - *dbuffer[0] = sw; - } + if (si != 1) + git_buf_swap(dbuffer[0], dbuffer[1]); - git_buffer_free(&local); /* don't leak if we allocated locally */ + git_buf_free(&local); /* don't leak if we allocated locally */ return 0; } int git_filter_list_apply_to_file( - git_buffer *out, + git_buf *out, git_filter_list *filters, git_repository *repo, const char *path) @@ -634,11 +636,9 @@ int git_filter_list_apply_to_file( if (!(error = git_path_join_unrooted(&abspath, path, base, NULL)) && !(error = git_futils_readbuffer(&raw, abspath.ptr))) { - git_buffer in = GIT_BUFFER_FROM_BUF(&raw); - - error = git_filter_list_apply_to_data(out, filters, &in); + error = git_filter_list_apply_to_data(out, filters, &raw); - git_buffer_free(&in); + git_buf_free(&raw); } git_buf_free(&abspath); @@ -646,12 +646,12 @@ int git_filter_list_apply_to_file( } int git_filter_list_apply_to_blob( - git_buffer *out, + git_buf *out, git_filter_list *filters, git_blob *blob) { - git_buffer in = { - (char *)git_blob_rawcontent(blob), git_blob_rawsize(blob), 0 + git_buf in = { + (char *)git_blob_rawcontent(blob), 0, git_blob_rawsize(blob) }; if (filters) diff --git a/src/ident.c b/src/ident.c index aedb973f9..3ea949859 100644 --- a/src/ident.c +++ b/src/ident.c @@ -9,7 +9,7 @@ #include "filter.h" #include "buffer.h" -int ident_find_id( +static int ident_find_id( const char **id_start, const char **id_end, const char *start, size_t len) { const char *found; @@ -36,12 +36,11 @@ int ident_find_id( } static int ident_insert_id( - git_buffer *to, const git_buffer *from, const git_filter_source *src) + git_buf *to, const git_buf *from, const git_filter_source *src) { char oid[GIT_OID_HEXSZ+1]; const char *id_start, *id_end, *from_end = from->ptr + from->size; size_t need_size; - git_buf to_buf = GIT_BUF_FROM_BUFFER(to); /* replace $Id$ with blob id */ @@ -57,28 +56,23 @@ static int ident_insert_id( 5 /* "$Id: " */ + GIT_OID_HEXSZ + 1 /* "$" */ + (size_t)(from_end - id_end); - if (git_buf_grow(&to_buf, need_size) < 0) + if (git_buf_grow(to, need_size) < 0) return -1; - git_buf_set(&to_buf, from->ptr, (size_t)(id_start - from->ptr)); - git_buf_put(&to_buf, "$Id: ", 5); - git_buf_put(&to_buf, oid, GIT_OID_HEXSZ); - git_buf_putc(&to_buf, '$'); - git_buf_put(&to_buf, id_end, (size_t)(from_end - id_end)); + git_buf_set(to, from->ptr, (size_t)(id_start - from->ptr)); + git_buf_put(to, "$Id: ", 5); + git_buf_put(to, oid, GIT_OID_HEXSZ); + git_buf_putc(to, '$'); + git_buf_put(to, id_end, (size_t)(from_end - id_end)); - if (git_buf_oom(&to_buf)) - return -1; - - git_buffer_from_buf(to, &to_buf); - return 0; + return git_buf_oom(to) ? -1 : 0; } static int ident_remove_id( - git_buffer *to, const git_buffer *from) + git_buf *to, const git_buf *from) { const char *id_start, *id_end, *from_end = from->ptr + from->size; size_t need_size; - git_buf to_buf = GIT_BUF_FROM_BUFFER(to); if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0) return GIT_ENOTFOUND; @@ -86,25 +80,21 @@ static int ident_remove_id( need_size = (size_t)(id_start - from->ptr) + 4 /* "$Id$" */ + (size_t)(from_end - id_end); - if (git_buf_grow(&to_buf, need_size) < 0) + if (git_buf_grow(to, need_size) < 0) return -1; - git_buf_set(&to_buf, from->ptr, (size_t)(id_start - from->ptr)); - git_buf_put(&to_buf, "$Id$", 4); - git_buf_put(&to_buf, id_end, (size_t)(from_end - id_end)); + git_buf_set(to, from->ptr, (size_t)(id_start - from->ptr)); + git_buf_put(to, "$Id$", 4); + git_buf_put(to, id_end, (size_t)(from_end - id_end)); - if (git_buf_oom(&to_buf)) - return -1; - - git_buffer_from_buf(to, &to_buf); - return 0; + return git_buf_oom(to) ? -1 : 0; } static int ident_apply( - git_filter *self, - void **payload, - git_buffer *to, - const git_buffer *from, + git_filter *self, + void **payload, + git_buf *to, + const git_buf *from, const git_filter_source *src) { GIT_UNUSED(self); GIT_UNUSED(payload); @@ -192,16 +192,16 @@ int git_odb__hashfd_filtered( */ if (!(error = git_futils_readbuffer_fd(&raw, fd, size))) { - git_buffer pre = GIT_BUFFER_FROM_BUF(&raw), post = GIT_BUFFER_INIT; + git_buf post = GIT_BUF_INIT; - error = git_filter_list_apply_to_data(&post, fl, &pre); + error = git_filter_list_apply_to_data(&post, fl, &raw); - git_buffer_free(&pre); + git_buf_free(&raw); if (!error) error = git_odb_hash(out, post.ptr, post.size, type); - git_buffer_free(&post); + git_buf_free(&post); } return error; diff --git a/src/path.c b/src/path.c index 56b0b49ca..42b3d6f3e 100644 --- a/src/path.c +++ b/src/path.c @@ -565,7 +565,7 @@ static bool _check_dir_contents( size_t sub_size = strlen(sub); /* leave base valid even if we could not make space for subdir */ - if (git_buf_try_grow(dir, dir_size + sub_size + 2, false) < 0) + if (git_buf_try_grow(dir, dir_size + sub_size + 2, false, false) < 0) return false; /* save excursion */ diff --git a/src/util.c b/src/util.c index d0c326ae5..151782346 100644 --- a/src/util.c +++ b/src/util.c @@ -679,6 +679,9 @@ size_t git__unescape(char *str) { char *scan, *pos = str; + if (!str) + return 0; + for (scan = str; *scan; pos++, scan++) { if (*scan == '\\' && *(scan + 1) != '\0') scan++; /* skip '\' but include next char */ |