我是靠谱客的博主 听话纸飞机,这篇文章主要介绍简单的malloc,free实现函数简单的malloc,free实现函数,现在分享给大家,希望可以做个参考。

简单的malloc,free实现函数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
struct mem_control_block { int is_avaliable; int size; }; int has_initialized = 0; void *managed_mem_start; void *last_valid_address; void mem_init() { last_valid_address = sbrk(0); managed_mem_start = last_valid_address; has_initalized = 1; } void free(*firstbyte) { struct mem_control_block *mcb; mcb = firstbyte - sizeof(struct mem_control_block); mcb->is_avaliable = 1; return; } void malloc(int numbyte) { struct mem_control_block *current_mcb; void *mem_location,current_location; if(!has_initialized) mem_init(); numbyte += sizeof(struct mem_control_block); mem_location = 0; current_location = managed_mem_start; while(current_location != last_valid_address) { current_mcb = (struct mem_control_block *)current_location; if(current_mcb->is_avaliable) if(current_mcb->size >= numbyte) { current_mcb->is_avaliable = 0; mem_location = current_location; break; } //current_location += sizeof(struct mem_control_block); current_location += current_mcb->size; } if(!memlocation) { sbrk(numbyte); mem_location = last_valid_address; last_valid_address += numbyte; current_mcb = mem_location; current_mcb->is_avaliable = 0; current_mcb->size = numbyte; } mem_location += sizeof(struct mem_control_block); return mem_location; }


 

最后

以上就是听话纸飞机最近收集整理的关于简单的malloc,free实现函数简单的malloc,free实现函数的全部内容,更多相关简单内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部