Skip to content
Snippets Groups Projects
Commit a2040fac authored by Simon Glass's avatar Simon Glass
Browse files

dm: core: Allow device names to be freed automatically


Some devices have a name that is stored in allocated memory. At present
there is no mechanism to free this memory when the device is unbound.

Add a device flag to track whether a name is allocated and a function to
add the flag. Free the memory when the device is unbound.

Signed-off-by: default avatarSimon Glass <sjg@chromium.org>
parent 72a85c0d
No related branches found
No related tags found
No related merge requests found
...@@ -112,6 +112,8 @@ int device_unbind(struct udevice *dev) ...@@ -112,6 +112,8 @@ int device_unbind(struct udevice *dev)
devres_release_all(dev); devres_release_all(dev);
if (dev->flags & DM_NAME_ALLOCED)
free((char *)dev->name);
free(dev); free(dev);
return 0; return 0;
......
...@@ -706,12 +706,18 @@ bool device_is_last_sibling(struct udevice *dev) ...@@ -706,12 +706,18 @@ bool device_is_last_sibling(struct udevice *dev)
return list_is_last(&dev->sibling_node, &parent->child_head); return list_is_last(&dev->sibling_node, &parent->child_head);
} }
void device_set_name_alloced(struct udevice *dev)
{
dev->flags |= DM_NAME_ALLOCED;
}
int device_set_name(struct udevice *dev, const char *name) int device_set_name(struct udevice *dev, const char *name)
{ {
name = strdup(name); name = strdup(name);
if (!name) if (!name)
return -ENOMEM; return -ENOMEM;
dev->name = name; dev->name = name;
device_set_name_alloced(dev);
return 0; return 0;
} }
...@@ -41,6 +41,9 @@ struct driver_info; ...@@ -41,6 +41,9 @@ struct driver_info;
/* Device is bound */ /* Device is bound */
#define DM_FLAG_BOUND (1 << 6) #define DM_FLAG_BOUND (1 << 6)
/* Device name is allocated and should be freed on unbind() */
#define DM_NAME_ALLOCED (1 << 7)
/** /**
* struct udevice - An instance of a driver * struct udevice - An instance of a driver
* *
...@@ -523,6 +526,9 @@ bool device_is_last_sibling(struct udevice *dev); ...@@ -523,6 +526,9 @@ bool device_is_last_sibling(struct udevice *dev);
* this is unnecessary but for probed devices which don't get a useful name * this is unnecessary but for probed devices which don't get a useful name
* this function can be helpful. * this function can be helpful.
* *
* The name is allocated and will be freed automatically when the device is
* unbound.
*
* @dev: Device to update * @dev: Device to update
* @name: New name (this string is allocated new memory and attached to * @name: New name (this string is allocated new memory and attached to
* the device) * the device)
...@@ -531,6 +537,16 @@ bool device_is_last_sibling(struct udevice *dev); ...@@ -531,6 +537,16 @@ bool device_is_last_sibling(struct udevice *dev);
*/ */
int device_set_name(struct udevice *dev, const char *name); int device_set_name(struct udevice *dev, const char *name);
/**
* device_set_name_alloced() - note that a device name is allocated
*
* This sets the DM_NAME_ALLOCED flag for the device, so that when it is
* unbound the name will be freed. This avoids memory leaks.
*
* @dev: Device to update
*/
void device_set_name_alloced(struct udevice *dev);
/** /**
* device_is_on_pci_bus - Test if a device is on a PCI bus * device_is_on_pci_bus - Test if a device is on a PCI bus
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment