-
Notifications
You must be signed in to change notification settings - Fork 603
/
Copy pathcache.c
213 lines (183 loc) · 6.14 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#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! //
///////////////////
//allocate space for the struct were going to return
struct cache_entry *ce = malloc(sizeof *ce); //size of dereference ce if dereffed it is cache_entry
//set all fields to values being passed in to initialize
//have a pointer to something but have to make sure what you are pointing to persists(make sure it doesnt accidentally get freed)
ce->path = malloc(strlen(path) + 1); //so make a copy of path we know will persist. +1 for null terminator
strcpy(ce->path, path); //(dest, args) copy path into allocated memory. this is deep copy.
ce->content_type = malloc(strlen(content_type) +1);
strcpy(ce->content_type, content_type);
ce->content = malloc(content_length); //content is void pointer so dont know what type so cant use strlen, but know its length so can use that
memcpy(ce->content, content, content_length); //(dest, src, size)
ce->content_length = content_length; //int so dont have to malloc
ce->prev = ce->next = NULL;
return ce;
}
/**
* Deallocate a cache entry
*/
void free_entry(struct cache_entry *entry)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
//have to free the cache entry passed in but have to free everything we malloc'ed space for
//free everything in stucture, then free the structure itself so entry last
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)
{
///////////////////
// IMPLEMENT ME! //
/////////////////// going in opposite order from cache free below. also refer to cache struct in cache.h
//allocate cache
struct cache *cache = malloc(sizeof *cache);
//functions in hashtable.h and c
cache->index = hashtable_create(hashsize, NULL); //size and pointer to a function (that takes in certain values and returns int)
//initialize head and tail null
cache->head = cache->tail = NULL;
cache->max_size = max_size; //int so dont allocate space
cache->cur_size = 0;
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);
}
void clean_lru(struct cache *cache)
{
while (cache->cur_size > cache->max_size) {
struct cache_entry *oldtail = dllist_remove_tail(cache);
hashtable_delete(cache->index, oldtail->path);
free_entry(oldtail);
}
}
/**
* 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! //
///////////////////
//Allocate a new cache entry with the passed parameters
struct cache_entry *ce = alloc_entry(path, content_type, content, content_length);
//Insert the entry at the head of the doubly-linked list
dllist_insert_head(cache, ce);
//Store the entry in the hashtable as well, indexed by the entry's path
hashtable_put(cache->index, ce->path, ce);
//Increment the current size of the cache
cache->cur_size++;
clean_lru(cache);
//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
// dllist_remove_tail(ce);
// //Remove that same entry from the hashtable, using the entry's path and the hashtable_delete function
// hashtable_delete(ce->path, ce);
// //Free the cache entry
// cache_free(ce);
// //Ensure the size counter for the number of entries in the cache is correct
// cache->cur_size--;
// }
}
/**
* Retrieve an entry from the cache
*/
struct cache_entry *cache_get(struct cache *cache, char *path)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
//Attempt to find the cache entry pointer by path in the hash table. If not found, return NULL
struct cache_entry *ce = hashtable_get(cache->index, path);
if (ce == NULL) {
return NULL;
}
//Move the cache entry to the head of the doubly-linked list
dllist_move_to_head(cache, ce);
//Return the cache entry pointer
return ce;
}