Commit 1162d1cd authored by Shivaji's avatar Shivaji

Merge branch 'lru-sample' into 'master'

Lru sample

See merge request !2
parents e7b03d37 584623de
......@@ -16,15 +16,17 @@
struct KV *array[MAX_SIZE];
struct queue *qu=NULL;
struct queue *last;
unsigned _Atomic lock=0;
void remove_element_from_deque(char *key)
{
while(ATOMIC_TEST_AND_SET(&(lock),1) == 1);
queue *present = qu , *previous=NULL;
if(present == NULL)
if(present == NULL){
CLEAR(&(lock),0);
return;
}
if(strcmp(present->key, key) == 0)
{
......@@ -32,6 +34,7 @@ void remove_element_from_deque(char *key)
if(last == present)
last = last->next;
free(present);
CLEAR(&(lock),0);
return;
}
......@@ -43,15 +46,18 @@ void remove_element_from_deque(char *key)
last = previous;
previous->next = present->next;
free(present);
CLEAR(&(lock),0);
return;
}
previous = present;
present = present->next;
}
CLEAR(&(lock),0);
}
void insert_into_queue(char *key)
{
while(ATOMIC_TEST_AND_SET(&(lock),1) == 1);
queue *temp = (queue *)malloc(sizeof(queue));
memcpy(temp->key, key, KEY_SIZE);
temp->next = NULL;
......@@ -65,10 +71,12 @@ void insert_into_queue(char *key)
last->next = temp;
last = temp;
}
CLEAR(&(lock),0);
}
int remove_front_element()
{
while(ATOMIC_TEST_AND_SET(&(lock),1) == 1);
queue *temp = qu;
qu = qu->next;
assert(temp != NULL);
......@@ -79,9 +87,11 @@ int remove_front_element()
{
// check if it is modified to write to file
array[i]->valid = FALSE;
CLEAR(&(lock),0);
return i;
}
}
CLEAR(&(lock),0);
}
int find_empty_location()
......@@ -89,9 +99,13 @@ int find_empty_location()
for(int i=0;i<MAX_SIZE;i++)
{
if(array[i]->valid == FALSE)
{
CLEAR(&(lock),0);
return i;
}
return remove_front_element();
}
int indx = remove_front_element();
return indx;
}
int cache_del(char *key)
......@@ -121,7 +135,6 @@ void cache_put(char *key, char *value)
if(array[i]->key!=NULL) {
if(strcmp(array[i]->key , key) == 0)
{
printf("ENTERING INTO QUEUE\n");
indx = i;
remove_element_from_deque(key);
break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment