-
Notifications
You must be signed in to change notification settings - Fork 603
/
Copy pathcache.c
178 lines (152 loc) · 3.99 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
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashtable.h"
#include "cache.h"
/**
* Allocate a cache entry
*/
struct cache_entry *alloc_entry(char *path, char *content_type, void *content, int content_length)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
struct cache_entry *entry = malloc(sizeof(struct cache_entry));
entry->path = path;
entry->content_type = content_type;
entry->content = content;
entry->content_length = content_length;
entry->next = NULL;
entry->prev = NULL;
return entry;
}
/**
* Deallocate a cache entry
*/
void free_entry(struct cache_entry *entry)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
free(entry);
}
/**
* Insert a cache entry at the head of the linked list
*/
void dllist_insert_head(struct cache *cache, struct cache_entry *ce)
{
// Insert at the head of the list
if (cache->head == NULL) {
cache->head = cache->tail = ce;
ce->prev = ce->next = NULL;
} else {
cache->head->prev = ce;
ce->next = cache->head;
ce->prev = NULL;
cache->head = ce;
}
}
/**
* Move a cache entry to the head of the list
*/
void dllist_move_to_head(struct cache *cache, struct cache_entry *ce)
{
if (ce != cache->head) {
if (ce == cache->tail) {
// We're the tail
cache->tail = ce->prev;
cache->tail->next = NULL;
} else {
// We're neither the head nor the tail
ce->prev->next = ce->next;
ce->next->prev = ce->prev;
}
ce->next = cache->head;
cache->head->prev = ce;
ce->prev = NULL;
cache->head = ce;
}
}
/**
* Removes the tail from the list and returns it
*
* NOTE: does not deallocate the tail
*/
struct cache_entry *dllist_remove_tail(struct cache *cache)
{
struct cache_entry *oldtail = cache->tail;
cache->tail = oldtail->prev;
cache->tail->next = NULL;
cache->cur_size--;
return oldtail;
}
/**
* Create a new cache
*
* max_size: maximum number of entries in the cache
* hashsize: hashtable size (0 for default)
*/
struct cache *cache_create(int max_size, int hashsize)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
struct cache *newcache = malloc(sizeof(struct cache));
newcache->index = hashtable_create(hashsize, NULL);
newcache->head = NULL;
newcache->tail = NULL;
newcache->max_size = max_size;
newcache->cur_size = 0;
return newcache;
}
void cache_free(struct cache *cache)
{
struct cache_entry *cur_entry = cache->head;
hashtable_destroy(cache->index);
while (cur_entry != NULL) {
struct cache_entry *next_entry = cur_entry->next;
free_entry(cur_entry);
cur_entry = next_entry;
}
free(cache);
}
/**
* Store an entry in the cache
*
* This will also remove the least-recently-used items as necessary.
*
* NOTE: doesn't check for duplicate cache entries
*/
void cache_put(struct cache *cache, char *path, char *content_type, void *content, int content_length)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
struct cache_entry *entry = alloc_entry(path, content_type, content, content_length);
dllist_insert_head(cache, entry);
hashtable_put(cache->index, path, entry);
cache->cur_size++;
if(cache->cur_size > cache->max_size){
struct cache_entry *tail = dllist_remove_tail(cache);
hashtable_delete(cache->index, tail->path);
free_entry(tail);
if(cache->cur_size > cache->max_size){
cache->cur_size--;
}
}
}
/**
* Retrieve an entry from the cache
*/
struct cache_entry *cache_get(struct cache *cache, char *path)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
struct cache_entry *pointer = hashtable_get(cache->index, path);
if(pointer == NULL){
return NULL;
}
dllist_move_to_head(cache, pointer);
return pointer;
}