From 8b1769031821cfbc7550219f7d7044f066bc89ac Mon Sep 17 00:00:00 2001 From: leitner Date: Wed, 17 Oct 2007 13:25:44 +0000 Subject: [PATCH] use callback based infrastructure to reset iob's --- io/iob_addbuf_free.c | 7 ++++++- io/iob_addbuf_internal.c | 7 ++++--- io/iob_addbuf_munmap.c | 10 ++++++++-- io/iob_addfile.c | 1 + io/iob_addfile_close.c | 7 ++++++- io/iob_prefetch.c | 2 +- io/iob_reset.c | 14 ++------------ io/iob_send.c | 10 +++++----- io/iob_write.c | 2 +- iob_internal.h | 6 ++++-- 10 files changed, 38 insertions(+), 28 deletions(-) diff --git a/io/iob_addbuf_free.c b/io/iob_addbuf_free.c index 7b0242e..c9a21d4 100644 --- a/io/iob_addbuf_free.c +++ b/io/iob_addbuf_free.c @@ -1,5 +1,10 @@ +#include #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); } diff --git a/io/iob_addbuf_internal.c b/io/iob_addbuf_internal.c index dc7af5a..69b9b2c 100644 --- a/io/iob_addbuf_internal.c +++ b/io/iob_addbuf_internal.c @@ -1,19 +1,20 @@ #include #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; diff --git a/io/iob_addbuf_munmap.c b/io/iob_addbuf_munmap.c index bd8f3c5..10b09b0 100644 --- a/io/iob_addbuf_munmap.c +++ b/io/iob_addbuf_munmap.c @@ -1,5 +1,11 @@ +#include +#include #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); } diff --git a/io/iob_addfile.c b/io/iob_addfile.c index dead5a2..b52a28f 100644 --- a/io/iob_addfile.c +++ b/io/iob_addfile.c @@ -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; diff --git a/io/iob_addfile_close.c b/io/iob_addfile_close.c index 764fa64..53541c9 100644 --- a/io/iob_addfile_close.c +++ b/io/iob_addfile_close.c @@ -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; diff --git a/io/iob_prefetch.c b/io/iob_prefetch.c index 5a15f14..e50e0cc 100644 --- a/io/iob_prefetch.c +++ b/io/iob_prefetch.c @@ -22,7 +22,7 @@ void iob_prefetch(io_batch* b,uint64 bytes) { e=(iob_entry*)array_start(&b->b); if (!e) return; for (; etype==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); diff --git a/io/iob_reset.c b/io/iob_reset.c index 027a3b0..0ccf241 100644 --- a/io/iob_reset.c +++ b/io/iob_reset.c @@ -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; ib); byte_zero(b,sizeof(*b)); diff --git a/io/iob_send.c b/io/iob_send.c index 17bff80..3b69bde 100644 --- a/io/iob_send.c +++ b/io/iob_send.c @@ -45,10 +45,10 @@ int64 iob_send(int64 s,io_batch* b) { for (i=0; x+isendfilequeued=1; @@ -144,18 +144,18 @@ int64 iob_send(int64 s,io_batch* b) { headers=trailers=0; #endif for (i=0; e+i