1 Star 1 Fork 54

sxudong/YuGC-Study

forked from 启明南/YuGC-Study 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.cpp 7.71 KB
一键复制 编辑 原始数据 按行查看 历史
sxudong 提交于 2021-12-20 20:14 . update
#include <iostream>
#include "include/memory_pool.h"
//运行打印乱码,对照:https://blog.csdn.net/weixin_43892298/article/details/105732781
//每个文件都要右窗口右下角选择 “GBK” -> "convert"
int main() {
// 创建一个内存池
MemoryPool *mem_pool = new MemoryPool();
// 创建一个chunk
MemoryChunk *mem_chunk = mem_pool->new_chunk(78);
// 分配内存
mem_chunk->malloc(8);
mem_chunk->malloc(16);
mem_chunk->malloc(32);
mem_chunk->malloc(24);
/* 输出:
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:216->print_used_table):[打印used_table]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=7, cell_end=10, cell_size=3
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=3, cell_end=7, cell_size=4
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=1, cell_end=3, cell_size=2
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=0, cell_end=1, cell_size=1
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:224->print_used_table):[打印used_table]结束
*/
// 模拟Chunk的内存分光了
// mem_chunk->malloc(8);
// [Error] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:162->malloc):没有满足条件的Chunk,无法分成内存,程序退出
mem_pool->print_chunks();
delete mem_pool;
return 0;
}
/* Output:
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:203->print_available_table):[打印available_table]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:208->print_available_table): cell_start=0, cell_end=10, cell_size=10
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:211->print_available_table):[打印available_table]结束
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_cell.cpp:108->to_string):[Cell信息]找到了满足条件的Cell:start=0, end=10, size=10
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:50->real_malloc):[真正分配内存]Data起始地址=EF1CB0, cell_start=0, ret=64FC68
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:203->print_available_table):[打印available_table]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:208->print_available_table): cell_start=1, cell_end=10, cell_size=9
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:211->print_available_table):[打印available_table]结束
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:216->print_used_table):[打印used_table]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=0, cell_end=1, cell_size=1
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:224->print_used_table):[打印used_table]结束
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_cell.cpp:108->to_string):[Cell信息]找到了满足条件的Cell:start=1, end=10, size=9
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:50->real_malloc):[真正分配内存]Data起始地址=EF1CB0, cell_start=1, ret=64FCA8
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:203->print_available_table):[打印available_table]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:208->print_available_table): cell_start=3, cell_end=10, cell_size=7
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:211->print_available_table):[打印available_table]结束
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:216->print_used_table):[打印used_table]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=1, cell_end=3, cell_size=2
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=0, cell_end=1, cell_size=1
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:224->print_used_table):[打印used_table]结束
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_cell.cpp:108->to_string):[Cell信息]找到了满足条件的Cell:start=3, end=10, size=7
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:50->real_malloc):[真正分配内存]Data起始地址=EF1CB0, cell_start=3, ret=64FD28
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:203->print_available_table):[打印available_table]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:208->print_available_table): cell_start=7, cell_end=10, cell_size=3
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:211->print_available_table):[打印available_table]结束
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:216->print_used_table):[打印used_table]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=3, cell_end=7, cell_size=4
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=1, cell_end=3, cell_size=2
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=0, cell_end=1, cell_size=1
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:224->print_used_table):[打印used_table]结束
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_cell.cpp:108->to_string):[Cell信息]找到了满足条件的Cell:start=7, end=10, size=3
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:50->real_malloc):[真正分配内存]Data起始地址=EF1CB0, cell_start=7, ret=64FE28
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:174->free_available_table):[释放available_table]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_cell.cpp:15->~MemoryCell):[调用析构函数~MemoryCell]释放资源
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:203->print_available_table):[打印available_table]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:211->print_available_table):[打印available_table]结束
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:216->print_used_table):[打印used_table]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=7, cell_end=10, cell_size=3
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=3, cell_end=7, cell_size=4
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=1, cell_end=3, cell_size=2
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:221->print_used_table): cell_start=0, cell_end=1, cell_size=1
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:224->print_used_table):[打印used_table]结束
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_pool.cpp:28->print_chunks):[打印未释放的内存]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_pool.cpp:35->print_chunks): [未释放的内存]申请位置:( G:\OtherWorkSpace\YuGC-Study\src\memory_pool.cpp:18 ), 内存大小:80 字节
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_pool.cpp:38->print_chunks):[打印未释放的内存]结束
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_pool.cpp:10->~MemoryPool):[调用析构函数]~MemoryPool
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:30->~MemoryChunk):[调用析构函数~MemoryChunk]释放资源
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:33->~MemoryChunk): 释放资源, 申请内存位置( G:\OtherWorkSpace\YuGC-Study\src\memory_pool.cpp:18 ),内存大小:80 字节
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:174->free_available_table):[释放available_table]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_chunk.cpp:186->free_used_table):[释放used_table]开始
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_cell.cpp:15->~MemoryCell):[调用析构函数~MemoryCell]释放资源
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_cell.cpp:15->~MemoryCell):[调用析构函数~MemoryCell]释放资源
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_cell.cpp:15->~MemoryCell):[调用析构函数~MemoryCell]释放资源
[Info] (G:\OtherWorkSpace\YuGC-Study\src\memory_cell.cpp:15->~MemoryCell):[调用析构函数~MemoryCell]释放资源
*///~
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/sxudong/YuGC-Study.git
git@gitee.com:sxudong/YuGC-Study.git
sxudong
YuGC-Study
YuGC-Study
memory-pool

搜索帮助

0d507c66 1850385 C8b1a773 1850385