Browse Source

[kenel] add static name for rt_object (#6422)

增加静态object 名字,用户可以根据内存实际使用情况决定使用动态还是静态。适用于资源极度受限的情况下使用。该功能在RT-Thread完整版本中不常用,主要用于RT-Thread Nano,以缩减对内存的占用。由于没有动态拼接支持,在静态名字下,空闲线程的名字在多核时将命名成相同的名字。
Man, Jianting (Meco) 2 years ago
parent
commit
061af7b092
4 changed files with 38 additions and 14 deletions
  1. 17 8
      include/rtdef.h
  2. 2 1
      src/Kconfig
  3. 9 1
      src/idle.c
  4. 10 4
      src/object.c

+ 17 - 8
include/rtdef.h

@@ -46,6 +46,7 @@
  * 2022-06-29     Meco Man     add RT_USING_LIBC and standard libc headers
  * 2022-08-16     Meco Man     change version number to v5.0.0
  * 2022-09-12     Meco Man     define rt_ssize_t
+ * 2022-12-20     Meco Man     add const name for rt_object
  */
 
 #ifndef __RT_DEF_H__
@@ -412,21 +413,25 @@ typedef struct rt_slist_node rt_slist_t;                /**< Type for single lis
  */
 struct rt_object
 {
-    char       name[RT_NAME_MAX];                       /**< name of kernel object */
-    rt_uint8_t type;                                    /**< type of kernel object */
-    rt_uint8_t flag;                                    /**< flag of kernel object */
+#if RT_NAME_MAX > 0
+    char        name[RT_NAME_MAX];                       /**< dynamic name of kernel object */
+#else
+    const char *name;                                    /**< static name of kernel object */
+#endif /* RT_NAME_MAX > 0 */
+    rt_uint8_t  type;                                    /**< type of kernel object */
+    rt_uint8_t  flag;                                    /**< flag of kernel object */
 
 #ifdef RT_USING_MODULE
-    void      *module_id;                               /**< id of application module */
+    void      * module_id;                               /**< id of application module */
 #endif /* RT_USING_MODULE */
 
 #ifdef RT_USING_SMART
-    int       lwp_ref_count;                            /**< ref count for lwp */
+    int         lwp_ref_count;                           /**< ref count for lwp */
 #endif /* RT_USING_SMART */
 
-    rt_list_t  list;                                    /**< list node of kernel object */
+    rt_list_t   list;                                    /**< list node of kernel object */
 };
-typedef struct rt_object *rt_object_t;                  /**< Type for kernel objects. */
+typedef struct rt_object *rt_object_t;                   /**< Type for kernel objects. */
 
 /**
  *  The object type can be one of the follows with specific
@@ -725,7 +730,11 @@ struct rt_user_context
 struct rt_thread
 {
     /* rt object */
-    char        name[RT_NAME_MAX];                      /**< the name of thread */
+#if RT_NAME_MAX > 0
+    char        name[RT_NAME_MAX];                      /**< dynamic name of kernel object */
+#else
+    const char *name;                                   /**< static name of kernel object */
+#endif /* RT_NAME_MAX > 0 */
     rt_uint8_t  type;                                   /**< type of object */
     rt_uint8_t  flags;                                  /**< thread's flags */
 

+ 2 - 1
src/Kconfig

@@ -3,11 +3,12 @@ menu "RT-Thread Kernel"
 
 config RT_NAME_MAX
     int "The maximal size of kernel object name"
-    range 2 32
+    range 0 64
     default 8
     help
         Each kernel object, such as thread, timer, semaphore etc, has a name,
         the RT_NAME_MAX is the maximal size of this object name.
+        If RT_NAME_MAX sets as 0, the name will be const.
 
 config RT_USING_ARCH_DATA_TYPE
     bool "Use the data types defined in ARCH_CPU"

+ 9 - 1
src/idle.c

@@ -306,13 +306,21 @@ static void rt_thread_system_entry(void *parameter)
 void rt_thread_idle_init(void)
 {
     rt_ubase_t i;
+#if RT_NAME_MAX > 0
     char idle_thread_name[RT_NAME_MAX];
+#endif /* RT_NAME_MAX > 0 */
 
     for (i = 0; i < _CPUS_NR; i++)
     {
-        rt_sprintf(idle_thread_name, "tidle%d", i);
+#if RT_NAME_MAX > 0
+        rt_snprintf(idle_thread_name, RT_NAME_MAX, "tidle%d", i);
+#endif /* RT_NAME_MAX > 0 */
         rt_thread_init(&idle_thread[i],
+#if RT_NAME_MAX > 0
                 idle_thread_name,
+#else
+                "tidle",
+#endif /* RT_NAME_MAX > 0 */
                 idle_thread_entry,
                 RT_NULL,
                 &idle_thread_stack[i][0],

+ 10 - 4
src/object.c

@@ -387,8 +387,11 @@ void rt_object_init(struct rt_object         *object,
     /* 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);
+#if RT_NAME_MAX > 0
+    rt_strncpy(object->name, name, RT_NAME_MAX);  /* copy name */
+#else
+    object->name = name;
+#endif /* RT_NAME_MAX > 0 */
 
     RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
 
@@ -483,8 +486,11 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
     /* set object flag */
     object->flag = 0;
 
-    /* copy name */
-    rt_strncpy(object->name, name, RT_NAME_MAX);
+#if RT_NAME_MAX > 0
+    rt_strncpy(object->name, name, RT_NAME_MAX); /* copy name */
+#else
+    object->name = name;
+#endif /* RT_NAME_MAX > 0 */
 
     RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));