kunit_alloc_...get_resource(9)
π kunit_alloc_and_get_resource(9) β from the Kernel API Manual, bestie π
okay so real talk, this one lives in the Kernel API Manual and it's lowkey one of my faves because it does your cleanup for you. no cap. let me put you on π
β¨ NAME β¨
kunit_alloc_and_get_resource β allocates and returns a test managed resource. that's it, that's the vibe π¦
πΈ SYNOPSIS πΈ
struct kunit_resource * kunit_alloc_and_get_resource (struct kunit *test
, kunit_resource_init_t init , kunit_resource_free_t free , gfp_t
internal_gfp , void *context );
like yes, that's a whole lot of parameters. breathe. we're gonna go through them one by one and it's gonna slay π
β ARGUMENTS β
testβ the test context object. this is your main character, the whole test lives here πinitβ a user supplied function to initialize the resource. you write it, you own it, icon behaviour honestly β¨freeβ a user supplied function to free the resource (if needed). the "(if needed)" is doing a lot of heavy lifting there, ngl πinternal_gfpβ thegfpto use for internal allocations. if you're unsure, useGFP_KERNEL. when in doubt,GFP_KERNEL, fr fr π₯contextβ for the user to pass in arbitrary data to theinitfunction. it's your little messenger bag of stuff π
π¦ DESCRIPTION π¦
so what it actually does: it allocates a test managed resource β meaning a resource that will automatically be cleaned up at the end of a test case. yes, automatic. no manual teardown spiral at 2am. we love her for that ππ
if you want an example of the resource itself, go peek at struct kunit_resource, there's one in there.
this function is effectively identical to kunit_alloc_resource, but with one iconic little difference: it returns the struct kunit_resource pointer, not just the 'data' pointer. that's the whole plot twist π«Ά
and because it hands you the resource itself, it also increments the resource's refcount. so heads up: you gotta call kunit_put_resource when you've finished with it. don't just walk away from her, that's rude π
π NOTE π
okay important one, bestie. KUnit needs to allocate memory for akunit_resourceobject. that's non-negotiable, that's just how it is. so you must specify aninternal_gfpthat is compatible with the use context of your resource. like, compatible-compatible. don't be out here passing something that doesn't match your context, it's giving undefined chaos π
π SEE ALSO π
Kernel file ./include/kunit/resource.h, plus the whole friend group:
struct kunit_resource(9)kunit_get_resource(9)kunit_put_resource(9)__kunit_add_resource(9)kunit_add_resource(9)kunit_add_named_resource(9)kunit_alloc_resource(9)kunit_resource_name_match(9)kunit_find_resource(9)kunit_find_named_resource(9)kunit_destroy_resource(9)kunit_remove_resource(9)KUNIT_DEFINE_ACTION_WRAPPER(9)kunit_add_action(9)kunit_add_action_or_reset(9)kunit_remove_action(9)kunit_release_action(9)
aaaand that's the whole page π
this one came out of ./include/kunit in October 2026 β so if you're reading this from the future, hiii, hope you called kunit_put_resource π€π bye bestie, go write some tests π§β¨
π¦ 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