Skip to content

Commit

Permalink
git-compat-util: move alloc macros to git-compat-util.h
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
calvin-wan-google authored and gitster committed Jul 5, 2023
1 parent da9502f commit 91c080d
Show file tree
Hide file tree
Showing 88 changed files with 75 additions and 161 deletions.
1 change: 0 additions & 1 deletion add-patch.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "git-compat-util.h"
#include "add-interactive.h"
#include "advice.h"
#include "alloc.h"
#include "editor.h"
#include "environment.h"
#include "gettext.h"
Expand Down
1 change: 0 additions & 1 deletion alias.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "alias.h"
#include "alloc.h"
#include "config.h"
#include "gettext.h"
#include "strbuf.h"
Expand Down
75 changes: 0 additions & 75 deletions alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,79 +17,4 @@ void *alloc_object_node(struct repository *r);
struct alloc_state *allocate_alloc_state(void);
void clear_alloc_state(struct alloc_state *s);

#define alloc_nr(x) (((x)+16)*3/2)

/**
* Dynamically growing an array using realloc() is error prone and boring.
*
* Define your array with:
*
* - a pointer (`item`) that points at the array, initialized to `NULL`
* (although please name the variable based on its contents, not on its
* type);
*
* - an integer variable (`alloc`) that keeps track of how big the current
* allocation is, initialized to `0`;
*
* - another integer variable (`nr`) to keep track of how many elements the
* array currently has, initialized to `0`.
*
* Then before adding `n`th element to the item, call `ALLOC_GROW(item, n,
* alloc)`. This ensures that the array can hold at least `n` elements by
* calling `realloc(3)` and adjusting `alloc` variable.
*
* ------------
* sometype *item;
* size_t nr;
* size_t alloc
*
* for (i = 0; i < nr; i++)
* if (we like item[i] already)
* return;
*
* // we did not like any existing one, so add one
* ALLOC_GROW(item, nr + 1, alloc);
* item[nr++] = value you like;
* ------------
*
* You are responsible for updating the `nr` variable.
*
* If you need to specify the number of elements to allocate explicitly
* then use the macro `REALLOC_ARRAY(item, alloc)` instead of `ALLOC_GROW`.
*
* Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some
* added niceties.
*
* DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'.
*/
#define ALLOC_GROW(x, nr, alloc) \
do { \
if ((nr) > alloc) { \
if (alloc_nr(alloc) < (nr)) \
alloc = (nr); \
else \
alloc = alloc_nr(alloc); \
REALLOC_ARRAY(x, alloc); \
} \
} while (0)

/*
* Similar to ALLOC_GROW but handles updating of the nr value and
* zeroing the bytes of the newly-grown array elements.
*
* DO NOT USE any expression with side-effect for any of the
* arguments.
*/
#define ALLOC_GROW_BY(x, nr, increase, alloc) \
do { \
if (increase) { \
size_t new_nr = nr + (increase); \
if (new_nr < nr) \
BUG("negative growth in ALLOC_GROW_BY"); \
ALLOC_GROW(x, new_nr, alloc); \
memset((x) + nr, 0, sizeof(*(x)) * (increase)); \
nr = new_nr; \
} \
} while (0)

#endif
1 change: 0 additions & 1 deletion apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "git-compat-util.h"
#include "abspath.h"
#include "alloc.h"
#include "base85.h"
#include "config.h"
#include "object-store-ll.h"
Expand Down
1 change: 0 additions & 1 deletion archive-tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Copyright (c) 2005, 2006 Rene Scharfe
*/
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "gettext.h"
#include "git-zlib.h"
Expand Down
1 change: 0 additions & 1 deletion archive.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
#include "alloc.h"
#include "config.h"
#include "convert.h"
#include "environment.h"
Expand Down
1 change: 0 additions & 1 deletion attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "exec-cmd.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "color.h"
#include "builtin.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/cat-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "alloc.h"
#include "config.h"
#include "convert.h"
#include "diff.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/checkout--worker.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "builtin.h"
#include "alloc.h"
#include "config.h"
#include "entry.h"
#include "gettext.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/config.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "builtin.h"
#include "abspath.h"
#include "alloc.h"
#include "config.h"
#include "color.h"
#include "editor.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/credential-cache--daemon.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "builtin.h"
#include "abspath.h"
#include "alloc.h"
#include "gettext.h"
#include "object-file.h"
#include "parse-options.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/fetch-pack.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "builtin.h"
#include "alloc.h"
#include "gettext.h"
#include "hex.h"
#include "object-file.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/fsmonitor--daemon.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "builtin.h"
#include "abspath.h"
#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Copyright (c) 2006 Junio C Hamano
*/
#include "builtin.h"
#include "alloc.h"
#include "gettext.h"
#include "hex.h"
#include "repository.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/index-pack.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "builtin.h"
#include "alloc.h"
#include "config.h"
#include "delta.h"
#include "environment.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
#include "git-compat-util.h"
#include "abspath.h"
#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "builtin.h"
#include "abspath.h"
#include "advice.h"
#include "alloc.h"
#include "config.h"
#include "editor.h"
#include "environment.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/mktree.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Copyright (c) Junio C Hamano, 2006, 2009
*/
#include "builtin.h"
#include "alloc.h"
#include "gettext.h"
#include "hex.h"
#include "quote.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/mv.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "builtin.h"
#include "abspath.h"
#include "advice.h"
#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/name-rev.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "builtin.h"
#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/pack-objects.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "builtin.h"
#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/repack.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "builtin.h"
#include "alloc.h"
#include "config.h"
#include "dir.h"
#include "environment.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/rev-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "abspath.h"
#include "alloc.h"
#include "config.h"
#include "commit.h"
#include "environment.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/revert.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "builtin.h"
#include "parse-options.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/rm.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "alloc.h"
#include "advice.h"
#include "config.h"
#include "lockfile.h"
Expand Down
1 change: 0 additions & 1 deletion builtin/submodule--helper.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "abspath.h"
#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
Expand Down
1 change: 0 additions & 1 deletion bulk-checkin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Copyright (c) 2011, Google Inc.
*/
#include "git-compat-util.h"
#include "alloc.h"
#include "bulk-checkin.h"
#include "environment.h"
#include "gettext.h"
Expand Down
1 change: 0 additions & 1 deletion cache-tree.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
#include "alloc.h"
#include "environment.h"
#include "hex.h"
#include "lockfile.h"
Expand Down
1 change: 0 additions & 1 deletion chunk-format.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
#include "alloc.h"
#include "chunk-format.h"
#include "csum-file.h"
#include "gettext.h"
Expand Down
1 change: 0 additions & 1 deletion commit-reach.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
#include "alloc.h"
#include "commit.h"
#include "commit-graph.h"
#include "decorate.h"
Expand Down
1 change: 0 additions & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "git-compat-util.h"
#include "abspath.h"
#include "advice.h"
#include "alloc.h"
#include "date.h"
#include "branch.h"
#include "config.h"
Expand Down
1 change: 0 additions & 1 deletion daemon.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "path.h"
Expand Down
1 change: 0 additions & 1 deletion delta-islands.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
#include "alloc.h"
#include "attr.h"
#include "object.h"
#include "blob.h"
Expand Down
1 change: 0 additions & 1 deletion diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
#include "git-compat-util.h"
#include "abspath.h"
#include "alloc.h"
#include "base85.h"
#include "config.h"
#include "convert.h"
Expand Down
1 change: 0 additions & 1 deletion diffcore-rename.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Copyright (C) 2005 Junio C Hamano
*/
#include "git-compat-util.h"
#include "alloc.h"
#include "diff.h"
#include "diffcore.h"
#include "object-store-ll.h"
Expand Down
1 change: 0 additions & 1 deletion dir-iterator.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
#include "alloc.h"
#include "dir.h"
#include "iterator.h"
#include "dir-iterator.h"
Expand Down
1 change: 0 additions & 1 deletion dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
#include "git-compat-util.h"
#include "abspath.h"
#include "alloc.h"
#include "config.h"
#include "convert.h"
#include "dir.h"
Expand Down
1 change: 0 additions & 1 deletion ewah/bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "git-compat-util.h"
#include "alloc.h"
#include "ewok.h"

#define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
Expand Down
1 change: 0 additions & 1 deletion ewah/ewah_bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "git-compat-util.h"
#include "alloc.h"
#include "ewok.h"
#include "ewok_rlw.h"

Expand Down
1 change: 0 additions & 1 deletion fetch-pack.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
#include "alloc.h"
#include "repository.h"
#include "config.h"
#include "date.h"
Expand Down
1 change: 0 additions & 1 deletion fmt-merge-msg.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "refs.h"
Expand Down
1 change: 0 additions & 1 deletion fsck.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
#include "alloc.h"
#include "date.h"
#include "dir.h"
#include "hex.h"
Expand Down
Loading

0 comments on commit 91c080d

Please sign in to comment.