From df835d3a0c013b9ef912172663c7c58dad87164d Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sat, 25 May 2013 11:08:02 +0200 Subject: add_rev_cmdline(): make a copy of the name argument Instead of assuming that the memory pointed to by the name argument will live forever, make a local copy of it before storing it in the ref_cmdline_info. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- revision.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'revision.c') diff --git a/revision.c b/revision.c index a67b615bfc..25e424cdb6 100644 --- a/revision.c +++ b/revision.c @@ -898,6 +898,10 @@ static int limit_list(struct rev_info *revs) return 0; } +/* + * Add an entry to refs->cmdline with the specified information. + * *name is copied. + */ static void add_rev_cmdline(struct rev_info *revs, struct object *item, const char *name, @@ -909,7 +913,7 @@ static void add_rev_cmdline(struct rev_info *revs, ALLOC_GROW(info->rev, nr + 1, info->alloc); info->rev[nr].item = item; - info->rev[nr].name = name; + info->rev[nr].name = xstrdup(name); info->rev[nr].whence = whence; info->rev[nr].flags = flags; info->nr++; -- cgit v1.2.3 From ff5f5f268fd36b6db059c06d124305866ecaa6ce Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sat, 25 May 2013 11:08:07 +0200 Subject: revision: split some overly-long lines Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- revision.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'revision.c') diff --git a/revision.c b/revision.c index 25e424cdb6..8ac88d6798 100644 --- a/revision.c +++ b/revision.c @@ -70,7 +70,8 @@ static int show_path_truncated(FILE *out, const struct name_path *path) return ours || emitted; } -void show_object_with_name(FILE *out, struct object *obj, const struct name_path *path, const char *component) +void show_object_with_name(FILE *out, struct object *obj, + const struct name_path *path, const char *component) { struct name_path leaf; leaf.up = (struct name_path *)path; @@ -186,7 +187,9 @@ void mark_parents_uninteresting(struct commit *commit) } } -static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode) +static void add_pending_object_with_mode(struct rev_info *revs, + struct object *obj, + const char *name, unsigned mode) { if (!obj) return; @@ -209,7 +212,8 @@ static void add_pending_object_with_mode(struct rev_info *revs, struct object *o add_object_array_with_mode(obj, name, &revs->pending, mode); } -void add_pending_object(struct rev_info *revs, struct object *obj, const char *name) +void add_pending_object(struct rev_info *revs, + struct object *obj, const char *name) { add_pending_object_with_mode(revs, obj, name, S_IFINVALID); } @@ -226,7 +230,9 @@ void add_head_to_pending(struct rev_info *revs) add_pending_object(revs, obj, "HEAD"); } -static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags) +static struct object *get_reference(struct rev_info *revs, const char *name, + const unsigned char *sha1, + unsigned int flags) { struct object *object; @@ -247,7 +253,8 @@ void add_pending_sha1(struct rev_info *revs, const char *name, add_pending_object(revs, object, name); } -static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name) +static struct commit *handle_commit(struct rev_info *revs, + struct object *object, const char *name) { unsigned long flags = object->flags; @@ -368,7 +375,8 @@ static void file_change(struct diff_options *options, DIFF_OPT_SET(options, HAS_CHANGES); } -static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit) +static int rev_compare_tree(struct rev_info *revs, + struct commit *parent, struct commit *commit) { struct tree *t1 = parent->tree; struct tree *t2 = commit->tree; -- cgit v1.2.3 From be6754c67f5aff02e9528116d06890391524f48e Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sat, 25 May 2013 11:08:09 +0200 Subject: revision: use object_array_filter() in implementation of gc_boundary() Use object_array_filter(), which will soon be made smarter about cleaning up discarded entries properly. Also add a function comment. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- revision.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'revision.c') diff --git a/revision.c b/revision.c index 8ac88d6798..be73cb46c9 100644 --- a/revision.c +++ b/revision.c @@ -2435,25 +2435,23 @@ static struct commit *get_revision_1(struct rev_info *revs) return NULL; } -static void gc_boundary(struct object_array *array) +/* + * Return true for entries that have not yet been shown. (This is an + * object_array_each_func_t.) + */ +static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused) { - unsigned nr = array->nr; - unsigned alloc = array->alloc; - struct object_array_entry *objects = array->objects; + return !(entry->item->flags & SHOWN); +} - if (alloc <= nr) { - unsigned i, j; - for (i = j = 0; i < nr; i++) { - if (objects[i].item->flags & SHOWN) - continue; - if (i != j) - objects[j] = objects[i]; - j++; - } - for (i = j; i < nr; i++) - objects[i].item = NULL; - array->nr = j; - } +/* + * If array is on the verge of a realloc, garbage-collect any entries + * that have already been shown to try to free up some space. + */ +static void gc_boundary(struct object_array *array) +{ + if (array->nr == array->alloc) + object_array_filter(array, entry_unshown, NULL); } static void create_boundary_commit_list(struct rev_info *revs) -- cgit v1.2.3 From 31faeb2088ef35d0108ad81df3550513d6cec798 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sat, 25 May 2013 11:08:14 +0200 Subject: object_array_entry: fix memory handling of the name field Previously, the memory management of the object_array_entry::name field was inconsistent and undocumented. object_array_entries are ultimately created by a single function, add_object_array_with_mode(), which has an argument "const char *name". This function used to simply set the name field to reference the string pointed to by the name parameter, and nobody on the object_array side ever freed the memory. Thus, it assumed that the memory for the name field would be managed by the caller, and that the lifetime of that string would be at least as long as the lifetime of the object_array_entry. But callers were inconsistent: * Some passed pointers to constant strings or argv entries, which was OK. * Some passed pointers to newly-allocated memory, but didn't arrange for the memory ever to be freed. * Some passed the return value of sha1_to_hex(), which is a pointer to a statically-allocated buffer that can be overwritten at any time. * Some passed pointers to refnames that they received from a for_each_ref()-type iteration, but the lifetimes of such refnames is not guaranteed by the refs API. Bring consistency to this mess by changing object_array to make its own copy for the object_array_entry::name field and free this memory when an object_array_entry is deleted from the array. Many callers were passing the empty string as the name parameter, so as a performance optimization, treat the empty string specially. Instead of making a copy, store a pointer to a statically-allocated empty string to object_array_entry::name. When deleting such an entry, skip the free(). Change the callers that were already passing copies to add_object_array_with_mode() to either skip the copy, or (if the memory needed to be allocated anyway) freeing the memory itself. A part of this commit effectively reverts 70d26c6e76 read_revisions_from_stdin: make copies for handle_revision_arg because the copying introduced by that commit (which is still necessary) is now done at a deeper level. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- revision.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'revision.c') diff --git a/revision.c b/revision.c index be73cb46c9..4aeda334db 100644 --- a/revision.c +++ b/revision.c @@ -88,7 +88,9 @@ void add_object(struct object *obj, struct name_path *path, const char *name) { - add_object_array(obj, path_name(path, name), p); + char *pn = path_name(path, name); + add_object_array(obj, pn, p); + free(pn); } static void mark_blob_uninteresting(struct blob *blob) @@ -1288,7 +1290,7 @@ static void read_revisions_from_stdin(struct rev_info *revs, } die("options not supported in --stdin mode"); } - if (handle_revision_arg(xstrdup(sb.buf), revs, 0, + if (handle_revision_arg(sb.buf, revs, 0, REVARG_CANNOT_BE_FILENAME)) die("bad revision '%s'", sb.buf); } -- cgit v1.2.3