refcount_sub_and_test(9)
๐ refcount_sub_and_test(9) โ from the Kernel API Manual, bestie
okay so this is refcount_sub_and_test(9) and honestly it's giving "math but make it kernel" ๐งฎโจ
โจ SYNOPSIS โจ
bool refcount_sub_and_test (int i , refcount_t *r );
like, you hand it a number and a refcount, and it hands back a bool. slay.
๐ ARGUMENTS ๐
iโ the amount you wanna subtract from the refcount. how much we're taking off. ๐rโ the refcount itself. the main character. the one we're subtracting from. ๐
๐ธ DESCRIPTION ๐ธ
so this one is similar to atomic_dec_and_test, but bestie, it has OPINIONS. like, it will not just quietly do the thing. instead:
- it will
WARNon underflow ๐ญ - it will return
falseon underflow - it will ultimately leak on underflow (yes, leak. fr fr. that's the vibe. ๐)
- and it will fail to decrement when it's saturated at
REFCOUNT_SATURATED
so like... it's the cautious one. it's not gonna let you vibe your way into an underflow without saying something about it. ๐จ
it also handles the memory ordering for you, which is honestly so thoughtful:
- it provides release memory ordering, so prior loads and stores are done before โ like, everything that came before is wrapped up and put away first ๐
- and on success it provides acquire ordering, so
freemust come after. no cutting in line, periodt. โ
โ ๏ธ but here's the thing: use of this function is not recommended for the normal reference counting use case โ you know, the super common one where references are taken and released one at a time. ๐ซถ
in those cases you should use refcount_dec, or one of its variants, to decrement a reference count instead. like, that's the recommended route. refcount_sub_and_test is not the moment for your normal one-at-a-time situation. iykyk ๐
๐ RETURN ๐
trueโ if the resulting refcount is0๐falseโ otherwise ๐
๐ฆ SEE ALSO ๐ฆ
Kernel file ./include/linux/refcount.h โ go read it, it's iconic ๐
plus all these besties:
refcount_set(9)refcount_set_release(9)refcount_read(9)refcount_add_not_zero(9)refcount_add_not_zero_acquire(9)refcount_add(9)refcount_inc_not_zero(9)refcount_inc_not_zero_acquire(9)refcount_inc(9)refcount_dec_and_test(9)refcount_dec(9)
that's the whole page, bestie! ๐ straight from ./include/linux, dated October 2026, and yeah โ it's refcount_sub_and_test(9), signed, sealed, delivered. mwah ๐ซถโจ๐
๐ฆ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms