--- IO-AIO/AIO.xs 2006/10/23 23:54:41 1.64 +++ IO-AIO/AIO.xs 2006/10/24 00:26:32 1.65 @@ -48,10 +48,22 @@ #if __ia64 # define STACKSIZE 65536 +#elif __i386 || __x86_64 /* 16k is unreasonably high :( */ +# define STACKSIZE PTHREAD_STACK_MIN #else -# define STACKSIZE 8192 +# define STACKSIZE 16384 #endif +/* buffer size for various temporary buffers */ +#define AIO_BUFSIZE 65536 + +#define dBUF \ + char *aio_buf = malloc (AIO_BUFSIZE); \ + if (!aio_buf) \ + return -1; + +#define fBUF free (aio_buf) + enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, @@ -209,11 +221,11 @@ while (nreqs) { aio_req req; -#if !(__x86 || __x86_64) /* safe without sempahore on this archs */ +#if !(__i386 || __x86_64) /* safe without sempahore on this archs */ pthread_mutex_lock (&reslock); #endif req = ress; -#if !(__x86 || __x86_64) /* safe without sempahore on this archs */ +#if !(__i386 || __x86_64) /* safe without sempahore on this archs */ pthread_mutex_unlock (&reslock); #endif @@ -596,17 +608,19 @@ static ssize_t readahead (int fd, off_t offset, size_t count) { - char readahead_buf[4096]; + dBUF; while (count > 0) { - size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); + size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; - pread (fd, readahead_buf, len, offset); + pread (fd, aio_buf, len, offset); offset += len; count -= len; } + fBUF; + errno = 0; } #endif @@ -701,14 +715,15 @@ ) { /* emulate sendfile. this is a major pain in the ass */ - char buf[4096]; + dBUF; + res = 0; while (count) { ssize_t cnt; - cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); + cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset); if (cnt <= 0) { @@ -716,7 +731,7 @@ break; } - cnt = write (ofd, buf, cnt); + cnt = write (ofd, aio_buf, cnt); if (cnt <= 0) { @@ -728,6 +743,8 @@ res += cnt; count -= cnt; } + + fBUF; } return res; @@ -736,12 +753,12 @@ /* read a full directory */ static int scandir_ (const char *path, void **namesp) { - DIR *dirp = opendir (path); + DIR *dirp; union { struct dirent d; char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; - } u; + } *u; struct dirent *entp; char *name, *names; int memlen = 4096; @@ -749,40 +766,45 @@ int res = 0; int errorno; + dirp = opendir (path); if (!dirp) return -1; + u = malloc (sizeof (*u)); names = malloc (memlen); - for (;;) - { - errno = 0, readdir_r (dirp, &u.d, &entp); + if (u && names) + for (;;) + { + errno = 0; + readdir_r (dirp, &u->d, &entp); - if (!entp) - break; + if (!entp) + break; - name = entp->d_name; + name = entp->d_name; - if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) - { - int len = strlen (name) + 1; + if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) + { + int len = strlen (name) + 1; - res++; + res++; - while (memofs + len > memlen) - { - memlen *= 2; - names = realloc (names, memlen); - if (!names) - break; - } + while (memofs + len > memlen) + { + memlen *= 2; + names = realloc (names, memlen); + if (!names) + break; + } - memcpy (names + memofs, name, len); - memofs += len; - } - } + memcpy (names + memofs, name, len); + memofs += len; + } + } errorno = errno; + free (u); closedir (dirp); if (errorno)