Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

分离内存分配接口与内存分配算法 #5175

Merged
merged 30 commits into from
Dec 16, 2021

Conversation

enkiller
Copy link
Contributor

@enkiller enkiller commented Oct 14, 2021

拉取/合并请求描述:(PR description)

[

改动的原因

  1. 实际使用中,存在使用多种算法管理不同内存的情况,需要将算法分离出来单独使用
  2. 内存接口中使用信号量作为线程锁不好,修改锁的类型可配置

核心改动

  1. mem/slab 全局定义改成结构体,放到 rtdef.h 中
  2. mem/slab 内存分配算法的接口更名,放到 rtthread.h 公共使用
  3. 移除 memheap 中 rt_malloc/rt_free 等相关接口
  4. rt_malloc/rt_free 相关接口放到 kservice.c 中实现
  5. memheap 分配算法支持最佳分配模式

影响

  1. rtconfig.h 需要全部更新
  2. 单实例更改为多实例,改动较大,可能改出问题

测试用例

编写中...

  • mem.c
  • memheap.c
  • slab.c

]

以下的内容不应该在提交PR时的message修改,修改下述message,PR会被直接关闭。请在提交PR后,浏览器查看PR并对以下检查项逐项check,没问题后逐条在页面上打钩。
The following content must not be changed in the submitted PR message. Otherwise, the PR will be closed immediately. After submitted PR, please use a web browser to visit PR, and check items one by one, and ticked them if no problem.

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 本拉取/合并请求代码是高质量的 Code in this PR is of high quality
  • 本拉取/合并使用formatting等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification

@CLAassistant
Copy link

CLAassistant commented Oct 14, 2021

CLA assistant check
All committers have signed the CLA.

@enkiller
Copy link
Contributor Author

@BernardXiong拉取/合并请求描述 有描述改动的原因及改动的方式。有时间帮忙看下这样子改动是否可行。会不会出现不兼容等情况。

@enkiller
Copy link
Contributor Author

enkiller commented Dec 1, 2021

@Guozhanxin 这个方案能合入不?改动比较大,兼容性上估计没啥问题。

begin = (rt_uint8_t *)&heap[1];
buf_size = buf + TEST_MEM_SIZE - begin;
rt_memset(buf, 0xAA, TEST_MEM_SIZE);
// small heap init
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释不太规范,/* */

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,注释我修改下

最好是先评估下这个方案能不能合并,免得白费功夫....

include/rtdef.h Outdated
@@ -431,7 +431,9 @@ enum rt_object_class_type
RT_Object_Class_Device = 0x09, /**< The object is a device. */
RT_Object_Class_Timer = 0x0a, /**< The object is a timer. */
RT_Object_Class_Module = 0x0b, /**< The object is a module. */
RT_Object_Class_Unknown = 0x0c, /**< The object is unknown. */
RT_Object_Class_Mem = 0x0c, /**< The object is a small memory. */
RT_Object_Class_Slab = 0x0d, /**< The object is a slab memory. */
Copy link
Member

@Guozhanxin Guozhanxin Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样搞的话,是不是每加一个内存算法都要加一个类型?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,类型该加的加嘛,我觉得这个没啥影响吧。

还是说搞一个类型?在通过某种方式区分子类型?

*
* @param end_addr the end address of system page.
*/
RT_WEAK void rt_system_heap_init(void *begin_addr, void *end_addr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看起来这么改,对用户自己实现的内存算法影响不大。可以开下这个软件包试试。http://packages.rt-thread.org/detail.html?package=tlsf

src/kservice.c Outdated
/* Enter critical zone */
level = _heap_lock();
/* Change the size of previously allocated memory block */
nptr = _MEM_REALLOC(&system_heap, rmem, newsize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

realloc 没有hook吗

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

realloc 原本也没有 hook

const char *name,
void *begin_addr,
rt_size_t size);
rt_err_t rt_mem_detach(struct rt_mem *m);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些api和结构体定义,一定要加到 rt-thread 头文件里面吗?这群其实用户都用不到的。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修改成多实例,就需要用到了

修改成多实例的原因:一个项目中,内存会被化成多个块供不同的组件使用,不同的内存块使用不同的管理算法。当前 rtt 不支持一个系统中使用多种内存管理算法,就只能魔改

所以就把内存管理算法做成多实例了

src/kservice.c Outdated
level = _heap_lock();
/* allocate memory block from system heap */
ptr = _MEM_MALLOC(&system_heap, size);
#if defined(RT_USING_MEMHEAP_AS_HEAP) && defined(RT_USING_MEMHEAP_AUTO_BINDING)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这部分的逻辑,应该是某一个具体的内存算法的功能,应该放到具体的内存算法内部吧?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这部分逻辑就很尴尬,在现有的 memheap 中,rt_malloc 是在内部实现的。现在把 rt_malloc 拿出来了。这部分代码就得跟着拿出来。

@enkiller
Copy link
Contributor Author

enkiller commented Dec 1, 2021

如沟通,调整方式如下:

  1. 添加通用 Mem 类型,内存管理算法从通用类型上继承
  2. small 及 slab 内存管理算法结构体移动到算法内部,对外暴露接口。初始化管理算法,返回通用的 Mem 类型
  3. 原先 memheap 实现的 rt_malloc 接口更改一个名字,跨内存块查找功能封装在 memheap 内部

@mysterywolf mysterywolf added the in progress PR/issue in progress. label Dec 2, 2021
@cndabai
Copy link
Contributor

cndabai commented Dec 9, 2021

image

@mysterywolf mysterywolf added the +1 Agree +1 label Dec 15, 2021
@mysterywolf
Copy link
Member

@Guozhanxin 郭老师看一下吧,合并的时候选择squash一下,commits数量太多了

@Guozhanxin Guozhanxin added the +2 Agree +2 label Dec 16, 2021
@Guozhanxin Guozhanxin merged commit d724eed into RT-Thread:master Dec 16, 2021
@enkiller enkiller deleted the 1013 branch December 17, 2021 06:03
Copy link

@yulincoder yulincoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请问一下给rt_memheap添加locked这个flag,这个修改作者是遇到什么问题从而进行的修复吗?我这边遇到了踩内存问题,发现厂商给我的代码和仓库缺少这个补丁 @enkiller

@enkiller
Copy link
Contributor Author

请问一下给rt_memheap添加locked这个flag,这个修改作者是遇到什么问题从而进行的修复吗?我这边遇到了踩内存问题,发现厂商给我的代码和仓库缺少这个补丁 @enkiller

与这个修改没关系

@Rbb666
Copy link
Member

Rbb666 commented Jan 18, 2022

V}C4YZ30R0MF478}TOE(EVP

你好,free的命令是不是有点问题?
我在ARI-PI上使能动态内存内存管理算法free出来的只有heap,没有sdram

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crucial +1 Agree +1 +2 Agree +2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants