From 3b01ae665985d315e329f5277bceefdc5b989064 Mon Sep 17 00:00:00 2001 From: visa Date: Sun, 13 Feb 2022 13:03:02 +0000 Subject: [PATCH] Add helper functions for f_modify and f_process to condense code These new functions, knote_modify() and knote_process(), implement the logic that is common to most f_modify and f_process instances. The code is inlined so as to not add yet another call frame on the already towering stack of kqueue functions. Also, the _fn versions allow direct calling of an event function when there is only one filter type to handle. --- sys/sys/event.h | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/sys/sys/event.h b/sys/sys/event.h index 2ec51183892..6aa346183cb 100644 --- a/sys/sys/event.h +++ b/sys/sys/event.h @@ -1,4 +1,4 @@ -/* $OpenBSD: event.h,v 1.65 2022/02/13 12:58:46 visa Exp $ */ +/* $OpenBSD: event.h,v 1.66 2022/02/13 13:03:02 visa Exp $ */ /*- * Copyright (c) 1999,2000,2001 Jonathan Lemon @@ -316,6 +316,45 @@ extern void klist_remove(struct klist *, struct knote *); extern void klist_remove_locked(struct klist *, struct knote *); extern void klist_invalidate(struct klist *); +static inline int +knote_modify_fn(const struct kevent *kev, struct knote *kn, + int (*f_event)(struct knote *, long)) +{ + knote_assign(kev, kn); + return ((*f_event)(kn, 0)); +} + +static inline int +knote_modify(const struct kevent *kev, struct knote *kn) +{ + return (knote_modify_fn(kev, kn, kn->kn_fop->f_event)); +} + +static inline int +knote_process_fn(struct knote *kn, struct kevent *kev, + int (*f_event)(struct knote *, long)) +{ + int active; + + /* + * If called from kqueue_scan(), skip f_event + * when EV_ONESHOT is set, to preserve old behaviour. + */ + if (kev != NULL && (kn->kn_flags & EV_ONESHOT)) + active = 1; + else + active = (*f_event)(kn, 0); + if (active) + knote_submit(kn, kev); + return (active); +} + +static inline int +knote_process(struct knote *kn, struct kevent *kev) +{ + return (knote_process_fn(kn, kev, kn->kn_fop->f_event)); +} + static inline int klist_empty(struct klist *klist) { -- 2.20.1