Browse Source

added initial buffer implementation.

master
Felix von Leitner 20 years ago
parent
commit
225f7c6135
20 changed files with 190 additions and 5 deletions
  1. +7
    -3
      Makefile
  2. +12
    -0
      buffer/buffer_0.c
  3. +12
    -0
      buffer/buffer_0small.c
  4. +7
    -0
      buffer/buffer_1.c
  5. +7
    -0
      buffer/buffer_1small.c
  6. +14
    -0
      buffer/buffer_feed.c
  7. +10
    -0
      buffer/buffer_flush.c
  8. +12
    -0
      buffer/buffer_get.c
  9. +9
    -0
      buffer/buffer_init.c
  10. +5
    -0
      buffer/buffer_peek.c
  11. +17
    -0
      buffer/buffer_put.c
  12. +16
    -0
      buffer/buffer_putalign.c
  13. +7
    -0
      buffer/buffer_putflush.c
  14. +6
    -0
      buffer/buffer_puts.c
  15. +6
    -0
      buffer/buffer_putsalign.c
  16. +6
    -0
      buffer/buffer_putsflush.c
  17. +15
    -0
      buffer/buffer_stubborn.c
  18. +12
    -0
      buffer/buffer_stubborn2.c
  19. +5
    -2
      socket/socket_connect6.c
  20. +5
    -0
      t.c

+ 7
- 3
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
all: t byte.a fmt.a scan.a str.a uint.a open.a stralloc.a unix.a socket.a buffer.a

VPATH=str:byte:fmt:scan:uint:open:stralloc:unix:socket
VPATH=str:byte:fmt:scan:uint:open:stralloc:unix:socket:buffer

CC=egcc
#CFLAGS=-I. -pipe -Wall -Os -march=pentiumpro -fomit-frame-pointer -fschedule-insns2 -Wall
@@ -17,6 +17,7 @@ OPEN_OBJS=$(patsubst open/%.c,%.o,$(wildcard open/*.c))
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))

$(BYTE_OBJS): byte.h
$(FMT_OBJS): fmt.h
@@ -25,6 +26,7 @@ $(STR_OBJS): str.h
$(UINT_OBJS): uint16.h uint32.h
$(STRA_OBJS): stralloc.h
$(SOCKET_OBJS): socket.h
$(BUFFER_OBJS): buffer.h

byte.a: $(BYTE_OBJS)
fmt.a: $(FMT_OBJS)
@@ -35,11 +37,13 @@ open.a: $(OPEN_OBJS)
stralloc.a: $(STRA_OBJS)
unix.a: $(UNIX_OBJS)
socket.a: $(SOCKET_OBJS)
buffer.a: $(BUFFER_OBJS)

%.a:
ar cr $@ $^

t: t.o socket.a stralloc.a str.a fmt.a scan.a str.a uint.a open.a byte.a
t: t.o socket.a stralloc.a fmt.a scan.a uint.a open.a buffer.a str.a \
byte.a
gcc -g -o $@ $^

.PHONY: clean tar


+ 12
- 0
buffer/buffer_0.c View File

@@ -0,0 +1,12 @@
#include <unistd.h>
#include "buffer.h"

static int b0read(int fd,const char* buf, unsigned int len) {
if (buffer_flush(buffer_1)<0) return -1;
return read(fd,buf,len);
}

char buffer_0_space[BUFFER_INSIZE];
static buffer it = BUFFER_INIT(b0read,0,buffer_0_space,sizeof buffer_0_space);
buffer *buffer_0 = &it;


+ 12
- 0
buffer/buffer_0small.c View File

@@ -0,0 +1,12 @@
#include <unistd.h>
#include "buffer.h"

static int b0read(int fd,const char* buf, unsigned int len) {
if (buffer_flush(buffer_1small)<0) return -1;
return read(fd,buf,len);
}

char buffer_0_space[128];
static buffer it = BUFFER_INIT(b0read,0,buffer_0_space,sizeof buffer_0_space);
buffer *buffer_0small = &it;


+ 7
- 0
buffer/buffer_1.c View File

@@ -0,0 +1,7 @@
#include <unistd.h>
#include "buffer.h"

char buffer_1_space[BUFFER_INSIZE];
static buffer it = BUFFER_INIT(write,1,buffer_1_space,sizeof buffer_1_space);
buffer *buffer_1 = &it;


+ 7
- 0
buffer/buffer_1small.c View File

@@ -0,0 +1,7 @@
#include <unistd.h>
#include "buffer.h"

char buffer_1_space[128];
static buffer it = BUFFER_INIT(write,1,buffer_1_space,sizeof buffer_1_space);
buffer *buffer_1small = &it;


+ 14
- 0
buffer/buffer_feed.c View File

@@ -0,0 +1,14 @@
#include "buffer.h"

extern int buffer_stubborn_read(int (*op)(),int fd,const char* buf, unsigned int len);

int buffer_feed(buffer* b) {
if (b->p==b->n) {
int w;
if ((w=buffer_stubborn_read(b->op,b->fd,b->x,b->n))<0)
return -1;
b->n=w;
b->p=0;
}
return b->n;
}

+ 10
- 0
buffer/buffer_flush.c View File

@@ -0,0 +1,10 @@
#include "buffer.h"

extern int buffer_stubborn(int (*op)(),int fd,const char* buf, unsigned int len);

extern int buffer_flush(buffer* b) {
register int p;
if (!(p=b->p)) return 0; /* buffer already empty */
b->p=0;
return buffer_stubborn(b->op,b->fd,b->x,p);
}

+ 12
- 0
buffer/buffer_get.c View File

@@ -0,0 +1,12 @@
#include "byte.h"
#include "buffer.h"

int buffer_get(buffer* b,char* x,unsigned int len) {
int blen;
if ((blen=buffer_feed(b))>=len)
blen=len;
if (blen<=0) return blen;
byte_copy(x,blen,b->x+b->p);
b->p+=blen;
return blen;
}

+ 9
- 0
buffer/buffer_init.c View File

@@ -0,0 +1,9 @@
#include "buffer.h"

void buffer_init(buffer* b,int (*op)(),int fd,char* y,unsigned int ylen) {
b->op=op;
b->fd=fd;
b->x=y;
b->n=ylen;
b->p=0;
}

+ 5
- 0
buffer/buffer_peek.c View File

@@ -0,0 +1,5 @@
#include "buffer.h"

char *buffer_peek(buffer* b) {
return b->x+b->p;
}

+ 17
- 0
buffer/buffer_put.c View File

@@ -0,0 +1,17 @@
#include "byte.h"
#include "buffer.h"

extern int buffer_stubborn(int (*op)(),int fd,const char* buf, unsigned int len);

int buffer_put(buffer* b,const char* buf,unsigned int len) {
if (len>b->n-b->p) { /* doesn't fit */
if (buffer_flush(b)==-1) return -1;
if (len>b->n) {
if (buffer_stubborn(b->op,b->fd,buf,len)<0) return -1;
return 0;
}
}
byte_copy(b->x+b->p, len, buf);
b->p+=len;
return 0;
}

+ 16
- 0
buffer/buffer_putalign.c View File

@@ -0,0 +1,16 @@
#include "byte.h"
#include "buffer.h"

int buffer_putalign(buffer* b,const char* buf,unsigned int len) {
int tmp;
while (len>(tmp=b->n-b->p)) {
byte_copy(b->x+b->p, tmp, buf);
b->p+=tmp;
buf+=tmp;
len-=tmp;
if (buffer_flush(b)<0) return -1;
}
byte_copy(b->x+b->p, len, buf);
b->p+=len;
return 0;
}

+ 7
- 0
buffer/buffer_putflush.c View File

@@ -0,0 +1,7 @@
#include "buffer.h"

int buffer_putflush(buffer* b,const char* x,unsigned int len) {
if (buffer_put(b,x,len)<0) return -1;
if (buffer_flush(b)<0) return -1;
return 0;
}

+ 6
- 0
buffer/buffer_puts.c View File

@@ -0,0 +1,6 @@
#include "str.h"
#include "buffer.h"

int buffer_puts(buffer* b,const char* x) {
buffer_put(b,x,str_len(x));
}

+ 6
- 0
buffer/buffer_putsalign.c View File

@@ -0,0 +1,6 @@
#include "str.h"
#include "buffer.h"

int buffer_puts(buffer* b,const char* x) {
buffer_putalign(b,x,str_len(x));
}

+ 6
- 0
buffer/buffer_putsflush.c View File

@@ -0,0 +1,6 @@
#include "str.h"
#include "buffer.h"

int buffer_puts(buffer* b,const char* x) {
buffer_putflush(b,x,str_len(x));
}

+ 15
- 0
buffer/buffer_stubborn.c View File

@@ -0,0 +1,15 @@
#include <errno.h>
#include "buffer.h"

int buffer_stubborn(int (*op)(),int fd,const char* buf, unsigned int len) {
int w;
while (len) {
if ((w=op(fd,buf,len))<0) {
if (errno == EINTR) continue;
return -1;
};
buf+=w;
len-=w;
}
return 0;
}

+ 12
- 0
buffer/buffer_stubborn2.c View File

@@ -0,0 +1,12 @@
#include <errno.h>
#include "buffer.h"

int buffer_stubborn_read(int (*op)(),int fd,const char* buf, unsigned int len) {
int w;
for (;;) {
if ((w=op(fd,buf,len))<0)
if (errno == EINTR) continue;
}
return w;
}


+ 5
- 2
socket/socket_connect6.c View File

@@ -1,5 +1,8 @@
#include <sys/param.h>
#include "sockaddr_in6.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include "byte.h"
#include "socket.h"
#include "ip6.h"
@@ -29,7 +32,7 @@ int socket_connect6(int s,const char ip[16],uint16 port,uint32 scope_id)

return connect(s,(struct sockaddr *) &sa,sizeof sa);
#else
errno=error_proto;
errno=EPROTO;
return -1;
#endif
}

+ 5
- 0
t.c View File

@@ -5,14 +5,19 @@
#include "uint32.h"
#include "stralloc.h"
#include "socket.h"
#include "buffer.h"

#define rdtscl(low) \
__asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx")

int main(int argc,char* argv[]) {
buffer_puts(buffer_1small,"hello, world\n");
buffer_flush(buffer_1small);
#if 0
int s=socket_tcp4();
char ip[4]={127,0,0,1};
int t=socket_connect4(s,ip,80);
#endif
#if 0
char buf[100]="foo bar baz fnord ";
char buf2[100]="foo braz fnord";


Loading…
Cancel
Save