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

add-patch.c

Lines changed: 0 additions & 1 deletion
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"

alias.c

Lines changed: 0 additions & 1 deletion
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"

alloc.h

Lines changed: 0 additions & 75 deletions
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

apply.c

Lines changed: 0 additions & 1 deletion
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"

archive-tar.c

Lines changed: 0 additions & 1 deletion
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"

archive.c

Lines changed: 0 additions & 1 deletion
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"

attr.c

Lines changed: 0 additions & 1 deletion
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"

builtin/blame.c

Lines changed: 0 additions & 1 deletion
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"

builtin/cat-file.c

Lines changed: 0 additions & 1 deletion
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"

builtin/checkout--worker.c

Lines changed: 0 additions & 1 deletion
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"

0 commit comments

Comments
 (0)