Skip to content

Commit

Permalink
Convert memcpy(a,b,20) to hashcpy(a,b).
Browse files Browse the repository at this point in the history
This abstracts away the size of the hash values when copying them
from memory location to memory location, much as the introduction
of hashcmp abstracted away hash value comparsion.

A few call sites were using char* rather than unsigned char* so
I added the cast rather than open hashcpy to be void*.  This is a
reasonable tradeoff as most call sites already use unsigned char*
and the existing hashcmp is also declared to be unsigned char*.

[jc: Splitted the patch to "master" part, to be followed by a
 patch for merge-recursive.c which is not in "master" yet.

 Fixed the cast in the latter hunk to combine-diff.c which was
 wrong in the original.

 Also converted ones left-over in combine-diff.c, diff-lib.c and
 upload-pack.c ]

Signed-off-by: Shawn O. Pearce <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
spearce authored and Junio C Hamano committed Aug 23, 2006
1 parent b05faa2 commit e702496
Show file tree
Hide file tree
Showing 34 changed files with 81 additions and 78 deletions.
4 changes: 2 additions & 2 deletions blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ static int get_blob_sha1(struct tree *t, const char *pathname,
if (i == 20)
return -1;

memcpy(sha1, blob_sha1, 20);
hashcpy(sha1, blob_sha1);
return 0;
}

Expand All @@ -191,7 +191,7 @@ static int get_blob_sha1_internal(const unsigned char *sha1, const char *base,
strcmp(blame_file + baselen, pathname))
return -1;

memcpy(blob_sha1, sha1, 20);
hashcpy(blob_sha1, sha1);
return -1;
}

Expand Down
4 changes: 2 additions & 2 deletions builtin-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ static int builtin_diff_combined(struct rev_info *revs,
parent = xmalloc(ents * sizeof(*parent));
/* Again, the revs are all reverse */
for (i = 0; i < ents; i++)
memcpy(parent + i, ent[ents - 1 - i].item->sha1, 20);
hashcpy((unsigned char*)parent + i, ent[ents - 1 - i].item->sha1);
diff_tree_combined(parent[0], parent + 1, ents - 1,
revs->dense_combined_merges, revs);
return 0;
Expand Down Expand Up @@ -290,7 +290,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
if (obj->type == OBJ_BLOB) {
if (2 <= blobs)
die("more than two blobs given: '%s'", name);
memcpy(blob[blobs].sha1, obj->sha1, 20);
hashcpy(blob[blobs].sha1, obj->sha1);
blob[blobs].name = name;
blobs++;
continue;
Expand Down
6 changes: 3 additions & 3 deletions builtin-pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclud
entry = objects + idx;
nr_objects = idx + 1;
memset(entry, 0, sizeof(*entry));
memcpy(entry->sha1, sha1, 20);
hashcpy(entry->sha1, sha1);
entry->hash = hash;

if (object_ix_hashsz * 3 <= nr_objects * 4)
Expand Down Expand Up @@ -649,7 +649,7 @@ static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
free(ent->tree_data);
nent = ent;
}
memcpy(nent->sha1, sha1, 20);
hashcpy(nent->sha1, sha1);
nent->tree_data = data;
nent->tree_size = size;
nent->ref = 1;
Expand Down Expand Up @@ -799,7 +799,7 @@ static void add_preferred_base(unsigned char *sha1)
it->next = pbase_tree;
pbase_tree = it;

memcpy(it->pcache.sha1, tree_sha1, 20);
hashcpy(it->pcache.sha1, tree_sha1);
it->pcache.tree_data = data;
it->pcache.tree_size = size;
}
Expand Down
2 changes: 1 addition & 1 deletion builtin-read-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
struct name_entry entry;
int cnt;

memcpy(it->sha1, tree->object.sha1, 20);
hashcpy(it->sha1, tree->object.sha1);
desc.buf = tree->buffer;
desc.size = tree->size;
cnt = 0;
Expand Down
4 changes: 2 additions & 2 deletions builtin-unpack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned lo
{
struct delta_info *info = xmalloc(sizeof(*info));

memcpy(info->base_sha1, base_sha1, 20);
hashcpy(info->base_sha1, base_sha1);
info->size = size;
info->delta = delta;
info->next = delta_list;
Expand Down Expand Up @@ -173,7 +173,7 @@ static int unpack_delta_entry(unsigned long delta_size)
unsigned char base_sha1[20];
int result;

memcpy(base_sha1, fill(20), 20);
hashcpy(base_sha1, fill(20));
use(20);

delta_data = get_data(delta_size);
Expand Down
4 changes: 2 additions & 2 deletions builtin-update-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
size = cache_entry_size(len);
ce = xcalloc(1, size);

memcpy(ce->sha1, sha1, 20);
hashcpy(ce->sha1, sha1);
memcpy(ce->name, path, len);
ce->ce_flags = create_ce_flags(len, stage);
ce->ce_mode = create_ce_mode(mode);
Expand Down Expand Up @@ -333,7 +333,7 @@ static struct cache_entry *read_one_ent(const char *which,
size = cache_entry_size(namelen);
ce = xcalloc(1, size);

memcpy(ce->sha1, sha1, 20);
hashcpy(ce->sha1, sha1);
memcpy(ce->name, path, namelen);
ce->ce_flags = create_ce_flags(namelen, stage);
ce->ce_mode = create_ce_mode(mode);
Expand Down
4 changes: 2 additions & 2 deletions builtin-write-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ int write_tree(unsigned char *sha1, int missing_ok, const char *prefix)
if (prefix) {
struct cache_tree *subtree =
cache_tree_find(active_cache_tree, prefix);
memcpy(sha1, subtree->sha1, 20);
hashcpy(sha1, subtree->sha1);
}
else
memcpy(sha1, active_cache_tree->sha1, 20);
hashcpy(sha1, active_cache_tree->sha1);

rollback_lock_file(lock_file);

Expand Down
6 changes: 3 additions & 3 deletions cache-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ static int update_one(struct cache_tree *it,
offset += sprintf(buffer + offset,
"%o %.*s", mode, entlen, path + baselen);
buffer[offset++] = 0;
memcpy(buffer + offset, sha1, 20);
hashcpy((unsigned char*)buffer + offset, sha1);
offset += 20;

#if DEBUG
Expand Down Expand Up @@ -412,7 +412,7 @@ static void *write_one(struct cache_tree *it,
#endif

if (0 <= it->entry_count) {
memcpy(buffer + *offset, it->sha1, 20);
hashcpy((unsigned char*)buffer + *offset, it->sha1);
*offset += 20;
}
for (i = 0; i < it->subtree_nr; i++) {
Expand Down Expand Up @@ -478,7 +478,7 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
if (0 <= it->entry_count) {
if (size < 20)
goto free_return;
memcpy(it->sha1, buf, 20);
hashcpy(it->sha1, (unsigned char*)buf);
buf += 20;
size -= 20;
}
Expand Down
4 changes: 4 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
{
return memcmp(sha1, sha2, 20);
}
static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
{
memcpy(sha_dst, sha_src, 20);
}

int git_mkstemp(char *path, size_t n, const char *template);

Expand Down
10 changes: 5 additions & 5 deletions combine-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr,
memset(p->parent, 0,
sizeof(p->parent[0]) * num_parent);

memcpy(p->sha1, q->queue[i]->two->sha1, 20);
hashcpy(p->sha1, q->queue[i]->two->sha1);
p->mode = q->queue[i]->two->mode;
memcpy(p->parent[n].sha1, q->queue[i]->one->sha1, 20);
hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
p->parent[n].mode = q->queue[i]->one->mode;
p->parent[n].status = q->queue[i]->status;
*tail = p;
Expand All @@ -56,8 +56,7 @@ static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr,
len = strlen(path);
if (len == p->len && !memcmp(path, p->path, len)) {
found = 1;
memcpy(p->parent[n].sha1,
q->queue[i]->one->sha1, 20);
hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
p->parent[n].mode = q->queue[i]->one->mode;
p->parent[n].status = q->queue[i]->status;
break;
Expand Down Expand Up @@ -927,6 +926,7 @@ void diff_tree_combined_merge(const unsigned char *sha1,
for (parents = commit->parents, num_parent = 0;
parents;
parents = parents->next, num_parent++)
memcpy(parent + num_parent, parents->item->object.sha1, 20);
hashcpy((unsigned char*)(parent + num_parent),
parents->item->object.sha1);
diff_tree_combined(sha1, parent, num_parent, dense, rev);
}
6 changes: 3 additions & 3 deletions connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct ref **get_remote_heads(int in, struct ref **list,
if (nr_match && !path_match(name, nr_match, match))
continue;
ref = xcalloc(1, sizeof(*ref) + len - 40);
memcpy(ref->old_sha1, old_sha1, 20);
hashcpy(ref->old_sha1, old_sha1);
memcpy(ref->name, buffer + 41, len - 40);
*list = ref;
list = &ref->next;
Expand Down Expand Up @@ -208,7 +208,7 @@ static struct ref *try_explicit_object_name(const char *name)
len = strlen(name) + 1;
ref = xcalloc(1, sizeof(*ref) + len);
memcpy(ref->name, name, len);
memcpy(ref->new_sha1, sha1, 20);
hashcpy(ref->new_sha1, sha1);
return ref;
}

Expand Down Expand Up @@ -318,7 +318,7 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
int len = strlen(src->name) + 1;
dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
memcpy(dst_peer->name, src->name, len);
memcpy(dst_peer->new_sha1, src->new_sha1, 20);
hashcpy(dst_peer->new_sha1, src->new_sha1);
link_dst_tail(dst_peer, dst_tail);
}
dst_peer->peer_ref = src;
Expand Down
6 changes: 3 additions & 3 deletions convert-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static struct entry * convert_entry(unsigned char *sha1);
static struct entry *insert_new(unsigned char *sha1, int pos)
{
struct entry *new = xcalloc(1, sizeof(struct entry));
memcpy(new->old_sha1, sha1, 20);
hashcpy(new->old_sha1, sha1);
memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));
convert[pos] = new;
nr_convert++;
Expand Down Expand Up @@ -54,7 +54,7 @@ static struct entry *lookup_entry(unsigned char *sha1)
static void convert_binary_sha1(void *buffer)
{
struct entry *entry = convert_entry(buffer);
memcpy(buffer, entry->new_sha1, 20);
hashcpy(buffer, entry->new_sha1);
}

static void convert_ascii_sha1(void *buffer)
Expand Down Expand Up @@ -104,7 +104,7 @@ static int write_subdirectory(void *buffer, unsigned long size, const char *base
if (!slash) {
newlen += sprintf(new + newlen, "%o %s", mode, path);
new[newlen++] = '\0';
memcpy(new + newlen, (char *) buffer + len - 20, 20);
hashcpy((unsigned char*)new + newlen, (unsigned char *) buffer + len - 20);
newlen += 20;

used += len;
Expand Down
2 changes: 1 addition & 1 deletion csum-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int sha1close(struct sha1file *f, unsigned char *result, int update)
}
SHA1_Final(f->buffer, &f->ctx);
if (result)
memcpy(result, f->buffer, 20);
hashcpy(result, f->buffer);
if (update)
sha1flush(f, 20);
if (close(f->fd))
Expand Down
3 changes: 1 addition & 2 deletions diff-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ int run_diff_files(struct rev_info *revs, int silent_on_removed)
if (2 <= stage) {
int mode = ntohl(nce->ce_mode);
num_compare_stages++;
memcpy(dpath->parent[stage-2].sha1,
nce->sha1, 20);
hashcpy(dpath->parent[stage-2].sha1, nce->sha1);
dpath->parent[stage-2].mode =
canon_mode(mode);
dpath->parent[stage-2].status =
Expand Down
6 changes: 3 additions & 3 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
{
if (mode) {
spec->mode = canon_mode(mode);
memcpy(spec->sha1, sha1, 20);
hashcpy(spec->sha1, sha1);
spec->sha1_valid = !is_null_sha1(sha1);
}
}
Expand Down Expand Up @@ -1194,7 +1194,7 @@ static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
sizeof(*sha1_size_cache));
e = xmalloc(sizeof(struct sha1_size_cache));
sha1_size_cache[first] = e;
memcpy(e->sha1, sha1, 20);
hashcpy(e->sha1, sha1);
e->size = size;
return e;
}
Expand Down Expand Up @@ -1516,7 +1516,7 @@ static void diff_fill_sha1_info(struct diff_filespec *one)
}
}
else
memset(one->sha1, 0, 20);
hashclr(one->sha1);
}

static void run_diff(struct diff_filepair *p, struct diff_options *o)
Expand Down
2 changes: 1 addition & 1 deletion fetch-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ static int everything_local(struct ref **refs, int nr_match, char **match)
continue;
}

memcpy(ref->new_sha1, local, 20);
hashcpy(ref->new_sha1, local);
if (!verbose)
continue;
fprintf(stderr,
Expand Down
2 changes: 1 addition & 1 deletion fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static int process_commit(struct commit *commit)
if (commit->object.flags & COMPLETE)
return 0;

memcpy(current_commit_sha1, commit->object.sha1, 20);
hashcpy(current_commit_sha1, commit->object.sha1);

pull_say("walk %s\n", sha1_to_hex(commit->object.sha1));

Expand Down
2 changes: 1 addition & 1 deletion fsck-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ static void add_sha1_list(unsigned char *sha1, unsigned long ino)
int nr;

entry->ino = ino;
memcpy(entry->sha1, sha1, 20);
hashcpy(entry->sha1, sha1);
nr = sha1_list.nr;
if (nr == MAX_SHA1_ENTRIES) {
fsck_sha1_list();
Expand Down
2 changes: 1 addition & 1 deletion http-fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ void prefetch(unsigned char *sha1)
char *filename = sha1_file_name(sha1);

newreq = xmalloc(sizeof(*newreq));
memcpy(newreq->sha1, sha1, 20);
hashcpy(newreq->sha1, sha1);
newreq->repo = alt;
newreq->url = NULL;
newreq->local = -1;
Expand Down
6 changes: 3 additions & 3 deletions http-push.c
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,7 @@ static int one_local_ref(const char *refname, const unsigned char *sha1)
struct ref *ref;
int len = strlen(refname) + 1;
ref = xcalloc(1, sizeof(*ref) + len);
memcpy(ref->new_sha1, sha1, 20);
hashcpy(ref->new_sha1, sha1);
memcpy(ref->name, refname, len);
*local_tail = ref;
local_tail = &ref->next;
Expand Down Expand Up @@ -1909,7 +1909,7 @@ static void one_remote_ref(char *refname)
}

ref = xcalloc(1, sizeof(*ref) + len);
memcpy(ref->old_sha1, remote_sha1, 20);
hashcpy(ref->old_sha1, remote_sha1);
memcpy(ref->name, refname, len);
*remote_tail = ref;
remote_tail = &ref->next;
Expand Down Expand Up @@ -2445,7 +2445,7 @@ int main(int argc, char **argv)
continue;
}
}
memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
if (is_zero_sha1(ref->new_sha1)) {
error("cannot happen anymore");
rc = -3;
Expand Down
4 changes: 2 additions & 2 deletions index-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static void *unpack_raw_entry(unsigned long offset,
case OBJ_DELTA:
if (pos + 20 >= pack_limit)
bad_object(offset, "object extends past end of pack");
memcpy(delta_base, pack_base + pos, 20);
hashcpy(delta_base, pack_base + pos);
pos += 20;
/* fallthru */
case OBJ_COMMIT:
Expand Down Expand Up @@ -304,7 +304,7 @@ static void parse_pack_objects(void)
if (obj->type == OBJ_DELTA) {
struct delta_entry *delta = &deltas[nr_deltas++];
delta->obj = obj;
memcpy(delta->base_sha1, base_sha1, 20);
hashcpy(delta->base_sha1, base_sha1);
} else
sha1_object(data, data_size, obj->type, obj->sha1);
free(data);
Expand Down
4 changes: 2 additions & 2 deletions mktree.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
ent->mode = mode;
ent->len = len;
memcpy(ent->sha1, sha1, 20);
hashcpy(ent->sha1, sha1);
memcpy(ent->name, path, len+1);
}

Expand Down Expand Up @@ -64,7 +64,7 @@ static void write_tree(unsigned char *sha1)
offset += sprintf(buffer + offset, "%o ", ent->mode);
offset += sprintf(buffer + offset, "%s", ent->name);
buffer[offset++] = 0;
memcpy(buffer + offset, ent->sha1, 20);
hashcpy((unsigned char*)buffer + offset, ent->sha1);
offset += 20;
}
write_sha1_file(buffer, offset, tree_type, sha1);
Expand Down
2 changes: 1 addition & 1 deletion object.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void created_object(const unsigned char *sha1, struct object *obj)
obj->used = 0;
obj->type = OBJ_NONE;
obj->flags = 0;
memcpy(obj->sha1, sha1, 20);
hashcpy(obj->sha1, sha1);

if (obj_hash_size - 1 <= nr_objs * 2)
grow_object_hash();
Expand Down
Loading

0 comments on commit e702496

Please sign in to comment.