We are simply incapable of doing anything in a simple way. I see James' point but I disagree with his implementation.
I would recommend setting the refcount to 1 on allocate and doing just > if (mutextp->refcount_dec() == 0) { > mutextp->free(); > } If you are doing this because you presume the mutex might be shared with other internals, then you *really* need to make sure the initial refcount is 1. And if you do that, you shouldn't check for 0 (in fact, you should sdk_assert() the ref count is > 0). As a side note, I utterly fail to see the point of REF_COUNT_OBJ_REFCOUNT_DEC as other than to be discarded as soon as possible legacy cruft.