-
Notifications
You must be signed in to change notification settings - Fork 603
/
Copy pathcache.c
198 lines (172 loc) · 4.76 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashtable.h"
#include "cache.h"
// day 2 stuff
/**
* Allocate a cache entry
*/
struct cache_entry *alloc_entry(char *path, char *content_type, void *content, int content_length)
{
// free spaces for cache entry
struct cache_entry *new_entry = malloc(sizeof(struct cache_entry));
// deep copy path
new_entry->path = malloc(strlen(path) + 1);
strcpy(new_entry->path, path);
// deep copy content type
new_entry->content_type = malloc(strlen(content_type) + 1);
strcpy(new_entry->content_type, content_type);
// deep copy content length
new_entry->content_length = malloc(strlen(content_length) + 1);
memcpy(new_entry->content_length, content, content_length);
new_entry->content_length = content_length;
new_entry->prev = new_entry->next = NULL;
return new_entry;
}
/**
* Deallocate a cache entry
*/
void free_entry(struct cache_entry *entry)
{
// free entry struct properties
free(entry->content);
free(entry->content_length);
free(entry->content_type);
// free the struct itself
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)
{
struct cache *new_cache = malloc(sizeof(struct cache));
struct hashtable *hash_table = hashtable_create(hashsize, NULL);
new_cache->head = new_cache->tail = NULL;
new_cache->cur_size = 0;
new_cache->max_size = max_size;
new_cache->index = hash_table;
return new_cache;
}
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)
{
// Allocate a new cache entry
struct cache_entry *new_entry = alloc_entry(path, content_type, content, content_length);
// Insert the entry at the head of the dll
dllist_insert_head(cache, new_entry);
// Store the entry into the hashtable
hashtable_put(cache->index, path, new_entry);
// Increment the current size of the cache
cache->cur_size += 1;
// If the cache size is greater than max size
if (cache->cur_size > cache->max_size)
{
// Remove the entry from dll tail
struct cache_entry *old_entry = dllist_remove_tail(cache);
// remove the entry from hash
hashtable_delete(cache->index, old_entry->path);
// Free the cache entry
free_entry(old_entry);
// bring down current size to be no bigger than max size
if (cache->cur_size > cache->max_size)
{
cache->cur_size -= 1;
}
}
}
/**
* Retrieve an entry from the cache
*/
struct cache_entry *cache_get(struct cache *cache, char *path)
{
// if entry at index is NULL
if (hashtable_get(cache->index, path) == NULL)
{
return NULL;
}
else
{ // Move entry to head of the dll
dllist_move_to_head(cache, hashtable_get(cache->index, path));
return cache->head;
}
}