From: Junio C Hamano <gitster@pobox.com>
To: "René Scharfe" <l.s.r@web.de>
Cc: Git Mailing List <git@vger.kernel.org>
Subject: Re: [PATCH 1/2] add macro REALLOCARRAY
Date: Mon, 15 Sep 2014 11:24:04 -0700 [thread overview]
Message-ID: <xmqqtx487nej.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <5415C89C.4090509@web.de> ("René Scharfe"'s message of "Sun, 14 Sep 2014 18:55:56 +0200")
René Scharfe <l.s.r@web.de> writes:
> The macro ALLOC_GROW manages several aspects of dynamic memory
> allocations for arrays: It performs overprovisioning in order to avoid
> reallocations in future calls, updates the allocation size variable,
> multiplies the item size and thus allows users to simply specify the
> item count, performs the reallocation and updates the array pointer.
>
> Sometimes this is too much. Add the macro REALLOCARRAY, which only
> takes care of the latter three points and allows users to specify the
> number of items an array can store directly. It can increase and
> also decrease its size. Using this macro avoids duplicating the
> array pointer name and takes care of item sizes automatically.
>
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
Makes sense. Originally I had two minor gripes against this
#1 a macro that modifies its arguments feels a bit too magical, and
we would not want to overuse them.
#2 REALLOC_ARRAY(array, size) would have been easier to read.
But #1 is shared with ALLOC_GROW(), and as long as we use it
everywhere (and by the looks of 2/2 we fairly widely do), readers
will get used to the pattern. #2 still stands, though.
Thanks.
> Documentation/technical/api-allocation-growing.txt | 3 +++
> git-compat-util.h | 2 ++
> 2 files changed, 5 insertions(+)
>
> diff --git a/Documentation/technical/api-allocation-growing.txt b/Documentation/technical/api-allocation-growing.txt
> index 542946b..4b5f049 100644
> --- a/Documentation/technical/api-allocation-growing.txt
> +++ b/Documentation/technical/api-allocation-growing.txt
> @@ -34,3 +34,6 @@ 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 `REALLOCARRAY(item, alloc)` instead of `ALLOC_GROW`.
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 4e7e3f8..d926e4c 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -626,6 +626,8 @@ extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
> extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
> extern char *xgetcwd(void);
>
> +#define REALLOCARRAY(x, alloc) x = xrealloc((x), (alloc) * sizeof(*(x)))
> +
> static inline size_t xsize_t(off_t len)
> {
> if (len > (size_t) len)
next prev parent reply other threads:[~2014-09-15 18:24 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-14 16:55 [PATCH 1/2] add macro REALLOCARRAY René Scharfe
2014-09-14 16:57 ` [PATCH 2/2] use REALLOCARRAY for changing the allocation size of arrays René Scharfe
2014-09-15 18:24 ` Junio C Hamano [this message]
2014-09-17 8:17 ` [PATCH 1/2] add macro REALLOCARRAY Jeff King
2014-09-16 3:04 ` Junio C Hamano
2014-09-16 18:52 ` René Scharfe
2014-09-16 18:56 ` [PATCH 1/2] add macro REALLOC_ARRAY René Scharfe
2014-09-24 7:32 ` Michael Haggerty
2014-09-24 17:59 ` Junio C Hamano
2014-09-24 19:27 ` René Scharfe
2014-09-16 18:56 ` [PATCH 2/2] use REALLOC_ARRAY for changing the allocation size of arrays René Scharfe
2014-09-24 18:47 ` Jonathan Nieder
2014-09-24 19:27 ` René Scharfe
2014-09-16 19:27 ` [PATCH 1/2] add macro REALLOCARRAY Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqtx487nej.fsf@gitster.dls.corp.google.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=l.s.r@web.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.