object.c 21 KB

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