git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Jonathan Nieder <jrnieder@gmail.com>
Cc: git@vger.kernel.org
Subject: [PATCH 1/2] compat: move unaligned helpers to bswap.h
Date: Thu, 23 Jan 2014 13:35:23 -0500	[thread overview]
Message-ID: <20140123183522.GA26447@sigill.intra.peff.net> (raw)
In-Reply-To: <20140123183320.GA22995@sigill.intra.peff.net>

From: Vicent Marti <tanoku@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 <tanoku@gmail.com>
Signed-off-by: Jeff King <peff@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);
 	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 = ntoh_s(ondisk2->flags2) << 16;
+		extended_flags = align_ntohs(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);
-- 
1.8.5.2.500.g8060133

  reply	other threads:[~2014-01-23 18:35 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-21 13:56 [PATCH v4 0/22] pack bitmaps Jeff King
2013-12-21 13:59 ` [PATCH v4 01/23] sha1write: make buffer const-correct Jeff King
2013-12-22  9:06   ` Christian Couder
2013-12-21 13:59 ` [PATCH v4 02/23] revindex: Export new APIs Jeff King
2013-12-21 13:59 ` [PATCH v4 03/23] pack-objects: Refactor the packing list Jeff King
2013-12-21 13:59 ` [PATCH v4 04/23] pack-objects: factor out name_hash Jeff King
2013-12-21 13:59 ` [PATCH v4 05/23] revision: allow setting custom limiter function Jeff King
2013-12-21 13:59 ` [PATCH v4 06/23] sha1_file: export `git_open_noatime` Jeff King
2013-12-21 13:59 ` [PATCH v4 07/23] compat: add endianness helpers Jeff King
2013-12-21 13:59 ` [PATCH v4 08/23] ewah: compressed bitmap implementation Jeff King
2014-01-23  2:05   ` Jonathan Nieder
2014-01-23 18:33     ` Jeff King
2014-01-23 18:35       ` Jeff King [this message]
2014-01-23 19:41         ` [PATCH 1/2] compat: move unaligned helpers to bswap.h Jonathan Nieder
2014-01-23 19:44           ` Jeff King
2014-01-23 19:56             ` Jonathan Nieder
2014-01-23 20:04               ` Jeff King
2014-01-23 20:08                 ` Jonathan Nieder
2014-01-23 20:09                   ` Jeff King
2014-01-23 18:35       ` [PATCH 2/2] ewah: support platforms that require aligned reads Jeff King
2014-01-23 19:52       ` [PATCH v4 08/23] ewah: compressed bitmap implementation Jonathan Nieder
2014-01-23 20:03         ` Jeff King
2014-01-23 20:12           ` Jonathan Nieder
2014-01-23 20:13             ` Jeff King
2014-01-23 20:23           ` Jonathan Nieder
2014-01-23 20:29             ` Jeff King
2014-01-23 20:38           ` Jeff King
2014-01-23 20:14       ` Shawn Pearce
2014-01-23 20:26         ` Jeff King
2014-01-23 21:53           ` brian m. carlson
2014-01-23 22:07             ` Jeff King
2014-01-23 22:17               ` Jonathan Nieder
2014-01-23 22:26                 ` Jeff King
2014-01-23 22:33                   ` Jonathan Nieder
2014-01-23 20:18       ` Jonathan Nieder
2014-01-23 21:20       ` [PATCH v2 0/3] unaligned reads from .bitmap files Jeff King
2014-01-23 21:23         ` [PATCH 1/3] block-sha1: factor out get_be and put_be wrappers Jeff King
2014-01-23 23:19           ` Jonathan Nieder
2014-01-23 21:26         ` [PATCH 2/3] read-cache: use get_be32 instead of hand-rolled ntoh_l Jeff King
2014-01-23 23:34           ` Jonathan Nieder
2014-01-24  2:22             ` Jeff King
2014-01-23 21:27         ` [PATCH 3/3] ewah: support platforms that require aligned reads Jeff King
2014-01-23 23:44           ` Jonathan Nieder
2014-01-23 23:49             ` Vicent Martí
2014-01-24  0:15               ` Jonathan Nieder
2014-01-23 23:17         ` [PATCH v2 0/3] unaligned reads from .bitmap files Jonathan Nieder
2013-12-21 13:59 ` [PATCH v4 09/23] documentation: add documentation for the bitmap format Jeff King
2013-12-21 14:00 ` [PATCH v4 10/23] pack-bitmap: add support for bitmap indexes Jeff King
2013-12-21 14:00 ` [PATCH v4 11/23] pack-objects: split add_object_entry Jeff King
2013-12-21 14:00 ` [PATCH v4 12/23] pack-objects: use bitmaps when packing objects Jeff King
2013-12-21 14:00 ` [PATCH v4 13/23] rev-list: add bitmap mode to speed up object lists Jeff King
2013-12-21 14:00 ` [PATCH v4 14/23] pack-objects: implement bitmap writing Jeff King
2013-12-21 14:00 ` [PATCH v4 15/23] repack: stop using magic number for ARRAY_SIZE(exts) Jeff King
2013-12-21 14:00 ` [PATCH v4 16/23] repack: turn exts array into array-of-struct Jeff King
2013-12-21 14:00 ` [PATCH v4 17/23] repack: handle optional files created by pack-objects Jeff King
2013-12-21 14:00 ` [PATCH v4 18/23] repack: consider bitmaps when performing repacks Jeff King
2013-12-21 14:00 ` [PATCH v4 19/23] count-objects: recognize .bitmap in garbage-checking Jeff King
2013-12-21 14:00 ` [PATCH v4 20/23] t: add basic bitmap functionality tests Jeff King
2013-12-21 14:00 ` [PATCH v4 21/23] t/perf: add tests for pack bitmaps Jeff King
2013-12-21 14:00 ` [PATCH v4 22/23] pack-bitmap: implement optional name_hash cache Jeff King
2013-12-21 14:00 ` [PATCH v4 23/23] compat/mingw.h: Fix the MinGW and msvc builds Jeff King
2013-12-25 22:08   ` Erik Faye-Lund
2013-12-28 10:00     ` Jeff King
2013-12-28 10:06       ` Vicent Martí
2013-12-28 15:58       ` Ramsay Jones
2013-12-21 14:03 ` [PATCH v4 0/22] pack bitmaps Jeff King
2013-12-21 14:05 ` Jeff King
2013-12-21 18:34 ` Thomas Rast

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140123183522.GA26447@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=jrnieder@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).