10 void *alloc_blob_node(struct repository
*r
);
11 void *alloc_tree_node(struct repository
*r
);
12 void init_commit_node(struct commit
*c
);
13 void *alloc_commit_node(struct repository
*r
);
14 void *alloc_tag_node(struct repository
*r
);
15 void *alloc_object_node(struct repository
*r
);
17 struct alloc_state
*allocate_alloc_state(void);
18 void clear_alloc_state(struct alloc_state
*s
);
20 #define alloc_nr(x) (((x)+16)*3/2)
23 * Dynamically growing an array using realloc() is error prone and boring.
25 * Define your array with:
27 * - a pointer (`item`) that points at the array, initialized to `NULL`
28 * (although please name the variable based on its contents, not on its
31 * - an integer variable (`alloc`) that keeps track of how big the current
32 * allocation is, initialized to `0`;
34 * - another integer variable (`nr`) to keep track of how many elements the
35 * array currently has, initialized to `0`.
37 * Then before adding `n`th element to the item, call `ALLOC_GROW(item, n,
38 * alloc)`. This ensures that the array can hold at least `n` elements by
39 * calling `realloc(3)` and adjusting `alloc` variable.
46 * for (i = 0; i < nr; i++)
47 * if (we like item[i] already)
50 * // we did not like any existing one, so add one
51 * ALLOC_GROW(item, nr + 1, alloc);
52 * item[nr++] = value you like;
55 * You are responsible for updating the `nr` variable.
57 * If you need to specify the number of elements to allocate explicitly
58 * then use the macro `REALLOC_ARRAY(item, alloc)` instead of `ALLOC_GROW`.
60 * Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some
63 * DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'.
65 #define ALLOC_GROW(x, nr, alloc) \
68 if (alloc_nr(alloc) < (nr)) \
71 alloc = alloc_nr(alloc); \
72 REALLOC_ARRAY(x, alloc); \
77 * Similar to ALLOC_GROW but handles updating of the nr value and
78 * zeroing the bytes of the newly-grown array elements.
80 * DO NOT USE any expression with side-effect for any of the
83 #define ALLOC_GROW_BY(x, nr, increase, alloc) \
86 size_t new_nr = nr + (increase); \
88 BUG("negative growth in ALLOC_GROW_BY"); \
89 ALLOC_GROW(x, new_nr, alloc); \
90 memset((x) + nr, 0, sizeof(*(x)) * (increase)); \