Merge branch 'rs/realloc-array'
authorJunio C Hamano <gitster@pobox.com>
Fri, 26 Sep 2014 21:39:45 +0000 (14:39 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 26 Sep 2014 21:39:45 +0000 (14:39 -0700)
Code cleanup.

* rs/realloc-array:
use REALLOC_ARRAY for changing the allocation size of arrays
add macro REALLOC_ARRAY

1  2 
builtin/for-each-ref.c
builtin/index-pack.c
builtin/log.c
builtin/merge.c
cache.h
fast-import.c
git-compat-util.h
git.c
graph.c
object.c
diff --combined builtin/for-each-ref.c
index 69bba067189776d8411b1926084b53659799d0da,7f55e6816765f51ec60950060b3c8e5e484151a5..fda0f047125f16f080288c1cda76b0e337784a54
@@@ -138,10 -138,8 +138,8 @@@ static int parse_atom(const char *atom
        /* Add it in, including the deref prefix */
        at = used_atom_cnt;
        used_atom_cnt++;
-       used_atom = xrealloc(used_atom,
-                            (sizeof *used_atom) * used_atom_cnt);
-       used_atom_type = xrealloc(used_atom_type,
-                                 (sizeof(*used_atom_type) * used_atom_cnt));
+       REALLOC_ARRAY(used_atom, used_atom_cnt);
+       REALLOC_ARRAY(used_atom_type, used_atom_cnt);
        used_atom[at] = xmemdupz(atom, ep - atom);
        used_atom_type[at] = valid_atom[i].cmp_type;
        if (*atom == '*')
@@@ -633,7 -631,7 +631,7 @@@ static void populate_value(struct refin
        unsigned long size;
        const unsigned char *tagged;
  
 -      ref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt);
 +      ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
  
        if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
                unsigned char unused1[20];
@@@ -870,8 -868,7 +868,7 @@@ static int grab_single_ref(const char *
        ref->flag = flag;
  
        cnt = cb->grab_cnt;
-       cb->grab_array = xrealloc(cb->grab_array,
-                                 sizeof(*cb->grab_array) * (cnt + 1));
+       REALLOC_ARRAY(cb->grab_array, cnt + 1);
        cb->grab_array[cnt++] = ref;
        cb->grab_cnt = cnt;
        return 0;
diff --combined builtin/index-pack.c
index f89df017c200fbf7e2d5ec016d5f85975f1e7109,783623dbe2dd9c49cffdd62d38d9ec13da4d6464..792c66ca5932638e11f2587f928a6b98c08e7854
@@@ -112,10 -112,6 +112,10 @@@ static pthread_mutex_t deepest_delta_mu
  #define deepest_delta_lock()  lock_mutex(&deepest_delta_mutex)
  #define deepest_delta_unlock()        unlock_mutex(&deepest_delta_mutex)
  
 +static pthread_mutex_t type_cas_mutex;
 +#define type_cas_lock()               lock_mutex(&type_cas_mutex)
 +#define type_cas_unlock()     unlock_mutex(&type_cas_mutex)
 +
  static pthread_key_t key;
  
  static inline void lock_mutex(pthread_mutex_t *mutex)
@@@ -139,7 -135,6 +139,7 @@@ static void init_thread(void
        init_recursive_mutex(&read_mutex);
        pthread_mutex_init(&counter_mutex, NULL);
        pthread_mutex_init(&work_mutex, NULL);
 +      pthread_mutex_init(&type_cas_mutex, NULL);
        if (show_stat)
                pthread_mutex_init(&deepest_delta_mutex, NULL);
        pthread_key_create(&key, NULL);
@@@ -162,7 -157,6 +162,7 @@@ static void cleanup_thread(void
        pthread_mutex_destroy(&read_mutex);
        pthread_mutex_destroy(&counter_mutex);
        pthread_mutex_destroy(&work_mutex);
 +      pthread_mutex_destroy(&type_cas_mutex);
        if (show_stat)
                pthread_mutex_destroy(&deepest_delta_mutex);
        for (i = 0; i < nr_threads; i++)
@@@ -779,8 -773,7 +779,8 @@@ static void sha1_object(const void *dat
                        if (!obj)
                                die(_("invalid %s"), typename(type));
                        if (do_fsck_object &&
 -                          fsck_object(obj, 1, fsck_error_function))
 +                          fsck_object(obj, buf, size, 1,
 +                                  fsck_error_function))
                                die(_("Error in object"));
                        if (fsck_walk(obj, mark_link, NULL))
                                die(_("Not all child objects of %s are reachable"), sha1_to_hex(obj->sha1));
@@@ -869,6 -862,7 +869,6 @@@ static void resolve_delta(struct object
  {
        void *base_data, *delta_data;
  
 -      delta_obj->real_type = base->obj->real_type;
        if (show_stat) {
                delta_obj->delta_depth = base->obj->delta_depth + 1;
                deepest_delta_lock();
        counter_unlock();
  }
  
 +/*
 + * Standard boolean compare-and-swap: atomically check whether "*type" is
 + * "want"; if so, swap in "set" and return true. Otherwise, leave it untouched
 + * and return false.
 + */
 +static int compare_and_swap_type(enum object_type *type,
 +                               enum object_type want,
 +                               enum object_type set)
 +{
 +      enum object_type old;
 +
 +      type_cas_lock();
 +      old = *type;
 +      if (old == want)
 +              *type = set;
 +      type_cas_unlock();
 +
 +      return old == want;
 +}
 +
  static struct base_data *find_unresolved_deltas_1(struct base_data *base,
                                                  struct base_data *prev_base)
  {
                struct object_entry *child = objects + deltas[base->ref_first].obj_no;
                struct base_data *result = alloc_base_data();
  
 -              assert(child->real_type == OBJ_REF_DELTA);
 +              if (!compare_and_swap_type(&child->real_type, OBJ_REF_DELTA,
 +                                         base->obj->real_type))
 +                      die("BUG: child->real_type != OBJ_REF_DELTA");
 +
                resolve_delta(child, base, result);
                if (base->ref_first == base->ref_last && base->ofs_last == -1)
                        free_base_data(base);
                struct base_data *result = alloc_base_data();
  
                assert(child->real_type == OBJ_OFS_DELTA);
 +              child->real_type = base->obj->real_type;
                resolve_delta(child, base, result);
                if (base->ofs_first == base->ofs_last)
                        free_base_data(base);
@@@ -1170,9 -1140,7 +1170,7 @@@ static void conclude_pack(int fix_thin_
                int nr_objects_initial = nr_objects;
                if (nr_unresolved <= 0)
                        die(_("confusion beyond insanity"));
-               objects = xrealloc(objects,
-                                  (nr_objects + nr_unresolved + 1)
-                                  * sizeof(*objects));
+               REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1);
                memset(objects + nr_objects + 1, 0,
                       nr_unresolved * sizeof(*objects));
                f = sha1fd(output_fd, curr_pack);
diff --combined builtin/log.c
index 4ea71fee6819e4d94812e64cc20cbf1027655013,7643396aec901f944e9fafe094c0effc4a5906c9..2fb34c7de96a4236327d46bf1392d54fa882c17f
@@@ -78,7 -78,7 +78,7 @@@ static int decorate_callback(const stru
                decoration_style = DECORATE_SHORT_REFS;
  
        if (decoration_style < 0)
 -              die("invalid --decorate option: %s", arg);
 +              die(_("invalid --decorate option: %s"), arg);
  
        decoration_given = 1;
  
@@@ -130,7 -130,7 +130,7 @@@ static void cmd_log_init_finish(int arg
                { OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"),
                  PARSE_OPT_OPTARG, decorate_callback},
                OPT_CALLBACK('L', NULL, &line_cb, "n,m:file",
 -                           "Process line range n,m in file, counting from 1",
 +                           N_("Process line range n,m in file, counting from 1"),
                             log_line_range_callback),
                OPT_END()
        };
  
        /* Any arguments at this point are not recognized */
        if (argc > 1)
 -              die("unrecognized argument: %s", argv[1]);
 +              die(_("unrecognized argument: %s"), argv[1]);
  
        memset(&w, 0, sizeof(w));
        userformat_find_requirements(NULL, &w);
@@@ -447,13 -447,13 +447,13 @@@ static int show_blob_object(const unsig
                return stream_blob_to_fd(1, sha1, NULL, 0);
  
        if (get_sha1_with_context(obj_name, 0, sha1c, &obj_context))
 -              die("Not a valid object name %s", obj_name);
 +              die(_("Not a valid object name %s"), obj_name);
        if (!obj_context.path[0] ||
            !textconv_object(obj_context.path, obj_context.mode, sha1c, 1, &buf, &size))
                return stream_blob_to_fd(1, sha1, NULL, 0);
  
        if (!buf)
 -              die("git show %s: bad file", obj_name);
 +              die(_("git show %s: bad file"), obj_name);
  
        write_or_die(1, buf, size);
        return 0;
@@@ -1440,7 -1440,7 +1440,7 @@@ int cmd_format_patch(int argc, const ch
                        continue;
  
                nr++;
-               list = xrealloc(list, nr * sizeof(list[0]));
+               REALLOC_ARRAY(list, nr);
                list[nr - 1] = commit;
        }
        if (nr == 0)
diff --combined builtin/merge.c
index ec6fa9398faf2e5c61ebce198a5006cfb6dc8192,cb9af1e3dde874d4779f54821de5d6dc7959d7cb..dff043dac33ea88a8c5a05ea5ac3da71ca00c0d3
@@@ -556,7 -556,7 +556,7 @@@ static void parse_branch_merge_options(
        if (argc < 0)
                die(_("Bad branch.%s.mergeoptions string: %s"), branch,
                    split_cmdline_strerror(argc));
-       argv = xrealloc(argv, sizeof(*argv) * (argc + 2));
+       REALLOC_ARRAY(argv, argc + 2);
        memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
        argc++;
        argv[0] = "branch.*.mergeoptions";
@@@ -1143,14 -1143,14 +1143,14 @@@ int cmd_merge(int argc, const char **ar
                 */
                if (advice_resolve_conflict)
                        die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
 -                                "Please, commit your changes before you can merge."));
 +                                "Please, commit your changes before you merge."));
                else
                        die(_("You have not concluded your merge (MERGE_HEAD exists)."));
        }
        if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
                if (advice_resolve_conflict)
                        die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
 -                          "Please, commit your changes before you can merge."));
 +                          "Please, commit your changes before you merge."));
                else
                        die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
        }
diff --combined cache.h
index a9eb7902c97dc33cbeefcb7d5c1c3a98d6cd2a65,6cbef2ca79e97e17a804ab59e31c7be0de5e744e..8206039cfe9545b4f6eb6757dfd62bf468b3be52
+++ b/cache.h
@@@ -482,7 -482,7 +482,7 @@@ extern int daemonize(void)
                                alloc = (nr); \
                        else \
                                alloc = alloc_nr(alloc); \
-                       x = xrealloc((x), alloc * sizeof(*(x))); \
+                       REALLOC_ARRAY(x, alloc); \
                } \
        } while (0)
  
@@@ -1039,7 -1039,6 +1039,7 @@@ enum date_mode 
        DATE_SHORT,
        DATE_LOCAL,
        DATE_ISO8601,
 +      DATE_ISO8601_STRICT,
        DATE_RFC2822,
        DATE_RAW
  };
  const char *show_date(unsigned long time, int timezone, enum date_mode mode);
  void show_date_relative(unsigned long time, int tz, const struct timeval *now,
                        struct strbuf *timebuf);
 -int parse_date(const char *date, char *buf, int bufsize);
 +int parse_date(const char *date, struct strbuf *out);
  int parse_date_basic(const char *date, unsigned long *timestamp, int *offset);
  int parse_expiry_date(const char *date, unsigned long *timestamp);
 -void datestamp(char *buf, int bufsize);
 +void datestamp(struct strbuf *out);
  #define approxidate(s) approxidate_careful((s), NULL)
  unsigned long approxidate_careful(const char *, int *);
  unsigned long approxidate_relative(const char *date, const struct timeval *now);
@@@ -1285,8 -1284,6 +1285,8 @@@ extern int update_server_info(int)
  #define CONFIG_INVALID_PATTERN 6
  #define CONFIG_GENERIC_ERROR 7
  
 +#define CONFIG_REGEX_NONE ((void *)1)
 +
  struct git_config_source {
        unsigned int use_stdin:1;
        const char *file;
@@@ -1430,8 -1427,6 +1430,8 @@@ extern const char *git_mailmap_blob
  
  /* IO helper functions */
  extern void maybe_flush_or_die(FILE *, const char *);
 +__attribute__((format (printf, 2, 3)))
 +extern void fprintf_or_die(FILE *, const char *fmt, ...);
  extern int copy_fd(int ifd, int ofd);
  extern int copy_file(const char *dst, const char *src, int mode);
  extern int copy_file_with_time(const char *dst, const char *src, int mode);
diff --combined fast-import.c
index 487f1f81ac170acf87bae2801f6472c0768e3e24,07f65ec6926c9dd095bec36ce8e88039cfd7b5ec..96b0f4236a2083044e4ae1dfa3b8180e3e586ba1
@@@ -878,7 -878,7 +878,7 @@@ static void start_packfile(void
        pack_size = sizeof(hdr);
        object_count = 0;
  
-       all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
+       REALLOC_ARRAY(all_packs, pack_id + 1);
        all_packs[pack_id] = p;
  }
  
@@@ -1422,7 -1422,7 +1422,7 @@@ static void mktree(struct tree_content 
  
  static void store_tree(struct tree_entry *root)
  {
 -      struct tree_content *t = root->tree;
 +      struct tree_content *t;
        unsigned int i, j, del;
        struct last_object lo = { STRBUF_INIT, 0, 0, /* no_swap */ 1 };
        struct object_entry *le = NULL;
        if (!is_null_sha1(root->versions[1].sha1))
                return;
  
 +      if (!root->tree)
 +              load_tree(root);
 +      t = root->tree;
 +
        for (i = 0; i < t->entry_count; i++) {
                if (t->entries[i]->tree)
                        store_tree(t->entries[i]);
@@@ -2000,7 -1996,7 +2000,7 @@@ static int parse_data(struct strbuf *sb
        return 1;
  }
  
 -static int validate_raw_date(const char *src, char *result, int maxlen)
 +static int validate_raw_date(const char *src, struct strbuf *result)
  {
        const char *orig_src = src;
        char *endp;
                return -1;
  
        num = strtoul(src + 1, &endp, 10);
 -      if (errno || endp == src + 1 || *endp || (endp - orig_src) >= maxlen ||
 -          1400 < num)
 +      if (errno || endp == src + 1 || *endp || 1400 < num)
                return -1;
  
 -      strcpy(result, orig_src);
 +      strbuf_addstr(result, orig_src);
        return 0;
  }
  
@@@ -2029,7 -2026,7 +2029,7 @@@ static char *parse_ident(const char *bu
  {
        const char *ltgt;
        size_t name_len;
 -      char *ident;
 +      struct strbuf ident = STRBUF_INIT;
  
        /* ensure there is a space delimiter even if there is no name */
        if (*buf == '<')
                die("Missing space after > in ident string: %s", buf);
        ltgt++;
        name_len = ltgt - buf;
 -      ident = xmalloc(name_len + 24);
 -      strncpy(ident, buf, name_len);
 +      strbuf_add(&ident, buf, name_len);
  
        switch (whenspec) {
        case WHENSPEC_RAW:
 -              if (validate_raw_date(ltgt, ident + name_len, 24) < 0)
 +              if (validate_raw_date(ltgt, &ident) < 0)
                        die("Invalid raw date \"%s\" in ident: %s", ltgt, buf);
                break;
        case WHENSPEC_RFC2822:
 -              if (parse_date(ltgt, ident + name_len, 24) < 0)
 +              if (parse_date(ltgt, &ident) < 0)
                        die("Invalid rfc2822 date \"%s\" in ident: %s", ltgt, buf);
                break;
        case WHENSPEC_NOW:
                if (strcmp("now", ltgt))
                        die("Date in ident must be 'now': %s", buf);
 -              datestamp(ident + name_len, 24);
 +              datestamp(&ident);
                break;
        }
  
 -      return ident;
 +      return strbuf_detach(&ident, NULL);
  }
  
  static void parse_and_store_blob(
diff --combined git-compat-util.h
index 08a9ee2970aaef6f40fbb3c73f9c38ad176cf601,5a15b53d67b2fd0650f2bfe929f70dcd625c2cbd..0c4e663928cb8fb03c73a6d3de6a506c7d98fc9a
@@@ -82,7 -82,6 +82,7 @@@
  #define _ALL_SOURCE 1
  #define _GNU_SOURCE 1
  #define _BSD_SOURCE 1
 +#define _DEFAULT_SOURCE 1
  #define _NETBSD_SOURCE 1
  #define _SGI_SOURCE 1
  
@@@ -627,6 -626,8 +627,8 @@@ extern int odb_mkstemp(char *template, 
  extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
  extern char *xgetcwd(void);
  
+ #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x)))
  static inline size_t xsize_t(off_t len)
  {
        if (len > (size_t) len)
diff --combined git.c
index 523768da616c5884d2279acebc43cf57c98dd3e2,d5bb674814ffb4cbebd377a063afaeee4a29bd10..4076b014aba6c488e51a9bae8e5cd4b26e4e2476
--- 1/git.c
--- 2/git.c
+++ b/git.c
@@@ -14,7 -14,7 +14,7 @@@ const char git_usage_string[] 
        "           <command> [<args>]";
  
  const char git_more_info_string[] =
 -      N_("'git help -a' and 'git help -g' lists available subcommands and some\n"
 +      N_("'git help -a' and 'git help -g' list available subcommands and some\n"
           "concept guides. See 'git help <command>' or 'git help <concept>'\n"
           "to read about a specific subcommand or concept.");
  
@@@ -282,8 -282,7 +282,7 @@@ static int handle_alias(int *argcp, con
                                  "trace: alias expansion: %s =>",
                                  alias_command);
  
-               new_argv = xrealloc(new_argv, sizeof(char *) *
-                                   (count + *argcp));
+               REALLOC_ARRAY(new_argv, count + *argcp);
                /* insert after command name */
                memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
  
diff --combined graph.c
index dfb99f6436b41f5a567bc7c7a5785588bef15b41,288b9a6f582befee2c075682dc5d75063675c6a4..23fff73e18771c4163a93ac6b16d7b2021419e90
+++ b/graph.c
@@@ -267,16 -267,10 +267,10 @@@ static void graph_ensure_capacity(struc
                graph->column_capacity *= 2;
        } while (graph->column_capacity < num_columns);
  
-       graph->columns = xrealloc(graph->columns,
-                                 sizeof(struct column) *
-                                 graph->column_capacity);
-       graph->new_columns = xrealloc(graph->new_columns,
-                                     sizeof(struct column) *
-                                     graph->column_capacity);
-       graph->mapping = xrealloc(graph->mapping,
-                                 sizeof(int) * 2 * graph->column_capacity);
-       graph->new_mapping = xrealloc(graph->new_mapping,
-                                     sizeof(int) * 2 * graph->column_capacity);
+       REALLOC_ARRAY(graph->columns, graph->column_capacity);
+       REALLOC_ARRAY(graph->new_columns, graph->column_capacity);
+       REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2);
+       REALLOC_ARRAY(graph->new_mapping, graph->column_capacity * 2);
  }
  
  /*
@@@ -1145,7 -1139,7 +1139,7 @@@ int graph_next_line(struct git_graph *g
  
  static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
  {
 -      int i, j;
 +      int i;
  
        if (graph->state != GRAPH_COMMIT) {
                graph_next_line(graph, sb);
                                strbuf_addch(sb, ' ');
                        else {
                                int num_spaces = ((graph->num_parents - 2) * 2);
 -                              for (j = 0; j < num_spaces; j++)
 -                                      strbuf_addch(sb, ' ');
 +                              strbuf_addchars(sb, ' ', num_spaces);
                        }
                } else {
                        strbuf_write_column(sb, col, '|');
diff --combined object.c
index aedac243f0db05351a1ec7ae8b5ed297e51121e8,4e36669a71d70f106d1e5bdca6039fe5117c408c..ca9d790f4d2b0f30ff585df7a6f8edf515e40a20
+++ b/object.c
@@@ -33,20 -33,13 +33,20 @@@ const char *typename(unsigned int type
        return object_type_strings[type];
  }
  
 -int type_from_string(const char *str)
 +int type_from_string_gently(const char *str, ssize_t len, int gentle)
  {
        int i;
  
 +      if (len < 0)
 +              len = strlen(str);
 +
        for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
 -              if (!strcmp(str, object_type_strings[i]))
 +              if (!strncmp(str, object_type_strings[i], len))
                        return i;
 +
 +      if (gentle)
 +              return -1;
 +
        die("invalid object type \"%s\"", str);
  }
  
@@@ -319,7 -312,7 +319,7 @@@ static void add_object_array_with_mode_
  
        if (nr >= alloc) {
                alloc = (alloc + 32) * 2;
-               objects = xrealloc(objects, alloc * sizeof(*objects));
+               REALLOC_ARRAY(objects, alloc);
                array->alloc = alloc;
                array->objects = objects;
        }