[PATCH 1/2] compat: move unaligned helpers to bswap.h
From: Vicent Marti tan...@gmail.com Commit d60c49c (read-cache.c: allow unaligned mapping of the index file, 2012-04-03) introduced helpers to access unaligned data. Let's factor them out to make them more widely available. While we're at it, we'll give the helpers more readable names, add a helper for the ntohll form, and add the appropriate Makefile knob. Signed-off-by: Vicent Marti tan...@gmail.com Signed-off-by: Jeff King p...@peff.net --- Makefile | 7 +++ compat/bswap.h | 28 read-cache.c | 44 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 4136c4f..5711c0e 100644 --- a/Makefile +++ b/Makefile @@ -342,6 +342,9 @@ all:: # Define DEFAULT_HELP_FORMAT to man, info or html # (defaults to man) if you want to have a different default when # git help is called without a parameter specifying the format. +# +# Define NEEDS_ALIGNED_ACCESS if your platform cannot handle unaligned +# access to integers in mmap'd files. GIT-VERSION-FILE: FORCE @$(SHELL_PATH) ./GIT-VERSION-GEN @@ -1505,6 +1508,10 @@ ifneq (,$(XDL_FAST_HASH)) BASIC_CFLAGS += -DXDL_FAST_HASH endif +ifdef NEEDS_ALIGNED_ACCESS + BASIC_CFLAGS += -DNEEDS_ALIGNED_ACCESS +endif + ifeq ($(TCLTK_PATH),) NO_TCLTK = NoThanks endif diff --git a/compat/bswap.h b/compat/bswap.h index c18a78e..80abc54 100644 --- a/compat/bswap.h +++ b/compat/bswap.h @@ -122,3 +122,31 @@ static inline uint64_t git_bswap64(uint64_t x) #endif #endif + +#ifndef NEEDS_ALIGNED_ACCESS +#define align_ntohs(var) ntohs(var) +#define align_ntohl(var) ntohl(var) +#define align_ntohll(var) ntohll(var) +#else +static inline uint16_t ntohs_force_align(void *p) +{ + uint16_t x; + memcpy(x, p, sizeof(x)); + return ntohs(x); +} +static inline uint32_t ntohl_force_align(void *p) +{ + uint32_t x; + memcpy(x, p, sizeof(x)); + return ntohl(x); +} +static inline uint64_t ntohll_force_align(void *p) +{ + uint64_t x; + memcpy(x, p, sizeof(x)); + return ntohll(x); +} +#define align_ntohs(var) ntohs_force_align((var)) +#define align_ntohl(var) ntohl_force_align((var)) +#define align_ntohll(var) ntohll_force_align((var)) +#endif diff --git a/read-cache.c b/read-cache.c index 33dd676..fa53504 100644 --- a/read-cache.c +++ b/read-cache.c @@ -1313,26 +1313,6 @@ 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 *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk, unsigned int flags, const char *name, @@ -1340,16 +1320,16 @@ static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *on { struct cache_entry *ce = xmalloc(cache_entry_size(len)); - ce-ce_stat_data.sd_ctime.sec = ntoh_l(ondisk-ctime.sec); - ce-ce_stat_data.sd_mtime.sec = ntoh_l(ondisk-mtime.sec); - ce-ce_stat_data.sd_ctime.nsec = ntoh_l(ondisk-ctime.nsec); - ce-ce_stat_data.sd_mtime.nsec = ntoh_l(ondisk-mtime.nsec); - ce-ce_stat_data.sd_dev = ntoh_l(ondisk-dev); - ce-ce_stat_data.sd_ino = ntoh_l(ondisk-ino); - ce-ce_mode = ntoh_l(ondisk-mode); - ce-ce_stat_data.sd_uid = ntoh_l(ondisk-uid); - ce-ce_stat_data.sd_gid = ntoh_l(ondisk-gid); - ce-ce_stat_data.sd_size = ntoh_l(ondisk-size); + ce-ce_stat_data.sd_ctime.sec = align_ntohl(ondisk-ctime.sec); + ce-ce_stat_data.sd_mtime.sec = align_ntohl(ondisk-mtime.sec); + ce-ce_stat_data.sd_ctime.nsec = align_ntohl(ondisk-ctime.nsec); + ce-ce_stat_data.sd_mtime.nsec = align_ntohl(ondisk-mtime.nsec); + ce-ce_stat_data.sd_dev = align_ntohl(ondisk-dev); + ce-ce_stat_data.sd_ino = align_ntohl(ondisk-ino); + ce-ce_mode = align_ntohl(ondisk-mode); + ce-ce_stat_data.sd_uid = align_ntohl(ondisk-uid); + ce-ce_stat_data.sd_gid = align_ntohl(ondisk-gid); + ce-ce_stat_data.sd_size = align_ntohl(ondisk-size); ce-ce_flags = flags ~CE_NAMEMASK; ce-ce_namelen = len; hashcpy(ce-sha1, ondisk-sha1); @@ -1389,14 +1369,14 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk, unsigned int flags; /* On-disk flags are just 16 bits */ - flags = ntoh_s(ondisk-flags); + flags = align_ntohs(ondisk-flags);
Re: [PATCH 1/2] compat: move unaligned helpers to bswap.h
Jeff King wrote: Commit d60c49c (read-cache.c: allow unaligned mapping of the index file, 2012-04-03) introduced helpers to access unaligned data. Let's factor them out to make them more widely available. While we're at it, we'll give the helpers more readable names, add a helper for the ntohll form, and add the appropriate Makefile knob. Weird. Why wasn't git broken on the relevant platforms before (given that no one has been setting NEEDS_ALIGNED_ACCESS for them)? Puzzled, Jonathan -- To unsubscribe from this list: send the line unsubscribe git in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 1/2] compat: move unaligned helpers to bswap.h
On Thu, Jan 23, 2014 at 11:41:18AM -0800, Jonathan Nieder wrote: Jeff King wrote: Commit d60c49c (read-cache.c: allow unaligned mapping of the index file, 2012-04-03) introduced helpers to access unaligned data. Let's factor them out to make them more widely available. While we're at it, we'll give the helpers more readable names, add a helper for the ntohll form, and add the appropriate Makefile knob. Weird. Why wasn't git broken on the relevant platforms before (given that no one has been setting NEEDS_ALIGNED_ACCESS for them)? Because most of our data structures support aligned access. Thomas mentioned this as a potential issue earlier, and I said in a re-roll cover letter: I did not include the NEEDS_ALIGNED_ACCESS patch. I note that we do not even have a Makefile knob for this, and the code in read-cache.c has probably never actually been used. Are there real systems that have a problem? The read-cache code was in support of the index v4 experiment, which did away with the 8-byte padding. So it could be that we simply don't see it, because everything is currently aligned. I think it was a bug waiting to surface if index v4 ever got wide use. -Peff -- To unsubscribe from this list: send the line unsubscribe git in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 1/2] compat: move unaligned helpers to bswap.h
Jeff King wrote: I think it was a bug waiting to surface if index v4 ever got wide use. Ah, ok. In that case I think git-compat-util.h should include something like what block-sha1/sha1.c has: #if !defined(__i386__) !defined(__x86_64__) \ !defined(_M_IX86) !defined(_M_X64) \ !defined(__ppc__) !defined(__ppc64__) \ !defined(__powerpc__) !defined(__powerpc64__) \ !defined(__s390__) !defined(__s390x__) #define NEEDS_ALIGNED_ACCESS #endif Otherwise we are relying on the person building to know their own architecture intimately, which shouldn't be necessary. Meanwhile, as mentioned in the other message, I suspect the NEEDS_ALIGNED_ACCESS code path is broken for aggressive compilers anyway. Looking more. Thanks, Jonathan -- To unsubscribe from this list: send the line unsubscribe git in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 1/2] compat: move unaligned helpers to bswap.h
On Thu, Jan 23, 2014 at 12:08:04PM -0800, Jonathan Nieder wrote: Jeff King wrote: On Thu, Jan 23, 2014 at 11:56:43AM -0800, Jonathan Nieder wrote: In that case I think git-compat-util.h should include something like what block-sha1/sha1.c has: #if !defined(__i386__) !defined(__x86_64__) \ !defined(_M_IX86) !defined(_M_X64) \ !defined(__ppc__) !defined(__ppc64__) \ !defined(__powerpc__) !defined(__powerpc64__) \ !defined(__s390__) !defined(__s390x__) #define NEEDS_ALIGNED_ACCESS #endif Otherwise we are relying on the person building to know their own architecture intimately, which shouldn't be necessary. Yeah, I agree it would be nice to autodetect. The nice thing is that false positives are harmless, modulo slowing down git a little if the compiler doesn't figure out how to optimize the NEEDS_ALIGNED_ACCESS codepath when on an unlisted platform that doesn't, in fact, need aligned access. OK, I'll refactor the knob. -Peff -- To unsubscribe from this list: send the line unsubscribe git in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 1/2] compat: move unaligned helpers to bswap.h
Jeff King wrote: On Thu, Jan 23, 2014 at 11:56:43AM -0800, Jonathan Nieder wrote: In that case I think git-compat-util.h should include something like what block-sha1/sha1.c has: #if !defined(__i386__) !defined(__x86_64__) \ !defined(_M_IX86) !defined(_M_X64) \ !defined(__ppc__) !defined(__ppc64__) \ !defined(__powerpc__) !defined(__powerpc64__) \ !defined(__s390__) !defined(__s390x__) #define NEEDS_ALIGNED_ACCESS #endif Otherwise we are relying on the person building to know their own architecture intimately, which shouldn't be necessary. Yeah, I agree it would be nice to autodetect. The nice thing is that false positives are harmless, modulo slowing down git a little if the compiler doesn't figure out how to optimize the NEEDS_ALIGNED_ACCESS codepath when on an unlisted platform that doesn't, in fact, need aligned access. In other words, it would work out of the box for everybody. -- To unsubscribe from this list: send the line unsubscribe git in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html