Re: [PATCH 1/3] tree.c: update read_tree_recursive callback to pass strbuf as base

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Nov 8, 2014 at 6:00 AM, Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx> wrote:
> This allows the callback to use 'base' as a temporary buffer to
> quickly assemble full path "without" extra allocation. The caller has
> to restore it afterwards of course.
>
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx>
> ---
> diff --git a/merge-recursive.c b/merge-recursive.c
> index 1d332b8..1bd8c0d 100644
> --- a/merge-recursive.c
> +++ b/merge-recursive.c
> @@ -276,23 +276,20 @@ struct tree *write_tree_from_memory(struct merge_options *o)
>  }
>
>  static int save_files_dirs(const unsigned char *sha1,
> -               const char *base, int baselen, const char *path,
> +               struct strbuf *base, const char *path,
>                 unsigned int mode, int stage, void *context)
>  {
> -       int len = strlen(path);
> -       char *newpath = xmalloc(baselen + len + 1);
> +       int base_len = base->len;

Nit: The removed function argument was named 'baselen'. Also, patch
3/3 introduces a variable 'baseline' for the same purpose. Thus,
'base_len' here is a bit inconsistent.

>         struct merge_options *o = context;
>
> -       memcpy(newpath, base, baselen);
> -       memcpy(newpath + baselen, path, len);
> -       newpath[baselen + len] = '\0';
> +       strbuf_addstr(base, path);
>
>         if (S_ISDIR(mode))
> -               string_list_insert(&o->current_directory_set, newpath);
> +               string_list_insert(&o->current_directory_set, base->buf);
>         else
> -               string_list_insert(&o->current_file_set, newpath);
> -       free(newpath);
> +               string_list_insert(&o->current_file_set, base->buf);
>
> +       strbuf_setlen(base, base_len);
>         return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
>  }
>
> diff --git a/tree.c b/tree.c
> index bb02c1c..58ebfce 100644
> --- a/tree.c
> +++ b/tree.c
> @@ -30,9 +30,12 @@ static int read_one_entry_opt(const unsigned char *sha1, const char *base, int b
>         return add_cache_entry(ce, opt);
>  }
>
> -static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
> +static int read_one_entry(const unsigned char *sha1, struct strbuf *base,
> +                         const char *pathname, unsigned mode, int stage,
> +                         void *context)
>  {
> -       return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
> +       return read_one_entry_opt(sha1, base->buf, base->len, pathname,
> +                                 mode, stage,
>                                   ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
>  }
>
> @@ -40,9 +43,12 @@ static int read_one_entry(const unsigned char *sha1, const char *base, int basel
>   * This is used when the caller knows there is no existing entries at
>   * the stage that will conflict with the entry being added.
>   */
> -static int read_one_entry_quick(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
> +static int read_one_entry_quick(const unsigned char *sha1, struct strbuf *base,
> +                               const char *pathname, unsigned mode, int stage,
> +                               void *context)
>  {
> -       return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
> +       return read_one_entry_opt(sha1, base->buf, base->len, pathname,
> +                                 mode, stage,
>                                   ADD_CACHE_JUST_APPEND);
>  }
>
> @@ -70,7 +76,7 @@ static int read_tree_1(struct tree *tree, struct strbuf *base,
>                                 continue;
>                 }
>
> -               switch (fn(entry.sha1, base->buf, base->len,
> +               switch (fn(entry.sha1, base,
>                            entry.path, entry.mode, stage, context)) {
>                 case 0:
>                         continue;
> diff --git a/tree.h b/tree.h
> index d84ac63..d24125f 100644
> --- a/tree.h
> +++ b/tree.h
> @@ -4,6 +4,7 @@
>  #include "object.h"
>
>  extern const char *tree_type;
> +struct strbuf;
>
>  struct tree {
>         struct object object;
> @@ -22,7 +23,7 @@ void free_tree_buffer(struct tree *tree);
>  struct tree *parse_tree_indirect(const unsigned char *sha1);
>
>  #define READ_TREE_RECURSIVE 1
> -typedef int (*read_tree_fn_t)(const unsigned char *, const char *, int, const char *, unsigned int, int, void *);
> +typedef int (*read_tree_fn_t)(const unsigned char *, struct strbuf *, const char *, unsigned int, int, void *);
>
>  extern int read_tree_recursive(struct tree *tree,
>                                const char *base, int baselen,
> --
> 2.1.0.rc0.78.gc0d8480
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@xxxxxxxxxxxxxxx
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]