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

Re: [PATCH 3/3] log-tree: use FLEX_ARRAY in name_decoration

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Aug 27, 2014, 00:30 UTC
Message-ID
<CAPig+cTHLOAe+mHr9jiR-k-3yrXfrAEjUSrHG=92kRSEGyto8Q@mail.gmail.com>
In-Reply-To
<20140826102420.GC25687@peff.net>
On Tue, Aug 26, 2014 at 6:24 AM, Jeff King <peff@peff.net> wrote:
Show 8 quoted lines
> We are already using the flex-array technique; let's
> annotate it with our usual FLEX_ARRAY macro. Besides being
> more readable, this is slightly more efficient on compilers
> that understand flex-arrays.
>
> Note that we need to bump the allocation in add_name_decoration,
> which did not explicitly add one byte for the NUL terminator
> of the string we putting into the flex-array (it did not
s/we/we are/
Show 40 quoted lines
> need to before, because the struct itself was over-allocated
> by one byte).
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---
> This could come first in the series, but doing it last means we only
> have to update one spot. :)
>
>  commit.h   | 2 +-
>  log-tree.c | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/commit.h b/commit.h
> index 263b49e..1516fc9 100644
> --- a/commit.h
> +++ b/commit.h
> @@ -29,7 +29,7 @@ extern const char *commit_type;
>  struct name_decoration {
>         struct name_decoration *next;
>         int type;
> -       char name[1];
> +       char name[FLEX_ARRAY];
>  };
>
>  enum decoration_type {
> diff --git a/log-tree.c b/log-tree.c
> index 7cbc4ee..fb60018 100644
> --- a/log-tree.c
> +++ b/log-tree.c
> @@ -77,7 +77,7 @@ int parse_decorate_color_config(const char *var, const int ofs, const char *valu
>  void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
>  {
>         int nlen = strlen(name);
> -       struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);
> +       struct name_decoration *res = xmalloc(sizeof(*res) + nlen + 1);
>         memcpy(res->name, name, nlen + 1);
>         res->type = type;
>         res->next = add_decoration(&name_decoration, obj, res);
> --
> 2.1.0.346.ga0367b9
Previous: Jeff KingNext: Junio C Hamano
Message 30 of 32 in “bisect: save heap memory. allocate only the required amount”
  1. bisect: save heap memory. allocate only the required amountArjun Sreedharan, Aug 24, 2014
  2. Stefan BellerAug 24, 2014
  3. Junio C HamanoAug 24, 2014
  4. Jeff KingAug 25, 2014
  5. Junio C HamanoAug 25, 2014
  6. Jeff KingAug 26, 2014
  7. Ramsay JonesAug 26, 2014
  8. Jeff KingAug 26, 2014
  9. Ramsay JonesAug 26, 2014
  10. Jeff KingAug 26, 2014
  11. Ramsay JonesAug 26, 2014
  12. Ramsay JonesAug 24, 2014
  13. bisect: save heap memory. allocate only the required amountArjun Sreedharan, Aug 24, 2014
  14. Jeff KingAug 25, 2014
  15. Christian CouderAug 25, 2014
  16. Jeff KingAug 25, 2014
  17. Junio C HamanoAug 25, 2014
  18. Jeff KingAug 25, 2014
  19. Arjun SreedharanAug 25, 2014
  20. Junio C HamanoAug 25, 2014
  21. 0/3 name_decoration cleanupsJeff King, Aug 26, 2014
  22. 1/3 log-tree: make add_name_decoration a public functionJeff King, Aug 26, 2014
  23. Ramsay JonesAug 26, 2014
  24. Jeff KingAug 26, 2014
  25. 2/3 log-tree: make name_decoration hash staticJeff King, Aug 26, 2014
  26. Junio C HamanoAug 26, 2014
  27. Jeff KingAug 26, 2014
  28. Junio C HamanoAug 26, 2014
  29. 3/3 log-tree: use FLEX_ARRAY in name_decorationJeff King, Aug 26, 2014
  30. Eric SunshineAug 27, 2014
  31. Junio C HamanoAug 25, 2014
  32. Arjun SreedharanAug 26, 2014

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.