git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] git-compat-util.h: implement a different ARRAY_SIZE macro for for safely deriving the size of array

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 27, 2015, 17:56 UTC
Message-ID
<xmqq1tj5xxo1.fsf@gitster.dls.corp.google.com>
In-Reply-To
<1430140269-11784-1-git-send-email-gitter.spiros@gmail.com>
Elia Pinto <gitter.spiros@gmail.com> writes:
> This is the second version of this patch.  It had not been
> discussed before. In the second version, I just tried to clarify
> the comment in the commit. I resend it just in case you missed

I do not recall seeing it before. No discussion usually means no interest, so I'll see what happens this time on the list for a few days before picking it up.

Show 8 quoted lines
> +#if SUPPORT__BUILTIN_TYPES_COMPATIBLE_P
> +/* &arr[0] degrades to a pointer: a different type from an array */
> +#define _array_size_chk(arr)						\
> +	BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(arr), \
> +							typeof(&(arr)[0])))
> +#else
> +#define _array_size_chk(arr) 0
> +#endif

Wouldn't there be a more sensible name? _chk does not tell us anything about what is being checked, and the only thing this name gives us is "what uses it" (i.e. it is some magic used by array-size and does not say what it checks and what for).

I think you are checking arr is an array and not a pointer. Perhaps "#define is_an_array(arr)" or something along that line may be a more descriptive name for it.

I doubt the leading underscore is particularly a good idea, though.
Show 12 quoted lines
> +/*
> + * ARRAY_SIZE - get the number of elements in a visible array
> + *  <at> x: the array whose size you want.
> + *
> + * This does not work on pointers, or arrays declared as [], or
> + * function parameters.  With correct compiler support, such usage
> + * will cause a build error (see build_assert).
> + */
> +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + _array_size_chk(x))
>  #define bitsizeof(x)  (CHAR_BIT * sizeof(x))
>  
>  #define maximum_signed_value_of_type(a) \
Previous: Elia PintoNext: Junio C Hamano
Message 2 of 4 in “git-compat-util.h: implement a different ARRAY_SIZE macro for for safely deriving the size of array”
  1. git-compat-util.h: implement a different ARRAY_SIZE macro for for safely deriving the size of arrayElia Pinto, Apr 27, 2015
  2. Junio C HamanoApr 27, 2015
  3. Junio C HamanoApr 27, 2015
  4. Jeff KingApr 28, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.