git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Elfyn McBratney <beu@gentoo.org>
To: Git Mailing List <git@vger.kernel.org>
Cc: Linus Torvalds <torvalds@osdl.org>,
	Alex Riesen <fork0@users.sourceforge.net>,
	Chuck Lever <cel@citi.umich.edu>,
	Git Mailing List <git@vger.kernel.org>,
	Junio C Hamano <junkio@cox.net>, Christopher Faylor <me@cgf.cx>,
	"H. Peter Anvin" <hpa@zytor.com>
Subject: Re: First cut at git port to Cygwin
Date: Sat, 8 Oct 2005 18:38:13 +0100	[thread overview]
Message-ID: <20051008173812.GA20870@gentoo.org> (raw)
In-Reply-To: <Pine.LNX.4.64.0510080900510.31407@g5.osdl.org>

[-- Attachment #1: Type: text/plain, Size: 2779 bytes --]

On Sat, Oct 08, 2005 at 09:11:03AM -0700, Linus Torvalds wrote:
 > 
 > On Fri, 7 Oct 2005, Alex Riesen wrote:
 > > 
 > > Make read_cache copy the index into memory, to improve portability on
 > > other OS's which have mmap too, tend to use it less commonly.
 > 
 > I really think that you should just get rid of the mmap.
 > 
 > As it is, you're just slowing the code down on sane architectures. That's 
 > not good.
 > 
 > So I'd suggest something like this instead.
 > 
 > Totally untested, of course.
 > 
 > 		Linus

Slightly adjusted diff below so it compiles ;)  (Note: only the second
die() un hunk #1 was changed.)

Best,
Elfyn

----
diff --git a/read-cache.c b/read-cache.c
--- a/read-cache.c
+++ b/read-cache.c
@@ -454,13 +454,39 @@ static int verify_hdr(struct cache_heade
 	return 0;
 }
 
+static void *map_index_file(int fd, size_t size)
+{
+	void *map;
+#ifdef NO_MMAP
+	map = malloc(size);
+	if (!map)
+		die("Unable to allocate index file mapping");
+	if (read(fd, map, size) != size)
+		die("Unable to read %z bytes from index", size);
+#else
+	map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+	if (map == MAP_FAILED)
+		die("index file mmap failed (%s)", strerror(errno));
+#endif
+	return map;
+}
+
+static void unmap_index_file(void *map, size_t size)
+{
+#ifdef NO_MMAP
+	free(map);
+#else
+	munmap(map, size);
+#endif
+}
+
 int read_cache(void)
 {
 	int fd, i;
 	struct stat st;
 	unsigned long size, offset;
-	void *map;
 	struct cache_header *hdr;
+	void *map;
 
 	errno = EBUSY;
 	if (active_cache)
@@ -475,16 +501,15 @@ int read_cache(void)
 	}
 
 	size = 0; // avoid gcc warning
-	map = MAP_FAILED;
-	if (!fstat(fd, &st)) {
-		size = st.st_size;
-		errno = EINVAL;
-		if (size >= sizeof(struct cache_header) + 20)
-			map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
-	}
+	if (fstat(fd, &st))
+		die("unable to fstat index file");
+
+	size = st.st_size;
+	errno = EINVAL;
+	if (size < sizeof(struct cache_header) + 20)
+		goto corrupt;
+	map = map_index_file(fd, size);
 	close(fd);
-	if (map == MAP_FAILED)
-		die("index file mmap failed (%s)", strerror(errno));
 
 	hdr = map;
 	if (verify_hdr(hdr, size) < 0)
@@ -503,8 +528,9 @@ int read_cache(void)
 	return active_nr;
 
 unmap:
-	munmap(map, size);
+	unmap_index_file(map, size);
 	errno = EINVAL;
+corrupt:
 	die("index file corrupt");
 }


-- 
Elfyn McBratney
Gentoo Developer/Perl Team Lead
beu/irc.freenode.net                            http://dev.gentoo.org/~beu/
+------------O.o--------------------- http://dev.gentoo.org/~beu/pubkey.asc

PGP Key ID: 0x69DF17AD
PGP Key Fingerprint:
  DBD3 B756 ED58 B1B4 47B9  B3BD 8D41 E597 69DF 17AD

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

  reply	other threads:[~2005-10-08 17:38 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-29  0:53 First cut at git port to Cygwin H. Peter Anvin
2005-09-29  4:30 ` Junio C Hamano
2005-09-29  5:07   ` H. Peter Anvin
2005-09-29  4:46 ` Martin Langhoff
2005-09-29  5:13   ` Junio C Hamano
2005-09-29  6:19   ` H. Peter Anvin
2005-09-29  8:46 ` Johannes Schindelin
2005-09-29 16:11   ` H. Peter Anvin
2005-09-29 17:25   ` H. Peter Anvin
2005-09-30 10:02 ` Junio C Hamano
2005-09-30 17:01   ` H. Peter Anvin
2005-09-30 19:08     ` H. Peter Anvin
2005-10-04 12:31 ` Alex Riesen
2005-10-04 13:06   ` Alex Riesen
2005-10-04 14:06   ` H. Peter Anvin
2005-10-05  3:15     ` Christopher Faylor
2005-10-04 15:03   ` H. Peter Anvin
2005-10-05  3:16     ` Christopher Faylor
2005-10-05  5:25       ` H. Peter Anvin
2005-10-05 11:24     ` Alex Riesen
2005-10-05 15:46       ` Alex Riesen
2005-10-05 15:54         ` Christopher Faylor
2005-10-05 16:09           ` Davide Libenzi
2005-10-05 16:15             ` Christopher Faylor
2005-10-05 16:23               ` H. Peter Anvin
2005-10-05 16:28                 ` Christopher Faylor
2005-10-05 17:29               ` Davide Libenzi
2005-10-05 19:17           ` Alex Riesen
2005-10-05 20:29             ` Christopher Faylor
2005-10-06  9:05               ` Alex Riesen
2005-10-06 10:07                 ` Alex Riesen
2005-10-07 12:44                   ` Alex Riesen
2005-10-07 15:34                     ` Linus Torvalds
2005-10-07 20:54                       ` Alex Riesen
2005-10-07 21:22                         ` Alex Riesen
2005-10-07 21:29                           ` Chuck Lever
2005-10-07 21:39                             ` Alex Riesen
2005-10-08 16:11                               ` Linus Torvalds
2005-10-08 17:38                                 ` Elfyn McBratney [this message]
2005-10-08 17:43                                 ` Elfyn McBratney
2005-10-08 18:27                                 ` Johannes Schindelin
2005-10-08 18:44                                   ` Junio C Hamano
2005-10-08 19:04                                     ` Johannes Schindelin
2005-10-08 21:10                                       ` Junio C Hamano
2005-10-08 22:06                                         ` Johannes Schindelin
2005-10-10 18:43                                     ` H. Peter Anvin
2005-10-10 19:01                                       ` Johannes Schindelin
2005-10-10 19:26                                         ` H. Peter Anvin
2005-10-10 19:42                                           ` Johannes Schindelin
2005-10-10 20:21                                           ` Junio C Hamano
2005-10-10 20:34                                           ` Junio C Hamano
2005-10-10 20:52                                             ` H. Peter Anvin
2005-10-10 20:27                                         ` Daniel Barkalow
2005-10-08 18:49                                   ` Alex Riesen
2005-10-09 20:40                           ` Commit text BEFORE the dashes (Re: First cut at git port to Cygwin) Matthias Urlichs
     [not found]                         ` <7vfyrdyre5.fsf@assigned-by-dhcp.cox.net>
2005-10-07 23:45                           ` First cut at git port to Cygwin Alex Riesen
2005-10-08  1:00                             ` Elfyn McBratney
2005-10-10 18:45                               ` H. Peter Anvin
2005-10-05 13:16 ` Jonas Fonseca
2005-10-05 13:58   ` Johannes Schindelin
2005-10-05 15:52     ` [PATCH] Fix symbolic ref validation Jonas Fonseca
2005-10-05 16:54       ` Junio C Hamano

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=20051008173812.GA20870@gentoo.org \
    --to=beu@gentoo.org \
    --cc=cel@citi.umich.edu \
    --cc=fork0@users.sourceforge.net \
    --cc=git@vger.kernel.org \
    --cc=hpa@zytor.com \
    --cc=junkio@cox.net \
    --cc=me@cgf.cx \
    --cc=torvalds@osdl.org \
    /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).