- 论坛徽章:
- 0
|
正在看malloc的源代码。
malloc/free算法非常低效。
尤其是改变块大小的时候有可能要copy移动。
而且存在碎片的问题。
为什么就不能有一个精细的巧妙的算法呢?
这个算法我基本实现了一个。
- * Why use this malloc?
- This is not the fastest, most space-conserving, most portable, or
- most tunable malloc ever written. However it is among the fastest
- while also being among the most space-conserving, portable and tunable.
- Consistent balance across these factors results in a good general-purpose
- allocator for malloc-intensive programs.
复制代码
可见作者在实现的时候考虑了除速度以外的很多方面的因素,速度是关键,但不是唯一要考虑的
- The main properties of the algorithms are:
- * For large (>= 512 bytes) requests, it is a pure best-fit allocator,
- with ties normally decided via FIFO (i.e. least recently used).
- * For small (<= 64 bytes by default) requests, it is a caching
- allocator, that maintains pools of quickly recycled chunks.
- * In between, and for combinations of large and small requests, it does
- the best it can trying to meet both goals at once.
- * For very large requests (>= 128KB by default), it relies on system
- memory mapping facilities, if supported.
复制代码
作者也考虑了针对不同大小的内存请求进行优化
刚开始看,听了各位的讨论,深有收获,继续看 |
|