STRUCT KUNIT_RESOURCE(9)
π struct kunit_resource(9) π
okay bestie, this is the Kernel API Manual entry for struct kunit_resource(9). Itβs giving βtest managed resourceβ main character energy, and honestly? iconic. β¨
β¨ NAME β¨
struct kunit_resource β represents a test managed resource π
π¦ SYNOPSIS π¦
struct kunit_resource {
void *data;
const char *name;
kunit_resource_free_t free;
};
π Members π
dataβ a spot for you, the user, to store arbitrary data. no cap, itβs your little stash πnameβ optional name πfreeβ a user-supplied function that frees the resource. this is the cleanup bestie π₯
π Description π
So struct kunit_resource represents a test managed resource, meaning a resource that gets automatically cleaned up at the end of a test case. That cleanup is performed by the free function. The struct kunit_resource itself is freed automatically with kfree if it was allocated by KUnit (e.g., by kunit_alloc_resource), but it must be freed by the user otherwise. periodt. π
Resources are reference counted, so if a resource is retrieved via kunit_alloc_and_get_resource or kunit_find_resource, we need to call kunit_put_resource to reduce the resource reference count when weβre finished with it. Note that kunit_alloc_resource does not require a kunit_resource_put because it does not retrieve the resource itself. yes, thatβs the exact spelling on the page β iykyk π
πΈ Example πΈ
struct kunit_kmalloc_params {
size_t size;
gfp_t gfp;
};
static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
{
struct kunit_kmalloc_params *params = context;
res->data = kmalloc(params->size, params->gfp);
if (!res->data)
return -ENOMEM;
return 0;
}
static void kunit_kmalloc_free(struct kunit_resource *res)
{
kfree(res->data);
}
void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
{
struct kunit_kmalloc_params params;
params.size = size;
params.gfp = gfp;
return kunit_alloc_resource(test, kunit_kmalloc_init,
kunit_kmalloc_free, gfp, params);
}
Resources can also be named, with lookup/removal done on a name basis also: kunit_add_named_resource, kunit_find_named_resource and kunit_destroy_named_resource. Resource names must be unique within the test instance. no duplicates, bestie β thatβs the vibe check π¦
π₯ SEE ALSO π₯
- Kernel file
./include/kunit/resource.h kunit_get_resource(9)kunit_put_resource(9)__kunit_add_resource(9)kunit_add_resource(9)kunit_add_named_resource(9)kunit_alloc_and_get_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)
mwah π thatβs the whole page, bestie β struct kunit_resource(9), sourced from ./include/kunit, dated October 2026. slay π«Ά
π¦ 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