object.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-14 Bernard the first version
  9. * 2006-04-21 Bernard change the scheduler lock to interrupt lock
  10. * 2006-05-18 Bernard fix the object init bug
  11. * 2006-08-03 Bernard add hook support
  12. * 2007-01-28 Bernard rename RT_OBJECT_Class_Static to RT_Object_Class_Static
  13. * 2010-10-26 yi.qiu add module support in rt_object_allocate and rt_object_free
  14. * 2017-12-10 Bernard Add object_info enum.
  15. * 2018-01-25 Bernard Fix the object find issue when enable MODULE.
  16. * 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to object.c
  17. */
  18. #include <rtthread.h>
  19. #include <rthw.h>
  20. #ifdef RT_USING_MODULE
  21. #include <dlmodule.h>
  22. #endif /* RT_USING_MODULE */
  23. #ifdef RT_USING_SMART
  24. #include <lwp.h>
  25. #endif
  26. struct rt_custom_object
  27. {
  28. struct rt_object parent;
  29. rt_err_t (*destroy)(void *);
  30. void *data;
  31. };
  32. /*
  33. * define object_info for the number of _object_container items.
  34. */
  35. enum rt_object_info_type
  36. {
  37. RT_Object_Info_Thread = 0, /**< The object is a thread. */
  38. #ifdef RT_USING_SEMAPHORE
  39. RT_Object_Info_Semaphore, /**< The object is a semaphore. */
  40. #endif
  41. #ifdef RT_USING_MUTEX
  42. RT_Object_Info_Mutex, /**< The object is a mutex. */
  43. #endif
  44. #ifdef RT_USING_EVENT
  45. RT_Object_Info_Event, /**< The object is a event. */
  46. #endif
  47. #ifdef RT_USING_MAILBOX
  48. RT_Object_Info_MailBox, /**< The object is a mail box. */
  49. #endif
  50. #ifdef RT_USING_MESSAGEQUEUE
  51. RT_Object_Info_MessageQueue, /**< The object is a message queue. */
  52. #endif
  53. #ifdef RT_USING_MEMHEAP
  54. RT_Object_Info_MemHeap, /**< The object is a memory heap */
  55. #endif
  56. #ifdef RT_USING_MEMPOOL
  57. RT_Object_Info_MemPool, /**< The object is a memory pool. */
  58. #endif
  59. #ifdef RT_USING_DEVICE
  60. RT_Object_Info_Device, /**< The object is a device */
  61. #endif
  62. RT_Object_Info_Timer, /**< The object is a timer. */
  63. #ifdef RT_USING_MODULE
  64. RT_Object_Info_Module, /**< The object is a module. */
  65. #endif
  66. #ifdef RT_USING_HEAP
  67. RT_Object_Info_Memory, /**< The object is a memory. */
  68. #endif
  69. #ifdef RT_USING_SMART
  70. RT_Object_Info_Channel, /**< The object is a IPC channel */
  71. #endif
  72. #ifdef RT_USING_HEAP
  73. RT_Object_Info_Custom, /**< The object is a custom object */
  74. #endif
  75. RT_Object_Info_Unknown, /**< The object is unknown. */
  76. };
  77. #define _OBJ_CONTAINER_LIST_INIT(c) \
  78. {&(_object_container[c].object_list), &(_object_container[c].object_list)}
  79. static struct rt_object_information _object_container[RT_Object_Info_Unknown] =
  80. {
  81. /* initialize object container - thread */
  82. {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread), sizeof(struct rt_thread)},
  83. #ifdef RT_USING_SEMAPHORE
  84. /* initialize object container - semaphore */
  85. {RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Semaphore), sizeof(struct rt_semaphore)},
  86. #endif
  87. #ifdef RT_USING_MUTEX
  88. /* initialize object container - mutex */
  89. {RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Mutex), sizeof(struct rt_mutex)},
  90. #endif
  91. #ifdef RT_USING_EVENT
  92. /* initialize object container - event */
  93. {RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Event), sizeof(struct rt_event)},
  94. #endif
  95. #ifdef RT_USING_MAILBOX
  96. /* initialize object container - mailbox */
  97. {RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MailBox), sizeof(struct rt_mailbox)},
  98. #endif
  99. #ifdef RT_USING_MESSAGEQUEUE
  100. /* initialize object container - message queue */
  101. {RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MessageQueue), sizeof(struct rt_messagequeue)},
  102. #endif
  103. #ifdef RT_USING_MEMHEAP
  104. /* initialize object container - memory heap */
  105. {RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemHeap), sizeof(struct rt_memheap)},
  106. #endif
  107. #ifdef RT_USING_MEMPOOL
  108. /* initialize object container - memory pool */
  109. {RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemPool), sizeof(struct rt_mempool)},
  110. #endif
  111. #ifdef RT_USING_DEVICE
  112. /* initialize object container - device */
  113. {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Device), sizeof(struct rt_device)},
  114. #endif
  115. /* initialize object container - timer */
  116. {RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Timer), sizeof(struct rt_timer)},
  117. #ifdef RT_USING_MODULE
  118. /* initialize object container - module */
  119. {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Module), sizeof(struct rt_dlmodule)},
  120. #endif
  121. #ifdef RT_USING_HEAP
  122. /* initialize object container - small memory */
  123. {RT_Object_Class_Memory, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Memory), sizeof(struct rt_memory)},
  124. #endif
  125. #ifdef RT_USING_SMART
  126. /* initialize object container - module */
  127. {RT_Object_Class_Channel, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Channel), sizeof(struct rt_channel)},
  128. {RT_Object_Class_Custom, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Custom), sizeof(struct rt_custom_object)},
  129. #endif
  130. };
  131. #ifndef __on_rt_object_attach_hook
  132. #define __on_rt_object_attach_hook(obj) __ON_HOOK_ARGS(rt_object_attach_hook, (obj))
  133. #endif
  134. #ifndef __on_rt_object_detach_hook
  135. #define __on_rt_object_detach_hook(obj) __ON_HOOK_ARGS(rt_object_detach_hook, (obj))
  136. #endif
  137. #ifndef __on_rt_object_trytake_hook
  138. #define __on_rt_object_trytake_hook(parent) __ON_HOOK_ARGS(rt_object_trytake_hook, (parent))
  139. #endif
  140. #ifndef __on_rt_object_take_hook
  141. #define __on_rt_object_take_hook(parent) __ON_HOOK_ARGS(rt_object_take_hook, (parent))
  142. #endif
  143. #ifndef __on_rt_object_put_hook
  144. #define __on_rt_object_put_hook(parent) __ON_HOOK_ARGS(rt_object_put_hook, (parent))
  145. #endif
  146. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  147. static void (*rt_object_attach_hook)(struct rt_object *object);
  148. static void (*rt_object_detach_hook)(struct rt_object *object);
  149. void (*rt_object_trytake_hook)(struct rt_object *object);
  150. void (*rt_object_take_hook)(struct rt_object *object);
  151. void (*rt_object_put_hook)(struct rt_object *object);
  152. /**
  153. * @addtogroup Hook
  154. */
  155. /**@{*/
  156. /**
  157. * @brief This function will set a hook function, which will be invoked when object
  158. * attaches to kernel object system.
  159. *
  160. * @param hook is the hook function.
  161. */
  162. void rt_object_attach_sethook(void (*hook)(struct rt_object *object))
  163. {
  164. rt_object_attach_hook = hook;
  165. }
  166. /**
  167. * @brief This function will set a hook function, which will be invoked when object
  168. * detaches from kernel object system.
  169. *
  170. * @param hook is the hook function
  171. */
  172. void rt_object_detach_sethook(void (*hook)(struct rt_object *object))
  173. {
  174. rt_object_detach_hook = hook;
  175. }
  176. /**
  177. * @brief This function will set a hook function, which will be invoked when object
  178. * is taken from kernel object system.
  179. *
  180. * The object is taken means:
  181. * semaphore - semaphore is taken by thread
  182. * mutex - mutex is taken by thread
  183. * event - event is received by thread
  184. * mailbox - mail is received by thread
  185. * message queue - message is received by thread
  186. *
  187. * @param hook is the hook function.
  188. */
  189. void rt_object_trytake_sethook(void (*hook)(struct rt_object *object))
  190. {
  191. rt_object_trytake_hook = hook;
  192. }
  193. /**
  194. * @brief This function will set a hook function, which will be invoked when object
  195. * have been taken from kernel object system.
  196. *
  197. * The object have been taken means:
  198. * semaphore - semaphore have been taken by thread
  199. * mutex - mutex have been taken by thread
  200. * event - event have been received by thread
  201. * mailbox - mail have been received by thread
  202. * message queue - message have been received by thread
  203. * timer - timer is started
  204. *
  205. * @param hook the hook function.
  206. */
  207. void rt_object_take_sethook(void (*hook)(struct rt_object *object))
  208. {
  209. rt_object_take_hook = hook;
  210. }
  211. /**
  212. * @brief This function will set a hook function, which will be invoked when object
  213. * is put to kernel object system.
  214. *
  215. * @param hook is the hook function
  216. */
  217. void rt_object_put_sethook(void (*hook)(struct rt_object *object))
  218. {
  219. rt_object_put_hook = hook;
  220. }
  221. /**@}*/
  222. #endif /* RT_USING_HOOK */
  223. /**
  224. * @addtogroup KernelObject
  225. */
  226. /**@{*/
  227. /**
  228. * @brief This function will return the specified type of object information.
  229. *
  230. * @param type is the type of object, which can be
  231. * RT_Object_Class_Thread/Semaphore/Mutex... etc
  232. *
  233. * @return the object type information or RT_NULL
  234. */
  235. struct rt_object_information *
  236. rt_object_get_information(enum rt_object_class_type type)
  237. {
  238. int index;
  239. for (index = 0; index < RT_Object_Info_Unknown; index ++)
  240. if (_object_container[index].type == type) return &_object_container[index];
  241. return RT_NULL;
  242. }
  243. RTM_EXPORT(rt_object_get_information);
  244. /**
  245. * @brief This function will return the length of object list in object container.
  246. *
  247. * @param type is the type of object, which can be
  248. * RT_Object_Class_Thread/Semaphore/Mutex... etc
  249. *
  250. * @return the length of object list
  251. */
  252. int rt_object_get_length(enum rt_object_class_type type)
  253. {
  254. int count = 0;
  255. rt_base_t level;
  256. struct rt_list_node *node = RT_NULL;
  257. struct rt_object_information *information = RT_NULL;
  258. information = rt_object_get_information((enum rt_object_class_type)type);
  259. if (information == RT_NULL) return 0;
  260. level = rt_hw_interrupt_disable();
  261. /* get the count of objects */
  262. rt_list_for_each(node, &(information->object_list))
  263. {
  264. count ++;
  265. }
  266. rt_hw_interrupt_enable(level);
  267. return count;
  268. }
  269. RTM_EXPORT(rt_object_get_length);
  270. /**
  271. * @brief This function will copy the object pointer of the specified type,
  272. * with the maximum size specified by maxlen.
  273. *
  274. * @param type is the type of object, which can be
  275. * RT_Object_Class_Thread/Semaphore/Mutex... etc
  276. *
  277. * @param pointers is the pointer will be saved to.
  278. *
  279. * @param maxlen is the maximum number of pointers can be saved.
  280. *
  281. * @return the copied number of object pointers.
  282. */
  283. int rt_object_get_pointers(enum rt_object_class_type type, rt_object_t *pointers, int maxlen)
  284. {
  285. int index = 0;
  286. rt_base_t level;
  287. struct rt_object *object;
  288. struct rt_list_node *node = RT_NULL;
  289. struct rt_object_information *information = RT_NULL;
  290. if (maxlen <= 0) return 0;
  291. information = rt_object_get_information((enum rt_object_class_type)type);
  292. if (information == RT_NULL) return 0;
  293. level = rt_hw_interrupt_disable();
  294. /* retrieve pointer of object */
  295. rt_list_for_each(node, &(information->object_list))
  296. {
  297. object = rt_list_entry(node, struct rt_object, list);
  298. pointers[index] = object;
  299. index ++;
  300. if (index >= maxlen) break;
  301. }
  302. rt_hw_interrupt_enable(level);
  303. return index;
  304. }
  305. RTM_EXPORT(rt_object_get_pointers);
  306. /**
  307. * @brief This function will initialize an object and add it to object system
  308. * management.
  309. *
  310. * @param object is the specified object to be initialized.
  311. *
  312. * @param type is the object type.
  313. *
  314. * @param name is the object name. In system, the object's name must be unique.
  315. */
  316. void rt_object_init(struct rt_object *object,
  317. enum rt_object_class_type type,
  318. const char *name)
  319. {
  320. rt_base_t level;
  321. #ifdef RT_DEBUG
  322. struct rt_list_node *node = RT_NULL;
  323. #endif
  324. struct rt_object_information *information;
  325. #ifdef RT_USING_MODULE
  326. struct rt_dlmodule *module = dlmodule_self();
  327. #endif /* RT_USING_MODULE */
  328. /* get object information */
  329. information = rt_object_get_information(type);
  330. RT_ASSERT(information != RT_NULL);
  331. #ifdef RT_DEBUG
  332. /* check object type to avoid re-initialization */
  333. /* enter critical */
  334. rt_enter_critical();
  335. /* try to find object */
  336. for (node = information->object_list.next;
  337. node != &(information->object_list);
  338. node = node->next)
  339. {
  340. struct rt_object *obj;
  341. obj = rt_list_entry(node, struct rt_object, list);
  342. RT_ASSERT(obj != object);
  343. }
  344. /* leave critical */
  345. rt_exit_critical();
  346. #endif
  347. /* initialize object's parameters */
  348. /* set object type to static */
  349. object->type = type | RT_Object_Class_Static;
  350. #if RT_NAME_MAX > 0
  351. rt_strncpy(object->name, name, RT_NAME_MAX); /* copy name */
  352. #else
  353. object->name = name;
  354. #endif /* RT_NAME_MAX > 0 */
  355. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  356. /* lock interrupt */
  357. level = rt_hw_interrupt_disable();
  358. #ifdef RT_USING_MODULE
  359. if (module)
  360. {
  361. rt_list_insert_after(&(module->object_list), &(object->list));
  362. object->module_id = (void *)module;
  363. }
  364. else
  365. #endif /* RT_USING_MODULE */
  366. {
  367. /* insert object into information object list */
  368. rt_list_insert_after(&(information->object_list), &(object->list));
  369. }
  370. /* unlock interrupt */
  371. rt_hw_interrupt_enable(level);
  372. }
  373. /**
  374. * @brief This function will detach a static object from object system,
  375. * and the memory of static object is not freed.
  376. *
  377. * @param object the specified object to be detached.
  378. */
  379. void rt_object_detach(rt_object_t object)
  380. {
  381. rt_base_t level;
  382. /* object check */
  383. RT_ASSERT(object != RT_NULL);
  384. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  385. /* reset object type */
  386. object->type = 0;
  387. /* lock interrupt */
  388. level = rt_hw_interrupt_disable();
  389. /* remove from old list */
  390. rt_list_remove(&(object->list));
  391. /* unlock interrupt */
  392. rt_hw_interrupt_enable(level);
  393. }
  394. #ifdef RT_USING_HEAP
  395. /**
  396. * @brief This function will allocate an object from object system.
  397. *
  398. * @param type is the type of object.
  399. *
  400. * @param name is the object name. In system, the object's name must be unique.
  401. *
  402. * @return object
  403. */
  404. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
  405. {
  406. struct rt_object *object;
  407. rt_base_t level;
  408. struct rt_object_information *information;
  409. #ifdef RT_USING_MODULE
  410. struct rt_dlmodule *module = dlmodule_self();
  411. #endif /* RT_USING_MODULE */
  412. RT_DEBUG_NOT_IN_INTERRUPT;
  413. /* get object information */
  414. information = rt_object_get_information(type);
  415. RT_ASSERT(information != RT_NULL);
  416. object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
  417. if (object == RT_NULL)
  418. {
  419. /* no memory can be allocated */
  420. return RT_NULL;
  421. }
  422. /* clean memory data of object */
  423. rt_memset(object, 0x0, information->object_size);
  424. /* initialize object's parameters */
  425. /* set object type */
  426. object->type = type;
  427. /* set object flag */
  428. object->flag = 0;
  429. #if RT_NAME_MAX > 0
  430. rt_strncpy(object->name, name, RT_NAME_MAX); /* copy name */
  431. #else
  432. object->name = name;
  433. #endif /* RT_NAME_MAX > 0 */
  434. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  435. /* lock interrupt */
  436. level = rt_hw_interrupt_disable();
  437. #ifdef RT_USING_MODULE
  438. if (module)
  439. {
  440. rt_list_insert_after(&(module->object_list), &(object->list));
  441. object->module_id = (void *)module;
  442. }
  443. else
  444. #endif /* RT_USING_MODULE */
  445. {
  446. /* insert object into information object list */
  447. rt_list_insert_after(&(information->object_list), &(object->list));
  448. }
  449. /* unlock interrupt */
  450. rt_hw_interrupt_enable(level);
  451. /* return object */
  452. return object;
  453. }
  454. /**
  455. * @brief This function will delete an object and release object memory.
  456. *
  457. * @param object is the specified object to be deleted.
  458. */
  459. void rt_object_delete(rt_object_t object)
  460. {
  461. rt_base_t level;
  462. /* object check */
  463. RT_ASSERT(object != RT_NULL);
  464. RT_ASSERT(!(object->type & RT_Object_Class_Static));
  465. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  466. /* reset object type */
  467. object->type = RT_Object_Class_Null;
  468. /* lock interrupt */
  469. level = rt_hw_interrupt_disable();
  470. /* remove from old list */
  471. rt_list_remove(&(object->list));
  472. /* unlock interrupt */
  473. rt_hw_interrupt_enable(level);
  474. /* free the memory of object */
  475. RT_KERNEL_FREE(object);
  476. }
  477. #endif /* RT_USING_HEAP */
  478. /**
  479. * @brief This function will judge the object is system object or not.
  480. *
  481. * @note Normally, the system object is a static object and the type
  482. * of object set to RT_Object_Class_Static.
  483. *
  484. * @param object is the specified object to be judged.
  485. *
  486. * @return RT_TRUE if a system object, RT_FALSE for others.
  487. */
  488. rt_bool_t rt_object_is_systemobject(rt_object_t object)
  489. {
  490. /* object check */
  491. RT_ASSERT(object != RT_NULL);
  492. if (object->type & RT_Object_Class_Static)
  493. return RT_TRUE;
  494. return RT_FALSE;
  495. }
  496. /**
  497. * @brief This function will return the type of object without
  498. * RT_Object_Class_Static flag.
  499. *
  500. * @param object is the specified object to be get type.
  501. *
  502. * @return the type of object.
  503. */
  504. rt_uint8_t rt_object_get_type(rt_object_t object)
  505. {
  506. /* object check */
  507. RT_ASSERT(object != RT_NULL);
  508. return object->type & ~RT_Object_Class_Static;
  509. }
  510. /**
  511. * @brief This function will find specified name object from object
  512. * container.
  513. *
  514. * @param name is the specified name of object.
  515. *
  516. * @param type is the type of object
  517. *
  518. * @return the found object or RT_NULL if there is no this object
  519. * in object container.
  520. *
  521. * @note this function shall not be invoked in interrupt status.
  522. */
  523. rt_object_t rt_object_find(const char *name, rt_uint8_t type)
  524. {
  525. struct rt_object *object = RT_NULL;
  526. struct rt_list_node *node = RT_NULL;
  527. struct rt_object_information *information = RT_NULL;
  528. information = rt_object_get_information((enum rt_object_class_type)type);
  529. /* parameter check */
  530. if ((name == RT_NULL) || (information == RT_NULL)) return RT_NULL;
  531. /* which is invoke in interrupt status */
  532. RT_DEBUG_NOT_IN_INTERRUPT;
  533. /* enter critical */
  534. rt_enter_critical();
  535. /* try to find object */
  536. rt_list_for_each(node, &(information->object_list))
  537. {
  538. object = rt_list_entry(node, struct rt_object, list);
  539. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  540. {
  541. /* leave critical */
  542. rt_exit_critical();
  543. return object;
  544. }
  545. }
  546. /* leave critical */
  547. rt_exit_critical();
  548. return RT_NULL;
  549. }
  550. #ifdef RT_USING_HEAP
  551. /**
  552. * This function will create a custom object
  553. * container.
  554. *
  555. * @param name the specified name of object.
  556. * @param data the custom data
  557. * @param data_destroy the custom object destroy callback
  558. *
  559. * @return the found object or RT_NULL if there is no this object
  560. * in object container.
  561. *
  562. * @note this function shall not be invoked in interrupt status.
  563. */
  564. rt_object_t rt_custom_object_create(const char *name, void *data, rt_err_t (*data_destroy)(void *))
  565. {
  566. struct rt_custom_object *cobj = RT_NULL;
  567. cobj = (struct rt_custom_object *)rt_object_allocate(RT_Object_Class_Custom, name);
  568. if (!cobj)
  569. {
  570. return RT_NULL;
  571. }
  572. cobj->destroy = data_destroy;
  573. cobj->data = data;
  574. return (struct rt_object *)cobj;
  575. }
  576. /**
  577. * This function will destroy a custom object
  578. * container.
  579. *
  580. * @param obj the specified name of object.
  581. *
  582. * @note this function shall not be invoked in interrupt status.
  583. */
  584. rt_err_t rt_custom_object_destroy(rt_object_t obj)
  585. {
  586. rt_err_t ret = -1;
  587. struct rt_custom_object *cobj = (struct rt_custom_object *)obj;
  588. if (obj && obj->type == RT_Object_Class_Custom)
  589. {
  590. if (cobj->destroy)
  591. {
  592. ret = cobj->destroy(cobj->data);
  593. }
  594. rt_object_delete(obj);
  595. }
  596. return ret;
  597. }
  598. #endif
  599. /**@}*/