From 50a97669cf62b443aec80ac2a88297db817bf105 Mon Sep 17 00:00:00 2001 From: BernardXiong Date: Tue, 26 Mar 2019 13:49:18 +0000 Subject: [Kernel] Add object re-initialization check. --- src/object.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/object.c') diff --git a/src/object.c b/src/object.c index 70b9a5c04e..050bb17757 100644 --- a/src/object.c +++ b/src/object.c @@ -251,6 +251,9 @@ void rt_object_init(struct rt_object *object, /* initialize object's parameters */ + /* check object type to avoid re-initialization */ + RT_ASSERT(object->type != (type | RT_Object_Class_Static)); + /* set object type to static */ object->type = type | RT_Object_Class_Static; -- cgit v1.3.1 From 4d3b4abfebc0a4a98c814e66cee807918dcad53d Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Tue, 2 Apr 2019 17:33:07 +0800 Subject: [Kernel] To check object re-initialization in the list --- src/object.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/object.c') diff --git a/src/object.c b/src/object.c index 050bb17757..e85737ce8f 100644 --- a/src/object.c +++ b/src/object.c @@ -240,6 +240,7 @@ void rt_object_init(struct rt_object *object, const char *name) { register rt_base_t temp; + struct rt_list_node *node = RT_NULL; struct rt_object_information *information; #ifdef RT_USING_MODULE struct rt_dlmodule *module = dlmodule_self(); @@ -249,14 +250,26 @@ void rt_object_init(struct rt_object *object, information = rt_object_get_information(type); RT_ASSERT(information != RT_NULL); - /* initialize object's parameters */ - /* check object type to avoid re-initialization */ - RT_ASSERT(object->type != (type | RT_Object_Class_Static)); + /* enter critical */ + rt_enter_critical(); + /* try to find object */ + for (node = information->object_list.next; + node != &(information->object_list); + node = node->next) + { + struct rt_object *obj; + + obj = rt_list_entry(node, struct rt_object, list); + RT_ASSERT(obj != object); + } + /* leave critical */ + rt_exit_critical(); + + /* initialize object's parameters */ /* set object type to static */ object->type = type | RT_Object_Class_Static; - /* copy name */ rt_strncpy(object->name, name, RT_NAME_MAX); -- cgit v1.3.1 From 3463a756d9ece07266be5636a7b2cd45cadbe784 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Fri, 7 Jun 2019 22:44:24 +0800 Subject: [kernel] skip warning when disable debug. --- src/object.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/object.c') diff --git a/src/object.c b/src/object.c index e85737ce8f..4ee2f6a1a1 100644 --- a/src/object.c +++ b/src/object.c @@ -262,7 +262,10 @@ void rt_object_init(struct rt_object *object, struct rt_object *obj; obj = rt_list_entry(node, struct rt_object, list); - RT_ASSERT(obj != object); + if (obj) /* skip warning when disable debug */ + { + RT_ASSERT(obj != object); + } } /* leave critical */ rt_exit_critical(); -- cgit v1.3.1 From b1eb3943a3ab1e4b39574d01eb98480afabb29d1 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Fri, 10 Apr 2020 00:01:58 +0800 Subject: [Kernel] Add rt_object_get_length/rt_object_get_pointers APIs. --- include/rtthread.h | 3 ++ src/object.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 79 insertions(+), 11 deletions(-) (limited to 'src/object.c') diff --git a/include/rtthread.h b/include/rtthread.h index 70d4f38896..a76601a3bf 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -41,6 +41,9 @@ extern "C" { void rt_system_object_init(void); struct rt_object_information * rt_object_get_information(enum rt_object_class_type type); +int rt_object_get_length(enum rt_object_class_type type); +int rt_object_get_pointers(enum rt_object_class_type type, rt_object_t *pointers, int maxlen); + void rt_object_init(struct rt_object *object, enum rt_object_class_type type, const char *name); diff --git a/src/object.c b/src/object.c index 4ee2f6a1a1..62d70816dc 100644 --- a/src/object.c +++ b/src/object.c @@ -212,7 +212,9 @@ void rt_system_object_init(void) /** * This function will return the specified type of object information. * - * @param type the type of object + * @param type the type of object, which can be + * RT_Object_Class_Thread/Semaphore/Mutex... etc + * * @return the object type information or RT_NULL */ struct rt_object_information * @@ -227,6 +229,75 @@ rt_object_get_information(enum rt_object_class_type type) } RTM_EXPORT(rt_object_get_information); +/** + * This function will return the length of object list in object container. + * + * @param type the type of object, which can be + * RT_Object_Class_Thread/Semaphore/Mutex... etc + * @return the length of object list + */ +int rt_object_get_length(enum rt_object_class_type type) +{ + int count = 0; + rt_ubase_t level; + struct rt_list_node *node = RT_NULL; + struct rt_object_information *information = RT_NULL; + + information = rt_object_get_information((enum rt_object_class_type)type); + if (information == RT_NULL) return 0; + + level = rt_hw_interrupt_disable(); + /* get the count of objects */ + rt_list_for_each(node, &(information->object_list)) + { + count ++; + } + rt_hw_interrupt_enable(level); + + return count; +} +RTM_EXPORT(rt_object_get_length); + +/** + * This function will copy the object pointer of the specified type, + * with the maximum size specified by maxlen. + * + * @param type the type of object, which can be + * RT_Object_Class_Thread/Semaphore/Mutex... etc + * @param pointers the pointers will be saved to + * @param maxlen the maximum number of pointers can be saved + * + * @return the copied number of object pointers + */ +int rt_object_get_pointers(enum rt_object_class_type type, rt_object_t *pointers, int maxlen) +{ + int index = 0; + rt_ubase_t level; + + struct rt_object *object; + struct rt_list_node *node = RT_NULL; + struct rt_object_information *information = RT_NULL; + + if (maxlen <= 0) return 0; + + information = rt_object_get_information((enum rt_object_class_type)type); + if (information == RT_NULL) return 0; + + level = rt_hw_interrupt_disable(); + /* retrieve pointer of object */ + rt_list_for_each(node, &(information->object_list)) + { + object = rt_list_entry(node, struct rt_object, list); + + pointers[index] = object; + index ++; + } + rt_hw_interrupt_enable(level); + + return index; +} +RTM_EXPORT(rt_object_get_pointers); + /** * This function will initialize an object and add it to object system * management. @@ -482,9 +553,10 @@ rt_object_t rt_object_find(const char *name, rt_uint8_t type) struct rt_list_node *node = RT_NULL; struct rt_object_information *information = RT_NULL; + information = rt_object_get_information((enum rt_object_class_type)type); + /* parameter check */ - if ((name == RT_NULL) || (type > RT_Object_Class_Unknown)) - return RT_NULL; + if ((name == RT_NULL) || (information == RT_NULL)) return RT_NULL; /* which is invoke in interrupt status */ RT_DEBUG_NOT_IN_INTERRUPT; @@ -493,14 +565,7 @@ rt_object_t rt_object_find(const char *name, rt_uint8_t type) rt_enter_critical(); /* try to find object */ - if (information == RT_NULL) - { - information = rt_object_get_information((enum rt_object_class_type)type); - RT_ASSERT(information != RT_NULL); - } - for (node = information->object_list.next; - node != &(information->object_list); - node = node->next) + rt_list_for_each(node, &(information->object_list)) { object = rt_list_entry(node, struct rt_object, list); if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0) -- cgit v1.3.1 From b98690e2110a9ddf083004340b5170715c84996e Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Sun, 17 May 2020 23:14:24 +0800 Subject: [Kernel] Fix the maxlen issue in rt_object_get_pointers --- src/object.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/object.c') diff --git a/src/object.c b/src/object.c index 62d70816dc..6866d805ca 100644 --- a/src/object.c +++ b/src/object.c @@ -291,6 +291,8 @@ int rt_object_get_pointers(enum rt_object_class_type type, rt_object_t *pointers pointers[index] = object; index ++; + + if (index >= maxlen) break; } rt_hw_interrupt_enable(level); -- cgit v1.3.1