Skip to content

Commit b1eb394

Browse files
committed
[Kernel] Add rt_object_get_length/rt_object_get_pointers APIs.
1 parent 0f57faa commit b1eb394

File tree

2 files changed

+79
-11
lines changed

2 files changed

+79
-11
lines changed

include/rtthread.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ extern "C" {
4141
void rt_system_object_init(void);
4242
struct rt_object_information *
4343
rt_object_get_information(enum rt_object_class_type type);
44+
int rt_object_get_length(enum rt_object_class_type type);
45+
int rt_object_get_pointers(enum rt_object_class_type type, rt_object_t *pointers, int maxlen);
46+
4447
void rt_object_init(struct rt_object *object,
4548
enum rt_object_class_type type,
4649
const char *name);

src/object.c

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ void rt_system_object_init(void)
212212
/**
213213
* This function will return the specified type of object information.
214214
*
215-
* @param type the type of object
215+
* @param type the type of object, which can be
216+
* RT_Object_Class_Thread/Semaphore/Mutex... etc
217+
*
216218
* @return the object type information or RT_NULL
217219
*/
218220
struct rt_object_information *
@@ -227,6 +229,75 @@ rt_object_get_information(enum rt_object_class_type type)
227229
}
228230
RTM_EXPORT(rt_object_get_information);
229231

232+
/**
233+
* This function will return the length of object list in object container.
234+
*
235+
* @param type the type of object, which can be
236+
* RT_Object_Class_Thread/Semaphore/Mutex... etc
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_ubase_t level;
243+
struct rt_list_node *node = RT_NULL;
244+
struct rt_object_information *information = RT_NULL;
245+
246+
information = rt_object_get_information((enum rt_object_class_type)type);
247+
if (information == RT_NULL) return 0;
248+
249+
level = rt_hw_interrupt_disable();
250+
/* get the count of objects */
251+
rt_list_for_each(node, &(information->object_list))
252+
{
253+
count ++;
254+
}
255+
rt_hw_interrupt_enable(level);
256+
257+
return count;
258+
}
259+
RTM_EXPORT(rt_object_get_length);
260+
261+
/**
262+
* This function will copy the object pointer of the specified type,
263+
* with the maximum size specified by maxlen.
264+
*
265+
* @param type the type of object, which can be
266+
* RT_Object_Class_Thread/Semaphore/Mutex... etc
267+
* @param pointers the pointers will be saved to
268+
* @param maxlen the maximum number of pointers can be saved
269+
*
270+
* @return the copied number of object pointers
271+
*/
272+
int rt_object_get_pointers(enum rt_object_class_type type, rt_object_t *pointers, int maxlen)
273+
{
274+
int index = 0;
275+
rt_ubase_t level;
276+
277+
struct rt_object *object;
278+
struct rt_list_node *node = RT_NULL;
279+
struct rt_object_information *information = RT_NULL;
280+
281+
if (maxlen <= 0) return 0;
282+
283+
information = rt_object_get_information((enum rt_object_class_type)type);
284+
if (information == RT_NULL) return 0;
285+
286+
level = rt_hw_interrupt_disable();
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+
292+
pointers[index] = object;
293+
index ++;
294+
}
295+
rt_hw_interrupt_enable(level);
296+
297+
return index;
298+
}
299+
RTM_EXPORT(rt_object_get_pointers);
300+
230301
/**
231302
* This function will initialize an object and add it to object system
232303
* management.
@@ -482,9 +553,10 @@ rt_object_t rt_object_find(const char *name, rt_uint8_t type)
482553
struct rt_list_node *node = RT_NULL;
483554
struct rt_object_information *information = RT_NULL;
484555

556+
information = rt_object_get_information((enum rt_object_class_type)type);
557+
485558
/* parameter check */
486-
if ((name == RT_NULL) || (type > RT_Object_Class_Unknown))
487-
return RT_NULL;
559+
if ((name == RT_NULL) || (information == RT_NULL)) return RT_NULL;
488560

489561
/* which is invoke in interrupt status */
490562
RT_DEBUG_NOT_IN_INTERRUPT;
@@ -493,14 +565,7 @@ rt_object_t rt_object_find(const char *name, rt_uint8_t type)
493565
rt_enter_critical();
494566

495567
/* try to find object */
496-
if (information == RT_NULL)
497-
{
498-
information = rt_object_get_information((enum rt_object_class_type)type);
499-
RT_ASSERT(information != RT_NULL);
500-
}
501-
for (node = information->object_list.next;
502-
node != &(information->object_list);
503-
node = node->next)
568+
rt_list_for_each(node, &(information->object_list))
504569
{
505570
object = rt_list_entry(node, struct rt_object, list);
506571
if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)

0 commit comments

Comments
 (0)