Commit c4db4fc6 authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Implemented everything.

parent ea77b74f
......@@ -4,6 +4,13 @@
* Used in diskomd.
*/
#include <stdio.h>
#include <kom-types.h>
#include "exp.h"
#include "cache-node.h"
#include "smalloc.h"
#include "lyskomd.h"
EXPORT const Cache_node EMPTY_CACHE_NODE =
((Cache_node){{ 0, 0, 0}, NULL, NULL, 0, 0, 0, 0, NULL, NULL, 0});
......@@ -21,17 +28,20 @@ unlink_lru(Cache_node *node,
{
Cache_node *link;
link = node->lru;
link = node->next;
if ( node->lru != NULL )
node->lru->mru = node->mru;
if ( node->next != NULL )
node->next->prev = node->prev;
else if (*lru == node)
*lru = node->mru;
*lru = node->prev;
if ( node->mru != NULL )
node->mru->lru = link;
if ( node->prev != NULL )
node->prev->next = link;
else if (*mru == node)
*mru = link;
node->next = NULL;
node->prev = NULL;
}
EXPORT void
......@@ -39,14 +49,38 @@ insert_mru(Cache_node *node,
Cache_node **lru,
Cache_node **mru)
{
node->mru = NULL;
node->lru = *mru;
node->prev = NULL;
node->next = *mru;
*mru = node;
if ( *lru == NULL )
*lru = node;
if ( node->lru != NULL )
node->lru->mru = node;
if ( node->next != NULL )
node->next->prev = node;
}
static Cache_node_mcb *
alloc_cache_node_mcb(void)
{
Cache_node_mcb *t;
t = smalloc(sizeof(Cache_node_mcb));
*t = EMPTY_CACHE_NODE_MCB;
return t;
}
static Cache_node_block *
alloc_cache_node_block(int table_size)
{
Cache_node_block *t;
t = smalloc(sizeof(Cache_node_block));
*t = EMPTY_CACHE_NODE_BLOCK;
t->nodes = smalloc (table_size * sizeof (Cache_node));
t->next_free = 0;
t->link = NULL;
return t;
}
......@@ -61,7 +95,7 @@ create_cache_node_mcb(int mcb_size,
tmp->lookup_table = smalloc(sizeof(Cache_node *) * table_size);
tmp->lookup_table_size = table_size;
tmp->mcb_size = mcb_size;
tmp->last_block = alloc_cache_node_block();
tmp->last_block = NULL;
return tmp;
}
......@@ -78,15 +112,36 @@ get_cache_node (Cache_node_mcb *control,
}
static Cache_node *
alloc_cache_node(void)
alloc_cache_node (Cache_node_mcb *control)
{
Cache_node *c;
c = smalloc(sizeof(Cache_node));
if ( control->last_block == NULL
|| control->last_block->next_free >= control->mcb_size )
{
Cache_node_block *new_block;
new_block = alloc_cache_node_block (control->mcb_size);
new_block->link = control->last_block;
control->last_block = new_block;
}
c = &control->last_block->nodes[ control->last_block->next_free++ ];
*c = EMPTY_CACHE_NODE;
return c;
}
EXPORT void
destruct_cache_node(Cache_node_mcb *control,
u_long key)
{
if ( key >= control->lookup_table_size )
return;
control->lookup_table[ key ] = NULL;
}
EXPORT void
create_cache_node (Cache_node_mcb *control,
u_long key)
......@@ -96,9 +151,22 @@ create_cache_node (Cache_node_mcb *control,
"lookup_table_size = %lu\n",
control->lookup_table_size);
control->lookup_table[ key ] = alloc_cache_node();
control->lookup_table[ key ] = alloc_cache_node(control);
}
EXPORT void
zero_init_cache_node (Cache_node_mcb *control,
u_long key)
{
if ( key >= control->lookup_table_size )
restart_kom("ERROR: set_cache_node(%lu, %lu, value): "
"lookup_table_size = %lu\n",
control->lookup_table_size);
control->lookup_table[ key ] = NULL;
}
EXPORT void
set_mru(Cache_node_mcb *mcb,
......@@ -114,4 +182,3 @@ set_mru(Cache_node_mcb *mcb,
unlink_lru(node, &mcb->lru, &mcb->mru);
insert_mru(node, &mcb->lru, &mcb->mru);
}
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