ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
(Generate patch)

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.64 by root, Mon Oct 23 23:54:41 2006 UTC vs.
Revision 1.65 by root, Tue Oct 24 00:26:32 2006 UTC

46# define NAME_MAX 4096 46# define NAME_MAX 4096
47#endif 47#endif
48 48
49#if __ia64 49#if __ia64
50# define STACKSIZE 65536 50# define STACKSIZE 65536
51#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
52# define STACKSIZE PTHREAD_STACK_MIN
51#else 53#else
52# define STACKSIZE 8192 54# define STACKSIZE 16384
53#endif 55#endif
56
57/* buffer size for various temporary buffers */
58#define AIO_BUFSIZE 65536
59
60#define dBUF \
61 char *aio_buf = malloc (AIO_BUFSIZE); \
62 if (!aio_buf) \
63 return -1;
64
65#define fBUF free (aio_buf)
54 66
55enum { 67enum {
56 REQ_QUIT, 68 REQ_QUIT,
57 REQ_OPEN, REQ_CLOSE, 69 REQ_OPEN, REQ_CLOSE,
58 REQ_READ, REQ_WRITE, REQ_READAHEAD, 70 REQ_READ, REQ_WRITE, REQ_READAHEAD,
207 fd_set rfd; 219 fd_set rfd;
208 220
209 while (nreqs) 221 while (nreqs)
210 { 222 {
211 aio_req req; 223 aio_req req;
212#if !(__x86 || __x86_64) /* safe without sempahore on this archs */ 224#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
213 pthread_mutex_lock (&reslock); 225 pthread_mutex_lock (&reslock);
214#endif 226#endif
215 req = ress; 227 req = ress;
216#if !(__x86 || __x86_64) /* safe without sempahore on this archs */ 228#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
217 pthread_mutex_unlock (&reslock); 229 pthread_mutex_unlock (&reslock);
218#endif 230#endif
219 231
220 if (req) 232 if (req)
221 return; 233 return;
594#if !HAVE_READAHEAD 606#if !HAVE_READAHEAD
595# define readahead aio_readahead 607# define readahead aio_readahead
596 608
597static ssize_t readahead (int fd, off_t offset, size_t count) 609static ssize_t readahead (int fd, off_t offset, size_t count)
598{ 610{
599 char readahead_buf[4096]; 611 dBUF;
600 612
601 while (count > 0) 613 while (count > 0)
602 { 614 {
603 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 615 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
604 616
605 pread (fd, readahead_buf, len, offset); 617 pread (fd, aio_buf, len, offset);
606 offset += len; 618 offset += len;
607 count -= len; 619 count -= len;
608 } 620 }
621
622 fBUF;
609 623
610 errno = 0; 624 errno = 0;
611} 625}
612#endif 626#endif
613 627
699#endif 713#endif
700 ) 714 )
701 ) 715 )
702 { 716 {
703 /* emulate sendfile. this is a major pain in the ass */ 717 /* emulate sendfile. this is a major pain in the ass */
704 char buf[4096]; 718 dBUF;
719
705 res = 0; 720 res = 0;
706 721
707 while (count) 722 while (count)
708 { 723 {
709 ssize_t cnt; 724 ssize_t cnt;
710 725
711 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 726 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
712 727
713 if (cnt <= 0) 728 if (cnt <= 0)
714 { 729 {
715 if (cnt && !res) res = -1; 730 if (cnt && !res) res = -1;
716 break; 731 break;
717 } 732 }
718 733
719 cnt = write (ofd, buf, cnt); 734 cnt = write (ofd, aio_buf, cnt);
720 735
721 if (cnt <= 0) 736 if (cnt <= 0)
722 { 737 {
723 if (cnt && !res) res = -1; 738 if (cnt && !res) res = -1;
724 break; 739 break;
726 741
727 offset += cnt; 742 offset += cnt;
728 res += cnt; 743 res += cnt;
729 count -= cnt; 744 count -= cnt;
730 } 745 }
746
747 fBUF;
731 } 748 }
732 749
733 return res; 750 return res;
734} 751}
735 752
736/* read a full directory */ 753/* read a full directory */
737static int scandir_ (const char *path, void **namesp) 754static int scandir_ (const char *path, void **namesp)
738{ 755{
739 DIR *dirp = opendir (path); 756 DIR *dirp;
740 union 757 union
741 { 758 {
742 struct dirent d; 759 struct dirent d;
743 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 760 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
744 } u; 761 } *u;
745 struct dirent *entp; 762 struct dirent *entp;
746 char *name, *names; 763 char *name, *names;
747 int memlen = 4096; 764 int memlen = 4096;
748 int memofs = 0; 765 int memofs = 0;
749 int res = 0; 766 int res = 0;
750 int errorno; 767 int errorno;
751 768
769 dirp = opendir (path);
752 if (!dirp) 770 if (!dirp)
753 return -1; 771 return -1;
754 772
773 u = malloc (sizeof (*u));
755 names = malloc (memlen); 774 names = malloc (memlen);
756 775
776 if (u && names)
757 for (;;) 777 for (;;)
758 { 778 {
779 errno = 0;
759 errno = 0, readdir_r (dirp, &u.d, &entp); 780 readdir_r (dirp, &u->d, &entp);
760 781
761 if (!entp) 782 if (!entp)
762 break; 783 break;
763 784
764 name = entp->d_name; 785 name = entp->d_name;
765 786
766 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 787 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
767 { 788 {
768 int len = strlen (name) + 1; 789 int len = strlen (name) + 1;
769 790
770 res++; 791 res++;
771 792
772 while (memofs + len > memlen) 793 while (memofs + len > memlen)
773 { 794 {
774 memlen *= 2; 795 memlen *= 2;
775 names = realloc (names, memlen); 796 names = realloc (names, memlen);
776 if (!names) 797 if (!names)
777 break; 798 break;
778 } 799 }
779 800
780 memcpy (names + memofs, name, len); 801 memcpy (names + memofs, name, len);
781 memofs += len; 802 memofs += len;
782 } 803 }
783 } 804 }
784 805
785 errorno = errno; 806 errorno = errno;
807 free (u);
786 closedir (dirp); 808 closedir (dirp);
787 809
788 if (errorno) 810 if (errorno)
789 { 811 {
790 free (names); 812 free (names);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines