document the return value expected from the callback given to iob_write

remove unused #include in iob_reset
if iob_addfile_close fails, it now closes the fd
if iob_addbuf_munmap fails, it now munmaps the buf
if iob_addbuf_free fails, it now frees the buf
some win32 cross-compile fixes for iarray
master
leitner 13 years ago
parent 4f1e1d300c
commit c2a2a15c12

@ -9,7 +9,11 @@ void* iarray_allocate(iarray* ia,size_t pos) {
return ia->pages[y]+(pos%ia->elemperpage)*ia->elemsize; return ia->pages[y]+(pos%ia->elemperpage)*ia->elemsize;
/* the case where ia->pages == NULL is implicit */ /* the case where ia->pages == NULL is implicit */
#ifdef __MINGW32__
EnterCriticalSection(&ia->cs);
#else
pthread_mutex_lock(&ia->m); pthread_mutex_lock(&ia->m);
#endif
if (__unlikely(y >= ia->pagefence)) { if (__unlikely(y >= ia->pagefence)) {
char** np; char** np;
@ -37,11 +41,19 @@ void* iarray_allocate(iarray* ia,size_t pos) {
* however */ * however */
if (__unlikely(ia->pages[y]==0 && (ia->pages[y]=malloc(ia->bytesperpage))==0)) { if (__unlikely(ia->pages[y]==0 && (ia->pages[y]=malloc(ia->bytesperpage))==0)) {
unlockandfail: unlockandfail:
#ifdef __MINGW32__
LeaveCriticalSection(&ia->cs);
#else
pthread_mutex_unlock(&ia->m); pthread_mutex_unlock(&ia->m);
#endif
return 0; return 0;
} }
#ifdef __MINGW32__
LeaveCriticalSection(&ia->cs);
#else
pthread_mutex_unlock(&ia->m); pthread_mutex_unlock(&ia->m);
#endif
return ia->pages[y] + (pos%ia->elemperpage)*ia->elemsize; return ia->pages[y] + (pos%ia->elemperpage)*ia->elemsize;
} }

@ -6,7 +6,11 @@
#include "uint64.h" #include "uint64.h"
#include <stddef.h> #include <stddef.h>
#ifdef __MINGW32__
#include <windows.h>
#else
#include <pthread.h> #include <pthread.h>
#endif
/* this is an indirect array; it only reallocs the indirect index, not /* this is an indirect array; it only reallocs the indirect index, not
* the whole array. The actual data does not move. So there is no need * the whole array. The actual data does not move. So there is no need
@ -17,7 +21,11 @@ typedef struct {
size_t elemsize,pagefence,elemperpage,bytesperpage; size_t elemsize,pagefence,elemperpage,bytesperpage;
/* pagefence is the number of pages + 1, /* pagefence is the number of pages + 1,
* i.e. the first out of bounds index in "pages" */ * i.e. the first out of bounds index in "pages" */
#ifdef __MINGW32__
CRITICAL_SECTION cs;
#else
pthread_mutex_t m; pthread_mutex_t m;
#endif
} iarray; } iarray;
void iarray_init(iarray* ia,size_t elemsize); void iarray_init(iarray* ia,size_t elemsize);

@ -6,5 +6,8 @@ static void cleanup(struct iob_entry* x) {
} }
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,cleanup); int r=iob_addbuf_internal(b,buf,n,cleanup);
if (r==0)
free((char*)buf);
return r;
} }

@ -1,9 +1,9 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/mman.h> #include "mmap.h"
#include "iob_internal.h" #include "iob_internal.h"
static void cleanup(struct iob_entry* x) { static void cleanup(struct iob_entry* x) {
munmap((char*)x->buf,x->offset+x->n); mmap_unmap((char*)x->buf,x->offset+x->n);
} }
int iob_addbuf_munmap(io_batch* b,const void* buf,uint64 n) { int iob_addbuf_munmap(io_batch* b,const void* buf,uint64 n) {

@ -13,7 +13,10 @@ int iob_addfile_close(io_batch* b,int64 fd,uint64 off,uint64 n) {
io_fd(fd); io_fd(fd);
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) {
io_close(fd);
return 0;
}
e->type=FROMFILE; e->type=FROMFILE;
e->fd=fd; e->fd=fd;
e->buf=0; e->buf=0;

@ -1,5 +1,4 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/mman.h>
#include "byte.h" #include "byte.h"
#include "iob_internal.h" #include "iob_internal.h"

@ -23,9 +23,14 @@ error (for example "connection reset by peer").
The normal usage pattern is using io_wait to know when a descriptor is The normal usage pattern is using io_wait to know when a descriptor is
writable, and then calling iob_write until it returns 0, -1 or -3. writable, and then calling iob_write until it returns 0, -1 or -3.
If it returns 0, terminate the loop (everything was written OK). If it If iob_write returns 0, terminate the loop (everything was written OK). If it
returns -1, call io_wait again. If it returned -3, signal an error. returns -1, call io_wait again. If it returned -3, signal an error.
The callback is supposed to behave like write(2), i.e. return the number
of bytes written, 0 for EOF, -1 for error (iob_write will return -3
then). Return -1 with errno==EAGAIN if using non-blocking I/O when we
need to wait for the next write event. iob_write will then return -1.
.SH NOTE .SH NOTE
iob_write will continue to call your callback until it returns an error. iob_write will continue to call your callback until it returns an error.
So if you are in a state machine, for example a web server using this So if you are in a state machine, for example a web server using this

Loading…
Cancel
Save