117117 * Return codes */
118118#define EV_OK 0
119119#define EV_ERR -1
120+ #define EV_OOM -2
120121
121122/*
122123 * Event types, meant to be OR-ed on a bitmask to define the type of an event
@@ -180,7 +181,7 @@ struct ev {
180181 * The first thing done is the initialization of the api pointer according to
181182 * the Mux IO backend found on the host machine
182183 */
183- void ev_init (ev_context * , int );
184+ int ev_init (ev_context * , int );
184185
185186/*
186187 * Just check if the ev_context is running, return 0 if it's not running, 1
@@ -322,12 +323,15 @@ static int epoll_del(int efd, int fd) {
322323 return epoll_ctl (efd , EPOLL_CTL_DEL , fd , NULL );
323324}
324325
325- static void ev_api_init (ev_context * ctx , int events_nr ) {
326+ static int ev_api_init (ev_context * ctx , int events_nr ) {
326327 struct epoll_api * e_api = malloc (sizeof (* e_api ));
328+ if (!e_api )
329+ return EV_OOM ;
327330 e_api -> fd = epoll_create1 (0 );
328331 e_api -> events = calloc (events_nr , sizeof (struct epoll_event ));
329332 ctx -> api = e_api ;
330333 ctx -> maxfd = events_nr ;
334+ return EV_OK ;
331335}
332336
333337static void ev_api_destroy (ev_context * ctx ) {
@@ -417,13 +421,16 @@ struct poll_api {
417421 struct pollfd * fds ;
418422};
419423
420- static void ev_api_init (ev_context * ctx , int events_nr ) {
424+ static int ev_api_init (ev_context * ctx , int events_nr ) {
421425 struct poll_api * p_api = malloc (sizeof (* p_api ));
426+ if (!p_api )
427+ return EV_OOM ;
422428 p_api -> nfds = 0 ;
423429 p_api -> fds = calloc (events_nr , sizeof (struct pollfd ));
424430 p_api -> events_monitored = events_nr ;
425431 ctx -> api = p_api ;
426432 ctx -> maxfd = events_nr ;
433+ return EV_OK ;
427434}
428435
429436static void ev_api_destroy (ev_context * ctx ) {
@@ -550,17 +557,20 @@ struct select_api {
550557 fd_set _rfds , _wfds ;
551558};
552559
553- static void ev_api_init (ev_context * ctx , int events_nr ) {
560+ static int ev_api_init (ev_context * ctx , int events_nr ) {
554561 /*
555562 * fd_set is an array of 32 i32 and each FD is represented by a bit so
556563 * 32 x 32 = 1024 as hard limit
557564 */
558565 assert (events_nr <= SELECT_FDS_HARDCAP );
559566 struct select_api * s_api = malloc (sizeof (* s_api ));
567+ if (!s_api )
568+ return EV_OOM ;
560569 FD_ZERO (& s_api -> rfds );
561570 FD_ZERO (& s_api -> wfds );
562571 ctx -> api = s_api ;
563572 ctx -> maxfd = 0 ;
573+ return EV_OK ;
564574}
565575
566576static void ev_api_destroy (ev_context * ctx ) {
@@ -677,12 +687,15 @@ struct kqueue_api {
677687 struct kevent * events ;
678688};
679689
680- static void ev_api_init (ev_context * ctx , int events_nr ) {
690+ static int ev_api_init (ev_context * ctx , int events_nr ) {
681691 struct kqueue_api * k_api = malloc (sizeof (* k_api ));
692+ if (!k_api )
693+ return EV_OOM ;
682694 k_api -> fd = kqueue ();
683695 k_api -> events = calloc (events_nr , sizeof (struct kevent ));
684696 ctx -> api = k_api ;
685697 ctx -> maxfd = events_nr ;
698+ return EV_OK ;
686699}
687700
688701static void ev_api_destroy (ev_context * ctx ) {
@@ -891,7 +904,9 @@ ev_context *ev_get_ev_context(void) {
891904#endif
892905 signal (SIGINT , ev_sigint_handler );
893906 signal (SIGTERM , ev_sigint_handler );
894- ev_init (& ev_default_ctx , EVENTLOOP_MAX_EVENTS );
907+ int err = ev_init (& ev_default_ctx , EVENTLOOP_MAX_EVENTS );
908+ if (err < EV_OK )
909+ return NULL ;
895910#ifdef __linux__
896911 ev_register_event (& ev_default_ctx , quit_sig ,
897912 EV_CLOSEFD | EV_READ , ev_stop_callback , NULL );
@@ -904,14 +919,17 @@ ev_context *ev_get_ev_context(void) {
904919 return & ev_default_ctx ;
905920}
906921
907- void ev_init (ev_context * ctx , int events_nr ) {
908- ev_api_init (ctx , events_nr );
922+ int ev_init (ev_context * ctx , int events_nr ) {
923+ int err = ev_api_init (ctx , events_nr );
924+ if (err < EV_OK )
925+ return err ;
909926 ctx -> stop = 0 ;
910927 ctx -> fired_events = 0 ;
911928 ctx -> is_running = 0 ;
912929 ctx -> maxevents = events_nr ;
913930 ctx -> events_nr = events_nr ;
914931 ctx -> events_monitored = calloc (events_nr , sizeof (struct ev ));
932+ return EV_OK ;
915933}
916934
917935int ev_is_running (const ev_context * ctx ) {
0 commit comments