2
0
mirror of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git synced 2025-09-04 20:19:47 +08:00

tools: ynl-gen: print alloc helper for multi-val attrs

In general YNL provides allocation and free helpers for types.
For pure nested structs which are used as multi-attr (and therefore
have to be allocated dynamically) we already print a free helper
as it's needed by free of the containing struct.

Add printing of the alloc helper for consistency. The helper
takes the number of entries to allocate as an argument, e.g.:

  static inline struct netdev_queue_id *netdev_queue_id_alloc(unsigned int n)
  {
	return calloc(n, sizeof(struct netdev_queue_id));
  }

Reviewed-by: Donald Hunter <donald.hunter@gmail.com>
Acked-by: Stanislav Fomichev <sdf@fomichev.me>
Link: https://patch.msgid.link/20250723171046.4027470-4-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Jakub Kicinski 2025-07-23 10:10:44 -07:00
parent cf58699777
commit 2c222dde61

View File

@ -2472,11 +2472,22 @@ def free_arg_name(direction):
return 'obj'
def print_alloc_wrapper(ri, direction):
def print_alloc_wrapper(ri, direction, struct=None):
name = op_prefix(ri, direction)
ri.cw.write_func_prot(f'static inline struct {name} *', f"{name}_alloc", [f"void"])
struct_name = name
if ri.type_name_conflict:
struct_name += '_'
args = ["void"]
cnt = "1"
if struct and struct.in_multi_val:
args = ["unsigned int n"]
cnt = "n"
ri.cw.write_func_prot(f'static inline struct {struct_name} *',
f"{name}_alloc", args)
ri.cw.block_start()
ri.cw.p(f'return calloc(1, sizeof(struct {name}));')
ri.cw.p(f'return calloc({cnt}, sizeof(struct {struct_name}));')
ri.cw.block_end()
@ -2547,6 +2558,8 @@ def print_type_full(ri, struct):
_print_type(ri, "", struct)
if struct.request and struct.in_multi_val:
print_alloc_wrapper(ri, "", struct)
ri.cw.nl()
free_rsp_nested_prototype(ri)
ri.cw.nl()