forked from bloominstituteoftechnology/C-Web-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.c
138 lines (118 loc) · 2.81 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
#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! //
///////////////////
}
/**
* Deallocate a cache entry
*/
void free_entry(struct cache_entry *entry)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
}
/**
* 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! //
///////////////////
}
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! //
///////////////////
}
/**
* Retrieve an entry from the cache
*/
struct cache_entry *cache_get(struct cache *cache, char *path)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
}