-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextmem.c
156 lines (126 loc) · 2.98 KB
/
extmem.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
/*
* extmem.c
* Zhaonian Zou
* Harbin Institute of Technology
* Jun 22, 2011
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "extmem.h"
Buffer *initBuffer(size_t bufSize, size_t blkSize, Buffer *buf)
{
int i;
buf->numIO = 0;
buf->bufSize = bufSize;
buf->blkSize = blkSize;
buf->numAllBlk = bufSize / (blkSize + 1);
buf->numFreeBlk = buf->numAllBlk;
buf->data = (unsigned char*)malloc(bufSize * sizeof(unsigned char));
if (!buf->data)
{
perror("Buffer Initialization Failed!\n");
return NULL;
}
memset(buf->data, 0, bufSize * sizeof(unsigned char));
return buf;
}
void freeBuffer(Buffer *buf)
{
free(buf->data);
}
unsigned char *getNewBlockInBuffer(Buffer *buf)
{
unsigned char *blkPtr;
if (buf->numFreeBlk == 0)
{
perror("Buffer is full!\n");
return NULL;
}
blkPtr = buf->data;
while (blkPtr < buf->data + (buf->blkSize + 1) * buf->numAllBlk)
{
if (*blkPtr == BLOCK_AVAILABLE)
break;
else
blkPtr += buf->blkSize + 1;
}
*blkPtr = BLOCK_UNAVAILABLE;
buf->numFreeBlk--;
return blkPtr + 1;
}
void freeBlockInBuffer(unsigned char *blk, Buffer *buf)
{
*(blk - 1) = BLOCK_AVAILABLE;
buf->numFreeBlk++;
}
int dropBlockOnDisk(unsigned int addr)
{
char filename[40];
sprintf(filename, "data/%d.blk", addr);
if (remove(filename) == -1)
{
perror("Dropping Block Fails!\n");
return -1;
}
return 0;
}
unsigned char *readBlockFromDisk(unsigned int addr, Buffer *buf)
{
char filename[40];
unsigned char *blkPtr, *bytePtr;
char ch;
if (buf->numFreeBlk == 0)
{
perror("Buffer Overflows!\n");
return NULL;
}
blkPtr = buf->data;
while (blkPtr < buf->data + (buf->blkSize + 1) * buf->numAllBlk)
{
if (*blkPtr == BLOCK_AVAILABLE)
break;
else
blkPtr += buf->blkSize + 1;
}
sprintf(filename, "../data/%d.blk", addr);
FILE *fp = fopen(filename, "r");
if (!fp)
{
perror("Reading Block Failed!");
printf("%d\n",addr);
return NULL;
}
*blkPtr = BLOCK_UNAVAILABLE;
blkPtr++;
bytePtr = blkPtr;
while (bytePtr < blkPtr + buf->blkSize)
{
ch = fgetc(fp);
*bytePtr = ch;
bytePtr++;
}
fclose(fp);
buf->numFreeBlk--;
buf->numIO++;
return blkPtr;
}
int writeBlockToDisk(unsigned char *blkPtr, unsigned int addr, Buffer *buf)
{
char filename[40];
unsigned char *bytePtr;
sprintf(filename, "../data/%d.blk", addr);
FILE *fp = fopen(filename, "w");
if (!fp)
{
perror("Writing Block Failed!\n");
return -1;
}
for (bytePtr = blkPtr; bytePtr < blkPtr + buf->blkSize; bytePtr++)
fputc((int)(*bytePtr), fp);
fclose(fp);
*(blkPtr - 1) = BLOCK_AVAILABLE;//重新将块置为可用,特别要注意这里
buf->numFreeBlk++;
buf->numIO++;
return 0;
}