All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Typedefs | Functions | Variables
rlm_cache_rbtree.c File Reference

Simple rbtree based cache. More...

#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/heap.h>
#include <freeradius-devel/rad_assert.h>
#include "../../rlm_cache.h"
+ Include dependency graph for rlm_cache_rbtree.c:

Go to the source code of this file.

Data Structures

struct  rlm_cache_rbtree
 
struct  rlm_cache_rbtree_entry
 

Macros

#define PTHREAD_MUTEX_LOCK(_x)
 
#define PTHREAD_MUTEX_UNLOCK(_x)
 

Typedefs

typedef struct
rlm_cache_rbtree_entry 
rlm_cache_rbtree_entry_t
 
typedef struct rlm_cache_rbtree rlm_cache_rbtree_t
 

Functions

static int _cache_entry_free (UNUSED void *ctx, void *data)
 Walk over the cache rbtree. More...
 
static int _mod_detach (rlm_cache_rbtree_t *driver)
 Cleanup a cache_rbtree instance. More...
 
static int cache_acquire (void **handle, UNUSED rlm_cache_config_t const *config, UNUSED void *driver_inst, REQUEST *request)
 Lock the rbtree. More...
 
static rlm_cache_entry_tcache_entry_alloc (UNUSED rlm_cache_config_t const *config, UNUSED void *driver_inst, REQUEST *request)
 Custom allocation function for the driver. More...
 
static int cache_entry_cmp (void const *one, void const *two)
 Compare two entries by key. More...
 
static uint32_t cache_entry_count (UNUSED rlm_cache_config_t const *config, void *driver_inst, REQUEST *request, void *handle)
 Return the number of entries in the cache. More...
 
static cache_status_t cache_entry_expire (UNUSED rlm_cache_config_t const *config, void *driver_inst, REQUEST *request, void *handle, uint8_t const *key, size_t key_len)
 Free an entry and remove it from the data store. More...
 
static cache_status_t cache_entry_find (rlm_cache_entry_t **out, UNUSED rlm_cache_config_t const *config, void *driver_inst, REQUEST *request, void *handle, uint8_t const *key, size_t key_len)
 Locate a cache entry. More...
 
static cache_status_t cache_entry_insert (rlm_cache_config_t const *config, void *driver_inst, REQUEST *request, void *handle, rlm_cache_entry_t const *c)
 Insert a new entry into the data store. More...
 
static cache_status_t cache_entry_set_ttl (UNUSED rlm_cache_config_t const *config, void *driver_inst, REQUEST *request, UNUSED void *handle, rlm_cache_entry_t *c)
 Update the TTL of an entry. More...
 
static int cache_heap_cmp (void const *one, void const *two)
 Compare two entries by expiry time. More...
 
static void cache_release (UNUSED rlm_cache_config_t const *config, UNUSED void *driver_inst, REQUEST *request, rlm_cache_handle_t *handle)
 Release an entry unlocking any mutexes. More...
 
static int mod_instantiate (UNUSED CONF_SECTION *conf, UNUSED rlm_cache_config_t const *config, void *driver_inst)
 Create a new cache_rbtree instance. More...
 

Variables

cache_driver_t rlm_cache_rbtree
 

Detailed Description

Simple rbtree based cache.

Id:
e6bb5fd790d07ef642206b534d093e4bfdadd73f

Definition in file rlm_cache_rbtree.c.


Data Structure Documentation

struct rlm_cache_rbtree

Definition at line 37 of file rlm_cache_rbtree.c.

+ Collaboration diagram for rlm_cache_rbtree:
Data Fields
rbtree_t * cache Tree for looking up cache keys.
fr_heap_t * heap For managing entry expiry.
struct rlm_cache_rbtree_entry

Definition at line 46 of file rlm_cache_rbtree.c.

+ Collaboration diagram for rlm_cache_rbtree_entry:
Data Fields
rlm_cache_entry_t fields Entry data.
size_t offset Offset used for heap.

Macro Definition Documentation

#define PTHREAD_MUTEX_LOCK (   _x)

Definition at line 33 of file rlm_cache_rbtree.c.

#define PTHREAD_MUTEX_UNLOCK (   _x)

Definition at line 34 of file rlm_cache_rbtree.c.

Typedef Documentation

Function Documentation

static int _cache_entry_free ( UNUSED void *  ctx,
void *  data 
)
static

Walk over the cache rbtree.

Used to free any entries left in the tree on detach.

Parameters
ctxunused.
datato free.
Returns
2

Definition at line 89 of file rlm_cache_rbtree.c.

+ Here is the caller graph for this function:

static int _mod_detach ( rlm_cache_rbtree_t driver)
static

Cleanup a cache_rbtree instance.

Definition at line 99 of file rlm_cache_rbtree.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int cache_acquire ( void **  handle,
UNUSED rlm_cache_config_t const *  config,
UNUSED void *  driver_inst,
REQUEST request 
)
static

Lock the rbtree.

Note
handle not used except for sanity checks.
This callback is optional. If it's not provided the handle argument to other callbacks will be NULL.
Parameters
[out]handleWhere to write pointer to handle to access the cache with.
[in]configfor this instance of the rlm_cache module.
[in]driver_instDriver specific instance data.
[in]requestThe current request.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 337 of file rlm_cache_rbtree.c.

static rlm_cache_entry_t* cache_entry_alloc ( UNUSED rlm_cache_config_t const *  config,
UNUSED void *  driver_inst,
REQUEST request 
)
static

Custom allocation function for the driver.

Allows allocation of cache entry structures with additional fields.

Definition at line 158 of file rlm_cache_rbtree.c.

static int cache_entry_cmp ( void const *  one,
void const *  two 
)
static

Compare two entries by key.

There may only be one entry with the same key.

Definition at line 55 of file rlm_cache_rbtree.c.

+ Here is the caller graph for this function:

static uint32_t cache_entry_count ( UNUSED rlm_cache_config_t const *  config,
void *  driver_inst,
REQUEST request,
void *  handle 
)
static

Return the number of entries in the cache.

Note
handle not used except for sanity checks.
This callback is optional. Though max_entries will not be enforced if it is not provided.
Parameters
[in]configfor this instance of the rlm_cache module.
[in]driver_instDriver specific instance data.
[in]requestThe current request.
handlethe driver gave us when we called cache_acquire_t, or NULL if no cache_acquire_t callback was provided.
Returns
number of entries in the cache.

Definition at line 317 of file rlm_cache_rbtree.c.

+ Here is the call graph for this function:

static cache_status_t cache_entry_expire ( UNUSED rlm_cache_config_t const *  config,
void *  driver_inst,
REQUEST request,
void *  handle,
uint8_t const *  key,
size_t  key_len 
)
static

Free an entry and remove it from the data store.

Note
handle not used except for sanity checks.
This callback is not optional.
Parameters
[in]configfor this instance of the rlm_cache module.
[in]driver_instDriver specific instance data.
[in]requestThe current request.
[in]handlethe driver gave us when we called cache_acquire_t, or NULL if no cache_acquire_t callback was provided.
[in]keyof entry to expire.
[in]key_lenthe length of the key string.
Returns

Definition at line 219 of file rlm_cache_rbtree.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static cache_status_t cache_entry_find ( rlm_cache_entry_t **  out,
UNUSED rlm_cache_config_t const *  config,
void *  driver_inst,
REQUEST request,
void *  handle,
uint8_t const *  key,
size_t  key_len 
)
static

Locate a cache entry.

Note
handle not used except for sanity checks.

If a cache entry is found, but the cache entry needs to be deserialized, the driver is expected to allocate an appropriately sized rlm_cache_entry_t, perform the deserialisation, and write a pointer to the new entry to out, returning CACHE_OK.

If the rlm_cache_handle_t is inviable, the driver should return CACHE_RECONNECT, to have it reinitialised/reconnected.

Parameters
[out]outWhere to write a pointer to the retrieved entry (if there was one).
[in]configfor this instance of the rlm_cache module.
[in]driver_instDriver specific instance data.
[in]requestThe current request.
[in]handlethe driver gave us when we called cache_acquire_t, or NULL if no cache_acquire_t callback was provided.
[in]keyto use to lookup cache entry
[in]key_lenthe length of the key string.
Returns

Definition at line 178 of file rlm_cache_rbtree.c.

+ Here is the call graph for this function:

static cache_status_t cache_entry_insert ( rlm_cache_config_t const *  config,
void *  driver_inst,
REQUEST request,
void *  handle,
rlm_cache_entry_t const *  c 
)
static

Insert a new entry into the data store.

Note
handle not used except for sanity checks.

Serialize (if necessary) the entry passed to us, and write it to the cache with the key c->key.

The cache entry should not be freed by the driver, irrespective of success or failure. If the entry needs to be freed after insertion because a local copy should not be kept, the driver should provide a cache_entry_free_t callback.

If the rlm_cache_handle_t is inviable, the driver should return CACHE_RECONNECT, to have it reinitialised/reconnected.

Note
This callback is not optional.
This callback must overwrite existing cache entries on insert.
Parameters
configfor this instance of the rlm_cache module.
driver_instDriver specific instance data.
requestThe current request.
handlethe driver gave us when we called cache_acquire_t, or NULL if no cache_acquire_t callback was provided.
cto insert.
Returns

Definition at line 246 of file rlm_cache_rbtree.c.

+ Here is the call graph for this function:

static cache_status_t cache_entry_set_ttl ( UNUSED rlm_cache_config_t const *  config,
void *  driver_inst,
REQUEST request,
UNUSED void *  handle,
rlm_cache_entry_t c 
)
static

Update the TTL of an entry.

Note
handle not used except for sanity checks.
This callback optional. If it's not specified the cache code will expire and recreate the entry with a new TTL.

If the rlm_cache_handle_t is inviable, the driver should return CACHE_RECONNECT, to have it reinitialised/reconnected.

Parameters
[in]configfor this instance of the rlm_cache module.
[in]driver_instDriver specific instance data.
[in]requestThe current request.
[in]handlethe driver gave us when we called cache_acquire_t, or NULL if no cache_acquire_t callback was provided.
[in]cto update the TTL of. c->ttl will have been set to the new value.
Returns

Definition at line 289 of file rlm_cache_rbtree.c.

+ Here is the call graph for this function:

static int cache_heap_cmp ( void const *  one,
void const *  two 
)
static

Compare two entries by expiry time.

There may be multiple entries with the same expiry time.

Definition at line 70 of file rlm_cache_rbtree.c.

+ Here is the caller graph for this function:

static void cache_release ( UNUSED rlm_cache_config_t const *  config,
UNUSED void *  driver_inst,
REQUEST request,
rlm_cache_handle_t handle 
)
static

Release an entry unlocking any mutexes.

Note
handle not used except for sanity checks.
This callback is optional.
Parameters
[in]configfor this instance of the rlm_cache module.
[in]driver_instDriver specific instance data.
[in]requestThe current request.
[in]handleto release.

Definition at line 364 of file rlm_cache_rbtree.c.

static int mod_instantiate ( UNUSED CONF_SECTION conf,
UNUSED rlm_cache_config_t const *  config,
void *  driver_inst 
)
static

Create a new cache_rbtree instance.

Function to handle any driver specific instantiation.

Parameters
confsection holding driver specific CONF_PAIR (s).
configof the rlm_cache module. Should not be modified.
driver_instA uint8_t array of inst_size if inst_size > 0, else NULL. Drivers should add their own cleanup function to this chunk using talloc_set_destructor.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 117 of file rlm_cache_rbtree.c.

+ Here is the call graph for this function:

Variable Documentation

Initial value:
= {
.name = "rlm_cache_rbtree",
.instantiate = mod_instantiate,
.inst_size = sizeof(rlm_cache_rbtree_t),
.insert = cache_entry_insert,
.expire = cache_entry_expire,
.set_ttl = cache_entry_set_ttl,
.acquire = cache_acquire,
.release = cache_release,
}
static uint32_t cache_entry_count(UNUSED rlm_cache_config_t const *config, void *driver_inst, REQUEST *request, void *handle)
Return the number of entries in the cache.
static cache_status_t cache_entry_set_ttl(UNUSED rlm_cache_config_t const *config, void *driver_inst, REQUEST *request, UNUSED void *handle, rlm_cache_entry_t *c)
Update the TTL of an entry.
static rlm_cache_entry_t * cache_entry_alloc(UNUSED rlm_cache_config_t const *config, UNUSED void *driver_inst, REQUEST *request)
Custom allocation function for the driver.
static void cache_release(UNUSED rlm_cache_config_t const *config, UNUSED void *driver_inst, REQUEST *request, rlm_cache_handle_t *handle)
Release an entry unlocking any mutexes.
static cache_status_t cache_entry_insert(rlm_cache_config_t const *config, void *driver_inst, REQUEST *request, void *handle, rlm_cache_entry_t const *c)
Insert a new entry into the data store.
static int cache_acquire(void **handle, UNUSED rlm_cache_config_t const *config, UNUSED void *driver_inst, REQUEST *request)
Lock the rbtree.
static int mod_instantiate(UNUSED CONF_SECTION *conf, UNUSED rlm_cache_config_t const *config, void *driver_inst)
Create a new cache_rbtree instance.
static cache_status_t cache_entry_expire(UNUSED rlm_cache_config_t const *config, void *driver_inst, REQUEST *request, void *handle, uint8_t const *key, size_t key_len)
Free an entry and remove it from the data store.
struct rlm_cache_rbtree rlm_cache_rbtree_t
static cache_status_t cache_entry_find(rlm_cache_entry_t **out, UNUSED rlm_cache_config_t const *config, void *driver_inst, REQUEST *request, void *handle, uint8_t const *key, size_t key_len)
Locate a cache entry.

Definition at line 380 of file rlm_cache_rbtree.c.