git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] If NO_MMAP is defined, fake mmap() and munmap()
@ 2005-10-07 22:55 Johannes Schindelin
  2005-10-07 23:44 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Johannes Schindelin @ 2005-10-07 22:55 UTC (permalink / raw)
  To: git; +Cc: junkio


Since some platforms do not support mmap() at all, and others do only just 
so, this patch introduces the option to fake mmap() and munmap() by 
malloc()ing and read()ing explicitely.

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>

---

Only lightly tested, but it seems to work correctly (after all, this 
commit was created after compiling with NO_MMAP=1).

 Makefile      |    6 +++
 cache.h       |   16 ++++++++
 compat/mmap.c |  113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mailsplit.c   |    1 -
 4 files changed, 135 insertions(+), 1 deletions(-)
 create mode 100644 compat/mmap.c

applies-to: 2c165e1b55bf857247c0f074e9bea680bf411586
3d4f1e103b35c28c8190f9a59ffa95221277fdb4
diff --git a/Makefile b/Makefile
index 2f7cdd4..fb4c410 100644
--- a/Makefile
+++ b/Makefile
@@ -27,6 +27,8 @@
 # Define NEEDS_SOCKET if linking with libc is not enough (SunOS,
 # Patrick Mauritz).
 #
+# Define NO_MMAP if you want to avoid mmap.
+#
 # Define WITH_OWN_SUBPROCESS_PY if you want to use with python 2.3.
 #
 # Define NO_IPV6 if you lack IPv6 support and getaddrinfo().
@@ -259,6 +261,10 @@ ifdef NO_STRCASESTR
 	DEFINES += -Dstrcasestr=gitstrcasestr
 	LIB_OBJS += compat/strcasestr.o
 endif
+ifdef NO_MMAP
+	DEFINES += -Dmmap=gitfakemmap -Dmunmap=gitfakemunmap -DNO_MMAP
+	LIB_OBJS += compat/mmap.o
+endif
 ifdef NO_IPV6
 	DEFINES += -DNO_IPV6 -Dsockaddr_storage=sockaddr_in
 endif
diff --git a/cache.h b/cache.h
index 514adb8..5987d4c 100644
--- a/cache.h
+++ b/cache.h
@@ -11,7 +11,9 @@
 #include <string.h>
 #include <errno.h>
 #include <limits.h>
+#ifndef NO_MMAP
 #include <sys/mman.h>
+#endif
 #include <sys/param.h>
 #include <netinet/in.h>
 #include <sys/types.h>
@@ -356,4 +358,18 @@ extern void packed_object_info_detail(st
 /* Dumb servers support */
 extern int update_server_info(int);
 
+#ifdef NO_MMAP
+
+#ifndef PROT_READ
+#define PROT_READ 1
+#define PROT_WRITE 2
+#define MAP_PRIVATE 1
+#define MAP_FAILED ((void*)-1)
+#endif
+
+extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);
+extern int gitfakemunmap(void *start, size_t length);
+
+#endif
+
 #endif /* CACHE_H */
diff --git a/compat/mmap.c b/compat/mmap.c
new file mode 100644
index 0000000..fca6321
--- /dev/null
+++ b/compat/mmap.c
@@ -0,0 +1,113 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include "../cache.h"
+
+typedef struct fakemmapwritable {
+	void *start;
+	size_t length;
+	int fd;
+	off_t offset;
+	struct fakemmapwritable *next;
+} fakemmapwritable;
+
+static fakemmapwritable *writablelist = NULL;
+
+void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset)
+{
+	int n = 0;
+
+	if(start != NULL)
+		die("Invalid usage of gitfakemmap.");
+
+	if(lseek(fd, offset, SEEK_SET)<0) {
+		errno = EINVAL;
+		return MAP_FAILED;
+	}
+
+	start = xmalloc(length);
+	if(start == NULL) {
+		errno = ENOMEM;
+		return MAP_FAILED;
+	}
+
+	while(n < length) {
+		int count = read(fd, start+n, length-n);
+
+		if(count == 0) {
+			memset(start+n, 0, length-n);
+			break;
+		}
+
+		if(count < 0) {
+			free(start);
+			errno = EACCES;
+			return MAP_FAILED;
+		}
+
+		n += count;
+	}
+
+	if(prot & PROT_WRITE) {
+		fakemmapwritable *next = xmalloc(sizeof(fakemmapwritable));
+		next->start = start;
+		next->length = length;
+		next->fd = dup(fd);
+		next->offset = offset;
+		next->next = writablelist;
+		writablelist = next;
+	}
+
+	return start;
+}
+
+int gitfakemunmap(void *start, size_t length)
+{
+	fakemmapwritable *writable = writablelist, *before = NULL;
+
+	while(writable && (writable->start > start + length
+			|| writable->start + writable->length < start)) {
+		before = writable;
+		writable = writable->next;
+	}
+
+	if(writable) {
+		/* need to write back the contents */
+		int n = 0;
+
+		if(writable->start != start || writable->length != length)
+			die("fakemmap does not support partial write back.");
+
+		if(lseek(writable->fd, writable->offset, SEEK_SET) < 0) {
+			free(start);
+			errno = EBADF;
+			return -1;
+		}
+
+		while(n < length) {
+			int count = write(writable->fd, start + n, length - n);
+
+			if(count < 0) {
+				errno = EINVAL;
+				return -1;
+			}
+
+			n += count;
+		}
+
+		close(writable->fd);
+
+		if(before)
+			before->next = writable->next;
+		else
+			writablelist = writable->next;
+
+		free(writable);
+	}
+
+	free(start);
+
+	return 0;
+}
+
diff --git a/mailsplit.c b/mailsplit.c
index 7981f87..0f8100d 100644
--- a/mailsplit.c
+++ b/mailsplit.c
@@ -9,7 +9,6 @@
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/mman.h>
 #include <string.h>
 #include <stdio.h>
 #include <ctype.h>
---
0.99.8.GIT

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] If NO_MMAP is defined, fake mmap() and munmap()
  2005-10-07 22:55 [PATCH] If NO_MMAP is defined, fake mmap() and munmap() Johannes Schindelin
@ 2005-10-07 23:44 ` Junio C Hamano
  2005-10-07 23:57   ` Alex Riesen
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2005-10-07 23:44 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> Since some platforms do not support mmap() at all, and others do only just 
> so, this patch introduces the option to fake mmap() and munmap() by 
> malloc()ing and read()ing explicitely.

I guess I can just drop Alex Riesen patch and any other recent
patches that try to work around mmap().  Happy!

 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] If NO_MMAP is defined, fake mmap() and munmap()
  2005-10-07 23:44 ` Junio C Hamano
@ 2005-10-07 23:57   ` Alex Riesen
  0 siblings, 0 replies; 3+ messages in thread
From: Alex Riesen @ 2005-10-07 23:57 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Johannes Schindelin, git

Junio C Hamano, Sat, Oct 08, 2005 01:44:22 +0200:
> > Since some platforms do not support mmap() at all, and others do only just 
> > so, this patch introduces the option to fake mmap() and munmap() by 
> > malloc()ing and read()ing explicitely.
> 
> I guess I can just drop Alex Riesen patch and any other recent
> patches that try to work around mmap().  Happy!
> 

Me too. I was just about to make a read_cache read the whole index in
(absolutely the same as Johannes, but not that elegant).

The platform(s) deserve such a treatment :)

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-10-07 23:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-07 22:55 [PATCH] If NO_MMAP is defined, fake mmap() and munmap() Johannes Schindelin
2005-10-07 23:44 ` Junio C Hamano
2005-10-07 23:57   ` Alex Riesen

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).