diff options
author | Kay Sievers <kay@vrfy.org> | 2013-11-28 00:21:14 +0100 |
---|---|---|
committer | Kay Sievers <kay@vrfy.org> | 2013-11-28 00:21:14 +0100 |
commit | 3e633f4ad80eb6f222c7aa021a6f75762a31b405 (patch) | |
tree | 6752c706adbc451b36d90e47dd8c3a1ed5bb86df | |
parent | 39403a433583cb91a1182623560347514b618818 (diff) | |
download | libabc-3e633f4ad80eb6f222c7aa021a6f75762a31b405.tar.gz |
always return NULL in _unref() APIs
Returning anything else but NULL would suggest the caller's
reference might still be valid, but it isn't, because the
caller just invoked _unref() after all. This turns the return
value into a typesafe shortcut that allows unreffing and
resetting a reference in one line. In contrast to solutions
for this which take a pointer to a pointer to accomplish the
same this solution is just syntactic sugar the developer can
make use of but doesn't have to, and this is particularly
useful when immediately unreffing objects returned by function
calls.
-rw-r--r-- | src/libabc.c | 7 |
1 files changed, 3 insertions, 4 deletions
diff --git a/src/libabc.c b/src/libabc.c index cbbc16a..c3c1fef 100644 --- a/src/libabc.c +++ b/src/libabc.c @@ -173,8 +173,7 @@ ABC_EXPORT struct abc_ctx *abc_ref(struct abc_ctx *ctx) * abc_unref: * @ctx: abc library context * - * Drop a reference of the abc library context. If the refcount - * reaches zero, the resources of the context will be released. + * Drop a reference of the abc library context. * **/ ABC_EXPORT struct abc_ctx *abc_unref(struct abc_ctx *ctx) @@ -183,7 +182,7 @@ ABC_EXPORT struct abc_ctx *abc_unref(struct abc_ctx *ctx) return NULL; ctx->refcount--; if (ctx->refcount > 0) - return ctx; + return NULL; info(ctx, "context %p released\n", ctx); free(ctx); return NULL; @@ -257,7 +256,7 @@ ABC_EXPORT struct abc_thing *abc_thing_unref(struct abc_thing *thing) return NULL; thing->refcount--; if (thing->refcount > 0) - return thing; + return NULL; dbg(thing->ctx, "context %p released\n", thing); free(thing); return NULL; |