-
Notifications
You must be signed in to change notification settings - Fork 603
/
Copy pathcache.c
182 lines (150 loc) · 4.26 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
#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)
{
///////////////////
struct cache_entry *ce = malloc(sizeof(struct cache_entry));
ce->path = malloc(strlen(path) + 1);
strcpy(ce->path, path);
ce->content_type = (strlen(content_type) + 1);
strcpy(ce->content_type, content_type);
ce->content_length = content_length;
ce->content = malloc(content_length);
memcpy(ce->content, content, content_length);
ce->prev = NULL;
ce->next = NULL;
return ce;
///////////////////
}
/**
* Deallocate a cache entry
*/
void free_entry(struct cache_entry *entry)
{
///////////////////
free(entry->path);
free(entry->content_type);
free(entry->content);
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 *cache = malloc(sizeof(struct cache));
cache->index = hashtable_create(hashsize, NULL);
cache->max_size = max_size;
cache->cur_size = 0;
cache->head = NULL;
cache->tail = NULL;
return 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 with the passed parameters.
struct cache_entry *new_ce = alloc_entry(path, content_type, content, content_length);
// Insert the entry at the head of the doubly-linked list.
dllist_insert_head(cache, new_ce);
// Store the entry in the hashtable as well, indexed by the entry's `path`.
// Increment the current size of the cache.
cache->cur_size += 1;
// If the cache size is greater than the max size:
if (cache->cur_size > cache->max_size) {
// Remove the cache entry at the tail of the linked list.
// Remove that same entry from the hashtable, using the entry's `path` and the `hashtable_delete` function.
// Free the cache entry.
// Ensure the size counter for the number of entries in the cache is correct.
}
///////////////////
}
/**
* Retrieve an entry from the cache
*/
struct cache_entry *cache_get(struct cache *cache, char *path)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
}