Browse Source

add mmap library.

master
Felix von Leitner 19 years ago
parent
commit
c453a99bff
7 changed files with 94 additions and 5 deletions
  1. +4
    -0
      CHANGES
  2. +8
    -5
      Makefile
  3. +19
    -0
      mmap.h
  4. +19
    -0
      mmap/mmap_private.c
  5. +19
    -0
      mmap/mmap_read.c
  6. +19
    -0
      mmap/mmap_shared.c
  7. +6
    -0
      t.c

+ 4
- 0
CHANGES View File

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


+ 8
- 5
Makefile View File

@@ -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
- 0
mmap.h View File

@@ -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
- 0
mmap/mmap_private.c View File

@@ -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
- 0
mmap/mmap_read.c View File

@@ -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
- 0
mmap/mmap_shared.c View File

@@ -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
- 0
t.c View File

@@ -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