Skip to content

Commit 1ebe6a8

Browse files
committed
Merge branch 'jk/name-decoration-alloc'
The API to allocate the structure to keep track of commit decoration was cumbersome to use, inviting lazy code to overallocate memory. * jk/name-decoration-alloc: log-tree: use FLEX_ARRAY in name_decoration log-tree: make name_decoration hash static log-tree: make add_name_decoration a public function
2 parents f28763d + 2e3dfb2 commit 1ebe6a8

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

bisect.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,12 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n
215215
}
216216
qsort(array, cnt, sizeof(*array), compare_commit_dist);
217217
for (p = list, i = 0; i < cnt; i++) {
218-
struct name_decoration *r = xmalloc(sizeof(*r) + 100);
218+
char buf[100]; /* enough for dist=%d */
219219
struct object *obj = &(array[i].commit->object);
220220

221-
sprintf(r->name, "dist=%d", array[i].distance);
222-
r->next = add_decoration(&name_decoration, obj, r);
221+
snprintf(buf, sizeof(buf), "dist=%d", array[i].distance);
222+
add_name_decoration(DECORATION_NONE, buf, obj);
223+
223224
p->item = array[i].commit;
224225
p = p->next;
225226
}

commit.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,25 @@ extern int save_commit_buffer;
2626
extern const char *commit_type;
2727

2828
/* While we can decorate any object with a name, it's only used for commits.. */
29-
extern struct decoration name_decoration;
3029
struct name_decoration {
3130
struct name_decoration *next;
3231
int type;
33-
char name[1];
32+
char name[FLEX_ARRAY];
3433
};
3534

35+
enum decoration_type {
36+
DECORATION_NONE = 0,
37+
DECORATION_REF_LOCAL,
38+
DECORATION_REF_REMOTE,
39+
DECORATION_REF_TAG,
40+
DECORATION_REF_STASH,
41+
DECORATION_REF_HEAD,
42+
DECORATION_GRAFTED,
43+
};
44+
45+
void add_name_decoration(enum decoration_type type, const char *name, struct object *obj);
46+
const struct name_decoration *get_name_decoration(const struct object *obj);
47+
3648
struct commit *lookup_commit(const unsigned char *sha1);
3749
struct commit *lookup_commit_reference(const unsigned char *sha1);
3850
struct commit *lookup_commit_reference_gently(const unsigned char *sha1,

log-tree.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,7 @@
1212
#include "sequencer.h"
1313
#include "line-log.h"
1414

15-
struct decoration name_decoration = { "object names" };
16-
17-
enum decoration_type {
18-
DECORATION_NONE = 0,
19-
DECORATION_REF_LOCAL,
20-
DECORATION_REF_REMOTE,
21-
DECORATION_REF_TAG,
22-
DECORATION_REF_STASH,
23-
DECORATION_REF_HEAD,
24-
DECORATION_GRAFTED,
25-
};
15+
static struct decoration name_decoration = { "object names" };
2616

2717
static char decoration_colors[][COLOR_MAXLEN] = {
2818
GIT_COLOR_RESET,
@@ -84,15 +74,20 @@ int parse_decorate_color_config(const char *var, const int ofs, const char *valu
8474
#define decorate_get_color_opt(o, ix) \
8575
decorate_get_color((o)->use_color, ix)
8676

87-
static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
77+
void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
8878
{
8979
int nlen = strlen(name);
90-
struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);
80+
struct name_decoration *res = xmalloc(sizeof(*res) + nlen + 1);
9181
memcpy(res->name, name, nlen + 1);
9282
res->type = type;
9383
res->next = add_decoration(&name_decoration, obj, res);
9484
}
9585

86+
const struct name_decoration *get_name_decoration(const struct object *obj)
87+
{
88+
return lookup_decoration(&name_decoration, obj);
89+
}
90+
9691
static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
9792
{
9893
struct object *obj;
@@ -187,13 +182,13 @@ void format_decorations(struct strbuf *sb,
187182
int use_color)
188183
{
189184
const char *prefix;
190-
struct name_decoration *decoration;
185+
const struct name_decoration *decoration;
191186
const char *color_commit =
192187
diff_get_color(use_color, DIFF_COMMIT);
193188
const char *color_reset =
194189
decorate_get_color(use_color, DECORATION_NONE);
195190

196-
decoration = lookup_decoration(&name_decoration, &commit->object);
191+
decoration = get_name_decoration(&commit->object);
197192
if (!decoration)
198193
return;
199194
prefix = " (";

revision.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ static int rev_compare_tree(struct rev_info *revs,
473473
* If we are simplifying by decoration, then the commit
474474
* is worth showing if it has a tag pointing at it.
475475
*/
476-
if (lookup_decoration(&name_decoration, &commit->object))
476+
if (get_name_decoration(&commit->object))
477477
return REV_TREE_DIFFERENT;
478478
/*
479479
* A commit that is not pointed by a tag is uninteresting

0 commit comments

Comments
 (0)