use callback based infrastructure to reset iob's

master
leitner 17 years ago
parent 527efc70b6
commit 8b17690318

@ -1,5 +1,10 @@
#include <stdlib.h>
#include "iob_internal.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) { 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);
} }

@ -1,19 +1,20 @@
#include <stdlib.h> #include <stdlib.h>
#include "iob_internal.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; iob_entry* e;
if (!n) { if (!n) {
if (_free) free((char*)buf);
return 1; return 1;
} }
e=array_allocate(&b->b,sizeof(iob_entry),array_length(&b->b,sizeof(iob_entry))); e=array_allocate(&b->b,sizeof(iob_entry),array_length(&b->b,sizeof(iob_entry)));
if (!e) return 0; if (!e) return 0;
e->type=_free?FROMBUF_FREE:FROMBUF; e->type=FROMBUF;
e->fd=-1; e->fd=-1;
e->buf=buf; e->buf=buf;
e->n=n; e->n=n;
e->offset=0; e->offset=0;
e->cleanup=cleanup;
b->bytesleft+=n; b->bytesleft+=n;
++b->bufs; ++b->bufs;
return 1; return 1;

@ -1,5 +1,11 @@
#include <sys/types.h>
#include <sys/mman.h>
#include "iob_internal.h" #include "iob_internal.h"
int iob_addbuf_free(io_batch* b,const void* buf,uint64 n) { static void cleanup(struct iob_entry* x) {
return iob_addbuf_internal(b,buf,n,2); 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);
} }

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

@ -1,5 +1,9 @@
#include "iob_internal.h" #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) { int iob_addfile_close(io_batch* b,int64 fd,uint64 off,uint64 n) {
iob_entry* e; iob_entry* e;
if (n==0) { 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), e=array_allocate(&b->b,sizeof(iob_entry),
array_length(&b->b,sizeof(iob_entry))); array_length(&b->b,sizeof(iob_entry)));
if (!e) return 0; if (!e) return 0;
e->type=FROMFILE_CLOSE; e->type=FROMFILE;
e->fd=fd; e->fd=fd;
e->buf=0; e->buf=0;
e->n=n; e->n=n;
e->offset=off; e->offset=off;
e->cleanup=cleanup;
b->bytesleft+=n; b->bytesleft+=n;
++b->files; ++b->files;
return 1; return 1;

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

@ -8,18 +8,8 @@ void iob_reset(io_batch* b) {
iob_entry* x=array_start(&b->b); iob_entry* x=array_start(&b->b);
l=array_length(&b->b,sizeof(iob_entry)); l=array_length(&b->b,sizeof(iob_entry));
for (i=0; i<l; ++i) { for (i=0; i<l; ++i) {
switch (x[i].type) { if (x[i].cleanup)
case FROMBUF_FREE: x[i].cleanup(x+i);
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;
}
} }
array_reset(&b->b); array_reset(&b->b);
byte_zero(b,sizeof(*b)); byte_zero(b,sizeof(*b));

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

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

@ -2,10 +2,12 @@
#include "array.h" #include "array.h"
typedef struct iob_entry { typedef struct iob_entry {
enum { FROMBUF, FROMBUF_FREE, FROMBUF_MUNMAP, FROMFILE, FROMFILE_CLOSE } type; enum { FROMBUF, FROMFILE } type;
int64 fd; int64 fd;
const char* buf; const char* buf;
uint64 offset,n; uint64 offset,n;
void (*cleanup)(struct iob_entry* x);
} iob_entry; } 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