@@ -790,12 +790,16 @@ poll_dealloc(PyObject *op)
790790#ifdef HAVE_SYS_DEVPOLL_H
791791static PyMethodDef devpoll_methods [];
792792
793+ #define DEVPOLL_IN_BUFFER_SIZE 128
794+ #define DEVPOLL_MAX_OUT_BUFFER_SIZE 1024
795+
793796typedef struct {
794797 PyObject_HEAD
795798 int fd_devpoll ;
796- int max_n_fds ;
797799 int n_fds ;
800+ int out_size ;
798801 struct pollfd * fds ;
802+ struct pollfd * out_fds ;
799803} devpollObject ;
800804
801805#define devpollObject_CAST (op ) ((devpollObject *)(op))
@@ -848,7 +852,7 @@ internal_devpoll_register(devpollObject *self, int fd,
848852 self -> fds [self -> n_fds ].fd = fd ;
849853 self -> fds [self -> n_fds ].events = POLLREMOVE ;
850854
851- if (++ self -> n_fds == self -> max_n_fds ) {
855+ if (++ self -> n_fds == DEVPOLL_IN_BUFFER_SIZE ) {
852856 if (devpoll_flush (self ))
853857 return NULL ;
854858 }
@@ -857,7 +861,7 @@ internal_devpoll_register(devpollObject *self, int fd,
857861 self -> fds [self -> n_fds ].fd = fd ;
858862 self -> fds [self -> n_fds ].events = (signed short )events ;
859863
860- if (++ self -> n_fds == self -> max_n_fds ) {
864+ if (++ self -> n_fds == DEVPOLL_IN_BUFFER_SIZE ) {
861865 if (devpoll_flush (self ))
862866 return NULL ;
863867 }
@@ -929,7 +933,7 @@ select_devpoll_unregister_impl(devpollObject *self, int fd)
929933 self -> fds [self -> n_fds ].fd = fd ;
930934 self -> fds [self -> n_fds ].events = POLLREMOVE ;
931935
932- if (++ self -> n_fds == self -> max_n_fds ) {
936+ if (++ self -> n_fds == DEVPOLL_IN_BUFFER_SIZE ) {
933937 if (devpoll_flush (self ))
934938 return NULL ;
935939 }
@@ -989,8 +993,8 @@ select_devpoll_poll_impl(devpollObject *self, PyObject *timeout_obj)
989993 if (devpoll_flush (self ))
990994 return NULL ;
991995
992- dvp .dp_fds = self -> fds ;
993- dvp .dp_nfds = self -> max_n_fds ;
996+ dvp .dp_fds = self -> out_fds ;
997+ dvp .dp_nfds = self -> out_size ;
994998 dvp .dp_timeout = (int )ms ;
995999
9961000 if (timeout >= 0 ) {
@@ -1034,8 +1038,8 @@ select_devpoll_poll_impl(devpollObject *self, PyObject *timeout_obj)
10341038 return NULL ;
10351039
10361040 for (i = 0 ; i < poll_result ; i ++ ) {
1037- num1 = PyLong_FromLong (self -> fds [i ].fd );
1038- num2 = PyLong_FromLong (self -> fds [i ].revents );
1041+ num1 = PyLong_FromLong (self -> out_fds [i ].fd );
1042+ num2 = PyLong_FromLong (self -> out_fds [i ].revents );
10391043 if ((num1 == NULL ) || (num2 == NULL )) {
10401044 Py_XDECREF (num1 );
10411045 Py_XDECREF (num2 );
@@ -1128,8 +1132,8 @@ static devpollObject *
11281132newDevPollObject (PyObject * module )
11291133{
11301134 devpollObject * self ;
1131- int fd_devpoll , limit_result ;
1132- struct pollfd * fds ;
1135+ int fd_devpoll , limit_result , out_size ;
1136+ struct pollfd * fds , * out_fds ;
11331137 struct rlimit limit ;
11341138
11351139 /*
@@ -1145,35 +1149,45 @@ newDevPollObject(PyObject *module)
11451149 }
11461150
11471151 /*
1148- ** If the limit is too high (or RLIM_INFINITY), we might allocate huge
1149- ** amounts of memory (or even fail to allocate). Because of that, we limit
1150- ** the number of allocated structs to 2^18 (which is ~4MB of memory).
1152+ ** If the limit is too high (or RLIM_INFINITY), we might
1153+ ** allocate huge amounts of memory or even fail to allocate.
11511154 */
1152- if (limit .rlim_cur > (rlim_t )262144 ) {
1153- limit .rlim_cur = (rlim_t )262144 ;
1155+ out_size = limit .rlim_cur ;
1156+ if (out_size > DEVPOLL_MAX_OUT_BUFFER_SIZE ) {
1157+ out_size = DEVPOLL_MAX_OUT_BUFFER_SIZE ;
11541158 }
11551159
11561160 fd_devpoll = _Py_open ("/dev/poll" , O_RDWR );
11571161 if (fd_devpoll == -1 )
11581162 return NULL ;
11591163
1160- fds = PyMem_NEW (struct pollfd , limit . rlim_cur );
1164+ fds = PyMem_NEW (struct pollfd , DEVPOLL_IN_BUFFER_SIZE );
11611165 if (fds == NULL ) {
11621166 close (fd_devpoll );
11631167 PyErr_NoMemory ();
11641168 return NULL ;
11651169 }
11661170
1171+ out_fds = PyMem_NEW (struct pollfd , out_size );
1172+ if (fds == NULL ) {
1173+ close (fd_devpoll );
1174+ PyMem_Free (fds );
1175+ PyErr_NoMemory ();
1176+ return NULL ;
1177+ }
1178+
11671179 self = PyObject_New (devpollObject , get_select_state (module )-> devpoll_Type );
11681180 if (self == NULL ) {
11691181 close (fd_devpoll );
11701182 PyMem_Free (fds );
1183+ PyMem_Free (out_fds );
11711184 return NULL ;
11721185 }
11731186 self -> fd_devpoll = fd_devpoll ;
1174- self -> max_n_fds = limit . rlim_cur ;
1187+ self -> max_n_fds = out_size ;
11751188 self -> n_fds = 0 ;
11761189 self -> fds = fds ;
1190+ self -> out_fds = out_fds ;
11771191
11781192 return self ;
11791193}
@@ -1185,6 +1199,7 @@ devpoll_dealloc(PyObject *op)
11851199 PyTypeObject * type = Py_TYPE (self );
11861200 (void )devpoll_internal_close (self );
11871201 PyMem_Free (self -> fds );
1202+ PyMem_Free (self -> out_fds );
11881203 PyObject_Free (self );
11891204 Py_DECREF (type );
11901205}
0 commit comments