From 0db49adcac61dbb48c0fd9261da7cce2e605f916 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 3 Feb 2020 19:05:06 -0800 Subject: libs/sig: add sig_ref() Added some rudimentary refcounting so sig_t's can be readily shared by multiple sig_t's. --- src/libs/sig/sig.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/libs/sig/sig.c') diff --git a/src/libs/sig/sig.c b/src/libs/sig/sig.c index c68a420..d260006 100644 --- a/src/libs/sig/sig.c +++ b/src/libs/sig/sig.c @@ -25,6 +25,7 @@ typedef union sig_context_t { typedef struct sig_t { const sig_ops_t *ops; + unsigned refcount; sig_context_t ctxt[]; } sig_t; @@ -55,15 +56,37 @@ sig_t * sig_new(const sig_ops_t *ops, ...) va_end(ap); sig->ops = ops; + sig->refcount = 1; return sig; } -/* free a signal generator, always returns NULL */ +/* add a reference to an existing signal generator, + * free/unref using sig_free() just like it had been returned by sig_new() + */ +sig_t * sig_ref(sig_t *sig) +{ + assert(sig); + + sig->refcount++; + + return sig; +} + + +/* free a signal generator, returns sig if sig still has references, + * otherwise returns NULL + */ sig_t * sig_free(sig_t *sig) { if (sig) { + assert(sig->refcount > 0); + + sig->refcount--; + if (sig->refcount) + return sig; + if (sig->ops->destroy) sig->ops->destroy(&sig->ctxt); -- cgit v1.2.3