1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-04-25 12:35:11 +02:00

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 <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Elijah Newren 2023-02-24 00:09:24 +00:00 committed by Junio C Hamano
parent 15db4e7f4a
commit 36bf195890
91 changed files with 219 additions and 134 deletions

View File

@ -1,5 +1,6 @@
#include "cache.h"
#include "add-interactive.h"
#include "alloc.h"
#include "strbuf.h"
#include "run-command.h"
#include "strvec.h"

View File

@ -1,6 +1,8 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alias.h"
#include "alloc.h"
#include "config.h"
#include "gettext.h"
#include "string-list.h"
struct config_alias_data {

75
alloc.h
View File

@ -17,4 +17,79 @@ 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

View File

@ -8,6 +8,7 @@
*/
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "object-store.h"
#include "blob.h"

View File

@ -1,7 +1,8 @@
/*
* Copyright (c) 2005, 2006 Rene Scharfe
*/
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "tar.h"
#include "archive.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "refs.h"
#include "object-store.h"

1
attr.c
View File

@ -7,6 +7,7 @@
*/
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "exec-cmd.h"
#include "attr.h"

View File

@ -5,7 +5,8 @@
* See COPYING for licensing conditions
*/
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "color.h"
#include "builtin.h"

View File

@ -5,6 +5,7 @@
*/
#define USE_THE_INDEX_VARIABLE
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "builtin.h"
#include "diff.h"

View File

@ -1,4 +1,5 @@
#include "builtin.h"
#include "alloc.h"
#include "config.h"
#include "entry.h"
#include "parallel-checkout.h"

View File

@ -1,5 +1,5 @@
#include "builtin.h"
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "color.h"
#include "parse-options.h"

View File

@ -1,4 +1,5 @@
#include "builtin.h"
#include "alloc.h"
#include "parse-options.h"
#ifndef NO_UNIX_SOCKETS

View File

@ -1,4 +1,5 @@
#include "builtin.h"
#include "alloc.h"
#include "pkt-line.h"
#include "fetch-pack.h"
#include "remote.h"

View File

@ -1,4 +1,5 @@
#include "builtin.h"
#include "alloc.h"
#include "config.h"
#include "parse-options.h"
#include "fsmonitor.h"

View File

@ -4,6 +4,7 @@
* Copyright (c) 2006 Junio C Hamano
*/
#include "cache.h"
#include "alloc.h"
#include "repository.h"
#include "config.h"
#include "blob.h"

View File

@ -1,4 +1,5 @@
#include "builtin.h"
#include "alloc.h"
#include "config.h"
#include "delta.h"
#include "pack.h"

View File

@ -4,7 +4,8 @@
* (C) Copyright 2006 Linus Torvalds
* 2006 Junio Hamano
*/
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "refs.h"
#include "object-store.h"

View File

@ -8,6 +8,7 @@
#define USE_THE_INDEX_VARIABLE
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "parse-options.h"
#include "builtin.h"

View File

@ -4,6 +4,7 @@
* Copyright (c) Junio C Hamano, 2006, 2009
*/
#include "builtin.h"
#include "alloc.h"
#include "quote.h"
#include "tree.h"
#include "parse-options.h"

View File

@ -5,6 +5,7 @@
*/
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "alloc.h"
#include "config.h"
#include "pathspec.h"
#include "lockfile.h"

View File

@ -1,5 +1,5 @@
#include "builtin.h"
#include "cache.h"
#include "alloc.h"
#include "repository.h"
#include "config.h"
#include "commit.h"

View File

@ -1,5 +1,5 @@
#include "builtin.h"
#include "cache.h"
#include "alloc.h"
#include "repository.h"
#include "config.h"
#include "attr.h"

View File

@ -1,5 +1,5 @@
#include "builtin.h"
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "dir.h"
#include "parse-options.h"

View File

@ -5,6 +5,7 @@
*/
#define USE_THE_INDEX_VARIABLE
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "commit.h"
#include "refs.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "builtin.h"
#include "parse-options.h"

View File

@ -5,6 +5,7 @@
*/
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "alloc.h"
#include "advice.h"
#include "config.h"
#include "lockfile.h"

View File

@ -1,5 +1,6 @@
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "alloc.h"
#include "repository.h"
#include "cache.h"
#include "config.h"

View File

@ -1,7 +1,8 @@
/*
* Copyright (c) 2011, Google Inc.
*/
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "bulk-checkin.h"
#include "lockfile.h"
#include "repository.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "lockfile.h"
#include "tree.h"
#include "tree-walk.h"

75
cache.h
View File

@ -656,81 +656,6 @@ void initialize_repository_version(int hash_algo, int reinit);
void sanitize_stdfds(void);
int daemonize(void);
#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)
/* Initialize and use the cache information */
struct lock_file;
void preload_index(struct index_state *index,

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "chunk-format.h"
#include "csum-file.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "commit.h"
#include "commit-graph.h"
#include "decorate.h"

View File

@ -7,6 +7,7 @@
#include "../strbuf.h"
#include "../run-command.h"
#include "../cache.h"
#include "../alloc.h"
#include "win32/lazyload.h"
#include "../config.h"
#include "dir.h"

View File

@ -5,7 +5,8 @@
* Copyright (C) Johannes Schindelin, 2005
*
*/
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "date.h"
#include "branch.h"
#include "config.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "pkt-line.h"
#include "run-command.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "attr.h"
#include "object.h"
#include "blob.h"

1
diff.c
View File

@ -2,6 +2,7 @@
* Copyright (C) 2005 Junio C Hamano
*/
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "tempfile.h"
#include "quote.h"

View File

@ -3,6 +3,7 @@
* Copyright (C) 2005 Junio C Hamano
*/
#include "cache.h"
#include "alloc.h"
#include "diff.h"
#include "diffcore.h"
#include "object-store.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "dir.h"
#include "iterator.h"
#include "dir-iterator.h"

3
dir.c
View File

@ -5,7 +5,8 @@
* Copyright (C) Linus Torvalds, 2005-2006
* Junio Hamano, 2005-2006
*/
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "dir.h"
#include "object-store.h"

View File

@ -16,7 +16,8 @@
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "ewok.h"
#define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))

View File

@ -17,9 +17,9 @@
* 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"
#include "cache.h"
static inline size_t min_size(size_t a, size_t b)
{

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "repository.h"
#include "config.h"
#include "lockfile.h"

View File

@ -1,4 +1,5 @@
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "refs.h"
#include "object-store.h"

3
fsck.c
View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "object-store.h"
#include "repository.h"
#include "object.h"

View File

@ -1,5 +1,6 @@
#include "cache.h"
#include "git-compat-util.h"
#include "config.h"
#include "gettext.h"
#include "repository.h"
#include "fsmonitor-ipc.h"
#include "fsmonitor-settings.h"

3
help.c
View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "builtin.h"
#include "exec-cmd.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "repository.h"
#include "refs.h"

View File

@ -1,4 +1,5 @@
#include "git-compat-util.h"
#include "alloc.h"
#include "line-range.h"
#include "cache.h"
#include "tag.h"

View File

@ -1,6 +1,8 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "commit.h"
#include "config.h"
#include "gettext.h"
#include "revision.h"
#include "strvec.h"
#include "list-objects.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "alloc.h"
#include "dir.h"
#include "tag.h"
#include "commit.h"

3
midx.c
View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "csum-file.h"
#include "dir.h"

View File

@ -6,7 +6,8 @@
* This handles basic git object files - packing, unpacking,
* creation etc.
*/
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "string-list.h"
#include "lockfile.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "oid-array.h"
#include "hash-lookup.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "object-store.h"
#include "commit.h"
#include "tag.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "commit.h"
#include "strbuf.h"
#include "tag.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "object.h"
#include "pack.h"
#include "pack-objects.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "list.h"
#include "pack.h"
#include "repository.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "entry.h"
#include "parallel-checkout.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "commit.h"
#include "utf8.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "prio-queue.h"
static inline int compare(struct prio_queue *queue, int i, int j)

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "alloc.h"
#include "quote.h"
#include "strvec.h"

View File

@ -4,6 +4,7 @@
* Copyright (C) Linus Torvalds, 2005
*/
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "diff.h"
#include "diffcore.h"

View File

@ -1,5 +1,5 @@
#include "builtin.h"
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "parse-options.h"
#include "refs.h"
#include "wildmatch.h"
@ -13,7 +13,6 @@
#include "ref-filter.h"
#include "revision.h"
#include "utf8.h"
#include "git-compat-util.h"
#include "version.h"
#include "trailer.h"
#include "wt-status.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "commit.h"
#include "refs.h"
#include "diff.h"

3
refs.c
View File

@ -2,7 +2,8 @@
* The backend-independent part of the reference module.
*/
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "hashmap.h"
#include "lockfile.h"

View File

@ -1,4 +1,5 @@
#include "../cache.h"
#include "../git-compat-util.h"
#include "../alloc.h"
#include "../config.h"
#include "../refs.h"
#include "refs-internal.h"

View File

@ -1,4 +1,5 @@
#include "../cache.h"
#include "../git-compat-util.h"
#include "../alloc.h"
#include "../refs.h"
#include "refs-internal.h"
#include "ref-cache.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "strvec.h"
#include "refs.h"
#include "refspec.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "remote.h"
#include "connect.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "remote.h"
#include "urlmatch.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "lockfile.h"
#include "string-list.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "object-store.h"
#include "tag.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "lockfile.h"
#include "dir.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "dir.h"
#include "repository.h"
#include "refs.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "repository.h"
#include "tempfile.h"
#include "lockfile.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "sigchain.h"
#define SIGCHAIN_MAX_SIGNALS 32

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "alloc.h"
#include "repository.h"
#include "sparse-index.h"
#include "tree.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "alloc.h"
#include "split-index.h"
#include "ewah/ewok.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "refs.h"
#include "string-list.h"
#include "utf8.h"

View File

@ -1,5 +1,6 @@
#include "cache.h"
#include "git-compat-util.h"
#include "string-list.h"
#include "alloc.h"
void string_list_init_nodup(struct string_list *list)
{

View File

@ -1,5 +1,6 @@
#include "cache.h"
#include "git-compat-util.h"
#include "strvec.h"
#include "alloc.h"
#include "strbuf.h"
const char *empty_strvec[] = { NULL };

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "alloc.h"
#include "dir.h"
#include "repository.h"
#include "config.h"

View File

@ -1,5 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "repository.h"
#include "config.h"
#include "submodule-config.h"

View File

@ -1,5 +1,5 @@
#include "test-tool.h"
#include "cache.h"
#include "alloc.h"
#include "commit.h"
#include "commit-reach.h"
#include "config.h"

View File

@ -1,5 +1,7 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "thread-utils.h"
#include "trace.h"
#include "trace2/tr2_tls.h"
/*

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "string-list.h"
#include "run-command.h"

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "transport.h"
#include "hook.h"
@ -10,6 +11,7 @@
#include "walker.h"
#include "bundle.h"
#include "dir.h"
#include "gettext.h"
#include "refs.h"
#include "refspec.h"
#include "branch.h"

View File

@ -1,5 +1,6 @@
#include "cache.h"
#include "tree-walk.h"
#include "alloc.h"
#include "dir.h"
#include "object-store.h"
#include "tree.h"

View File

@ -1,7 +1,9 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "userdiff.h"
#include "attr.h"
#include "strbuf.h"
static struct userdiff_driver *drivers;
static int ndrivers;

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "repository.h"
#include "refs.h"
#include "strbuf.h"