From 8ff8436fcde1d525cea6719b1f16cd29bc22c617 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Sun, 24 Dec 2017 00:00:18 +0800 Subject: [Kernel] Add device_create/destroy api --- src/device.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) (limited to 'src/device.c') diff --git a/src/device.c b/src/device.c index 3b707d338e..15e44e59d1 100644 --- a/src/device.c +++ b/src/device.c @@ -108,14 +108,13 @@ rt_device_t rt_device_find(const char *name) struct rt_list_node *node; struct rt_object_information *information; - extern struct rt_object_information rt_object_container[]; - /* enter critical */ if (rt_thread_self() != RT_NULL) rt_enter_critical(); /* try to find device object */ - information = &rt_object_container[RT_Object_Class_Device]; + information = rt_object_get_information(RT_Object_Class_Device); + RT_ASSERT(information != RT_NULL); for (node = information->object_list.next; node != &(information->object_list); node = node->next) @@ -140,6 +139,50 @@ rt_device_t rt_device_find(const char *name) } RTM_EXPORT(rt_device_find); +#ifdef RT_USING_HEAP +/** + * This function creates a device object with user data size. + * + * @param type, the kind type of this device object. + * @param attach_size, the size of user data. + * + * @return the allocated device object, or RT_NULL when failed. + */ +rt_device_t rt_device_create(int type, int attach_size) +{ + int size; + rt_device_t device; + + size = RT_ALIGN(sizeof(struct rt_device), RT_ALIGN_SIZE); + size += attach_size; + + device = (rt_device_t)rt_malloc(size); + if (device) + { + rt_memset(device, 0x0, sizeof(struct rt_device)); + device->type = type; + } + + return device; +} +RTM_EXPORT(rt_device_create); + +/** + * This function destroy the specific device object. + * + * @param device, the specific device object. + */ +void rt_device_destroy(rt_device_t device) +{ + /* unregister device firstly */ + rt_device_unregister(device); + + /* release this device object */ + rt_free(device); +} +RTM_EXPORT(rt_device_destroy); +#endif + /** * This function will initialize the specified device * -- cgit v1.3.1