Skip to content

Commit 91c080d

Browse files
calvin-wan-googlegitster
authored andcommitted
git-compat-util: move alloc macros to git-compat-util.h
alloc_nr, ALLOC_GROW, and ALLOC_GROW_BY are commonly used macros for dynamic array allocation. Moving these macros to git-compat-util.h with the other alloc macros focuses alloc.[ch] to allocation for Git objects and additionally allows us to remove inclusions to alloc.h from files that solely used the above macros. Signed-off-by: Calvin Wan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent da9502f commit 91c080d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+75
-161
lines changed

Diff for: add-patch.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "git-compat-util.h"
22
#include "add-interactive.h"
33
#include "advice.h"
4-
#include "alloc.h"
54
#include "editor.h"
65
#include "environment.h"
76
#include "gettext.h"

Diff for: alias.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "git-compat-util.h"
22
#include "alias.h"
3-
#include "alloc.h"
43
#include "config.h"
54
#include "gettext.h"
65
#include "strbuf.h"

Diff for: alloc.h

-75
Original file line numberDiff line numberDiff line change
@@ -17,79 +17,4 @@ void *alloc_object_node(struct repository *r);
1717
struct alloc_state *allocate_alloc_state(void);
1818
void clear_alloc_state(struct alloc_state *s);
1919

20-
#define alloc_nr(x) (((x)+16)*3/2)
21-
22-
/**
23-
* Dynamically growing an array using realloc() is error prone and boring.
24-
*
25-
* Define your array with:
26-
*
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
29-
* type);
30-
*
31-
* - an integer variable (`alloc`) that keeps track of how big the current
32-
* allocation is, initialized to `0`;
33-
*
34-
* - another integer variable (`nr`) to keep track of how many elements the
35-
* array currently has, initialized to `0`.
36-
*
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.
40-
*
41-
* ------------
42-
* sometype *item;
43-
* size_t nr;
44-
* size_t alloc
45-
*
46-
* for (i = 0; i < nr; i++)
47-
* if (we like item[i] already)
48-
* return;
49-
*
50-
* // we did not like any existing one, so add one
51-
* ALLOC_GROW(item, nr + 1, alloc);
52-
* item[nr++] = value you like;
53-
* ------------
54-
*
55-
* You are responsible for updating the `nr` variable.
56-
*
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`.
59-
*
60-
* Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some
61-
* added niceties.
62-
*
63-
* DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'.
64-
*/
65-
#define ALLOC_GROW(x, nr, alloc) \
66-
do { \
67-
if ((nr) > alloc) { \
68-
if (alloc_nr(alloc) < (nr)) \
69-
alloc = (nr); \
70-
else \
71-
alloc = alloc_nr(alloc); \
72-
REALLOC_ARRAY(x, alloc); \
73-
} \
74-
} while (0)
75-
76-
/*
77-
* Similar to ALLOC_GROW but handles updating of the nr value and
78-
* zeroing the bytes of the newly-grown array elements.
79-
*
80-
* DO NOT USE any expression with side-effect for any of the
81-
* arguments.
82-
*/
83-
#define ALLOC_GROW_BY(x, nr, increase, alloc) \
84-
do { \
85-
if (increase) { \
86-
size_t new_nr = nr + (increase); \
87-
if (new_nr < nr) \
88-
BUG("negative growth in ALLOC_GROW_BY"); \
89-
ALLOC_GROW(x, new_nr, alloc); \
90-
memset((x) + nr, 0, sizeof(*(x)) * (increase)); \
91-
nr = new_nr; \
92-
} \
93-
} while (0)
94-
9520
#endif

Diff for: apply.c

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include "git-compat-util.h"
1111
#include "abspath.h"
12-
#include "alloc.h"
1312
#include "base85.h"
1413
#include "config.h"
1514
#include "object-store-ll.h"

Diff for: archive-tar.c

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Copyright (c) 2005, 2006 Rene Scharfe
33
*/
44
#include "git-compat-util.h"
5-
#include "alloc.h"
65
#include "config.h"
76
#include "gettext.h"
87
#include "git-zlib.h"

Diff for: archive.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "git-compat-util.h"
22
#include "abspath.h"
3-
#include "alloc.h"
43
#include "config.h"
54
#include "convert.h"
65
#include "environment.h"

Diff for: attr.c

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
#include "git-compat-util.h"
10-
#include "alloc.h"
1110
#include "config.h"
1211
#include "environment.h"
1312
#include "exec-cmd.h"

Diff for: builtin/blame.c

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
#include "git-compat-util.h"
9-
#include "alloc.h"
109
#include "config.h"
1110
#include "color.h"
1211
#include "builtin.h"

Diff for: builtin/cat-file.c

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
#define USE_THE_INDEX_VARIABLE
77
#include "builtin.h"
8-
#include "alloc.h"
98
#include "config.h"
109
#include "convert.h"
1110
#include "diff.h"

Diff for: builtin/checkout--worker.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "builtin.h"
2-
#include "alloc.h"
32
#include "config.h"
43
#include "entry.h"
54
#include "gettext.h"

Diff for: builtin/config.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "builtin.h"
22
#include "abspath.h"
3-
#include "alloc.h"
43
#include "config.h"
54
#include "color.h"
65
#include "editor.h"

Diff for: builtin/credential-cache--daemon.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "builtin.h"
22
#include "abspath.h"
3-
#include "alloc.h"
43
#include "gettext.h"
54
#include "object-file.h"
65
#include "parse-options.h"

Diff for: builtin/fetch-pack.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "builtin.h"
2-
#include "alloc.h"
32
#include "gettext.h"
43
#include "hex.h"
54
#include "object-file.h"

Diff for: builtin/fsmonitor--daemon.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "builtin.h"
22
#include "abspath.h"
3-
#include "alloc.h"
43
#include "config.h"
54
#include "environment.h"
65
#include "gettext.h"

Diff for: builtin/grep.c

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* Copyright (c) 2006 Junio C Hamano
55
*/
66
#include "builtin.h"
7-
#include "alloc.h"
87
#include "gettext.h"
98
#include "hex.h"
109
#include "repository.h"

Diff for: builtin/index-pack.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "builtin.h"
2-
#include "alloc.h"
32
#include "config.h"
43
#include "delta.h"
54
#include "environment.h"

Diff for: builtin/log.c

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
#include "git-compat-util.h"
88
#include "abspath.h"
9-
#include "alloc.h"
109
#include "config.h"
1110
#include "environment.h"
1211
#include "gettext.h"

Diff for: builtin/merge.c

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "builtin.h"
1111
#include "abspath.h"
1212
#include "advice.h"
13-
#include "alloc.h"
1413
#include "config.h"
1514
#include "editor.h"
1615
#include "environment.h"

Diff for: builtin/mktree.c

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* Copyright (c) Junio C Hamano, 2006, 2009
55
*/
66
#include "builtin.h"
7-
#include "alloc.h"
87
#include "gettext.h"
98
#include "hex.h"
109
#include "quote.h"

Diff for: builtin/mv.c

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "builtin.h"
88
#include "abspath.h"
99
#include "advice.h"
10-
#include "alloc.h"
1110
#include "config.h"
1211
#include "environment.h"
1312
#include "gettext.h"

Diff for: builtin/name-rev.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "builtin.h"
2-
#include "alloc.h"
32
#include "environment.h"
43
#include "gettext.h"
54
#include "hex.h"

Diff for: builtin/pack-objects.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "builtin.h"
2-
#include "alloc.h"
32
#include "environment.h"
43
#include "gettext.h"
54
#include "hex.h"

Diff for: builtin/repack.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "builtin.h"
2-
#include "alloc.h"
32
#include "config.h"
43
#include "dir.h"
54
#include "environment.h"

Diff for: builtin/rev-parse.c

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#define USE_THE_INDEX_VARIABLE
77
#include "builtin.h"
88
#include "abspath.h"
9-
#include "alloc.h"
109
#include "config.h"
1110
#include "commit.h"
1211
#include "environment.h"

Diff for: builtin/revert.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "git-compat-util.h"
2-
#include "alloc.h"
32
#include "config.h"
43
#include "builtin.h"
54
#include "parse-options.h"

Diff for: builtin/rm.c

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
#define USE_THE_INDEX_VARIABLE
77
#include "builtin.h"
8-
#include "alloc.h"
98
#include "advice.h"
109
#include "config.h"
1110
#include "lockfile.h"

Diff for: builtin/submodule--helper.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#define USE_THE_INDEX_VARIABLE
22
#include "builtin.h"
33
#include "abspath.h"
4-
#include "alloc.h"
54
#include "environment.h"
65
#include "gettext.h"
76
#include "hex.h"

Diff for: bulk-checkin.c

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Copyright (c) 2011, Google Inc.
33
*/
44
#include "git-compat-util.h"
5-
#include "alloc.h"
65
#include "bulk-checkin.h"
76
#include "environment.h"
87
#include "gettext.h"

Diff for: cache-tree.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "git-compat-util.h"
2-
#include "alloc.h"
32
#include "environment.h"
43
#include "hex.h"
54
#include "lockfile.h"

Diff for: chunk-format.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "git-compat-util.h"
2-
#include "alloc.h"
32
#include "chunk-format.h"
43
#include "csum-file.h"
54
#include "gettext.h"

Diff for: commit-reach.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "git-compat-util.h"
2-
#include "alloc.h"
32
#include "commit.h"
43
#include "commit-graph.h"
54
#include "decorate.h"

Diff for: config.c

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "git-compat-util.h"
99
#include "abspath.h"
1010
#include "advice.h"
11-
#include "alloc.h"
1211
#include "date.h"
1312
#include "branch.h"
1413
#include "config.h"

Diff for: daemon.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "git-compat-util.h"
22
#include "abspath.h"
3-
#include "alloc.h"
43
#include "config.h"
54
#include "environment.h"
65
#include "path.h"

Diff for: delta-islands.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "git-compat-util.h"
2-
#include "alloc.h"
32
#include "attr.h"
43
#include "object.h"
54
#include "blob.h"

Diff for: diff.c

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*/
44
#include "git-compat-util.h"
55
#include "abspath.h"
6-
#include "alloc.h"
76
#include "base85.h"
87
#include "config.h"
98
#include "convert.h"

Diff for: diffcore-rename.c

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright (C) 2005 Junio C Hamano
44
*/
55
#include "git-compat-util.h"
6-
#include "alloc.h"
76
#include "diff.h"
87
#include "diffcore.h"
98
#include "object-store-ll.h"

Diff for: dir-iterator.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "git-compat-util.h"
2-
#include "alloc.h"
32
#include "dir.h"
43
#include "iterator.h"
54
#include "dir-iterator.h"

Diff for: dir.c

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88
#include "git-compat-util.h"
99
#include "abspath.h"
10-
#include "alloc.h"
1110
#include "config.h"
1211
#include "convert.h"
1312
#include "dir.h"

Diff for: ewah/bitmap.c

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* along with this program; if not, see <http://www.gnu.org/licenses/>.
1818
*/
1919
#include "git-compat-util.h"
20-
#include "alloc.h"
2120
#include "ewok.h"
2221

2322
#define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))

Diff for: ewah/ewah_bitmap.c

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* along with this program; if not, see <http://www.gnu.org/licenses/>.
1818
*/
1919
#include "git-compat-util.h"
20-
#include "alloc.h"
2120
#include "ewok.h"
2221
#include "ewok_rlw.h"
2322

Diff for: fetch-pack.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "git-compat-util.h"
2-
#include "alloc.h"
32
#include "repository.h"
43
#include "config.h"
54
#include "date.h"

Diff for: fmt-merge-msg.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "git-compat-util.h"
2-
#include "alloc.h"
32
#include "config.h"
43
#include "environment.h"
54
#include "refs.h"

Diff for: fsck.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "git-compat-util.h"
2-
#include "alloc.h"
32
#include "date.h"
43
#include "dir.h"
54
#include "hex.h"

0 commit comments

Comments
 (0)