object.c 21 KB

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