Skip to content

Commit 36bf195

Browse files
newrengitster
authored andcommitted
alloc.h: move ALLOC_GROW() functions from cache.h
This allows us to replace includes of cache.h with includes of the much smaller alloc.h in many places. It does mean that we also need to add includes of alloc.h in a number of C files. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 15db4e7 commit 36bf195

Some content is hidden

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

91 files changed

+219
-134
lines changed

add-patch.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "add-interactive.h"
3+
#include "alloc.h"
34
#include "strbuf.h"
45
#include "run-command.h"
56
#include "strvec.h"

alias.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
#include "cache.h"
1+
#include "git-compat-util.h"
22
#include "alias.h"
3+
#include "alloc.h"
34
#include "config.h"
5+
#include "gettext.h"
46
#include "string-list.h"
57

68
struct config_alias_data {

alloc.h

+75
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,79 @@ 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+
2095
#endif

apply.c

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

1010
#include "cache.h"
11+
#include "alloc.h"
1112
#include "config.h"
1213
#include "object-store.h"
1314
#include "blob.h"

archive-tar.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/*
22
* Copyright (c) 2005, 2006 Rene Scharfe
33
*/
4-
#include "cache.h"
4+
#include "git-compat-util.h"
5+
#include "alloc.h"
56
#include "config.h"
67
#include "tar.h"
78
#include "archive.h"

archive.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include "cache.h"
1+
#include "git-compat-util.h"
2+
#include "alloc.h"
23
#include "config.h"
34
#include "refs.h"
45
#include "object-store.h"

attr.c

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

99
#include "cache.h"
10+
#include "alloc.h"
1011
#include "config.h"
1112
#include "exec-cmd.h"
1213
#include "attr.h"

builtin/blame.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* See COPYING for licensing conditions
66
*/
77

8-
#include "cache.h"
8+
#include "git-compat-util.h"
9+
#include "alloc.h"
910
#include "config.h"
1011
#include "color.h"
1112
#include "builtin.h"

builtin/cat-file.c

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
#define USE_THE_INDEX_VARIABLE
77
#include "cache.h"
8+
#include "alloc.h"
89
#include "config.h"
910
#include "builtin.h"
1011
#include "diff.h"

builtin/checkout--worker.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "builtin.h"
2+
#include "alloc.h"
23
#include "config.h"
34
#include "entry.h"
45
#include "parallel-checkout.h"

builtin/config.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "builtin.h"
2-
#include "cache.h"
2+
#include "alloc.h"
33
#include "config.h"
44
#include "color.h"
55
#include "parse-options.h"

builtin/credential-cache--daemon.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "builtin.h"
2+
#include "alloc.h"
23
#include "parse-options.h"
34

45
#ifndef NO_UNIX_SOCKETS

builtin/fetch-pack.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "builtin.h"
2+
#include "alloc.h"
23
#include "pkt-line.h"
34
#include "fetch-pack.h"
45
#include "remote.h"

builtin/fsmonitor--daemon.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "builtin.h"
2+
#include "alloc.h"
23
#include "config.h"
34
#include "parse-options.h"
45
#include "fsmonitor.h"

builtin/grep.c

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Copyright (c) 2006 Junio C Hamano
55
*/
66
#include "cache.h"
7+
#include "alloc.h"
78
#include "repository.h"
89
#include "config.h"
910
#include "blob.h"

builtin/index-pack.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "builtin.h"
2+
#include "alloc.h"
23
#include "config.h"
34
#include "delta.h"
45
#include "pack.h"

builtin/log.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
* (C) Copyright 2006 Linus Torvalds
55
* 2006 Junio Hamano
66
*/
7-
#include "cache.h"
7+
#include "git-compat-util.h"
8+
#include "alloc.h"
89
#include "config.h"
910
#include "refs.h"
1011
#include "object-store.h"

builtin/merge.c

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#define USE_THE_INDEX_VARIABLE
1010
#include "cache.h"
11+
#include "alloc.h"
1112
#include "config.h"
1213
#include "parse-options.h"
1314
#include "builtin.h"

builtin/mktree.c

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Copyright (c) Junio C Hamano, 2006, 2009
55
*/
66
#include "builtin.h"
7+
#include "alloc.h"
78
#include "quote.h"
89
#include "tree.h"
910
#include "parse-options.h"

builtin/mv.c

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
#define USE_THE_INDEX_VARIABLE
77
#include "builtin.h"
8+
#include "alloc.h"
89
#include "config.h"
910
#include "pathspec.h"
1011
#include "lockfile.h"

builtin/name-rev.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "builtin.h"
2-
#include "cache.h"
2+
#include "alloc.h"
33
#include "repository.h"
44
#include "config.h"
55
#include "commit.h"

builtin/pack-objects.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "builtin.h"
2-
#include "cache.h"
2+
#include "alloc.h"
33
#include "repository.h"
44
#include "config.h"
55
#include "attr.h"

builtin/repack.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "builtin.h"
2-
#include "cache.h"
2+
#include "alloc.h"
33
#include "config.h"
44
#include "dir.h"
55
#include "parse-options.h"

builtin/rev-parse.c

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
#define USE_THE_INDEX_VARIABLE
77
#include "cache.h"
8+
#include "alloc.h"
89
#include "config.h"
910
#include "commit.h"
1011
#include "refs.h"

builtin/revert.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include "cache.h"
1+
#include "git-compat-util.h"
2+
#include "alloc.h"
23
#include "config.h"
34
#include "builtin.h"
45
#include "parse-options.h"

builtin/rm.c

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

builtin/submodule--helper.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#define USE_THE_INDEX_VARIABLE
22
#include "builtin.h"
3+
#include "alloc.h"
34
#include "repository.h"
45
#include "cache.h"
56
#include "config.h"

bulk-checkin.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/*
22
* Copyright (c) 2011, Google Inc.
33
*/
4-
#include "cache.h"
4+
#include "git-compat-util.h"
5+
#include "alloc.h"
56
#include "bulk-checkin.h"
67
#include "lockfile.h"
78
#include "repository.h"

cache-tree.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include "cache.h"
1+
#include "git-compat-util.h"
2+
#include "alloc.h"
23
#include "lockfile.h"
34
#include "tree.h"
45
#include "tree-walk.h"

cache.h

-75
Original file line numberDiff line numberDiff line change
@@ -656,81 +656,6 @@ void initialize_repository_version(int hash_algo, int reinit);
656656
void sanitize_stdfds(void);
657657
int daemonize(void);
658658

659-
#define alloc_nr(x) (((x)+16)*3/2)
660-
661-
/**
662-
* Dynamically growing an array using realloc() is error prone and boring.
663-
*
664-
* Define your array with:
665-
*
666-
* - a pointer (`item`) that points at the array, initialized to `NULL`
667-
* (although please name the variable based on its contents, not on its
668-
* type);
669-
*
670-
* - an integer variable (`alloc`) that keeps track of how big the current
671-
* allocation is, initialized to `0`;
672-
*
673-
* - another integer variable (`nr`) to keep track of how many elements the
674-
* array currently has, initialized to `0`.
675-
*
676-
* Then before adding `n`th element to the item, call `ALLOC_GROW(item, n,
677-
* alloc)`. This ensures that the array can hold at least `n` elements by
678-
* calling `realloc(3)` and adjusting `alloc` variable.
679-
*
680-
* ------------
681-
* sometype *item;
682-
* size_t nr;
683-
* size_t alloc
684-
*
685-
* for (i = 0; i < nr; i++)
686-
* if (we like item[i] already)
687-
* return;
688-
*
689-
* // we did not like any existing one, so add one
690-
* ALLOC_GROW(item, nr + 1, alloc);
691-
* item[nr++] = value you like;
692-
* ------------
693-
*
694-
* You are responsible for updating the `nr` variable.
695-
*
696-
* If you need to specify the number of elements to allocate explicitly
697-
* then use the macro `REALLOC_ARRAY(item, alloc)` instead of `ALLOC_GROW`.
698-
*
699-
* Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some
700-
* added niceties.
701-
*
702-
* DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'.
703-
*/
704-
#define ALLOC_GROW(x, nr, alloc) \
705-
do { \
706-
if ((nr) > alloc) { \
707-
if (alloc_nr(alloc) < (nr)) \
708-
alloc = (nr); \
709-
else \
710-
alloc = alloc_nr(alloc); \
711-
REALLOC_ARRAY(x, alloc); \
712-
} \
713-
} while (0)
714-
715-
/*
716-
* Similar to ALLOC_GROW but handles updating of the nr value and
717-
* zeroing the bytes of the newly-grown array elements.
718-
*
719-
* DO NOT USE any expression with side-effect for any of the
720-
* arguments.
721-
*/
722-
#define ALLOC_GROW_BY(x, nr, increase, alloc) \
723-
do { \
724-
if (increase) { \
725-
size_t new_nr = nr + (increase); \
726-
if (new_nr < nr) \
727-
BUG("negative growth in ALLOC_GROW_BY"); \
728-
ALLOC_GROW(x, new_nr, alloc); \
729-
memset((x) + nr, 0, sizeof(*(x)) * (increase)); \
730-
nr = new_nr; \
731-
} \
732-
} while (0)
733-
734659
/* Initialize and use the cache information */
735660
struct lock_file;
736661
void preload_index(struct index_state *index,

chunk-format.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include "cache.h"
1+
#include "git-compat-util.h"
2+
#include "alloc.h"
23
#include "chunk-format.h"
34
#include "csum-file.h"
45

commit-reach.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include "cache.h"
1+
#include "git-compat-util.h"
2+
#include "alloc.h"
23
#include "commit.h"
34
#include "commit-graph.h"
45
#include "decorate.h"

compat/mingw.c

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "../strbuf.h"
88
#include "../run-command.h"
99
#include "../cache.h"
10+
#include "../alloc.h"
1011
#include "win32/lazyload.h"
1112
#include "../config.h"
1213
#include "dir.h"

0 commit comments

Comments
 (0)