Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
90 changes: 82 additions & 8 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ 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;
// new_entry->content_type = content_type;
// new_entry->content = content;
// new_entry->content_length = content_length;

// return new_entry;
new_entry->path = malloc(strlen(path) + 1);
strcpy(new_entry->path, path);

new_entry->content_type = malloc(strlen(content_type) + 1);
strcpy(new_entry->content_type, content_type);

new_entry->content = malloc(content_length);
memcpy(new_entry->content, content, content_length);

new_entry->content_length = content_length;

new_entry->prev = new_entry->next = NULL;

return new_entry;
}

/**
Expand All @@ -22,6 +43,11 @@ void free_entry(struct cache_entry *entry)
///////////////////
// IMPLEMENT ME! //
///////////////////

free(entry->path);
free(entry->content_type);
free(entry->content);
free(entry);
}

/**
Expand All @@ -30,10 +56,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 +75,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 +97,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 @@ -94,6 +125,17 @@ struct cache *cache_create(int max_size, int hashsize)
///////////////////
// IMPLEMENT ME! //
///////////////////
struct cache *new_cache = malloc(sizeof(struct cache));
struct hashtable *hash_table = hashtable_create(hashsize, NULL);

// new_cache->head = NULL;
// new_cache->tail = 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)
Expand All @@ -102,7 +144,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 +168,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 All @@ -135,4 +198,15 @@ struct cache_entry *cache_get(struct cache *cache, char *path)
///////////////////
// IMPLEMENT ME! //
///////////////////
if (hashtable_get(cache->index, path) == NULL)
{
return NULL;
}
else
{
// Move the cache entry to the head of the doubly-linked list.
dllist_move_to_head(cache, hashtable_get(cache->index, path));
// Return the cache entry pointer.
return cache->head;
}
}
2 changes: 1 addition & 1 deletion src/cache_tests/cache_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ char *test_cache_put()
mu_assert(check_cache_entries(cache->head->next->prev, test_entry_3) == 0, "Your cache_put function did not update the head->next->prev pointer to point to the new head entry");
mu_assert(check_cache_entries(cache->head->next->next, test_entry_1) == 0, "Your cache_put function did not update the head->next->next pointer to point to the tail entry");
mu_assert(check_cache_entries(cache->tail->prev, test_entry_2) == 0, "Your cache_put function did not update the tail->prev pointer to poin to the second-to-last entry");
mu_assert(check_cache_entries(cache->tail, test_entry_1) == 0, "Your cache_put function did not correctly update the tail pointer of the cache");
mu_assert(check_cache_entries(cache->tail, test_entry_1) == 0, "Your cache_put function did not correctly update the tail pointer of the cache");

// Add in a fourth entry to the cache
cache_put(cache, test_entry_4->path, test_entry_4->content_type, test_entry_4->content, test_entry_4->content_length);
Expand Down
Loading