-
Notifications
You must be signed in to change notification settings - Fork 0
/
mymalloc_t.c
435 lines (368 loc) · 11.4 KB
/
mymalloc_t.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include<stdio.h>
#include<stdlib.h>
#include<semaphore.h>
#include"mymalloc_t.h"
/*全局变量*/
int hasInited = 0;/*记录内存区域是否已经被初始化 */
small_block_manage * small_manage;/*指向小内存块管理区域的指针 */
big_block_manage * big_manage; /*指向大内存块管理区域的指针 */
SEM_ID sem_small ;
SEM_ID sem_big;
SEM_ID sem_heap;
heap_add_node * p_head = NULL;
heap_add_node * p_tail = NULL;
/*计算每一块内存块的首地址,head_add为申请的一大块内存的首地址*/
void calculate_address(int* head_add){
int block_index = 0;
small_block * small_temp;
big_block * big_temp;
small_manage = head_add;
big_manage = small_manage + 1;
small_temp = big_manage + 1;/*第一个小内存块的首地址,由一个小内存块指针指向 */
big_temp = small_temp + SMALLNUM;/*第一个大内存块的首地址,由一个小内存块指针指向 */
/*初始化小内存块管理结构*/
(*small_manage).base_add = small_temp;
(*small_manage).busy_num = 0;
/*初始化大内存块管理结构*/
(*big_manage).base_add = big_temp;
(*big_manage).busy_num = 0;
for(block_index = 0;block_index < SMALLNUM;block_index++){
(*small_manage).busy[block_index] = BLOCKAVAILABLE;/*初始化,将每一个小内存块置为可用 */
/*printf("小内存块地址:%d\n",(*small_manage).base_add+block_index);*/
}
for(block_index = 0;block_index < BIGNUM;block_index++){
(*big_manage).busy[block_index] = BLOCKAVAILABLE;/*初始化,将每一个大内存块置为可用 */
/*printf("大内存块地址:%d\n",(*big_manage).base_add+block_index);*/
}
printf("==========================================================\n");
printf("||\t小内存管理块的首地址:%08dD,%08xH\t||\n",small_manage,small_manage);
printf("||\t大内存管理块的首地址:%08dD,%08xH\t||\n",big_manage,big_manage);
printf("||\t小内存块数组的首地址:%08dD,%08xH\t||\n",(*small_manage).base_add,(*small_manage).base_add);
printf("||\t大内存块数组的首地址:%08dD,%08xH\t||\n",(*big_manage).base_add,(*big_manage).base_add);
printf("==========================================================\n");
/* printf("第二个大内存块数组的首地址:%08xH,%08dD\n",big_temp+1,big_temp + 1);*/
}
/*内存初始化模块,计算要申请的内存区域大小,返回内存首地址*/
int* mem_init(){
int small_size;
int big_size;
int manage_size;
int pre_alloc_size;
int * array;
sem_small = semBCreate(SEM_Q_FIFO, SEM_FULL);
sem_big = semBCreate(SEM_Q_FIFO, SEM_FULL);
sem_heap = semBCreate(SEM_Q_FIFO, SEM_FULL);
if(hasInited > 0){
printf("已经完成内存管理初始化。\n");
return;
}
/*计算首先要申请多少字节的内存地址*/
small_size = SMALLNUM*sizeof(small_block);
big_size = BIGNUM*sizeof(big_block);
manage_size = sizeof(small_block_manage) + sizeof(big_block_manage);
pre_alloc_size = small_size + big_size + manage_size;
printf(" ========================================================\n");
printf("||\t小内存块数量:%d,每块占用%d字节,共%d字节\t||\n",SMALLNUM,sizeof(small_block),small_size);
printf("||\t大内存块数量:%d,每块占用%d字节,共%d字节\t||\n",BIGNUM,sizeof(big_block),big_size);
printf("||\t小内存管理块占用%d字节\t\t\t\t||\n",sizeof(small_block_manage));
printf("||\t大内存管理块占用%d字节\t\t\t\t||\n",sizeof(big_block_manage));
printf("||\t初始化时总共需申请%d字节的内存区域\t\t||\n",pre_alloc_size);
printf(" =========================================================\n");
p_head = (struct heap_add_node *)malloc(sizeof(heap_add_node));
p_head -> next = NULL;
p_tail = p_head;
/*array是申请pre_alloc_size大小的内存块返回的首地址*/
array = (int *)malloc(pre_alloc_size);
if(array == NULL){
printf("不能成功分配存储空间。\n");
return NULL;
}else{
hasInited = 1;
calculate_address(array);
printf(" ========================================================\n");
printf("||\t内存管理初始化完成,起始地址:%08dD\t\t||\n",array);
printf("||\t内存管理初始化完成,起始地址:%08xH\t\t||\n",array);
printf(" ========================================================\n");
return array;
}
}
/*按类型申请内存,返回申请到的内存块的首地址*/
int* malloc_by_type(int type,int size){
int index;
int* heap_add;
heap_add_node * p_new;
small_block * small_first;
big_block * big_first;
int flag_small_given;
int flag_big_given;
int flag_heap_given;
flag_small_given = 0;
flag_big_given = 0;
flag_heap_given = 0;
/*int* add = NULL;*/
index = 0;
heap_add = NULL;
p_new = NULL;
small_first=NULL;
big_first=NULL;
/*heap_add_node * tail = p_tail;*/
switch(type){
case SMALLAREA :
semTake(sem_small,WAIT_FOREVER);
(*small_manage).busy_num++;
small_first = (*small_manage).base_add;
for(index = 0;index < SMALLNUM; index++){
if((*small_manage).busy[index]==BLOCKAVAILABLE){
(*small_manage).busy[index]=BLOCKBUSY;
semGive(sem_small);
flag_small_given = 1;
return small_first + index;
}
}
if(flag_small_given == 0)
semGive(sem_small);
break;
case BIGAREA:
semTake(sem_big,WAIT_FOREVER);
(*big_manage).busy_num++;
big_first = (*big_manage).base_add;
for(index = 0;index < BIGNUM; index++){
if((*big_manage).busy[index] == BLOCKAVAILABLE){
(*big_manage).busy[index] = BLOCKBUSY;
semGive(sem_big);
flag_big_given = 1;
return big_first + index;
}
}
if(flag_big_given == 0)
semGive(sem_big);
break;
case HEAPAREA:
semTake(sem_heap,WAIT_FOREVER);
p_new = (heap_add_node *)malloc(sizeof(heap_add_node));
if(NULL == p_new){
printf("从heap分配内存失败,程序终止!\n");
semGive(sem_heap);
return NULL;
}
heap_add = (int*)malloc(sizeof(size));
p_new -> heap_add = heap_add;
p_new -> size = size;
p_new -> next = NULL;
p_tail -> next = p_new;
p_tail = p_new;
semGive(sem_heap);
return heap_add;
break;
default:
break;
}
}
/*
申请内存块程序
1.小于SMALLSIZE的部分申请小内存块,如果小内存块区域满,则申请大内存块
2.大于SMALLSIZE小于BIGSIZE申请大内存块,如果大内存块区域满,则从heap申请内存块
3.从堆申请内存块,并连接到heap内存块链表上
*/
int* mem_malloc(int size){
int small_busy;
int big_busy;
int flag;
small_busy = 0;
big_busy = 0;
flag = SMALL_FREE;
if(size > 0 && size <= SMALLSIZE){
printf("正在申请小内存块...\n");
small_busy = (*small_manage).busy_num;
if(small_busy == SMALLNUM){
printf("小内存块满...\n");
flag = SMALL_FULL;
}else{
return malloc_by_type(SMALLAREA,size);
}
}
if((size > SMALLSIZE && size <= BIGSIZE)||flag == SMALL_FULL){
printf("正在申请大内存块...\n");
big_busy = (*small_manage).busy_num;
if(big_busy == BIGNUM){
printf("大内存块满...\n");
flag = BIG_FULL;
}else{
return malloc_by_type(BIGAREA,size);
}
}
if(size > BIGSIZE||flag == BIG_FULL){
printf("正在从heap申请内存块...\n");
return malloc_by_type(HEAPAREA,size);
}
}
/*
首先检验地址合法性,不合法return;
合法判断是三条链中哪一条链上的内存块,执行free操作
*/
void mem_free(int free_add){
/*printf("正在检验地址合法性...\n");*/
if(free_add >= (*small_manage).base_add && free_add <= (*small_manage).base_add + SMALLNUM-1){
int index = 0;
small_block* small_first = (*small_manage).base_add;
for(index = 0;index<SMALLNUM;index++){
if(small_first+index==free_add){
(*small_manage).busy_num--;
(*small_manage).busy[index]=BLOCKAVAILABLE;
printf("已成功释放一块小内存\n");
}
}
}else if(free_add >= (*big_manage).base_add && free_add <= (*big_manage).base_add + BIGNUM-1){
int index = 0;
big_block* big_first = (*big_manage).base_add;
for(index = 0;index<BIGNUM;index++){
if(big_first+index==free_add){
(*big_manage).busy_num--;
(*big_manage).busy[index]=BLOCKAVAILABLE;
printf("已成功释放一块大内存\n");
}
}
}
/*
先判断是否是从heap申请的,
如果是,则释放
否则提示输入地址无效
*/
else if(p_head ->next != NULL){
heap_add_node * p = p_head;
heap_add_node * p_temp = NULL;
while(p->next!=NULL)
{
p_temp = p->next;
if(p_temp->heap_add == free_add){
free(free_add);
p->next = p_temp->next;
free(p_temp);
printf("已成功释放一块heap内存\n");
return;
} else{
p = p->next;
}
}
printf("该首地址无效。\n");
}else{
printf("该首地址无效。\n");
}
}
/*遍历记录从heap申请的内存块的链表*/
void traverse_list()
{
heap_add_node * p = p_head->next;
if(p_head ->next == NULL){
printf("链表为空!\n");
}else{
printf("从heap申请到的内存信息如下:\n");
while(p!=NULL)
{
printf("首地址:%08dD,首地址:%08xH,占用:%d字节\n",p->heap_add,p->heap_add,p->size);
p = p->next;
}
}
}
/*释放记录从heap申请的内存块的链表*/
void free_list(){
heap_add_node * p = p_head;
heap_add_node *p_temp = NULL;
while(p->next != NULL){
p_temp = p->next;
free(p_temp->heap_add);
p->next = p_temp->next;
free(p_temp);
printf("已成功释放一块heap内存\n");
}
}
/*展示内存的占用情况*/
void mem_show(){
double f1;
double f2;
int index;
if(hasInited == 0){
printf("内存暂未初始化。\n");
return;
}else if(hasInited > 0){
printf("\n============================内存使用情况如下============================\n");
f1 = (double)(*small_manage).busy_num/(double)SMALLNUM*100;;
printf("\n小内存块共%d个,已占用%d个,占用率%.2f%%\n",SMALLNUM,(*small_manage).busy_num,f1);
for(index=0; index<SMALLNUM; index++){
if((*small_manage).busy[index]==BLOCKAVAILABLE)
printf("%d块:可用\t",index);
if((*small_manage).busy[index]==BLOCKBUSY)
printf("%d块:占用\t",index);
if((index+1)%4==0)printf("\t||\n");
}
printf("\n");
f2 = (double)(*big_manage).busy_num/(double)BIGNUM*100;;
printf("大内存块共%d个,已占用%d个,占用率%.2f%%\n",BIGNUM,(*big_manage).busy_num,f2);
for(index=0; index<BIGNUM; index++){
if((*big_manage).busy[index]==BLOCKAVAILABLE)
printf("%d块:可用\t",index);
if((*big_manage).busy[index]==BLOCKBUSY)
printf("%d块:占用\t",index);
if((index+1)%4==0)printf("\t||\n");
}
printf("\n");
traverse_list();
printf("========================================================================\n");
}else{
printf("内存初始化出现问题!\n");
}
}
/*打印用户选择菜单*/
void print_menu(){
printf("*********************************************************\n");
printf("*\t\t\t0.退出程序。\t\t\t*\n");
printf("*\t\t\t1.申请内存。\t\t\t*\n");
printf("*\t\t\t2.释放内存。\t\t\t*\n");
printf("*\t\t\t3.展示占用。\t\t\t*\n");
printf("*********************************************************\n");
printf("请输入0-3的整数:");
}
/*主程序入口*/
void main(){
int * head_add;
int malloc_size;
int * add;
int free_add;
int choice;
printf("请等待程序初始化...\n");
head_add = mem_init();/*记录初始化时的首地址,程序结束时进行free */
print_menu();
choice = -1;
scanf("%d",&choice);
while(choice!=0){
switch(choice){
case 1:
printf("输入需要申请多少字节?\n");
scanf("%d",&malloc_size);
add = mem_malloc(malloc_size);
if(add!=NULL){
printf("申请到的地址为%08dD,%08xH\n",add,add);
}else{
printf("申请不到内存空间\n");
}
break;
case 2:
printf("输入需释放的首地址(十进制):");
free_add = -1;
scanf("%d",&free_add);
mem_free(free_add);
break;
case 3:
mem_show();
break;
default:
break;
}
print_menu();
scanf("%d",&choice);
}
free(head_add);/*释放大内存块,小内存块 */
free_list();/*释放存放heap内存块地址的链表 */
free(p_head);/*释放链表头 */
return 0;
}