From: Junio C Hamano Date: Fri, 26 Sep 2014 21:39:45 +0000 (-0700) Subject: Merge branch 'rs/realloc-array' X-Git-Tag: v2.2.0-rc0~83 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/1c2ea2cdc0e8c4e5af942c51b234c5af527944f6?hp=-c Merge branch 'rs/realloc-array' Code cleanup. * rs/realloc-array: use REALLOC_ARRAY for changing the allocation size of arrays add macro REALLOC_ARRAY --- 1c2ea2cdc0e8c4e5af942c51b234c5af527944f6 diff --combined builtin/for-each-ref.c index 69bba06718,7f55e68167..fda0f04712 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@@ -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 f89df017c2,783623dbe2..792c66ca59 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@@ -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(); @@@ -894,26 -888,6 +894,26 @@@ 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) { @@@ -941,10 -915,7 +941,10 @@@ 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); @@@ -958,7 -929,6 +958,7 @@@ 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 4ea71fee68,7643396aec..2fb34c7de9 --- a/builtin/log.c +++ b/builtin/log.c @@@ -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() }; @@@ -150,7 -150,7 +150,7 @@@ /* 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 ec6fa9398f,cb9af1e3dd..dff043dac3 --- a/builtin/merge.c +++ b/builtin/merge.c @@@ -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 a9eb7902c9,6cbef2ca79..8206039cfe --- a/cache.h +++ 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 }; @@@ -1047,10 -1046,10 +1047,10 @@@ 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 487f1f81ac,07f65ec692..96b0f4236a --- a/fast-import.c +++ b/fast-import.c @@@ -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; @@@ -1430,10 -1430,6 +1430,10 @@@ 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; @@@ -2018,10 -2014,11 +2018,10 @@@ 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 == '<') @@@ -2048,25 -2045,26 +2048,25 @@@ 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 08a9ee2970,5a15b53d67..0c4e663928 --- a/git-compat-util.h +++ b/git-compat-util.h @@@ -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 523768da61,d5bb674814..4076b014ab --- a/git.c +++ b/git.c @@@ -14,7 -14,7 +14,7 @@@ const char git_usage_string[] " []"; 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 ' or 'git help '\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 dfb99f6436,288b9a6f58..23fff73e18 --- a/graph.c +++ 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); @@@ -1169,7 -1163,8 +1163,7 @@@ 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 aedac243f0,4e36669a71..ca9d790f4d --- a/object.c +++ 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; }