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

Tim Zulf- WIP: #212

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 64 additions & 6 deletions src/cache.c
Expand Up @@ -6,12 +6,29 @@

/**
* Allocate a cache entry
LRU cache stuff
*/
struct cache_entry *alloc_entry(char *path, char *content_type, void *content, int content_length)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
struct cache_entry *entry = malloc(sizeof *entry);

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

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

entry->content_length = content_length;

entry->content = malloc(content_length);
//memcpy(entry->content, content, sizeof content_length);

//copy over contents from the actual varibales to the pointers of the struct.

return entry;
}

/**
Expand All @@ -22,6 +39,7 @@ void free_entry(struct cache_entry *entry)
///////////////////
// IMPLEMENT ME! //
///////////////////
free(entry);
}

/**
Expand Down Expand Up @@ -68,7 +86,7 @@ void dllist_move_to_head(struct cache *cache, struct cache_entry *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)
Expand All @@ -85,7 +103,7 @@ struct cache_entry *dllist_remove_tail(struct cache *cache)

/**
* Create a new cache
*
*
* max_size: maximum number of entries in the cache
* hashsize: hashtable size (0 for default)
*/
Expand All @@ -94,6 +112,16 @@ struct cache *cache_create(int max_size, int hashsize)
///////////////////
// IMPLEMENT ME! //
///////////////////
struct cache* created_cache = malloc(sizeof(struct cache));
struct hashtable *hash = hashtable_create(hashsize, NULL);

created_cache->index = hash;
created_cache->head = NULL;
created_cache->tail = NULL;
created_cache->cur_size = 0;
created_cache->max_size = max_size;

return created_cache;
}

void cache_free(struct cache *cache)
Expand All @@ -117,22 +145,52 @@ void cache_free(struct cache *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! //
///////////////////
// Allocate a new cache entry with the passed parameters.
struct cache_entry *new_dll = alloc_entry(path, content_type, content_length, content);
// insert new element into doubly linked list at the head
dllist_insert_head(cache, new_dll);
//put entry(now as a dll) into hashtable
hashtable_put(cache->index, path, new_dll);
//increase size of cache for each new entry.
//Ensuring the size counter for the number of entries in the cache is correct.
cache->cur_size++;
//If the cache size is greater than the max size:
struct cache_entry *tail;
while(cache->cur_size > cache->max_size){
//Remove the entry from the hashtable, using the entry's path and the hashtable_delete function.
tail = dllist_remove_tail(cache);
hashtable_delete(cache->index, tail->path);
//Remove the cache entry at the tail of the linked list.

//Free the cache entry.
free_entry(new_dll);
}

}

/**
* 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.
struct cache_entry *find = hashtable_get(cache->index, path);

if(find == NULL){
//If not found, return NULL.
return NULL;
} else {
//Move the cache entry to the head of the doubly-linked list.
dllist_move_to_head(cache, find);
//Return the cache entry pointer.
return find;
}
}
4 changes: 2 additions & 2 deletions src/file.c
Expand Up @@ -5,7 +5,7 @@

/**
* Loads a file into memory and returns a pointer to the data.
*
*
* Buffer is not NUL-terminated.
*/
struct file_data *file_load(char *filename)
Expand Down Expand Up @@ -72,4 +72,4 @@ void file_free(struct file_data *filedata)
{
free(filedata->data);
free(filedata);
}
}