From 84cd4773f43f4cbbf022d29fded0b881ced5a497 Mon Sep 17 00:00:00 2001 From: leitner Date: Sat, 12 May 2001 03:34:27 +0000 Subject: [PATCH] add mmap library. --- CHANGES | 4 ++++ Makefile | 13 ++++++++----- mmap.h | 19 +++++++++++++++++++ mmap/mmap_private.c | 19 +++++++++++++++++++ mmap/mmap_read.c | 19 +++++++++++++++++++ mmap/mmap_shared.c | 19 +++++++++++++++++++ t.c | 6 ++++++ 7 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 mmap.h create mode 100644 mmap/mmap_private.c create mode 100644 mmap/mmap_read.c create mode 100644 mmap/mmap_shared.c diff --git a/CHANGES b/CHANGES index 1458fef..82d3e6b 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +0.7: + oops, the read buffering was completely broken! + add mmap library (idea from Ingo Oeser) + 0.6: changed name to libowfat. fixed fmt_ulong (did not output 0 correctly). diff --git a/Makefile b/Makefile index 3c4ed22..5b264a8 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ -all: t byte.a fmt.a scan.a str.a uint.a open.a stralloc.a unix.a socket.a buffer.a libowfat.a +all: t byte.a fmt.a scan.a str.a uint.a open.a stralloc.a unix.a socket.a buffer.a mmap.a libowfat.a -VPATH=str:byte:fmt:scan:uint:open:stralloc:unix:socket:buffer +VPATH=str:byte:fmt:scan:uint:open:stralloc:unix:socket:buffer:mmap CC=egcc #CFLAGS=-I. -pipe -Wall -Os -march=pentiumpro -fomit-frame-pointer -fschedule-insns2 -Wall @@ -18,6 +18,7 @@ STRA_OBJS=$(patsubst stralloc/%.c,%.o,$(wildcard stralloc/*.c)) UNIX_OBJS=$(patsubst unix/%.c,%.o,$(wildcard unix/*.c)) SOCKET_OBJS=$(patsubst socket/%.c,%.o,$(wildcard socket/*.c)) BUFFER_OBJS=$(patsubst buffer/%.c,%.o,$(wildcard buffer/*.c)) +MMAP_OBJS=$(patsubst mmap/%.c,%.o,$(wildcard mmap/*.c)) $(BYTE_OBJS): byte.h $(FMT_OBJS): fmt.h @@ -27,6 +28,7 @@ $(UINT_OBJS): uint16.h uint32.h $(STRA_OBJS): stralloc.h $(SOCKET_OBJS): socket.h $(BUFFER_OBJS): buffer.h +$(MMAP_OBJS): mmap.h byte.a: $(BYTE_OBJS) fmt.a: $(FMT_OBJS) @@ -38,16 +40,17 @@ stralloc.a: $(STRA_OBJS) unix.a: $(UNIX_OBJS) socket.a: $(SOCKET_OBJS) buffer.a: $(BUFFER_OBJS) +mmap.a: $(MMAP_OBJS) libowfat.a: $(BYTE_OBJS) $(FMT_OBJS) $(SCAN_OBJS) $(STR_OBJS) \ $(UINT_OBJS) $(OPEN_OBJS) $(STRA_OBJS) $(UNIX_OBJS) $(SOCKET_OBJS) \ -$(BUFFER_OBJS) +$(BUFFER_OBJS) $(MMAP_OBJS) %.a: ar cr $@ $^ -t: t.o socket.a stralloc.a fmt.a scan.a uint.a open.a buffer.a str.a \ -byte.a +t: t.o socket.a stralloc.a fmt.a scan.a uint.a mmap.a open.a buffer.a \ +str.a byte.a gcc -g -o $@ $^ .PHONY: clean tar diff --git a/mmap.h b/mmap.h new file mode 100644 index 0000000..5e245c5 --- /dev/null +++ b/mmap.h @@ -0,0 +1,19 @@ +#ifndef MMAP_H +#define MMAP_H + +#include + +/* open file for reading, mmap whole file, close file, write length of + * map in filesize and return pointer to map. */ +extern char* mmap_read(const char *filename,unsigned long* filesize) __THROW; + +/* open file for writing, mmap whole file privately (copy on write), + * close file, write length of map in filesize and return pointer to + * map. */ +extern int mmap_write(const char *filename,unsigned long* filesize) __THROW; + +/* open file for writing, mmap whole file shared, close file, write + * length of map in filesize and return pointer to map. */ +extern int mmap_shared(const char *filename,unsigned long* filesize) __THROW; + +#endif diff --git a/mmap/mmap_private.c b/mmap/mmap_private.c new file mode 100644 index 0000000..e88aabc --- /dev/null +++ b/mmap/mmap_private.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include "open.h" +#include "mmap.h" + +extern char* mmap_private(const char* filename,unsigned long* filesize) { + int fd=open_read(filename); + char *map; + if (fd>=0) { + *filesize=lseek(fd,0,SEEK_END); + map=mmap(0,*filesize,PROT_WRITE,MAP_PRIVATE,fd,0); + if (map==(char*)-1) + map=0; + close(fd); + return map; + } + return 0; +} diff --git a/mmap/mmap_read.c b/mmap/mmap_read.c new file mode 100644 index 0000000..4c25c9e --- /dev/null +++ b/mmap/mmap_read.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include "open.h" +#include "mmap.h" + +extern char* mmap_read(const char* filename,unsigned long* filesize) { + int fd=open_read(filename); + char *map; + if (fd>=0) { + *filesize=lseek(fd,0,SEEK_END); + map=mmap(0,*filesize,PROT_READ,MAP_SHARED,fd,0); + if (map==(char*)-1) + map=0; + close(fd); + return map; + } + return 0; +} diff --git a/mmap/mmap_shared.c b/mmap/mmap_shared.c new file mode 100644 index 0000000..25365dd --- /dev/null +++ b/mmap/mmap_shared.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include "open.h" +#include "mmap.h" + +extern char* mmap_private(const char* filename,unsigned long* filesize) { + int fd=open_read(filename); + char *map; + if (fd>=0) { + *filesize=lseek(fd,0,SEEK_END); + map=mmap(0,*filesize,PROT_WRITE,MAP_SHARED,fd,0); + if (map==(char*)-1) + map=0; + close(fd); + return map; + } + return 0; +} diff --git a/t.c b/t.c index dbca921..7564341 100644 --- a/t.c +++ b/t.c @@ -7,15 +7,21 @@ #include "socket.h" #include "buffer.h" #include "ip4.h" +#include "mmap.h" #include #define rdtscl(low) \ __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx") int main(int argc,char* argv[]) { + unsigned long len; + char *c=mmap_read("/etc/passwd",&len); + printf("got map %p of len %lu\n",c,len); +#if 0 char c; printf("%d\n",buffer_getc(buffer_0,&c)); printf("%c\n",c); +#endif #if 0 char buf[100]="01234567890123456789012345678901234567890123456789"; long a,b,c;