<commit_before>#include "cache.h"
#include <debug.h>
#include <list.h>
#include "threads/synch.h"
#include "threads/thread.h"
#include "threads/malloc.h"
#include "devices/timer.h"
#include "filesys.h"

/* Cache with space for 64 blocks */
static struct list cache_list;
static struct lock cache_lock;
static uint32_t cache_size;

struct cache_entry
  {
    uint8_t block[BLOCK_SECTOR_SIZE];
    block_sector_t sector;

    struct list_elem cache_elem;
    bool is_dirty;
    /* For LRU cache replacement */
    int64_t last_access;
  };

void cache_init (void)
{
  list_init (&cache_list);
  lock_init (&cache_lock);
  cache_size = 0;

  /* Periodically write dirty blocks */
  thread_create ("write_task", PRI_DEFAULT, (void*)cache_task_evict_dirty, NULL);
}

bool cache_is_cached (block_sector_t sector)
{
  return cache_get_cache_entry (sector) != NULL;
}

/* Returns the corresponding cached entry for the given SECTOR.
 * Returns null if no cache is found. */
struct cache_entry* cache_get_cache_entry (block_sector_t sector)
{
  struct list_elem *e;
  for (e = list_begin (&cache_list); e != list_end (&cache_list); e = list_next (e))
    {
      struct cache_entry *entry = list_entry (e, struct cache_entry, cache_elem);
      if (entry->sector == sector)
        return entry;
    }
  return NULL;
}

/* Adds the given SECTOR to the cache and returns the cached entry */
struct cache_entry* cache_add (block_sector_t sector)
{
  // Cache has no space left
  if (cache_size >= MAX_CACHE_SECTORS_SIZE)
    cache_evict ();

  struct cache_entry *entry = (struct cache_entry*)malloc (sizeof (struct cache_entry));

  // Populate the new cache entry
  block_read (fs_device, sector, &entry->block);
  entry->sector = sector;
  entry->is_dirty = false;
  // Set last access time to current time
  entry->last_access = timer_ticks ();

  list_push_back (&cache_list, &entry->cache_elem);
  cache_size++;

  return entry;
}

/* Evicts one block from cache */
void cache_evict (void)
{
  // Get a dirty sector to evict
  struct cache_entry *entry;
  do {
    entry = cache_get_lru ();<commit_msg>Get rid of static variable cache_size and use list size<commit_after>#include "cache.h"
#include <debug.h>
#include <list.h>
#include "threads/synch.h"
#include "threads/thread.h"
#include "threads/malloc.h"
#include "devices/timer.h"
#include "filesys.h"

/* Cache with space for 64 blocks */
static struct list cache_list;
static struct lock cache_lock;

struct cache_entry
  {
    uint8_t block[BLOCK_SECTOR_SIZE];
    block_sector_t sector;

    struct list_elem cache_elem;
    bool is_dirty;
    /* For LRU cache replacement */
    int64_t last_access;
  };

void cache_init (void)
{
  list_init (&cache_list);
  lock_init (&cache_lock);

  /* Periodically write dirty blocks */
  thread_create ("write_task", PRI_DEFAULT, (void*)cache_task_evict_dirty, NULL);
}

bool cache_is_cached (block_sector_t sector)
{
  return cache_get_cache_entry (sector) != NULL;
}

/* Returns the corresponding cached entry for the given SECTOR.
 * Returns null if no cache is found. */
struct cache_entry* cache_get_cache_entry (block_sector_t sector)
{
  struct list_elem *e;
  for (e = list_begin (&cache_list); e != list_end (&cache_list); e = list_next (e))
    {
      struct cache_entry *entry = list_entry (e, struct cache_entry, cache_elem);
      if (entry->sector == sector)
        return entry;
    }
  return NULL;
}

/* Adds the given SECTOR to the cache and returns the cached entry */
struct cache_entry* cache_add (block_sector_t sector)
{
  // Cache has no space left
  uint32_t cache_size = list_size (&cache_list);
  if (cache_size >= MAX_CACHE_SECTORS_SIZE)
    cache_evict ();

  struct cache_entry *entry = (struct cache_entry*)malloc (sizeof (struct cache_entry));

  // Populate the new cache entry
  block_read (fs_device, sector, &entry->block);
  entry->sector = sector;
  entry->is_dirty = false;
  // Set last access time to current time
  entry->last_access = timer_ticks ();

  list_push_back (&cache_list, &entry->cache_elem);

  return entry;
}

/* Evicts one block from cache */
void cache_evict (void)
{
  // Get a dirty sector to evict
  struct cache_entry *entry;
  do {
    entry = cache_get_lru ();
{'fraction_non_alphanumeric': 0.10726971347383377, 'fraction_numerical': 0.003551977267345489, 'mean_word_length': 4.004739336492891, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6301857', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1393, 'n_words': 421}