Browse Source

use callback based infrastructure to reset iob's

master
Felix von Leitner 13 years ago
parent
commit
92b23094e1
10 changed files with 38 additions and 28 deletions
  1. +6
    -1
      io/iob_addbuf_free.c
  2. +4
    -3
      io/iob_addbuf_internal.c
  3. +8
    -2
      io/iob_addbuf_munmap.c
  4. +1
    -0
      io/iob_addfile.c
  5. +6
    -1
      io/iob_addfile_close.c
  6. +1
    -1
      io/iob_prefetch.c
  7. +2
    -12
      io/iob_reset.c
  8. +5
    -5
      io/iob_send.c
  9. +1
    -1
      io/iob_write.c
  10. +4
    -2
      iob_internal.h

+ 6
- 1
io/iob_addbuf_free.c View File

@@ -1,5 +1,10 @@
#include <stdlib.h>
#include "iob_internal.h"

static void cleanup(struct iob_entry* x) {
free((char*)x->buf);
}

int iob_addbuf_free(io_batch* b,const void* buf,uint64 n) {
return iob_addbuf_internal(b,buf,n,1);
return iob_addbuf_internal(b,buf,n,cleanup);
}

+ 4
- 3
io/iob_addbuf_internal.c View File

@@ -1,19 +1,20 @@
#include <stdlib.h>
#include "iob_internal.h"

int iob_addbuf_internal(io_batch* b,const void* buf,uint64 n,int _free) {
int iob_addbuf_internal(io_batch* b,const void* buf,uint64 n,
void (*cleanup)(struct iob_entry* x)) {
iob_entry* e;
if (!n) {
if (_free) free((char*)buf);
return 1;
}
e=array_allocate(&b->b,sizeof(iob_entry),array_length(&b->b,sizeof(iob_entry)));
if (!e) return 0;
e->type=_free?FROMBUF_FREE:FROMBUF;
e->type=FROMBUF;
e->fd=-1;
e->buf=buf;
e->n=n;
e->offset=0;
e->cleanup=cleanup;
b->bytesleft+=n;
++b->bufs;
return 1;


+ 8
- 2
io/iob_addbuf_munmap.c View File

@@ -1,5 +1,11 @@
#include <sys/types.h>
#include <sys/mman.h>
#include "iob_internal.h"

int iob_addbuf_free(io_batch* b,const void* buf,uint64 n) {
return iob_addbuf_internal(b,buf,n,2);
static void cleanup(struct iob_entry* x) {
munmap((char*)x->buf,x->n);
}

int iob_addbuf_munmap(io_batch* b,const void* buf,uint64 n) {
return iob_addbuf_internal(b,buf,n,cleanup);
}

+ 1
- 0
io/iob_addfile.c View File

@@ -12,6 +12,7 @@ int iob_addfile(io_batch* b,int64 fd,uint64 off,uint64 n) {
e->buf=0;
e->n=n;
e->offset=off;
e->cleanup=0;
b->bytesleft+=n;
++b->files;
return 1;


+ 6
- 1
io/iob_addfile_close.c View File

@@ -1,5 +1,9 @@
#include "iob_internal.h"

static void cleanup(struct iob_entry* x) {
io_close(x->fd);
}

int iob_addfile_close(io_batch* b,int64 fd,uint64 off,uint64 n) {
iob_entry* e;
if (n==0) {
@@ -10,11 +14,12 @@ int iob_addfile_close(io_batch* b,int64 fd,uint64 off,uint64 n) {
e=array_allocate(&b->b,sizeof(iob_entry),
array_length(&b->b,sizeof(iob_entry)));
if (!e) return 0;
e->type=FROMFILE_CLOSE;
e->type=FROMFILE;
e->fd=fd;
e->buf=0;
e->n=n;
e->offset=off;
e->cleanup=cleanup;
b->bytesleft+=n;
++b->files;
return 1;


+ 1
- 1
io/iob_prefetch.c View File

@@ -22,7 +22,7 @@ void iob_prefetch(io_batch* b,uint64 bytes) {
e=(iob_entry*)array_start(&b->b);
if (!e) return;
for (; e<last; ++e) {
if (e->type==FROMFILE || e->type==FROMFILE_CLOSE) {
if (e->type==FROMFILE) {
#ifdef MADV_WILLNEED
char* c;
c=mmap(0,bytes,PROT_READ,MAP_SHARED,e->fd,(e->offset|4095)+1);


+ 2
- 12
io/iob_reset.c View File

@@ -8,18 +8,8 @@ void iob_reset(io_batch* b) {
iob_entry* x=array_start(&b->b);
l=array_length(&b->b,sizeof(iob_entry));
for (i=0; i<l; ++i) {
switch (x[i].type) {
case FROMBUF_FREE:
free((char*)x[i].buf);
break;
case FROMBUF_MUNMAP:
munmap((char*)x[i].buf,x[i].n);
break;
case FROMFILE_CLOSE:
io_close(x[i].fd);
default:
break;
}
if (x[i].cleanup)
x[i].cleanup(x+i);
}
array_reset(&b->b);
byte_zero(b,sizeof(*b));


+ 5
- 5
io/iob_send.c View File

@@ -45,10 +45,10 @@ int64 iob_send(int64 s,io_batch* b) {
for (i=0; x+i<last; ++i)
if (x[i].n) break;

if (x[i].type==FROMBUF || x[i].type==FROMBUF_FREE || x[i].type==FROMBUF_MUNMAP) {
if (x[i].type==FROMBUF) {
fprintf(stderr,"found non-sent buffer batch entry at %d\n",i);
if (x+i+1 < last &&
(x[i+1].type==FROMFILE || x[i+1].type==FROMFILE_CLOSE)) {
(x[i+1].type==FROMFILE)) {
fprintf(stderr,"Next is a file, can use TransmitFile\n",i);
TRANSMIT_FILE_BUFFERS tfb;
e->sendfilequeued=1;
@@ -144,18 +144,18 @@ int64 iob_send(int64 s,io_batch* b) {
headers=trailers=0;
#endif
for (i=0; e+i<last; ++i) {
if (e[i].type==FROMFILE || e[i].type==FROMFILE_CLOSE) break;
if (e[i].type==FROMFILE) break;
v[i].iov_base=(char*)(e[i].buf+e[i].offset);
v[i].iov_len=e[i].n;
}
headers=i;
#ifdef HAVE_BSDSENDFILE
if (e[i].type==FROMFILE || e[i].type==FROMFILE_CLOSE) {
if (e[i].type==FROMFILE) {
off_t sbytes;
struct sf_hdtr hdr;
int r;
for (++i; e+i<last; ++i) {
if (e[i].type==FROMFILE || e[i].type==FROMFILE_CLOSE) break;
if (e[i].type==FROMFILE) break;
v[i-1].iov_base=(char*)(e[i].buf+e[i].offset);
v[i-1].iov_len=e[i].n;
++trailers;


+ 1
- 1
io/iob_write.c View File

@@ -15,7 +15,7 @@ int64 iob_write(int64 s,io_batch* b,io_write_callback cb) {
thatsit=0;
for (i=0; e+i<last; ++i) {
if (!e[i].n) continue;
if (e[i].type==FROMFILE || e[i].type==FROMFILE_CLOSE)
if (e[i].type==FROMFILE)
sent=io_mmapwritefile(s,e[i].fd,e[i].offset,e[i].n,cb);
else
sent=cb(s,e[i].buf+e[i].offset,e[i].n);


+ 4
- 2
iob_internal.h View File

@@ -2,10 +2,12 @@
#include "array.h"

typedef struct iob_entry {
enum { FROMBUF, FROMBUF_FREE, FROMBUF_MUNMAP, FROMFILE, FROMFILE_CLOSE } type;
enum { FROMBUF, FROMFILE } type;
int64 fd;
const char* buf;
uint64 offset,n;
void (*cleanup)(struct iob_entry* x);
} iob_entry;

int iob_addbuf_internal(io_batch* b,const void* buf,uint64 n,int free);
int iob_addbuf_internal(io_batch* b,const void* buf,uint64 n,
void (*cleanup)(struct iob_entry* x));

Loading…
Cancel
Save