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: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass
2016-05-01 13:52:23 -06:00
parent 72a85c0d2d
commit a2040facd2
3 changed files with 24 additions and 0 deletions

View File

@@ -112,6 +112,8 @@ int device_unbind(struct udevice *dev)
devres_release_all(dev);
if (dev->flags & DM_NAME_ALLOCED)
free((char *)dev->name);
free(dev);
return 0;

View File

@@ -706,12 +706,18 @@ bool device_is_last_sibling(struct udevice *dev)
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)
{
name = strdup(name);
if (!name)
return -ENOMEM;
dev->name = name;
device_set_name_alloced(dev);
return 0;
}