diff options
-rw-r--r-- | ev.c | 46 | ||||
-rw-r--r-- | ev_epoll.c | 2 | ||||
-rw-r--r-- | ev_kqueue.c | 4 | ||||
-rw-r--r-- | ev_port.c | 2 |
4 files changed, 38 insertions, 16 deletions
@@ -398,23 +398,44 @@ ev_now (EV_P) } #endif -#define array_roundsize(type,n) (((n) | 4) & ~3) +int inline_size +array_nextsize (int elem, int cur, int cnt) +{ + int ncur = cur + 1; + + do + ncur <<= 1; + while (cnt > ncur); + + /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ + if (elem * ncur > 4096) + { + ncur *= elem; + ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; + ncur = ncur - sizeof (void *) * 4; + ncur /= elem; + } + + return ncur; +} + +inline_speed void * +array_realloc (int elem, void *base, int *cur, int cnt) +{ + *cur = array_nextsize (elem, *cur, cnt); + return ev_realloc (base, elem * *cur); +} #define array_needsize(type,base,cur,cnt,init) \ - if (expect_false ((cnt) > cur)) \ + if (expect_false ((cnt) > (cur))) \ { \ - int newcnt = cur; \ - do \ - { \ - newcnt = array_roundsize (type, newcnt << 1); \ - } \ - while ((cnt) > newcnt); \ - \ - base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ - init (base + cur, newcnt - cur); \ - cur = newcnt; \ + int ocur_ = (cur); \ + (base) = (type *)array_realloc \ + (sizeof (type), (base), &(cur), (cnt)); \ + init ((base) + (ocur_), (cur) - ocur_); \ } +#if 0 #define array_slim(type,stem) \ if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ { \ @@ -422,6 +443,7 @@ ev_now (EV_P) base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ } +#endif #define array_free(stem, idx) \ ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; @@ -74,7 +74,7 @@ epoll_poll (EV_P_ ev_tstamp timeout) if (expect_false (eventcnt == epoll_eventmax)) { ev_free (epoll_events); - epoll_eventmax = array_roundsize (epoll_events, epoll_eventmax << 1); + epoll_eventmax = array_nextsize (sizeof (struct epoll_event), epoll_eventmax, epoll_eventmax + 1); epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax); } } diff --git a/ev_kqueue.c b/ev_kqueue.c index afd3f9c..4fd649f 100644 --- a/ev_kqueue.c +++ b/ev_kqueue.c @@ -83,7 +83,7 @@ kqueue_poll (EV_P_ ev_tstamp timeout) if (kqueue_changecnt > kqueue_eventmax) { ev_free (kqueue_events); - kqueue_eventmax = array_roundsize (struct kevent, kqueue_changecnt); + kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_changecnt); kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); } @@ -137,7 +137,7 @@ kqueue_poll (EV_P_ ev_tstamp timeout) if (expect_false (res == kqueue_eventmax)) { ev_free (kqueue_events); - kqueue_eventmax = array_roundsize (struct kevent, kqueue_eventmax << 1); + kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_eventmax + 1); kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); } } @@ -103,7 +103,7 @@ port_poll (EV_P_ ev_tstamp timeout) if (expect_false (nget == port_eventmax)) { ev_free (port_events); - port_eventmax = array_roundsize (port_event_t, port_eventmax << 1); + port_eventmax = array_nextsize (sizeof (port_event_t), port_eventmax, port_eventmax + 1); port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); } } |