Skip to content

Commit

Permalink
read-cache.c: allow unaligned mapping of the index file
Browse files Browse the repository at this point in the history
Both the on-disk format v2 and v3 pads the "name" field to the multiple of
eight to make sure that various quantities in network long/short type can
be accessed with ntohl/ntohs without having to worry about alignment, but
this forces us to waste disk I/O bandwidth.

Introduce ntoh_s()/ntoh_l() macros that the callers can use as if they were
the regular ntohs()/ntohl() on a field that may not be aligned correctly.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Junio C Hamano committed Apr 3, 2012
1 parent db3b313 commit d60c49c
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions read-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,26 @@ int read_index(struct index_state *istate)
return read_index_from(istate, get_index_file());
}

#ifndef NEEDS_ALIGNED_ACCESS
#define ntoh_s(var) ntohs(var)
#define ntoh_l(var) ntohl(var)
#else
static inline uint16_t ntoh_s_force_align(void *p)
{
uint16_t x;
memcpy(&x, p, sizeof(x));
return ntohs(x);
}
static inline uint32_t ntoh_l_force_align(void *p)
{
uint32_t x;
memcpy(&x, p, sizeof(x));
return ntohl(x);
}
#define ntoh_s(var) ntoh_s_force_align(&(var))
#define ntoh_l(var) ntoh_l_force_align(&(var))
#endif

static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk)
{
struct cache_entry *ce;
Expand All @@ -1293,14 +1313,14 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk)
unsigned int flags;

/* On-disk flags are just 16 bits */
flags = ntohs(ondisk->flags);
flags = ntoh_s(ondisk->flags);
len = flags & CE_NAMEMASK;

if (flags & CE_EXTENDED) {
struct ondisk_cache_entry_extended *ondisk2;
int extended_flags;
ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;
extended_flags = ntohs(ondisk2->flags2) << 16;
extended_flags = ntoh_s(ondisk2->flags2) << 16;
/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
if (extended_flags & ~CE_EXTENDED_FLAGS)
die("Unknown index entry format %08x", extended_flags);
Expand All @@ -1315,16 +1335,16 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk)

ce = xmalloc(cache_entry_size(len));

ce->ce_ctime.sec = ntohl(ondisk->ctime.sec);
ce->ce_mtime.sec = ntohl(ondisk->mtime.sec);
ce->ce_ctime.nsec = ntohl(ondisk->ctime.nsec);
ce->ce_mtime.nsec = ntohl(ondisk->mtime.nsec);
ce->ce_dev = ntohl(ondisk->dev);
ce->ce_ino = ntohl(ondisk->ino);
ce->ce_mode = ntohl(ondisk->mode);
ce->ce_uid = ntohl(ondisk->uid);
ce->ce_gid = ntohl(ondisk->gid);
ce->ce_size = ntohl(ondisk->size);
ce->ce_ctime.sec = ntoh_l(ondisk->ctime.sec);
ce->ce_mtime.sec = ntoh_l(ondisk->mtime.sec);
ce->ce_ctime.nsec = ntoh_l(ondisk->ctime.nsec);
ce->ce_mtime.nsec = ntoh_l(ondisk->mtime.nsec);
ce->ce_dev = ntoh_l(ondisk->dev);
ce->ce_ino = ntoh_l(ondisk->ino);
ce->ce_mode = ntoh_l(ondisk->mode);
ce->ce_uid = ntoh_l(ondisk->uid);
ce->ce_gid = ntoh_l(ondisk->gid);
ce->ce_size = ntoh_l(ondisk->size);
ce->ce_flags = flags;

hashcpy(ce->sha1, ondisk->sha1);
Expand Down

0 comments on commit d60c49c

Please sign in to comment.