Skip to content

Commit f64227c

Browse files
committed
Minor renames and fixes
1 parent 80bd6b0 commit f64227c

File tree

6 files changed

+117
-120
lines changed

6 files changed

+117
-120
lines changed

src/ev.c

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ static int ev_api_get_event_type(struct ev_ctx *ctx, int idx)
126126
{
127127
struct epoll_api *e_api = ctx->api;
128128
int events = e_api->events[idx].events;
129-
int ev_mask = ctx->events_monitored[e_api->events[idx].data.fd].mask;
129+
int ev_mask = ctx->monitored[e_api->events[idx].data.fd].mask;
130130
// We want to remember the previous events only if they're not of type
131131
// CLOSE or TIMER
132-
int mask = ev_mask & (EV_CLOSEFD | EV_TIMERFD) ? ev_mask : EV_NONE;
132+
int mask = ev_mask & (EV_CLOSEFD | EV_TIMERFD) ? ev_mask : EV_NONE;
133133
if (events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP))
134134
mask |= EV_DISCONNECT;
135135
if (events & EPOLLIN)
@@ -189,7 +189,7 @@ static inline struct ev *ev_api_fetch_event(const struct ev_ctx *ctx, int idx,
189189
int mask)
190190
{
191191
int fd = ((struct epoll_api *)ctx->api)->events[idx].data.fd;
192-
return ctx->events_monitored + fd;
192+
return ctx->monitored + fd;
193193
}
194194

195195
#elif defined(POLL)
@@ -212,18 +212,18 @@ static inline struct ev *ev_api_fetch_event(const struct ev_ctx *ctx, int idx,
212212

213213
struct poll_api {
214214
int nfds;
215-
int events_monitored;
215+
int monitored;
216216
struct pollfd *fds;
217217
};
218218

219219
static int ev_api_init(struct ev_ctx *ctx, int events_nr)
220220
{
221-
struct poll_api *p_api = try_alloc(sizeof(*p_api));
222-
p_api->nfds = 0;
223-
p_api->fds = try_calloc(events_nr, sizeof(struct pollfd));
224-
p_api->events_monitored = events_nr;
225-
ctx->api = p_api;
226-
ctx->maxfd = events_nr;
221+
struct poll_api *p_api = try_alloc(sizeof(*p_api));
222+
p_api->nfds = 0;
223+
p_api->fds = try_calloc(events_nr, sizeof(struct pollfd));
224+
p_api->monitored = events_nr;
225+
ctx->api = p_api;
226+
ctx->maxfd = events_nr;
227227
return EV_OK;
228228
}
229229

@@ -236,7 +236,7 @@ static void ev_api_destroy(struct ev_ctx *ctx)
236236
static int ev_api_get_event_type(struct ev_ctx *ctx, int idx)
237237
{
238238
struct poll_api *p_api = ctx->api;
239-
int ev_mask = ctx->events_monitored[p_api->fds[idx].fd].mask;
239+
int ev_mask = ctx->monitored[p_api->fds[idx].fd].mask;
240240
// We want to remember the previous events only if they're not of type
241241
// CLOSE or TIMER
242242
int mask = ev_mask & (EV_CLOSEFD | EV_TIMERFD) ? ev_mask : 0;
@@ -270,10 +270,10 @@ static int ev_api_watch_fd(struct ev_ctx *ctx, int fd)
270270
p_api->fds[p_api->nfds].fd = fd;
271271
p_api->fds[p_api->nfds].events = POLLIN;
272272
p_api->nfds++;
273-
if (p_api->nfds >= p_api->events_monitored) {
274-
p_api->events_monitored *= 2;
275-
p_api->fds = try_realloc(p_api->fds, p_api->events_monitored *
276-
sizeof(struct pollfd));
273+
if (p_api->nfds >= p_api->monitored) {
274+
p_api->monitored *= 2;
275+
p_api->fds =
276+
try_realloc(p_api->fds, p_api->monitored * sizeof(struct pollfd));
277277
}
278278
return EV_OK;
279279
}
@@ -307,10 +307,10 @@ static int ev_api_register_event(struct ev_ctx *ctx, int fd, int mask)
307307
if (mask & EV_WRITE)
308308
p_api->fds[p_api->nfds].events |= POLLOUT;
309309
p_api->nfds++;
310-
if (p_api->nfds >= p_api->events_monitored) {
311-
p_api->events_monitored *= 2;
312-
p_api->fds = try_realloc(p_api->fds, p_api->events_monitored *
313-
sizeof(struct pollfd));
310+
if (p_api->nfds >= p_api->monitored) {
311+
p_api->monitored *= 2;
312+
p_api->fds =
313+
try_realloc(p_api->fds, p_api->monitored * sizeof(struct pollfd));
314314
}
315315
return EV_OK;
316316
}
@@ -334,7 +334,7 @@ static int ev_api_fire_event(struct ev_ctx *ctx, int fd, int mask)
334334
static inline struct ev *ev_api_fetch_event(const struct ev_ctx *ctx, int idx,
335335
int mask)
336336
{
337-
return ctx->events_monitored + ((struct poll_api *)ctx->api)->fds[idx].fd;
337+
return ctx->monitored + ((struct poll_api *)ctx->api)->fds[idx].fd;
338338
}
339339

340340
#elif defined(SELECT)
@@ -380,7 +380,7 @@ static void ev_api_destroy(struct ev_ctx *ctx) { free_memory(ctx->api); }
380380
static int ev_api_get_event_type(struct ev_ctx *ctx, int idx)
381381
{
382382
struct select_api *s_api = ctx->api;
383-
int ev_mask = ctx->events_monitored[idx].mask;
383+
int ev_mask = ctx->monitored[idx].mask;
384384
// We want to remember the previous events only if they're not of type
385385
// CLOSE or TIMER
386386
int mask = ev_mask & (EV_CLOSEFD | EV_TIMERFD) ? ev_mask : 0;
@@ -477,7 +477,7 @@ static int ev_api_fire_event(struct ev_ctx *ctx, int fd, int mask)
477477
static inline struct ev *ev_api_fetch_event(const struct ev_ctx *ctx, int idx,
478478
int mask)
479479
{
480-
return ctx->events_monitored + idx;
480+
return ctx->monitored + idx;
481481
}
482482

483483
#elif defined(KQUEUE)
@@ -523,10 +523,10 @@ static int ev_api_get_event_type(struct ev_ctx *ctx, int idx)
523523
{
524524
struct kqueue_api *k_api = ctx->api;
525525
int events = k_api->events[idx].flags;
526-
int ev_mask = ctx->events_monitored[k_api->events[idx].ident].mask;
526+
int ev_mask = ctx->monitored[k_api->events[idx].ident].mask;
527527
// We want to remember the previous events only if they're not of type
528528
// CLOSE or TIMER
529-
int mask = ev_mask & (EV_CLOSEFD | EV_TIMERFD) ? ev_mask : EV_NONE;
529+
int mask = ev_mask & (EV_CLOSEFD | EV_TIMERFD) ? ev_mask : EV_NONE;
530530
if (events & (EV_EOF | EV_ERROR))
531531
mask |= EV_DISCONNECT;
532532
if (events & EVFILT_READ)
@@ -551,7 +551,7 @@ static int ev_api_del_fd(struct ev_ctx *ctx, int fd)
551551
{
552552
struct kqueue_api *k_api = ctx->api;
553553
struct kevent ke;
554-
int ev_mask = ctx->events_monitored[fd].mask;
554+
int ev_mask = ctx->monitored[fd].mask;
555555
int mask = 0;
556556
if (ev_mask & EV_READ)
557557
mask |= EVFILT_READ;
@@ -610,13 +610,13 @@ static inline struct ev *ev_api_fetch_event(const struct ev_ctx *ctx, int idx,
610610
{
611611
(void)mask; // silence compiler warning
612612
int fd = ((struct kqueue_api *)ctx->api)->events[idx].ident;
613-
return ctx->events_monitored + fd;
613+
return ctx->monitored + fd;
614614
}
615615

616616
#endif // KQUEUE
617617

618618
/*
619-
* Process the event at the position idx in the events_monitored array. Read or
619+
* Process the event at the position idx in the monitored array. Read or
620620
* write events can be executed on the same iteration, differentiating just
621621
* on EV_CLOSEFD or EV_EVENTFD.
622622
* Returns the number of fired callbacks.
@@ -670,8 +670,7 @@ static int ev_process_event(struct ev_ctx *ctx, int idx, int mask)
670670
* context.
671671
*/
672672
static void ev_add_monitored(struct ev_ctx *ctx, int fd, int mask,
673-
void (*callback)(struct ev_ctx *, void *),
674-
void *ptr)
673+
ev_callback cb, void *ptr)
675674
{
676675
/*
677676
* TODO check for fd <= 1024 if using SELECT
@@ -682,21 +681,21 @@ static void ev_add_monitored(struct ev_ctx *ctx, int fd, int mask,
682681
int i = ctx->maxevents;
683682
ctx->maxevents = fd;
684683
if (fd > ctx->events_nr) {
685-
ctx->events_monitored = try_realloc(ctx->events_monitored,
686-
(fd + 1) * sizeof(struct ev));
684+
ctx->monitored =
685+
try_realloc(ctx->monitored, (fd + 1) * sizeof(struct ev));
687686
for (; i < ctx->maxevents; ++i)
688-
ctx->events_monitored[i].mask = EV_NONE;
687+
ctx->monitored[i].mask = EV_NONE;
689688
}
690689
}
691-
ctx->events_monitored[fd].fd = fd;
692-
ctx->events_monitored[fd].mask |= mask;
690+
ctx->monitored[fd].fd = fd;
691+
ctx->monitored[fd].mask |= mask;
693692
if (mask & EV_READ) {
694-
ctx->events_monitored[fd].rdata = ptr;
695-
ctx->events_monitored[fd].rcallback = callback;
693+
ctx->monitored[fd].rdata = ptr;
694+
ctx->monitored[fd].rcallback = cb;
696695
}
697696
if (mask & EV_WRITE) {
698-
ctx->events_monitored[fd].wdata = ptr;
699-
ctx->events_monitored[fd].wcallback = callback;
697+
ctx->monitored[fd].wdata = ptr;
698+
ctx->monitored[fd].wcallback = cb;
700699
}
701700
}
702701

@@ -710,22 +709,22 @@ int ev_init(struct ev_ctx *ctx, int events_nr)
710709
int err = ev_api_init(ctx, events_nr);
711710
if (err < 0)
712711
return err;
713-
ctx->stop = 0;
714-
ctx->fired_events = 0;
715-
ctx->maxevents = events_nr;
716-
ctx->events_nr = events_nr;
717-
ctx->events_monitored = try_calloc(events_nr, sizeof(struct ev));
712+
ctx->stop = 0;
713+
ctx->fired_events = 0;
714+
ctx->maxevents = events_nr;
715+
ctx->events_nr = events_nr;
716+
ctx->monitored = try_calloc(events_nr, sizeof(struct ev));
718717
return EV_OK;
719718
}
720719

721-
void ev_destroy(struct ev_ctx *ctx)
720+
void ev_free(struct ev_ctx *ctx)
722721
{
723722
for (int i = 0; i < ctx->maxevents; ++i) {
724-
if (!(ctx->events_monitored[i].mask & EV_CLOSEFD) &&
725-
ctx->events_monitored[i].mask != EV_NONE)
726-
ev_del_fd(ctx, ctx->events_monitored[i].fd);
723+
if (!(ctx->monitored[i].mask & EV_CLOSEFD) &&
724+
ctx->monitored[i].mask != EV_NONE)
725+
ev_delete(ctx, ctx->monitored[i].fd);
727726
}
728-
free_memory(ctx->events_monitored);
727+
free_memory(ctx->monitored);
729728
ev_api_destroy(ctx);
730729
}
731730

@@ -761,15 +760,15 @@ int ev_run(struct ev_ctx *ctx)
761760

762761
void ev_stop(struct ev_ctx *ctx) { ctx->stop = 1; }
763762

764-
int ev_watch_fd(struct ev_ctx *ctx, int fd, int mask)
763+
int ev_watch(struct ev_ctx *ctx, int fd, int mask)
765764
{
766765
ev_add_monitored(ctx, fd, mask, NULL, NULL);
767766
return ev_api_watch_fd(ctx, fd);
768767
}
769768

770-
int ev_del_fd(struct ev_ctx *ctx, int fd)
769+
int ev_delete(struct ev_ctx *ctx, int fd)
771770
{
772-
memset(ctx->events_monitored + fd, 0x00, sizeof(struct ev));
771+
memset(ctx->monitored + fd, 0x00, sizeof(struct ev));
773772
return ev_api_del_fd(ctx, fd);
774773
}
775774

@@ -785,10 +784,10 @@ int ev_del_fd(struct ev_ctx *ctx, int fd)
785784
* - callback: is a function pointer to the routine we want to execute
786785
* - data: an opaque pointer to the arguments for the callback.
787786
*/
788-
int ev_register_event(struct ev_ctx *ctx, int fd, int mask,
789-
void (*callback)(struct ev_ctx *, void *), void *data)
787+
int ev_register_event(struct ev_ctx *ctx, int fd, int mask, ev_callback cb,
788+
void *data)
790789
{
791-
ev_add_monitored(ctx, fd, mask, callback, data);
790+
ev_add_monitored(ctx, fd, mask, cb, data);
792791
int ret = 0;
793792
ret = ev_api_register_event(ctx, fd, mask);
794793
if (ret < 0)
@@ -807,8 +806,7 @@ int ev_register_event(struct ev_ctx *ctx, int fd, int mask,
807806
* loop, specifying, seconds and/or nanoseconds defining how often the callback
808807
* should be executed.
809808
*/
810-
int ev_register_cron(struct ev_ctx *ctx,
811-
void (*callback)(struct ev_ctx *, void *), void *data,
809+
int ev_register_cron(struct ev_ctx *ctx, ev_callback cb, void *data,
812810
long long s, long long ns)
813811
{
814812
#ifdef __linux__
@@ -825,14 +823,14 @@ int ev_register_cron(struct ev_ctx *ctx,
825823
return -EV_ERR;
826824

827825
// Add the timer to the event loop
828-
ev_add_monitored(ctx, timerfd, EV_TIMERFD | EV_READ, callback, data);
826+
ev_add_monitored(ctx, timerfd, EV_TIMERFD | EV_READ, cb, data);
829827
return ev_api_watch_fd(ctx, timerfd);
830828
#else
831829
struct kqueue_api *k_api = ctx->api;
832830
// milliseconds
833831
unsigned period = (s * 1000) + (ns / 100);
834832
int fd = socket(AF_INET, SOCK_STREAM, 0);
835-
ev_add_monitored(ctx, fd, EV_TIMERFD | EV_READ, callback, data);
833+
ev_add_monitored(ctx, fd, EV_TIMERFD | EV_READ, cb, data);
836834
struct kevent ke;
837835
EV_SET(&ke, fd, EVFILT_TIMER, EV_ADD | EV_ENABLE, 0, period, 0);
838836
if (kevent(k_api->fd, &ke, 1, NULL, 0, NULL) == -1)
@@ -853,11 +851,10 @@ int ev_register_cron(struct ev_ctx *ctx,
853851
* - callback: is a function pointer to the routine we want to execute
854852
* - data: an opaque pointer to the arguments for the callback.
855853
*/
856-
int ev_fire_event(struct ev_ctx *ctx, int fd, int mask,
857-
void (*callback)(struct ev_ctx *, void *), void *data)
854+
int ev_add(struct ev_ctx *ctx, int fd, int mask, ev_callback cb, void *data)
858855
{
859856
int ret = 0;
860-
ev_add_monitored(ctx, fd, mask, callback, data);
857+
ev_add_monitored(ctx, fd, mask, cb, data);
861858
ret = ev_api_fire_event(ctx, fd, mask);
862859
if (ret < 0)
863860
return -EV_ERR;

src/ev.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,19 @@ enum ev_type {
5050

5151
struct ev_ctx;
5252

53+
typedef void (*ev_callback)(struct ev_ctx *, void *);
54+
5355
/*
5456
* Event struture used as the main carrier of clients informations, it will be
5557
* tracked by an array in every context created
5658
*/
5759
struct ev {
5860
int fd;
5961
int mask;
60-
void *rdata; // opaque pointer for read callback args
61-
void *wdata; // opaque pointer for write callback args
62-
void (*rcallback)(struct ev_ctx *, void *); // read callback
63-
void (*wcallback)(struct ev_ctx *, void *); // write callback
62+
void *rdata; // opaque pointer for read callback args
63+
void *wdata; // opaque pointer for write callback args
64+
ev_callback rcallback; // read callback
65+
ev_callback wcallback; // write callback
6466
};
6567

6668
/*
@@ -81,13 +83,13 @@ struct ev_ctx {
8183
int stop;
8284
int maxevents;
8385
unsigned long long fired_events;
84-
struct ev *events_monitored;
86+
struct ev *monitored;
8587
void *api; // opaque pointer to platform defined backends
8688
};
8789

8890
int ev_init(struct ev_ctx *, int);
8991

90-
void ev_destroy(struct ev_ctx *);
92+
void ev_free(struct ev_ctx *);
9193

9294
/*
9395
* Poll an event context for events, accepts a timeout or block forever,
@@ -113,32 +115,30 @@ void ev_stop(struct ev_ctx *);
113115
* ev_fire_event just without an event to be carried. Useful to add simple
114116
* descritors like a listening socket o message queue FD.
115117
*/
116-
int ev_watch_fd(struct ev_ctx *, int, int);
118+
int ev_watch(struct ev_ctx *, int, int);
117119

118120
/*
119121
* Remove a FD from the loop, even tho a close syscall is sufficient to remove
120122
* the FD from the underlying backend such as EPOLL/SELECT, this call ensure
121123
* that any associated events is cleaned out an set to EV_NONE
122124
*/
123-
int ev_del_fd(struct ev_ctx *, int);
125+
int ev_delete(struct ev_ctx *, int);
124126

125127
/*
126128
* Register a new event, semantically it's equal to ev_register_event but
127129
* it's meant to be used when an FD is not already watched by the event loop.
128130
* It could be easily integrated in ev_fire_event call but I prefer maintain
129131
* the samantic separation of responsibilities.
130132
*/
131-
int ev_register_event(struct ev_ctx *, int, int,
132-
void (*callback)(struct ev_ctx *, void *), void *);
133+
int ev_register_event(struct ev_ctx *, int, int, ev_callback, void *);
133134

134-
int ev_register_cron(struct ev_ctx *, void (*callback)(struct ev_ctx *, void *),
135-
void *, long long, long long);
135+
int ev_register_cron(struct ev_ctx *, ev_callback, void *, long long,
136+
long long);
136137

137138
/*
138139
* Register a new event for the next loop cycle to a FD. Equal to ev_watch_fd
139140
* but allow to carry an event object for the next cycle.
140141
*/
141-
int ev_fire_event(struct ev_ctx *, int, int,
142-
void (*callback)(struct ev_ctx *, void *), void *);
142+
int ev_add(struct ev_ctx *, int, int, ev_callback, void *);
143143

144144
#endif

0 commit comments

Comments
 (0)