From 730d48a2ef88a7fb7aa4409d40b1e6964a93267f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 8 Oct 2005 15:54:36 -0700 Subject: [PATCH] If NO_MMAP is defined, fake mmap() and munmap() 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 --- compat/mmap.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 compat/mmap.c (limited to 'compat/mmap.c') diff --git a/compat/mmap.c b/compat/mmap.c new file mode 100644 index 0000000000..fca6321ce0 --- /dev/null +++ b/compat/mmap.c @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#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; +} + -- cgit v1.2.3 From f48000fcbe1009c18f1cc46e56cde2cb632071fa Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 8 Oct 2005 15:54:36 -0700 Subject: Yank writing-back support from gitfakemmap. We do not write through our use of mmap(), so make sure callers pass MAP_PRIVATE and remove support for writing changes back. Signed-off-by: Junio C Hamano --- compat/mmap.c | 75 +++++------------------------------------------------------ 1 file changed, 6 insertions(+), 69 deletions(-) (limited to 'compat/mmap.c') diff --git a/compat/mmap.c b/compat/mmap.c index fca6321ce0..a051c4767d 100644 --- a/compat/mmap.c +++ b/compat/mmap.c @@ -4,43 +4,33 @@ #include #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) + if (start != NULL || !(flags & MAP_PRIVATE)) die("Invalid usage of gitfakemmap."); - if(lseek(fd, offset, SEEK_SET)<0) { + if (lseek(fd, offset, SEEK_SET) < 0) { errno = EINVAL; return MAP_FAILED; } start = xmalloc(length); - if(start == NULL) { + if (start == NULL) { errno = ENOMEM; return MAP_FAILED; } - while(n < length) { + while (n < length) { int count = read(fd, start+n, length-n); - if(count == 0) { + if (count == 0) { memset(start+n, 0, length-n); break; } - if(count < 0) { + if (count < 0) { free(start); errno = EACCES; return MAP_FAILED; @@ -49,65 +39,12 @@ void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_ 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; } -- cgit v1.2.3