@matheusbsbs / matheus-bagliano / commits / a5cdebea55
Fork 0

notes.c: Reorder functions in preparation for next commit

This patch introduces no functional change. It consists solely of reordering functions in notes.c to avoid use-before-declaration errors after applying the next commit in this series. Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Nov 9, 2010 at 22:49 UTC a5cdebea55d53406e117d9a1fd4cc316ef036553
1 file changed +73 -73
notes.c
+73 -73
@@ -149,6 +149,79 @@ static struct leaf_node *note_tree_find(struct notes_tree *t,
149 return NULL;
150 }
151
152 +/*
153 + * How to consolidate an int_node:
154 + * If there are > 1 non-NULL entries, give up and return non-zero.
155 + * Otherwise replace the int_node at the given index in the given parent node
156 + * with the only entry (or a NULL entry if no entries) from the given tree,
157 + * and return 0.
158 + */
159 +static int note_tree_consolidate(struct int_node *tree,
160 + struct int_node *parent, unsigned char index)
161 +{
162 + unsigned int i;
163 + void *p = NULL;
164 +
165 + assert(tree && parent);
166 + assert(CLR_PTR_TYPE(parent->a[index]) == tree);
167 +
168 + for (i = 0; i < 16; i++) {
169 + if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {
170 + if (p) /* more than one entry */
171 + return -2;
172 + p = tree->a[i];
173 + }
174 + }
175 +
176 + /* replace tree with p in parent[index] */
177 + parent->a[index] = p;
178 + free(tree);
179 + return 0;
180 +}
181 +
182 +/*
183 + * To remove a leaf_node:
184 + * Search to the tree location appropriate for the given leaf_node's key:
185 + * - If location does not hold a matching entry, abort and do nothing.
186 + * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
187 + * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
188 + */
189 +static void note_tree_remove(struct notes_tree *t, struct int_node *tree,
190 + unsigned char n, struct leaf_node *entry)
191 +{
192 + struct leaf_node *l;
193 + struct int_node *parent_stack[20];
194 + unsigned char i, j;
195 + void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
196 +
197 + assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
198 + if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
199 + return; /* type mismatch, nothing to remove */
200 + l = (struct leaf_node *) CLR_PTR_TYPE(*p);
201 + if (hashcmp(l->key_sha1, entry->key_sha1))
202 + return; /* key mismatch, nothing to remove */
203 +
204 + /* we have found a matching entry */
205 + free(l);
206 + *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
207 +
208 + /* consolidate this tree level, and parent levels, if possible */
209 + if (!n)
210 + return; /* cannot consolidate top level */
211 + /* first, build stack of ancestors between root and current node */
212 + parent_stack[0] = t->root;
213 + for (i = 0; i < n; i++) {
214 + j = GET_NIBBLE(i, entry->key_sha1);
215 + parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
216 + }
217 + assert(i == n && parent_stack[i] == tree);
218 + /* next, unwind stack until note_tree_consolidate() is done */
219 + while (i > 0 &&
220 + !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
221 + GET_NIBBLE(i - 1, entry->key_sha1)))
222 + i--;
223 +}
224 +
225 /*
226 * To insert a leaf_node:
227 * Search to the tree location appropriate for the given leaf_node's key:
@@ -229,79 +302,6 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,
302 note_tree_insert(t, new_node, n + 1, entry, type, combine_notes);
303 }
304
232 -/*
233 - * How to consolidate an int_node:
234 - * If there are > 1 non-NULL entries, give up and return non-zero.
235 - * Otherwise replace the int_node at the given index in the given parent node
236 - * with the only entry (or a NULL entry if no entries) from the given tree,
237 - * and return 0.
238 - */
239 -static int note_tree_consolidate(struct int_node *tree,
240 - struct int_node *parent, unsigned char index)
241 -{
242 - unsigned int i;
243 - void *p = NULL;
244 -
245 - assert(tree && parent);
246 - assert(CLR_PTR_TYPE(parent->a[index]) == tree);
247 -
248 - for (i = 0; i < 16; i++) {
249 - if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {
250 - if (p) /* more than one entry */
251 - return -2;
252 - p = tree->a[i];
253 - }
254 - }
255 -
256 - /* replace tree with p in parent[index] */
257 - parent->a[index] = p;
258 - free(tree);
259 - return 0;
260 -}
261 -
262 -/*
263 - * To remove a leaf_node:
264 - * Search to the tree location appropriate for the given leaf_node's key:
265 - * - If location does not hold a matching entry, abort and do nothing.
266 - * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
267 - * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
268 - */
269 -static void note_tree_remove(struct notes_tree *t, struct int_node *tree,
270 - unsigned char n, struct leaf_node *entry)
271 -{
272 - struct leaf_node *l;
273 - struct int_node *parent_stack[20];
274 - unsigned char i, j;
275 - void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
276 -
277 - assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
278 - if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
279 - return; /* type mismatch, nothing to remove */
280 - l = (struct leaf_node *) CLR_PTR_TYPE(*p);
281 - if (hashcmp(l->key_sha1, entry->key_sha1))
282 - return; /* key mismatch, nothing to remove */
283 -
284 - /* we have found a matching entry */
285 - free(l);
286 - *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
287 -
288 - /* consolidate this tree level, and parent levels, if possible */
289 - if (!n)
290 - return; /* cannot consolidate top level */
291 - /* first, build stack of ancestors between root and current node */
292 - parent_stack[0] = t->root;
293 - for (i = 0; i < n; i++) {
294 - j = GET_NIBBLE(i, entry->key_sha1);
295 - parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
296 - }
297 - assert(i == n && parent_stack[i] == tree);
298 - /* next, unwind stack until note_tree_consolidate() is done */
299 - while (i > 0 &&
300 - !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
301 - GET_NIBBLE(i - 1, entry->key_sha1)))
302 - i--;
303 -}
304 -
305 /* Free the entire notes data contained in the given tree */
306 static void note_tree_free(struct int_node *tree)
307 {