Class e_cache;
Class e_cacheslot {
Friend class e_cache;
Public:
E_cacheslot (INT slotsize)
{
Slot_size = slotsize;
Slot_mem = new char [slot_size];
Next_slot = NULL;
Last_time = 0;
File_id = 0;
Locked = 0;
}
Virtual ~ E_cacheslot ()
{
If (slot_mem)
{
Delete [] slot_mem;
Slot_mem = NULL;
}
}
Virtual void deletethis () = 0;
Void clearis ()
{
// Backup slot_mem to disk and delete the memory
Char file_name [max_file_name_len];
Sprintf (file_name, "% d", file_id );
File * fp = fopen (file_name, "WB ");
Fwrite (slot_mem, slot_size, 1, FP );
Fclose (FP );
Delete [] slot_mem;
Slot_mem = NULL;
}
Void accessbegin (e_cache * pcache)
{
++ Locked;
If (slot_mem = NULL)
{
// Allocate memory
Slot_mem = new char [slot_size];
// Reload data from disk
Char file_name [max_file_name_len];
Sprintf (file_name, "% d", file_id );
File * fp = fopen (file_name, "rb ");
Fread (slot_mem, slot_size, 1, FP );
Fclose (FP );
// Delete oldest item
Pcache-> purge_memory (slot_size );
}
}
Void accessential D (e_cache * pcache)
{
Last_time = pcache-> get_current_time ();
-- Locked;
}
Public:
Char * slot_mem;
PRIVATE:
E_cacheslot * next_slot;
Int slot_size;
Ulong last_time;
Int file_id;
Int locked;
};
// A cache for a thread, each thread shoshould has its own cache by this design
Class e_cache {
Public:
E_cache (INT cachesize)
{
Slots = NULL;
Limit = cachesize;
Current_time = 0;
Current_file_id = 0;
Memory_size = 0;
}
~ E_cache ()
{
E_cacheslot * pslot;
While (pslot = slots )! = NULL)
{
Slots = pslot-> next;
Pslot-> deletethis ();
}
}
Ulong get_current_time ()
{
Return (++ current_time );
}
Void add_slot (e_cacheslot * newslot)
{
If (newslot)
{
Newslot-> next_slot = slots;
Slots = newslot;
Newslot-> file_id = (++ current_file_id );
Purge_memory (newslot-> slot_size );
}
}
Void purge_memory (INT newaddsize)
{
Memory_size + = newaddsize;
// If there's no enough memory, delete oldest items
If (memory_size> Limit & slots)
{
E_cacheslot * oldestslot = slots;
E_cacheslot * pslot = slots-> next_slot;
While (pslot)
{
If (pslot-> locked = 0 & pslot-> last_time <oldestslot-> last_time)
{
Oldestslot = pslot;
}
Pslot = pslot-> next_slot;
}
If (oldestslot-> locked = 0)
{
Oldestslot-> clearthur is ();
Memory_size-= oldestslot-> slot_size;
}
}
}
PRIVATE:
E_cacheslot * slots;
Int limit;
Ulong memory_size;
Ulong current_time;
Int current_file_id;
};