Merge branch 'rs/move-array'

Code clean-up.

* rs/move-array:
  ls-files: don't try to prune an empty index
  apply: use COPY_ARRAY and MOVE_ARRAY in update_image()
  use MOVE_ARRAY
  add MOVE_ARRAY
maint
Junio C Hamano 2017-08-11 13:26:57 -07:00
commit 32f90258bd
12 changed files with 47 additions and 34 deletions

11
apply.c
View File

@ -2809,13 +2809,10 @@ static void update_image(struct apply_state *state,
img->line_allocated = img->line; img->line_allocated = img->line;
} }
if (preimage_limit != postimage->nr) if (preimage_limit != postimage->nr)
memmove(img->line + applied_pos + postimage->nr, MOVE_ARRAY(img->line + applied_pos + postimage->nr,
img->line + applied_pos + preimage_limit, img->line + applied_pos + preimage_limit,
(img->nr - (applied_pos + preimage_limit)) * img->nr - (applied_pos + preimage_limit));
sizeof(*img->line)); COPY_ARRAY(img->line + applied_pos, postimage->line, postimage->nr);
memcpy(img->line + applied_pos,
postimage->line,
postimage->nr * sizeof(*img->line));
if (!state->allow_overlap) if (!state->allow_overlap)
for (i = 0; i < postimage->nr; i++) for (i = 0; i < postimage->nr; i++)
img->line[applied_pos + i].flag |= LINE_PATCHED; img->line[applied_pos + i].flag |= LINE_PATCHED;

View File

@ -362,7 +362,7 @@ static void prune_index(struct index_state *istate,
int pos; int pos;
unsigned int first, last; unsigned int first, last;


if (!prefix) if (!prefix || !istate->cache_nr)
return; return;
pos = index_name_pos(istate, prefix, prefixlen); pos = index_name_pos(istate, prefix, prefixlen);
if (pos < 0) if (pos < 0)
@ -378,8 +378,7 @@ static void prune_index(struct index_state *istate,
} }
last = next; last = next;
} }
memmove(istate->cache, istate->cache + pos, MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
(last - pos) * sizeof(struct cache_entry *));
istate->cache_nr = last - pos; istate->cache_nr = last - pos;
} }



View File

@ -537,7 +537,7 @@ static void parse_branch_merge_options(char *bmo)
die(_("Bad branch.%s.mergeoptions string: %s"), branch, die(_("Bad branch.%s.mergeoptions string: %s"), branch,
split_cmdline_strerror(argc)); split_cmdline_strerror(argc));
REALLOC_ARRAY(argv, argc + 2); REALLOC_ARRAY(argv, argc + 2);
memmove(argv + 1, argv, sizeof(*argv) * (argc + 1)); MOVE_ARRAY(argv + 1, argv, argc + 1);
argc++; argc++;
argv[0] = "branch.*.mergeoptions"; argv[0] = "branch.*.mergeoptions";
parse_options(argc, argv, NULL, builtin_merge_options, parse_options(argc, argv, NULL, builtin_merge_options,

View File

@ -1298,9 +1298,8 @@ static int check_pbase_path(unsigned hash)
done_pbase_paths_alloc); done_pbase_paths_alloc);
done_pbase_paths_num++; done_pbase_paths_num++;
if (pos < done_pbase_paths_num) if (pos < done_pbase_paths_num)
memmove(done_pbase_paths + pos + 1, MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,
done_pbase_paths + pos, done_pbase_paths_num - pos - 1);
(done_pbase_paths_num - pos - 1) * sizeof(unsigned));
done_pbase_paths[pos] = hash; done_pbase_paths[pos] = hash;
return 0; return 0;
} }

View File

@ -131,9 +131,8 @@ static int do_invalidate_path(struct cache_tree *it, const char *path)
* move 4 and 5 up one place (2 entries) * move 4 and 5 up one place (2 entries)
* 2 = 6 - 3 - 1 = subtree_nr - pos - 1 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
*/ */
memmove(it->down+pos, it->down+pos+1, MOVE_ARRAY(it->down + pos, it->down + pos + 1,
sizeof(struct cache_tree_sub *) * it->subtree_nr - pos - 1);
(it->subtree_nr - pos - 1));
it->subtree_nr--; it->subtree_nr--;
} }
return 1; return 1;

View File

@ -223,9 +223,8 @@ int unregister_shallow(const struct object_id *oid)
if (pos < 0) if (pos < 0)
return -1; return -1;
if (pos + 1 < commit_graft_nr) if (pos + 1 < commit_graft_nr)
memmove(commit_graft + pos, commit_graft + pos + 1, MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1,
sizeof(struct commit_graft *) commit_graft_nr - pos - 1);
* (commit_graft_nr - pos - 1));
commit_graft_nr--; commit_graft_nr--;
return 0; return 0;
} }

View File

@ -25,6 +25,23 @@ expression n;
- memcpy(dst, src, n * sizeof(T)); - memcpy(dst, src, n * sizeof(T));
+ COPY_ARRAY(dst, src, n); + COPY_ARRAY(dst, src, n);


@@
type T;
T *dst;
T *src;
expression n;
@@
(
- memmove(dst, src, (n) * sizeof(*dst));
+ MOVE_ARRAY(dst, src, n);
|
- memmove(dst, src, (n) * sizeof(*src));
+ MOVE_ARRAY(dst, src, n);
|
- memmove(dst, src, (n) * sizeof(T));
+ MOVE_ARRAY(dst, src, n);
)

@@ @@
type T; type T;
T *ptr; T *ptr;

View File

@ -828,6 +828,14 @@ static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
memcpy(dst, src, st_mult(size, n)); memcpy(dst, src, st_mult(size, n));
} }


#define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \
BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src))))
static inline void move_array(void *dst, const void *src, size_t n, size_t size)
{
if (n)
memmove(dst, src, st_mult(size, n));
}

/* /*
* These functions help you allocate structs with flex arrays, and copy * These functions help you allocate structs with flex arrays, and copy
* the data directly into the array. For example, if you had: * the data directly into the array. For example, if you had:

View File

@ -99,8 +99,7 @@ static struct notes_merge_pair *find_notes_merge_pair_pos(
else { else {
*occupied = 0; *occupied = 0;
if (insert_new && i < len) { if (insert_new && i < len) {
memmove(list + i + 1, list + i, MOVE_ARRAY(list + i + 1, list + i, len - i);
(len - i) * sizeof(struct notes_merge_pair));
memset(list + i, 0, sizeof(struct notes_merge_pair)); memset(list + i, 0, sizeof(struct notes_merge_pair));
} }
} }

View File

@ -515,9 +515,8 @@ int remove_index_entry_at(struct index_state *istate, int pos)
istate->cache_nr--; istate->cache_nr--;
if (pos >= istate->cache_nr) if (pos >= istate->cache_nr)
return 0; return 0;
memmove(istate->cache + pos, MOVE_ARRAY(istate->cache + pos, istate->cache + pos + 1,
istate->cache + pos + 1, istate->cache_nr - pos);
(istate->cache_nr - pos) * sizeof(struct cache_entry *));
return 1; return 1;
} }



View File

@ -111,10 +111,9 @@ static struct commit_info *get_commit_info(struct commit *commit,
struct commit_info *result = &lifo->items[i]; struct commit_info *result = &lifo->items[i];
if (pop) { if (pop) {
if (i + 1 < lifo->nr) if (i + 1 < lifo->nr)
memmove(lifo->items + i, MOVE_ARRAY(lifo->items + i,
lifo->items + i + 1, lifo->items + i + 1,
(lifo->nr - i) * lifo->nr - i);
sizeof(struct commit_info));
lifo->nr--; lifo->nr--;
} }
return result; return result;

View File

@ -43,9 +43,8 @@ static int add_entry(int insert_at, struct string_list *list, const char *string


ALLOC_GROW(list->items, list->nr+1, list->alloc); ALLOC_GROW(list->items, list->nr+1, list->alloc);
if (index < list->nr) if (index < list->nr)
memmove(list->items + index + 1, list->items + index, MOVE_ARRAY(list->items + index + 1, list->items + index,
(list->nr - index) list->nr - index);
* sizeof(struct string_list_item));
list->items[index].string = list->strdup_strings ? list->items[index].string = list->strdup_strings ?
xstrdup(string) : (char *)string; xstrdup(string) : (char *)string;
list->items[index].util = NULL; list->items[index].util = NULL;
@ -77,8 +76,7 @@ void string_list_remove(struct string_list *list, const char *string,
free(list->items[i].util); free(list->items[i].util);


list->nr--; list->nr--;
memmove(list->items + i, list->items + i + 1, MOVE_ARRAY(list->items + i, list->items + i + 1, list->nr - i);
(list->nr - i) * sizeof(struct string_list_item));
} }
} }