shallow.c: use commit-slab for commit depth instead of commit->util
It's done so that commit->util can be removed. See more explanation in the commit that removes commit->util. While at there, plug a leak for keeping track of depth in this code. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
May 19, 2018 at 07:28 UTC
58dbe58faab42a70478627608b129db9a6d9c3e0
1 file changed
+28
-12
shallow.c
+28
-12
@@ -12,6 +12,7 @@
12
#include "commit-slab.h"
13
#include "revision.h"
14
#include "list-objects.h"
15
+#include "commit-slab.h"
16
17
static int is_shallow = -1;
18
static struct stat_validity shallow_stat;
@@ -74,6 +75,11 @@ int is_repository_shallow(void)
75
return is_shallow;
76
}
77
78
+/*
79
+ * TODO: use "int" elemtype instead of "int *" when/if commit-slab
80
+ * supports a "valid" flag.
81
+ */
82
+define_commit_slab(commit_depth, int *);
83
struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
84
int shallow_flag, int not_shallow_flag)
85
{
@@ -82,25 +88,29 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
88
struct object_array stack = OBJECT_ARRAY_INIT;
89
struct commit *commit = NULL;
90
struct commit_graft *graft;
91
+ struct commit_depth depths;
92
93
+ init_commit_depth(&depths);
94
while (commit || i < heads->nr || stack.nr) {
95
struct commit_list *p;
96
if (!commit) {
97
if (i < heads->nr) {
98
+ int **depth_slot;
99
commit = (struct commit *)
100
deref_tag(heads->objects[i++].item, NULL, 0);
101
if (!commit || commit->object.type != OBJ_COMMIT) {
102
commit = NULL;
103
continue;
104
}
96
- if (!commit->util)
97
- commit->util = xmalloc(sizeof(int));
98
- *(int *)commit->util = 0;
105
+ depth_slot = commit_depth_at(&depths, commit);
106
+ if (!*depth_slot)
107
+ *depth_slot = xmalloc(sizeof(int));
108
+ **depth_slot = 0;
109
cur_depth = 0;
110
} else {
111
commit = (struct commit *)
112
object_array_pop(&stack);
103
- cur_depth = *(int *)commit->util;
113
+ cur_depth = **commit_depth_at(&depths, commit);
114
}
115
}
116
parse_commit_or_die(commit);
@@ -116,25 +126,31 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
126
}
127
commit->object.flags |= not_shallow_flag;
128
for (p = commit->parents, commit = NULL; p; p = p->next) {
119
- if (!p->item->util) {
120
- int *pointer = xmalloc(sizeof(int));
121
- p->item->util = pointer;
122
- *pointer = cur_depth;
129
+ int **depth_slot = commit_depth_at(&depths, p->item);
130
+ if (!*depth_slot) {
131
+ *depth_slot = xmalloc(sizeof(int));
132
+ **depth_slot = cur_depth;
133
} else {
124
- int *pointer = p->item->util;
125
- if (cur_depth >= *pointer)
134
+ if (cur_depth >= **depth_slot)
135
continue;
127
- *pointer = cur_depth;
136
+ **depth_slot = cur_depth;
137
}
138
if (p->next)
139
add_object_array(&p->item->object,
140
NULL, &stack);
141
else {
142
commit = p->item;
134
- cur_depth = *(int *)commit->util;
143
+ cur_depth = **commit_depth_at(&depths, commit);
144
}
145
}
146
}
147
+ for (i = 0; i < depths.slab_count; i++) {
148
+ int j;
149
+
150
+ for (j = 0; j < depths.slab_size; j++)
151
+ free(depths.slab[i][j]);
152
+ }
153
+ clear_commit_depth(&depths);
154
155
return result;
156
}