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.63 by root, Mon Oct 23 23:48:31 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;
224#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
212 pthread_mutex_lock (&reslock); 225 pthread_mutex_lock (&reslock);
226#endif
213 req = ress; 227 req = ress;
228#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
214 pthread_mutex_unlock (&reslock); 229 pthread_mutex_unlock (&reslock);
230#endif
215 231
216 if (req) 232 if (req)
217 return; 233 return;
218 234
219 FD_ZERO(&rfd); 235 FD_ZERO(&rfd);
590#if !HAVE_READAHEAD 606#if !HAVE_READAHEAD
591# define readahead aio_readahead 607# define readahead aio_readahead
592 608
593static ssize_t readahead (int fd, off_t offset, size_t count) 609static ssize_t readahead (int fd, off_t offset, size_t count)
594{ 610{
595 char readahead_buf[4096]; 611 dBUF;
596 612
597 while (count > 0) 613 while (count > 0)
598 { 614 {
599 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 615 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
600 616
601 pread (fd, readahead_buf, len, offset); 617 pread (fd, aio_buf, len, offset);
602 offset += len; 618 offset += len;
603 count -= len; 619 count -= len;
604 } 620 }
621
622 fBUF;
605 623
606 errno = 0; 624 errno = 0;
607} 625}
608#endif 626#endif
609 627
695#endif 713#endif
696 ) 714 )
697 ) 715 )
698 { 716 {
699 /* emulate sendfile. this is a major pain in the ass */ 717 /* emulate sendfile. this is a major pain in the ass */
700 char buf[4096]; 718 dBUF;
719
701 res = 0; 720 res = 0;
702 721
703 while (count) 722 while (count)
704 { 723 {
705 ssize_t cnt; 724 ssize_t cnt;
706 725
707 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 726 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
708 727
709 if (cnt <= 0) 728 if (cnt <= 0)
710 { 729 {
711 if (cnt && !res) res = -1; 730 if (cnt && !res) res = -1;
712 break; 731 break;
713 } 732 }
714 733
715 cnt = write (ofd, buf, cnt); 734 cnt = write (ofd, aio_buf, cnt);
716 735
717 if (cnt <= 0) 736 if (cnt <= 0)
718 { 737 {
719 if (cnt && !res) res = -1; 738 if (cnt && !res) res = -1;
720 break; 739 break;
722 741
723 offset += cnt; 742 offset += cnt;
724 res += cnt; 743 res += cnt;
725 count -= cnt; 744 count -= cnt;
726 } 745 }
746
747 fBUF;
727 } 748 }
728 749
729 return res; 750 return res;
730} 751}
731 752
732/* read a full directory */ 753/* read a full directory */
733static int scandir_ (const char *path, void **namesp) 754static int scandir_ (const char *path, void **namesp)
734{ 755{
735 DIR *dirp = opendir (path); 756 DIR *dirp;
736 union 757 union
737 { 758 {
738 struct dirent d; 759 struct dirent d;
739 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 760 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
740 } u; 761 } *u;
741 struct dirent *entp; 762 struct dirent *entp;
742 char *name, *names; 763 char *name, *names;
743 int memlen = 4096; 764 int memlen = 4096;
744 int memofs = 0; 765 int memofs = 0;
745 int res = 0; 766 int res = 0;
746 int errorno; 767 int errorno;
747 768
769 dirp = opendir (path);
748 if (!dirp) 770 if (!dirp)
749 return -1; 771 return -1;
750 772
773 u = malloc (sizeof (*u));
751 names = malloc (memlen); 774 names = malloc (memlen);
752 775
776 if (u && names)
753 for (;;) 777 for (;;)
754 { 778 {
779 errno = 0;
755 errno = 0, readdir_r (dirp, &u.d, &entp); 780 readdir_r (dirp, &u->d, &entp);
756 781
757 if (!entp) 782 if (!entp)
758 break; 783 break;
759 784
760 name = entp->d_name; 785 name = entp->d_name;
761 786
762 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 787 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
763 { 788 {
764 int len = strlen (name) + 1; 789 int len = strlen (name) + 1;
765 790
766 res++; 791 res++;
767 792
768 while (memofs + len > memlen) 793 while (memofs + len > memlen)
769 { 794 {
770 memlen *= 2; 795 memlen *= 2;
771 names = realloc (names, memlen); 796 names = realloc (names, memlen);
772 if (!names) 797 if (!names)
773 break; 798 break;
774 } 799 }
775 800
776 memcpy (names + memofs, name, len); 801 memcpy (names + memofs, name, len);
777 memofs += len; 802 memofs += len;
778 } 803 }
779 } 804 }
780 805
781 errorno = errno; 806 errorno = errno;
807 free (u);
782 closedir (dirp); 808 closedir (dirp);
783 809
784 if (errorno) 810 if (errorno)
785 { 811 {
786 free (names); 812 free (names);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines