forked from sally20921/proxylab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.c
168 lines (136 loc) · 3.84 KB
/
cache.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
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "csapp.h"
/* Recommended max cache and object sizes */
#define MAX_CACHE_SIZE 1049000
#define MAX_OBJECT_SIZE 102400
struct cache_block
{
struct cache_block *next, *prev;
long LRU;
char *block; ssize_t block_size;
char *request;
};
struct cache_block * alloc_block(int size);
void cache_init();
int reader_check(char *request, char *block);
int remove_block(struct cache_block * blo);
void writer_check(char *request, char *block, int block_size);
#include "cache.h"
//multiple threads must be able to simultaneously read from the cache
//only one thread should be permitted to write to the cache at a time
//Implements First Readers-Writers Problem solution
static long lru;
struct CacheList *cache;
static int readcnt;
sem_t mutex,w;
void cache_init()
{
lru = 0;
cache = (struct CacheList *)Malloc(sizeof(struct CacheList));
cache->head = NULL;
cache->size = 0;
Sem_init(&mutex, 0, 1);
Sem_init(&w, 0, 1);
readcnt = 0;
}
struct cache_block *alloc_block(int size)
{
struct cache_block *temp;
temp = (struct cache_block *)Malloc(sizeof (struct cache_block));
temp->LRU = ++lru;
temp->block = (char *)Malloc(size);
temp->block_size = size;
return temp;
}
int remove_block(struct cache_block *temp)
{
Free(temp->block);
Free(temp->request);
Free(temp);
return 1;
}
int read_cache(char *request, char *buf)
{
int len = 0;
P(&mutex);
readcnt++;
if (readcnt == 1) { //first in
P(&w);
}
V(&mutex);
//critical section, reading happens
struct cache_block *temp = cache->head;
while (temp != NULL) {//traverse through cache list
if (!strncasecmp(temp->request, request, strlen(request)))
{
//found it, read it, copy it into buf
temp->LRU = ++lru;
memcpy(buf, temp->block, temp->block_size);
len = temp->block_size;
break;
}
temp = temp->next;
}
P(&mutex);
readcnt--;
if (readcnt == 0) { //last out
V(&w);
}
V(&mutex);
return len;
}
void write_cache(char *request, char *block_data, int block_size)
{
struct cache_block *temp;
P(&w);
while (cache->size + block_size >= MAX_CACHE_SIZE)
{
//evict according to LRU policy if it exceeds MAX_CACHE_SIZE
struct cache_block *evict = NULL;
//for (temp = head; temp; temp = temp->next)
temp = cache->head;
while ( temp != NULL)
{
if (temp->LRU < evict->LRU) {
evict = temp;
//find the least recently used block
}
temp = temp->next;
}
cache->size -= evict->block_size;
//evicting least recently used block
if (evict == cache->head) {
cache->head = evict->next;
if (cache->head) {
cache->head-> prev = NULL;
}
}
else {
evict->prev->next = evict->next;
if (evict->next) {
evict->next->prev = evict->prev;
}
}
(void) remove_block(evict);
}
//adding new cache block
cache->size += block_size;
temp = alloc_block(block_size);
//void *memcpy(void *destination, const void *source, size_t num);
memcpy(temp->block, block_data, block_size);
temp->request = (char *) Malloc(strlen(request)+1);
strncpy(temp->request, request, strlen(request));
if (cache->head) {
cache->head->prev = temp;
temp->next = cache->head;
temp->prev = NULL;
cache->head = temp;
}
else { //if it is the first item
cache->head = temp;
cache->head->next = cache->head->prev = NULL;
}
V(&w);
}