mlt 0.7.6
Public Member Functions | Data Fields | Private Member Functions

mlt_cache_s Struct Reference

Cache class. More...

Public Member Functions

void mlt_cache_close (mlt_cache cache)
 Destroy a cache.
mlt_cache_item mlt_cache_get (mlt_cache cache, void *object)
 Get a chunk of data from the cache.
int mlt_cache_get_size (mlt_cache cache)
 Get the numer of possible cache items.
mlt_cache mlt_cache_init ()
 Create a new cache.
void * mlt_cache_item_data (mlt_cache_item item, int *size)
 Get the data pointer from the cache item.
void mlt_cache_purge (mlt_cache cache, void *object)
 Remove cache entries for an object.
void mlt_cache_put (mlt_cache cache, void *object, void *data, int size, mlt_destructor destructor)
 Put a chunk of data in the cache.
void mlt_cache_set_size (mlt_cache cache, int size)
 Set the numer of items to cache.

Data Fields

void * A [MAX_CACHE_SIZE]
mlt_properties active
 a list of cache items some of which may no longer be in current but to which there are outstanding references
void * B [MAX_CACHE_SIZE]
int count
 the number of items currently in the cache
void * current
 pointer to the current array of pointers
mlt_properties garbage
 a list cache items pending release.
pthread_mutex_t mutex
 a mutex to prevent multi-threaded race conditions
int size
 the maximum number of items permitted in the cache <= MAX_CACHE_SIZE

Private Member Functions

static void cache_object_close (mlt_cache cache, void *object, void *data)
 Close a cache item given its parent object pointer.
static void ** shuffle_get_hit (mlt_cache cache, void *object)
 Shuffle the cache entries between the two arrays and return the cache entry for an object.

Detailed Description

Cache class.

This is a utility class for implementing a Least Recently Used (LRU) cache of data blobs indexed by the address of some other object (e.g., a service). Instead of sorting and manipulating linked lists, it tries to be simple and elegant by copying pointers between two arrays of fixed size to shuffle the order of elements.

This class is useful if you have a service that wants to cache something somewhat large, but will not scale if there are many instances of the service. Of course, the service will need to know how to recreate the cached element if it gets flushed from the cache,

The most obvious examples are the pixbuf and qimage producers that cache their respective objects representing a picture read from a file. If the picture is no longer in the cache, it can simply re-read it from file. However, a picture is often repeated over many frames and makes sense to cache instead of continually reading, parsing, and decoding. On the other hand, you might want to load hundreds of pictures as individual producers, which would use a lot of memory if every picture is held in memory!


Member Function Documentation

static void cache_object_close ( mlt_cache  cache,
void *  object,
void *  data 
) [private]

Close a cache item given its parent object pointer.

Parameters:
cachea cache
objectthe object to which the data object belongs
datathe data object, which might be in the garbage list (optional)
void mlt_cache_close ( mlt_cache  cache)

Destroy a cache.

Parameters:
cachethe cache to destroy
mlt_cache_item mlt_cache_get ( mlt_cache  cache,
void *  object 
)

Get a chunk of data from the cache.

Parameters:
cachea cache object
objectthe object for which you are trying to locate the data
Returns:
a mlt_cache_item if found or NULL if not found or has been flushed from the cache
int mlt_cache_get_size ( mlt_cache  cache)

Get the numer of possible cache items.

Parameters:
cachethe cache to check
Returns:
the current maximum size of the cache
mlt_cache mlt_cache_init ( )

Create a new cache.

The default size is DEFAULT_CACHE_SIZE.

Returns:
a new cache or NULL if there was an error
void * mlt_cache_item_data ( mlt_cache_item  item,
int *  size 
)

Get the data pointer from the cache item.

Parameters:
itema cache item
[out]sizethe number of bytes pointed at, if supplied when putting the data into the cache
Returns:
the data pointer
void mlt_cache_purge ( mlt_cache  cache,
void *  object 
)

Remove cache entries for an object.

Parameters:
cachea cache
objectthe object that owns the cached data
void mlt_cache_put ( mlt_cache  cache,
void *  object,
void *  data,
int  size,
mlt_destructor  destructor 
)

Put a chunk of data in the cache.

Parameters:
cachea cache object
objectthe object to which this data belongs
dataan opaque pointer to the data to cache
sizethe size of the data in bytes
destructora pointer to a function that can destroy or release a reference to the data.
void mlt_cache_set_size ( mlt_cache  cache,
int  size 
)

Set the numer of items to cache.

This must be called before using the cache. The size can not be more than MAX_CACHE_SIZE.

Parameters:
cachethe cache to adjust
sizethe new size of the cache
static void ** shuffle_get_hit ( mlt_cache  cache,
void *  object 
) [private]

Shuffle the cache entries between the two arrays and return the cache entry for an object.

Parameters:
cachea cache object
objectthe object that owns the cached data
Returns:
a cache entry if there was a hit or NULL for a miss

Field Documentation

void* mlt_cache_s::A[MAX_CACHE_SIZE]

a list of cache items some of which may no longer be in current but to which there are outstanding references

void* mlt_cache_s::B[MAX_CACHE_SIZE]

the number of items currently in the cache

pointer to the current array of pointers

a list cache items pending release.

A cache item is copied to this list when it is updated but there are outstanding references to the old data object.

pthread_mutex_t mlt_cache_s::mutex

a mutex to prevent multi-threaded race conditions

the maximum number of items permitted in the cache <= MAX_CACHE_SIZE


The documentation for this struct was generated from the following file:
TWiki Appliance - Powered by TurnKey Linux