Browse Source

add iob_free and man page for iob_reset.

check in some windoze compat crap (still does not compile through for
windoze)
master
Felix von Leitner 17 years ago
parent
commit
61a51950d4
  1. 1
      CHANGES
  2. 11
      buffer/buffer_close.c
  3. 9
      buffer/errmsg_cvt.c
  4. 19
      buffer/errmsg_info.c
  5. 2
      buffer/errmsg_infosys.c
  6. 19
      buffer/errmsg_warn.c
  7. 2
      buffer/errmsg_warnsys.c
  8. 18
      buffer/errmsg_writesys.c
  9. 9
      io/io_close.c
  10. 4
      io/io_fd.c
  11. 28
      io/io_mmapwritefile.c
  12. 10
      io/io_passfd.c
  13. 10
      io/io_receivefd.c
  14. 2
      io/io_sigpipe.c
  15. 4
      io/io_socketpair.c
  16. 13
      io/iob_free.3
  17. 7
      io/iob_free.c
  18. 16
      io/iob_reset.3
  19. 2
      io_internal.h
  20. 1
      iob.h
  21. 11
      test/b64encode.c

1
CHANGES

@ -6,6 +6,7 @@
' '; needed for web servers, so they can serve libstdc++.tar.gz)
fix iob_write to handle failure properly
document that the iob_write callback should limit itself
add iob_free, add man pages for iob_free and iob_reset
0.21:
errno cleanup and man page updates (Rolf Eike Beer)

11
buffer/buffer_close.c

@ -1,13 +1,22 @@
#include <buffer.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef __MINGW32__
#include <windows.h>
#else
#include <sys/mman.h>
#endif
void buffer_close(buffer* b) {
if (b->fd != -1) close(b->fd);
switch (b->todo) {
case FREE: free(b->x); break;
case MUNMAP: munmap(b->x,b->a); break;
case MUNMAP:
#ifdef __MINGW32__
UnmapViewOfFile(b->x);
#else
munmap(b->x,b->a); break;
#endif
default: ;
}
}

9
buffer/errmsg_cvt.c

@ -1,6 +1,15 @@
#include <stdarg.h>
#include <sys/types.h>
#ifdef __MINGW32__
#include "windows.h"
struct iovec {
LPCVOID iov_base;
DWORD iov_len;
};
#else
#include <sys/uio.h>
#endif
#include "errmsg.h"
#include "str.h"

19
buffer/errmsg_info.c

@ -1,6 +1,16 @@
#include <stdarg.h>
#include <sys/types.h>
#ifdef __MINGW32__
#include "windows.h"
#include <unistd.h>
struct iovec {
LPCVOID iov_base;
DWORD iov_len;
};
#else
#include <sys/uio.h>
#endif
#include "errmsg.h"
#include "str.h"
@ -10,6 +20,15 @@ void errmsg_info(const char* message, ...) {
struct iovec x[23];
va_list a;
va_start(a,message);
#ifdef __MINGW32__
{
int i,j;
j=errmsg_cvt(x,message,a);
for (i=0; i<j; ++i)
write(1,x[i].iov_base,x[i].iov_len);
}
#else
writev(1,x,errmsg_cvt(x,message,a));
#endif
va_end(a);
}

2
buffer/errmsg_infosys.c

@ -1,12 +1,10 @@
#include <stdarg.h>
#include <sys/types.h>
#include <sys/uio.h>
#include "errmsg.h"
#include "str.h"
#include <string.h>
#include <errno.h>
extern int errmsg_cvt(struct iovec* x,const char* message, va_list a);
extern void errmsg_writesys(int fd,const char* message,va_list list);
void errmsg_infosys(const char* message, ...) {

19
buffer/errmsg_warn.c

@ -1,6 +1,16 @@
#include <stdarg.h>
#include <sys/types.h>
#ifdef __MINGW32__
#include "windows.h"
#include <unistd.h>
struct iovec {
LPCVOID iov_base;
DWORD iov_len;
};
#else
#include <sys/uio.h>
#endif
#include "errmsg.h"
#include "str.h"
@ -10,6 +20,15 @@ void errmsg_warn(const char* message, ...) {
struct iovec x[23];
va_list a;
va_start(a,message);
#ifdef __MINGW32__
{
int i,j;
j=errmsg_cvt(x,message,a);
for (i=0; i<j; ++i)
write(2,x[i].iov_base,x[i].iov_len);
}
#else
writev(2,x,errmsg_cvt(x,message,a));
#endif
va_end(a);
}

2
buffer/errmsg_warnsys.c

@ -1,12 +1,10 @@
#include <stdarg.h>
#include <sys/types.h>
#include <sys/uio.h>
#include "errmsg.h"
#include "str.h"
#include <string.h>
#include <errno.h>
extern int errmsg_cvt(struct iovec* x,const char* message, va_list a);
extern void errmsg_writesys(int fd,const char* message,va_list list);
void errmsg_warnsys(const char* message, ...) {

18
buffer/errmsg_writesys.c

@ -1,6 +1,16 @@
#include <stdarg.h>
#include <sys/types.h>
#ifdef __MINGW32__
#include "windows.h"
#include <unistd.h>
struct iovec {
LPCVOID iov_base;
DWORD iov_len;
};
#else
#include <sys/uio.h>
#endif
#include "errmsg.h"
#include "str.h"
#include <string.h>
@ -22,5 +32,13 @@ void errmsg_writesys(int fd,const char* message,va_list list) {
x[i+1].iov_base="\n";
x[i+1].iov_len=1;
#ifdef __MINGW32__
{
int j;
for (j=0; j<i+2; ++j)
write(2,x[j].iov_base,x[j].iov_len);
}
#else
writev(2,x,i+2);
#endif
}

9
io/io_close.c

@ -1,6 +1,10 @@
#include <unistd.h>
#include <sys/types.h>
#ifdef __MINGW32__
#include <windows.h>
#else
#include <sys/mman.h>
#endif
#include "io_internal.h"
void io_close(int64 d) {
@ -10,7 +14,12 @@ void io_close(int64 d) {
io_dontwantread(d);
io_dontwantwrite(d);
if (e->mmapped) {
#ifdef __MINGW32__
UnmapViewOfFile(e->mmapped);
CloseHandle(e->mh);
#else
munmap(e->mmapped,e->maplen);
#endif
e->mmapped=0;
}
}

4
io/io_fd.c

@ -54,7 +54,9 @@ int io_fd(int64 d) {
if (!(e=array_allocate(&io_fds,sizeof(io_entry),d))) return 0;
byte_zero(e,sizeof(io_entry));
e->inuse=1;
#ifndef __MINGW32__
#ifdef __MINGW32__
e->mh=0;
#else
if (r&O_NDELAY) e->nonblock=1;
#endif
e->next_read=e->next_write=-1;

28
io/io_mmapwritefile.c

@ -2,7 +2,11 @@
#include <iob.h>
#include <unistd.h>
#include <sys/types.h>
#ifdef __MINGW32__
#include <windows.h>
#else
#include <sys/mman.h>
#endif
#define BUFSIZE 16384
@ -14,19 +18,33 @@ int64 io_mmapwritefile(int64 out,int64 in,uint64 off,uint64 bytes,io_write_callb
if (e) {
const char* c;
long left;
#ifdef __MINGW32__
if (!e->mh) e->mh=CreateFileMapping(out,0,PAGE_READONLY,0,0,NULL);
if (!e->mh) goto readwrite;
#endif
do {
if (e->mmapped) {
/* did we already map the right chunk? */
if (off>=e->mapofs && off<e->mapofs+e->maplen)
goto mapok; /* ok; mmapped the right chunk*/
#ifdef __MINGW32__
UnmapViewOfFile(e->mmapped);
#else
munmap(e->mmapped,e->maplen);
#endif
}
e->mapofs=off&0xffffffffffff0000ull;
if (e->mapofs+0x10000>off+bytes)
e->maplen=off+bytes-e->mapofs;
else
e->maplen=0x10000;
if ((e->mmapped=mmap(0,e->maplen,PROT_READ,MAP_SHARED,in,e->mapofs))==MAP_FAILED) {
#ifdef __MINGW32__
if ((e->mmapped=MapViewOfFile(e->mh,FILE_MAP_READ,(DWORD)(e->mapofs>>32),
(DWORD)e->mapofs,e->maplen))==0)
#else
if ((e->mmapped=mmap(0,e->maplen,PROT_READ,MAP_SHARED,in,e->mapofs))==MAP_FAILED)
#endif
{
e->mmapped=0;
goto readwrite;
}
@ -40,7 +58,11 @@ int64 io_mmapwritefile(int64 out,int64 in,uint64 off,uint64 bytes,io_write_callb
e->canwrite=0;
e->next_write=-1;
if (errno!=EAGAIN) {
#ifdef __MINGW32__
UnmapViewOfFile(e->mmapped);
#else
munmap(e->mmapped,e->maplen);
#endif
e->mmapped=0;
return -3;
}
@ -55,7 +77,11 @@ int64 io_mmapwritefile(int64 out,int64 in,uint64 off,uint64 bytes,io_write_callb
}
} while (bytes);
if (e->mmapped) {
#ifdef __MINGW32__
UnmapViewOfFile(e->mmapped);
#else
munmap(e->mmapped,e->maplen);
#endif
e->mmapped=0;
}
return sent;

10
io/io_passfd.c

@ -1,3 +1,12 @@
#ifdef __MINGW32__
#include "io_internal.h"
#include <errno.h>
int io_passfd(int64 sock,int64 fd) {
errno=EINVAL;
return -1;
}
#else
#include <stddef.h>
#include <sys/types.h>
#include <sys/socket.h>
@ -40,3 +49,4 @@ int io_passfd(int64 sock,int64 fd) {
#endif
return sendmsg(sock,&msg,0)>=0?0:-1;
}
#endif

10
io/io_receivefd.c

@ -1,3 +1,12 @@
#ifdef __MINGW32__
#include <errno.h>
#include "io_internal.h"
int64 io_receivefd(int64 sock) {
errno=EINVAL;
return -1;
}
#else
#include <stddef.h>
#include <sys/types.h>
#include <sys/socket.h>
@ -60,3 +69,4 @@ int64 io_receivefd(int64 sock) {
return fd;
#endif
}
#endif

2
io/io_sigpipe.c

@ -5,9 +5,11 @@
/* this is an internal function, called by io_trywrite and io_waitwrite */
void io_sigpipe(void) {
#ifndef __MINGW32__
static int isitdone;
if (!isitdone) {
signal(SIGPIPE,SIG_IGN);
isitdone=1;
}
#endif
}

4
io/io_socketpair.c

@ -1,12 +1,16 @@
#include <sys/types.h>
#include <unistd.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "windoze.h"
#include <errno.h>
#include "io_internal.h"
int io_socketpair(int64* d) {
int fds[2];
__winsock_init();
if (socketpair(AF_UNIX,SOCK_STREAM,0,fds)==-1)
if (socketpair(AF_INET6,SOCK_STREAM,IPPROTO_TCP,fds)==-1)
if (socketpair(AF_INET,SOCK_STREAM,IPPROTO_TCP,fds)==-1)

13
io/iob_free.3

@ -0,0 +1,13 @@
.TH iob_free 3
.SH NAME
iob_free \- free an I/O batch
.SH SYNTAX
.B #include <iob.h>
void \fBiob_free\fP(io_batch* b);
.SH DESCRIPTION
iob_free frees all resources associated with \fIb\fR. Calling iob_free
is equivalent to calling iob_reset and free.
avoiding unnecessary copying (using writev).
.SH "SEE ALSO"
iob_new(3), iob_reset(3)

7
io/iob_free.c

@ -0,0 +1,7 @@
#include <stdlib.h>
#include "iob_internal.h"
void iob_free(io_batch* b) {
iob_reset(b);
free(b);
}

16
io/iob_reset.3

@ -0,0 +1,16 @@
.TH iob_reset 3
.SH NAME
iob_reset \- reset an I/O batch
.SH SYNTAX
.B #include <iob.h>
void \fBiob_reset\fP(io_batch* b);
.SH DESCRIPTION
iob_free empties the list of transactions in an I/O batch. Files added
with iob_addfile_close are closed, and buffer added with iob_addbuf_free
of iob_adds_free are freed.
The I/O batch itself is not freed. You can start adding transactions to
it again.
.SH "SEE ALSO"
iob_new(3), iob_reset(3)

2
io_internal.h

@ -30,7 +30,7 @@ typedef struct {
uint64 mapofs;
#ifdef __MINGW32__
OVERLAPPED o;
HANDLE fd;
HANDLE /* fd, */ mh;
#endif
} io_entry;

1
iob.h

@ -31,6 +31,7 @@ int iob_addfile_close(io_batch* b,int64 fd,uint64 off,uint64 n);
int64 iob_send(int64 s,io_batch* b);
int64 iob_write(int64 s,io_batch* b,io_write_callback cb);
void iob_reset(io_batch* b);
void iob_free(io_batch* b);
void iob_prefetch(io_batch* b,uint64 bytes);
#endif

11
test/b64encode.c

@ -4,9 +4,9 @@
#include "textcode.h"
#include "havealloca.h"
void b64encode(const char* c) {
char* buf=alloca(strlen(c)*2+4);
buffer_put(buffer_1,buf,fmt_base64(buf,c,strlen(c)));
void b64encode(const char* c,long len) {
char* buf=alloca(len*2+4);
buffer_put(buffer_1,buf,fmt_base64(buf,c,len));
if (isatty(1))
buffer_putnlflush(buffer_1);
else
@ -16,15 +16,14 @@ void b64encode(const char* c) {
int main(int argc,char* argv[]) {
int i;
for (i=1; i<argc; ++i) {
b64encode(argv[i]);
b64encode(argv[i],strlen(argv[i]));
}
if (argc<2) {
char src[1024];
int len;
while ((len=read(0,src,sizeof(src)-1))>0) {
if (len==-1) return(1);
src[len]=0;
b64encode(src);
b64encode(src,len);
}
}
return 0;

Loading…
Cancel
Save