added initial buffer implementation.

master
leitner 24 years ago
parent 762298de12
commit a50b334f58

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

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

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

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

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

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

@ -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);
}

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

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

@ -0,0 +1,5 @@
#include "buffer.h"
char *buffer_peek(buffer* b) {
return b->x+b->p;
}

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

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

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

@ -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));
}

@ -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));
}

@ -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));
}

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

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

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

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