From: Martin Mares Date: Tue, 19 Apr 2011 12:44:46 +0000 (+0200) Subject: Resources: Allow res_free(NULL) and res_detach(NULL) X-Git-Tag: v5.0~74^2~13 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=0f88062c8973258611a8cba9a0e9668d1c688030;p=libucw.git Resources: Allow res_free(NULL) and res_detach(NULL) --- diff --git a/ucw/respool.c b/ucw/respool.c index a4978fdf..09b8953f 100644 --- a/ucw/respool.c +++ b/ucw/respool.c @@ -121,6 +121,8 @@ res_drop(struct resource *r) void res_detach(struct resource *r) { + if (!r) + return; if (r->rclass->detach) r->rclass->detach(r); res_drop(r); @@ -129,6 +131,8 @@ res_detach(struct resource *r) void res_free(struct resource *r) { + if (!r) + return; if (r->rclass->free) r->rclass->free(r); res_drop(r); diff --git a/ucw/respool.h b/ucw/respool.h index 55f17bee..56955707 100644 --- a/ucw/respool.h +++ b/ucw/respool.h @@ -77,7 +77,18 @@ static inline struct respool *rp_switch(struct respool *rp) struct resource *res_alloc(const struct res_class *rc) LIKE_MALLOC; // Returns NULL if there is no pool active void res_dump(struct resource *r, uns indent); /** Prints out a debugging dump of the resource to stdout. **/ -void res_free(struct resource *r); /** Frees a resource, unlinking it from its pool. **/ + +/** + * Frees a resource, unlinking it from its pool. + * When called with a NULL pointer, it does nothing, but safely. + **/ +void res_free(struct resource *r); + +/** + * Unlinks a resource from a pool and releases its meta-data. However, the resource itself is kept. + * When called with a NULL pointer, it does nothing, but safely. + **/ +void res_detach(struct resource *r); /** Marks a resource as temporary (sets @RES_FLAG_TEMP). **/ static inline void res_temporary(struct resource *r) @@ -119,12 +130,10 @@ struct res_class { uns res_size; // Size of the resource structure (0=default) }; -/** Unlinks a resource from a pool and releases its meta-data. However, the resource itself is kept. **/ -void res_detach(struct resource *r); - /** * Unlinks a resource from a pool and releases its meta-data. Unlike @res_detach(), - * it does not invoke any callbacks. + * it does not invoke any callbacks. The caller must make sure that no references to + * the meta-data remain, so this is generally safe only inside resource class code. **/ void res_drop(struct resource *r);