forked from TrekMax/LearningMasteringAlgorithms-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframes.c
73 lines (69 loc) · 1.43 KB
/
frames.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
/**
* @filename: frames.c
*
* @author: QinYUN575
*
* @create date: 2019/11/1
*
*
*
*/
#include <stdlib.h>
#include "frames.h"
#include "list.h"
/**
* 从空闲页帧表中获取空闲帧号
*
* @param List *frames 指定的空闲页帧表
*
* @return int >=0 获取到的空闲帧号
* -1 获取空闲帧号失败
*/
int alloc_frame(List *frames)
{
int frame_number, *data;
if (list_size(frames) == 0)
{
/* 无空闲帧可用 */
return -1;
}
else
{
if (list_rem_next(frames, NULL, (void**)&data) != 0)
{
/* 无法获取到空闲帧页 */
return -1;
}
else
{
/* 保存可用的空闲帧号 */
frame_number = *data;
free(data);
}
}
return frame_number;
}
/**
* 将一个页帧号返回到空闲页帧表中
*
* @param List *frames 指定的空闲页帧表
*
* @param int *frame_number 指定要返回到空闲页帧表的帧号
*
* @return int 0 插入空闲页帧表成功
* -1 插入空闲页帧表失败
*/
int free_frame(List *frames, int frame_number)
{
int *data;
if ((data = (int *)malloc(sizeof(int))) == NULL)
{
return -1;
}
*data = frame_number;
if (list_ins_next(frames, NULL, data) != 0)
{
return -1;
}
return 0;
}