Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rileyB-web-server #308

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ struct cache_entry *alloc_entry(char *path, char *content_type, void *content, i
///////////////////
// IMPLEMENT ME! //
///////////////////
struct cache_entry *new_entry = malloc(sizeof(struct cache_entry));
new_entry->path = path;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you will run into some seg faults if you don't strcpy/memcpy some of these variables.

new_entry->content_type = content_type;
new_entry->content = content;
new_entry->content_length = content_length;

return new_entry;
}

/**
Expand All @@ -30,10 +37,13 @@ void free_entry(struct cache_entry *entry)
void dllist_insert_head(struct cache *cache, struct cache_entry *ce)
{
// Insert at the head of the list
if (cache->head == NULL) {
if (cache->head == NULL)
{
cache->head = cache->tail = ce;
ce->prev = ce->next = NULL;
} else {
}
else
{
cache->head->prev = ce;
ce->next = cache->head;
ce->prev = NULL;
Expand All @@ -46,13 +56,16 @@ void dllist_insert_head(struct cache *cache, struct cache_entry *ce)
*/
void dllist_move_to_head(struct cache *cache, struct cache_entry *ce)
{
if (ce != cache->head) {
if (ce == cache->tail) {
if (ce != cache->head)
{
if (ce == cache->tail)
{
// We're the tail
cache->tail = ce->prev;
cache->tail->next = NULL;

} else {
}
else
{
// We're neither the head nor the tail
ce->prev->next = ce->next;
ce->next->prev = ce->prev;
Expand All @@ -65,7 +78,6 @@ void dllist_move_to_head(struct cache *cache, struct cache_entry *ce)
}
}


/**
* Removes the tail from the list and returns it
*
Expand Down Expand Up @@ -102,7 +114,8 @@ void cache_free(struct cache *cache)

hashtable_destroy(cache->index);

while (cur_entry != NULL) {
while (cur_entry != NULL)
{
struct cache_entry *next_entry = cur_entry->next;

free_entry(cur_entry);
Expand All @@ -125,6 +138,26 @@ void cache_put(struct cache *cache, char *path, char *content_type, void *conten
///////////////////
// IMPLEMENT ME! //
///////////////////

struct cache_entry *new_entry = alloc_entry(path, content_type, content, content_length);

dllist_insert_head(cache, new_entry);

hashtable_put(cache->index, path, new_entry);

cache->cur_size += 1;

if (cache->cur_size > cache->max_size)
{

struct cache_entry *old_entry = dllist_remove_tail(cache);
hashtable_delete(cache->index, old_entry->path);
free_entry(old_entry);
if (cache->cur_size > cache->max_size)
{
cache->cur_size -= 1;
}
}
}

/**
Expand Down