From 63c8ffe5f1258c12f26ffce41fa8bf8645ae44d5 Mon Sep 17 00:00:00 2001 From: "wtc%netscape.com" Date: Mon, 7 Dec 1998 01:39:48 +0000 Subject: [PATCH] Apply the patch submitted by Joe Keane to PR_Poll so that we use a struct pollfd array allocated on the stack when caller's poll descriptor array can fit in it. This avoids the cost of a malloc and a free call when the poll descriptor arrays are small. --- nsprpub/pr/src/pthreads/ptio.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/nsprpub/pr/src/pthreads/ptio.c b/nsprpub/pr/src/pthreads/ptio.c index e4e94d80d38d..bc39f87bb39d 100644 --- a/nsprpub/pr/src/pthreads/ptio.c +++ b/nsprpub/pr/src/pthreads/ptio.c @@ -2875,14 +2875,25 @@ PR_IMPLEMENT(PRInt32) PR_Poll( if (0 == npds) PR_Sleep(timeout); else { +#define STACK_POLL_DESC_COUNT 4 + struct pollfd stack_syspoll[STACK_POLL_DESC_COUNT]; + struct pollfd *syspoll; PRIntn index, msecs; - struct pollfd *syspoll = NULL; - syspoll = (struct pollfd*)PR_MALLOC(npds * sizeof(struct pollfd)); - if (NULL == syspoll) + + if (npds <= STACK_POLL_DESC_COUNT) { - PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); - return -1; + syspoll = stack_syspoll; } + else + { + syspoll = (struct pollfd*)PR_MALLOC(npds * sizeof(struct pollfd)); + if (NULL == syspoll) + { + PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); + return -1; + } + } + for (index = 0; index < npds; ++index) { PRInt16 in_flags_read = 0, in_flags_write = 0; @@ -3080,8 +3091,8 @@ retry: } } - PR_DELETE(syspoll); - + if (syspoll != stack_syspoll) + PR_DELETE(syspoll); } return ready;