Skip to contentSkip to conversation

ALSA: hda: allocate nodes with tree

PATCH Applied Rosen Penev 2 participants 3 messages Apr 25 – Apr 27 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: hda: allocate nodes with tree

Use a flexible array member to combine allocations. Simplifies allocation and freeing.

---
 sound/hda/core/sysfs.c | 22 ++++------------------
 1 file changed, 4 insertions(+), 18 deletions(-)
sound/hda/core/sysfs.c +4 -18
index 3e102538e058..f14ce1203a9f 100644
@@ -13,7 +13,7 @@
 struct hdac_widget_tree {
 	struct kobject *root;
 	struct kobject *afg;
-	struct kobject **nodes;
+	struct kobject *nodes[];
 };
 
 #define CODEC_ATTR(type)					\
@@ -325,11 +325,8 @@ static void widget_tree_free(struct hdac_device *codec)
 	if (!tree)
 		return;
 	free_widget_node(tree->afg, &widget_afg_group);
-	if (tree->nodes) {
-		for (p = tree->nodes; *p; p++)
-			free_widget_node(*p, &widget_node_group);
-		kfree(tree->nodes);
-	}
+	for (p = tree->nodes; *p; p++)
+		free_widget_node(*p, &widget_node_group);
 	kobject_put(tree->root);
 	kfree(tree);
 	codec->widgets = NULL;
@@ -366,7 +363,7 @@ static int widget_tree_create(struct hdac_device *codec)
 	int i, err;
 	hda_nid_t nid;
 
-	tree = codec->widgets = kzalloc_obj(*tree);
+	tree = codec->widgets = kzalloc_flex(*tree, nodes, codec->num_nodes + 1);
 	if (!tree)
 		return -ENOMEM;
 
@@ -374,10 +371,6 @@ static int widget_tree_create(struct hdac_device *codec)
 	if (!tree->root)
 		return -ENOMEM;
 
-	tree->nodes = kzalloc_objs(*tree->nodes, codec->num_nodes + 1);
-	if (!tree->nodes)
-		return -ENOMEM;
-
 	for (i = 0, nid = codec->start_nid; i < codec->num_nodes; i++, nid++) {
 		err = add_widget_node(tree->root, nid, &widget_node_group,
 				      &tree->nodes[i]);
@@ -435,12 +428,6 @@ int hda_widget_sysfs_reinit(struct hdac_device *codec,
 	if (!tree)
 		return -ENOMEM;
 
-	tree->nodes = kzalloc_objs(*tree->nodes, num_nodes + 1);
-	if (!tree->nodes) {
-		kfree(tree);
-		return -ENOMEM;
-	}
-
 	/* prune non-existing nodes */
 	for (i = 0, nid = codec->start_nid; i < codec->num_nodes; i++, nid++) {
 		if (nid < start_nid || nid >= end_nid)
@@ -459,7 +446,6 @@ int hda_widget_sysfs_reinit(struct hdac_device *codec,
 	}
 
 	/* replace with the new tree */
-	kfree(codec->widgets->nodes);
 	kfree(codec->widgets);
 	codec->widgets = tree;
 
Signature · 1 line
2.54.0
Applied
#1 lore raw
Quoted 5 lines · Rosen Penev

Use a flexible array member to combine allocations. Simplifies allocation and freeing.

Applied to for-next branch now. Thanks.

Takashi

#2 lore raw
Quoted 10 lines · Takashi Iwai
Quoted 5 lines · Rosen Penev

Use a flexible array member to combine allocations. Simplifies allocation and freeing.

Applied to for-next branch now. Thanks.

Scratch that. I re-read the code, and noticed that it doesn't handle the case in hda_widget_sysfs_reinit(). It can resize and reallocate the node table, hence the embedded flex array won't work.

thanks,

Takashi