devm_kstrdup_const(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… devm_kstrdup_const(9) β€” from the Kernel API Manual, bestie

okay so we're in the Kernel API Manual today and the star of the show is devm_kstrdup_const ✨ it's that girl who duplicates your string, but only when she has to, and then cleans up after herself. very demure, very mindful.


✨ NAME ✨

devm_kstrdup_const β€” resource managed conditional string duplication πŸŽ€

like, it duplicates a string, but conditionally, and it's managed by the device resource system. the whole package fr fr.


🌸 SYNOPSIS 🌸

const char * devm_kstrdup_const (struct device *dev , const char *s ,
gfp_t gfp );

that's the whole signature, no notes πŸ‘‘


πŸ¦‹ ARGUMENTS πŸ¦‹

  • dev β€” the device you're duplicating the string for. it's the main character here, everything gets tied to it 🫢
  • s β€” the string to duplicate. the actual content, the moment, the vibe πŸ’œ
  • gfp β€” the GFP mask used in the kmalloc call when allocating memory. yes she does actually call kmalloc, we'll get to that 🌈

πŸ’– DESCRIPTION πŸ’–

so here's the tea β˜• β€” anything allocated by devm_kstrdup_const gets automatically freed when the associated device is detached. no manual kfree, no leak anxiety, no 3am debugging session. the devres system handles it. periodt πŸ’…


⭐ RETURN ⭐

you get the source string itself back if it lives in the .rodata section β€” no allocation, she just points at it, iconic β€” otherwise it falls back to devm_kstrdup. so it's giving "I'll only do work if I genuinely have to" 😭πŸ”₯


πŸ‘‘ SEE ALSO πŸ‘‘

the kernel file ./drivers/base/devres.c and honestly the whole devres friend group:

go read them, iykyk πŸ“šπŸ’«


ngl bestie, .rodata means your string is literally constant forever, so duplicating it would be a whole waste of a kmalloc. that's exactly why this function exists. no cap πŸ™ˆ

okk that's the whole page!! brought to you from ./drivers/base, dated October 2026, and the page itself is devm_kstrdup_const(9) β€” slay, go free your strings (or don't, she's already handled πŸ’…πŸŒΈπŸ’–)

πŸ“¦ where this page comes from