3737#endif
3838#include "ev.h"
3939#include "util.h"
40+ #include "memory.h"
4041#include "config.h"
4142
4243#if defined(EPOLL )
@@ -103,20 +104,18 @@ static int epoll_del(int efd, int fd) {
103104}
104105
105106static int ev_api_init (struct ev_ctx * ctx , int events_nr ) {
106- struct epoll_api * e_api = xmalloc (sizeof (* e_api ));
107- if (!e_api )
108- return - EV_OOM ;
107+ struct epoll_api * e_api = try_alloc (sizeof (* e_api ));
109108 e_api -> fd = epoll_create1 (0 );
110- e_api -> events = xcalloc (events_nr , sizeof (struct epoll_event ));
109+ e_api -> events = try_calloc (events_nr , sizeof (struct epoll_event ));
111110 ctx -> api = e_api ;
112111 ctx -> maxfd = events_nr ;
113112 return EV_OK ;
114113}
115114
116115static void ev_api_destroy (struct ev_ctx * ctx ) {
117116 close (((struct epoll_api * ) ctx -> api )-> fd );
118- xfree (((struct epoll_api * ) ctx -> api )-> events );
119- xfree (ctx -> api );
117+ free_memory (((struct epoll_api * ) ctx -> api )-> events );
118+ free_memory (ctx -> api );
120119}
121120
122121static int ev_api_get_event_type (struct ev_ctx * ctx , int idx ) {
@@ -200,20 +199,18 @@ struct poll_api {
200199};
201200
202201static int ev_api_init (struct ev_ctx * ctx , int events_nr ) {
203- struct poll_api * p_api = xmalloc (sizeof (* p_api ));
204- if (!p_api )
205- return - EV_OOM ;
202+ struct poll_api * p_api = try_alloc (sizeof (* p_api ));
206203 p_api -> nfds = 0 ;
207- p_api -> fds = xcalloc (events_nr , sizeof (struct pollfd ));
204+ p_api -> fds = try_calloc (events_nr , sizeof (struct pollfd ));
208205 p_api -> events_monitored = events_nr ;
209206 ctx -> api = p_api ;
210207 ctx -> maxfd = events_nr ;
211208 return EV_OK ;
212209}
213210
214211static void ev_api_destroy (struct ev_ctx * ctx ) {
215- xfree (((struct poll_api * ) ctx -> api )-> fds );
216- xfree (ctx -> api );
212+ free_memory (((struct poll_api * ) ctx -> api )-> fds );
213+ free_memory (ctx -> api );
217214}
218215
219216static int ev_api_get_event_type (struct ev_ctx * ctx , int idx ) {
@@ -249,7 +246,7 @@ static int ev_api_watch_fd(struct ev_ctx *ctx, int fd) {
249246 p_api -> nfds ++ ;
250247 if (p_api -> nfds >= p_api -> events_monitored ) {
251248 p_api -> events_monitored *= 2 ;
252- p_api -> fds = xrealloc (p_api -> fds ,
249+ p_api -> fds = try_realloc (p_api -> fds ,
253250 p_api -> events_monitored * sizeof (struct pollfd ));
254251 }
255252 return EV_OK ;
@@ -282,7 +279,7 @@ static int ev_api_register_event(struct ev_ctx *ctx, int fd, int mask) {
282279 p_api -> nfds ++ ;
283280 if (p_api -> nfds >= p_api -> events_monitored ) {
284281 p_api -> events_monitored *= 2 ;
285- p_api -> fds = xrealloc (p_api -> fds ,
282+ p_api -> fds = try_realloc (p_api -> fds ,
286283 p_api -> events_monitored * sizeof (struct pollfd ));
287284 }
288285 return EV_OK ;
@@ -337,9 +334,7 @@ static int ev_api_init(struct ev_ctx *ctx, int events_nr) {
337334 * 32 x 32 = 1024 as hard limit
338335 */
339336 assert (events_nr <= 1024 );
340- struct select_api * s_api = xmalloc (sizeof (* s_api ));
341- if (!s_api )
342- return - EV_OOM ;
337+ struct select_api * s_api = try_alloc (sizeof (* s_api ));
343338 FD_ZERO (& s_api -> rfds );
344339 FD_ZERO (& s_api -> wfds );
345340 ctx -> api = s_api ;
@@ -348,7 +343,7 @@ static int ev_api_init(struct ev_ctx *ctx, int events_nr) {
348343}
349344
350345static void ev_api_destroy (struct ev_ctx * ctx ) {
351- xfree (ctx -> api );
346+ free_memory (ctx -> api );
352347}
353348
354349static int ev_api_get_event_type (struct ev_ctx * ctx , int idx ) {
@@ -462,20 +457,18 @@ struct kqueue_api {
462457};
463458
464459static int ev_api_init (struct ev_ctx * ctx , int events_nr ) {
465- struct kqueue_api * k_api = xmalloc (sizeof (* k_api ));
466- if (!k_api )
467- return - EV_OOM ;
460+ struct kqueue_api * k_api = try_alloc (sizeof (* k_api ));
468461 k_api -> fd = kqueue ();
469- k_api -> events = xcalloc (events_nr , sizeof (struct kevent ));
462+ k_api -> events = try_calloc (events_nr , sizeof (struct kevent ));
470463 ctx -> api = k_api ;
471464 ctx -> maxfd = events_nr ;
472465 return EV_OK ;
473466}
474467
475468static void ev_api_destroy (struct ev_ctx * ctx ) {
476469 close (((struct kqueue_api * ) ctx -> api )-> fd );
477- xfree (((struct kqueue_api * ) ctx -> api )-> events );
478- xfree (ctx -> api );
470+ free_memory (((struct kqueue_api * ) ctx -> api )-> events );
471+ free_memory (ctx -> api );
479472}
480473
481474static int ev_api_get_event_type (struct ev_ctx * ctx , int idx ) {
@@ -620,8 +613,8 @@ static void ev_add_monitored(struct ev_ctx *ctx, int fd, int mask,
620613 int i = ctx -> maxevents ;
621614 ctx -> maxevents = fd ;
622615 if (fd > ctx -> events_nr ) {
623- ctx -> events_monitored =
624- xrealloc ( ctx -> events_monitored , (fd + 1 ) * sizeof (struct ev ));
616+ ctx -> events_monitored = try_realloc ( ctx -> events_monitored ,
617+ (fd + 1 ) * sizeof (struct ev ));
625618 for (; i < ctx -> maxevents ; ++ i )
626619 ctx -> events_monitored [i ].mask = EV_NONE ;
627620 }
@@ -650,7 +643,7 @@ int ev_init(struct ev_ctx *ctx, int events_nr) {
650643 ctx -> fired_events = 0 ;
651644 ctx -> maxevents = events_nr ;
652645 ctx -> events_nr = events_nr ;
653- ctx -> events_monitored = xcalloc (events_nr , sizeof (struct ev ));
646+ ctx -> events_monitored = try_calloc (events_nr , sizeof (struct ev ));
654647 return EV_OK ;
655648}
656649
@@ -660,7 +653,7 @@ void ev_destroy(struct ev_ctx *ctx) {
660653 ctx -> events_monitored [i ].mask != EV_NONE )
661654 ev_del_fd (ctx , ctx -> events_monitored [i ].fd );
662655 }
663- xfree (ctx -> events_monitored );
656+ free_memory (ctx -> events_monitored );
664657 ev_api_destroy (ctx );
665658}
666659
0 commit comments