Giter Club home page Giter Club logo

c-lru-cache's Introduction

C LRU Cache

A simple least recently used cache written in C.

Usage

Select a maximum size for the cache to grow to (e.g 60MB), determine the average size of items in the cache (a guess is OK), then use the cache using five functions:

// cache size, and hypothetical item #define MAX_SIZE (60 * 1024 * 1024) #define AVG_SIZE (2 * 1024) void *key = (void *)“test”; int key_length = 4; void *item = (void *)“value”; int item_length = 5; // create a new cache lru_cache *cache = lru_cache_new(MAX_SIZE, AVG_SIZE); // set an item lru_cache_set(cache, key, key_length, item, item_length); // get an item lru_cache_get(cache, key, key_length, &item); // delete an item lru_cache_delete(cache, key, key_length); // free a cache lru_cache_free(cache);

Implementation

There are no external dependencies.

c-lru-cache's People

Contributors

willcannings avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

c-lru-cache's Issues

LICENSE ?

What is the license to reuse this ?

Patch for memleaks

There are memleaks in the implementation because:

  • free_items list is not freed
  • neither is the mutex
  • nor any of the items' value or key ...

Here is a patch to fix that

diff --git a/lruc.c b/lruc.c
index 1de4cc5..354c483 100644
--- a/lruc.c
+++ b/lruc.c
@@ -173,6 +173,8 @@ lruc_error lruc_free(lruc *cache) {
       item = cache->items[i];    
       while(item) {
         next = (lruc_item *) item->next;
+        free(item->key);
+        free(item->value);
         free(item);
         item = next;
       }
@@ -180,12 +182,21 @@ lruc_error lruc_free(lruc *cache) {
     free(cache->items);
   }
   
+  if(cache->free_items) {
+    item = cache->free_items;
+    while(item) {
+      next = (lruc_item *) item->next;
+      free(item);
+      item = next;
+    }
+  }
   // free the cache
   if(cache->mutex) {
     if(pthread_mutex_destroy(cache->mutex)) {
       perror("LRU Cache unable to destroy mutex");
       return LRUC_PTHREAD_ERROR;
     }
+    free(cache->mutex);
   }
   free(cache);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.