Skip to content

Commit 061af7b

Browse files
authored
[kenel] add static name for rt_object (#6422)
增加静态object 名字,用户可以根据内存实际使用情况决定使用动态还是静态。适用于资源极度受限的情况下使用。该功能在RT-Thread完整版本中不常用,主要用于RT-Thread Nano,以缩减对内存的占用。由于没有动态拼接支持,在静态名字下,空闲线程的名字在多核时将命名成相同的名字。
1 parent 91cdcea commit 061af7b

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

include/rtdef.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* 2022-06-29 Meco Man add RT_USING_LIBC and standard libc headers
4747
* 2022-08-16 Meco Man change version number to v5.0.0
4848
* 2022-09-12 Meco Man define rt_ssize_t
49+
* 2022-12-20 Meco Man add const name for rt_object
4950
*/
5051

5152
#ifndef __RT_DEF_H__
@@ -412,21 +413,25 @@ typedef struct rt_slist_node rt_slist_t; /**< Type for single lis
412413
*/
413414
struct rt_object
414415
{
415-
char name[RT_NAME_MAX]; /**< name of kernel object */
416-
rt_uint8_t type; /**< type of kernel object */
417-
rt_uint8_t flag; /**< flag of kernel object */
416+
#if RT_NAME_MAX > 0
417+
char name[RT_NAME_MAX]; /**< dynamic name of kernel object */
418+
#else
419+
const char *name; /**< static name of kernel object */
420+
#endif /* RT_NAME_MAX > 0 */
421+
rt_uint8_t type; /**< type of kernel object */
422+
rt_uint8_t flag; /**< flag of kernel object */
418423

419424
#ifdef RT_USING_MODULE
420-
void *module_id; /**< id of application module */
425+
void * module_id; /**< id of application module */
421426
#endif /* RT_USING_MODULE */
422427

423428
#ifdef RT_USING_SMART
424-
int lwp_ref_count; /**< ref count for lwp */
429+
int lwp_ref_count; /**< ref count for lwp */
425430
#endif /* RT_USING_SMART */
426431

427-
rt_list_t list; /**< list node of kernel object */
432+
rt_list_t list; /**< list node of kernel object */
428433
};
429-
typedef struct rt_object *rt_object_t; /**< Type for kernel objects. */
434+
typedef struct rt_object *rt_object_t; /**< Type for kernel objects. */
430435

431436
/**
432437
* The object type can be one of the follows with specific
@@ -725,7 +730,11 @@ struct rt_user_context
725730
struct rt_thread
726731
{
727732
/* rt object */
728-
char name[RT_NAME_MAX]; /**< the name of thread */
733+
#if RT_NAME_MAX > 0
734+
char name[RT_NAME_MAX]; /**< dynamic name of kernel object */
735+
#else
736+
const char *name; /**< static name of kernel object */
737+
#endif /* RT_NAME_MAX > 0 */
729738
rt_uint8_t type; /**< type of object */
730739
rt_uint8_t flags; /**< thread's flags */
731740

src/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ menu "RT-Thread Kernel"
33

44
config RT_NAME_MAX
55
int "The maximal size of kernel object name"
6-
range 2 32
6+
range 0 64
77
default 8
88
help
99
Each kernel object, such as thread, timer, semaphore etc, has a name,
1010
the RT_NAME_MAX is the maximal size of this object name.
11+
If RT_NAME_MAX sets as 0, the name will be const.
1112

1213
config RT_USING_ARCH_DATA_TYPE
1314
bool "Use the data types defined in ARCH_CPU"

src/idle.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,21 @@ static void rt_thread_system_entry(void *parameter)
306306
void rt_thread_idle_init(void)
307307
{
308308
rt_ubase_t i;
309+
#if RT_NAME_MAX > 0
309310
char idle_thread_name[RT_NAME_MAX];
311+
#endif /* RT_NAME_MAX > 0 */
310312

311313
for (i = 0; i < _CPUS_NR; i++)
312314
{
313-
rt_sprintf(idle_thread_name, "tidle%d", i);
315+
#if RT_NAME_MAX > 0
316+
rt_snprintf(idle_thread_name, RT_NAME_MAX, "tidle%d", i);
317+
#endif /* RT_NAME_MAX > 0 */
314318
rt_thread_init(&idle_thread[i],
319+
#if RT_NAME_MAX > 0
315320
idle_thread_name,
321+
#else
322+
"tidle",
323+
#endif /* RT_NAME_MAX > 0 */
316324
idle_thread_entry,
317325
RT_NULL,
318326
&idle_thread_stack[i][0],

src/object.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,11 @@ void rt_object_init(struct rt_object *object,
387387
/* initialize object's parameters */
388388
/* set object type to static */
389389
object->type = type | RT_Object_Class_Static;
390-
/* copy name */
391-
rt_strncpy(object->name, name, RT_NAME_MAX);
390+
#if RT_NAME_MAX > 0
391+
rt_strncpy(object->name, name, RT_NAME_MAX); /* copy name */
392+
#else
393+
object->name = name;
394+
#endif /* RT_NAME_MAX > 0 */
392395

393396
RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
394397

@@ -483,8 +486,11 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
483486
/* set object flag */
484487
object->flag = 0;
485488

486-
/* copy name */
487-
rt_strncpy(object->name, name, RT_NAME_MAX);
489+
#if RT_NAME_MAX > 0
490+
rt_strncpy(object->name, name, RT_NAME_MAX); /* copy name */
491+
#else
492+
object->name = name;
493+
#endif /* RT_NAME_MAX > 0 */
488494

489495
RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
490496

0 commit comments

Comments
 (0)