Browse Source

add mmap library.

master
Felix von Leitner 21 years ago
parent
commit
c453a99bff
  1. 4
      CHANGES
  2. 13
      Makefile
  3. 19
      mmap.h
  4. 19
      mmap/mmap_private.c
  5. 19
      mmap/mmap_read.c
  6. 19
      mmap/mmap_shared.c
  7. 6
      t.c

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

13
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

19
mmap.h

@ -0,0 +1,19 @@
#ifndef MMAP_H
#define MMAP_H
#include <sys/cdefs.h>
/* 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

19
mmap/mmap_private.c

@ -0,0 +1,19 @@
#include <sys/types.h>
#include <unistd.h>
#include <sys/mman.h>
#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;
}

19
mmap/mmap_read.c

@ -0,0 +1,19 @@
#include <sys/types.h>
#include <unistd.h>
#include <sys/mman.h>
#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;
}

19
mmap/mmap_shared.c

@ -0,0 +1,19 @@
#include <sys/types.h>
#include <unistd.h>
#include <sys/mman.h>
#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;
}

6
t.c

@ -7,15 +7,21 @@
#include "socket.h"
#include "buffer.h"
#include "ip4.h"
#include "mmap.h"
#include <stdio.h>
#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;

Loading…
Cancel
Save