(相关资料图)
背景
学习RT-Thread,除了基础的应用,应该花点时间,研究下底层内核的实现方法。RT-Thread内核,整体代码量不大,很适合研究,后期,打算在这个基础上,开发一些软件包。之前了解了一点数据结构的知识,主要为结构体、链表。不过感觉没有真正的使用起来。程序 = 数据结构+算法。万事开头难,先从最基本的概念开始。内核对象控制块
对象(object)结构体对象可以派生具体的对象导火索
今天,想仔细看看RT-Thread 的内核的对象,基本上都是结构体,看看结构体的大小,占用RAM的大小。单片机的程序,短小精悍是每个嵌入式软件工程师追求的,实现相同功能,占用最少资源,意味着硬件成本会进一步降低。编写一个函数,打印一下rt_object 等对象的结构体占用的内存(RAM)大小。void print_kernel_object_size(void){ struct rt_object obj; rt_kprintf("struct rt_object size=%d\\n", sizeof(obj)); rt_kprintf("rt_list_t size=%d\\n", sizeof(rt_list_t)); rt_kprintf("struct rt_timer size=%d\\n", sizeof(struct rt_timer)); rt_kprintf("struct rt_threadsize=%d\\n", sizeof(struct rt_thread)); rt_kprintf("struct rt_ipc_object size=%d\\n", sizeof(struct rt_ipc_object)); rt_kprintf("struct rt_semaphore size=%d\\n", sizeof(struct rt_semaphore)); rt_kprintf("struct rt_mutex size=%d\\n", sizeof(struct rt_mutex)); rt_kprintf("struct rt_event size=%d\\n", sizeof(struct rt_event)); rt_kprintf("struct rt_mailbox size=%d\\n", sizeof(struct rt_mailbox)); rt_kprintf("struct rt_messagequeue size=%d\\n", sizeof(struct rt_messagequeue)); rt_kprintf("struct rt_mempool size=%d\\n", sizeof(struct rt_mempool)); rt_kprintf("struct rt_device size=%d\\n", sizeof(struct rt_device)); rt_kprintf("struct rt_device_graphic_info size=%d\\n", sizeof(struct rt_device_graphic_info));}MSH_CMD_EXPORT(print_kernel_object_size, print_kernel_object_size); //导出命令
运行效果msh / >priprint_kernel_object_sizemsh / >print_kernel_object_sizestruct rt_object size=20rt_list_t size=8struct rt_timer size=44struct rt_thread size=128struct rt_ipc_object size=28struct rt_semaphore size=32struct rt_mutex size=36struct rt_event size=32struct rt_mailbox size=48struct rt_messagequeue size=60struct rt_mempool size=52struct rt_device size=80struct rt_device_graphic_info size=12
struct rt_object size=20,说明:RT_NAME_MAX为8时,内核对象大小为:20字节(8+ (4)+8 = 20)初步分析,rt_uint8_t type,rt_uint8_t flag,因为内存对齐,占用了2+2=4个字节。(KeilMDK5 armcc)/** * Base structure of Kernel object */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 */#ifdef RT_USING_MODULE void *module_id; /**< id of application module */#endif rt_list_t list; /**< list node of kernel object */};
当我更改内核对象中的:rt_uint8_t type --> rt_uint32_t type,验证下结构体大小时,却发生了Assert 栈溢出。\\ | /- RT - Thread Operating System/ | \\ 4.0.3 build Jan 16 20212006 - 2020 Copyright by rt-thread teamthread: stack overflow
经过调试,发现是线程结构体引起的对齐问题。结构体,一定要注意结构与大小一致。线程控制块(结构体)rt_thread,使用了内核对象块,是重写的。看来,还是不要直接改基础的内核控制块。要改rt_object的结构体大小,其他对象派生的对象,都要注意下。总结
通过打印rt_object等结构体的大小,了解了rt_object的基本构成。基本了解到了其他内核对象,如rt_thread(线程控制块),rt_timer(定时器控制块),rt_list_t(双向链表)的RAM占用大小。了解到rt_object的派生方式,增加其他对象的特殊属性,构成其他的对象,用链表链接起来,用于OS管理。关键词: