#include <assert.h>
#include <apr_thread_mutex.h>
#include "svn_pools.h"
#include "svn_private_config.h"
#include "cache.h"
typedef struct {
apr_hash_t *hash;
apr_ssize_t klen;
svn_cache__dup_func_t dup_func;
apr_int64_t unallocated_pages;
apr_int64_t items_per_page;
struct cache_page *sentinel;
struct cache_page *partial_page;
apr_int64_t partial_page_number_filled;
apr_pool_t *cache_pool;
#if APR_HAS_THREADS
apr_thread_mutex_t *mutex;
#endif
} inprocess_cache_t;
struct cache_page {
struct cache_page *prev;
struct cache_page *next;
apr_pool_t *page_pool;
struct cache_entry *first_entry;
};
struct cache_entry {
const void *key;
void *value;
struct cache_page *page;
struct cache_entry *next_entry;
};
static void
remove_page_from_list(struct cache_page *page)
{
page->prev->next = page->next;
page->next->prev = page->prev;
}
static void
insert_page(inprocess_cache_t *cache,
struct cache_page *page)
{
struct cache_page *pred = cache->sentinel;
page->prev = pred;
page->next = pred->next;
page->prev->next = page;
page->next->prev = page;
}
static void
move_page_to_front(inprocess_cache_t *cache,
struct cache_page *page)
{
assert(page != cache->sentinel);
if (! page->next)
return;
remove_page_from_list(page);
insert_page(cache, page);
}
static svn_error_t *
duplicate_value(void **value_p,
inprocess_cache_t *cache,
void *value,
apr_pool_t *pool)
{
if (value)
SVN_ERR((cache->dup_func)(value_p, value, pool));
else
*value_p = NULL;
return SVN_NO_ERROR;
}
static const void *
duplicate_key(inprocess_cache_t *cache,
const void *key,
apr_pool_t *pool)
{
if (cache->klen == APR_HASH_KEY_STRING)
return apr_pstrdup(pool, key);
else
return apr_pmemdup(pool, key, cache->klen);
}
static svn_error_t *
lock_cache(inprocess_cache_t *cache)
{
#if APR_HAS_THREADS
apr_status_t status;
if (! cache->mutex)
return SVN_NO_ERROR;
status = apr_thread_mutex_lock(cache->mutex);
if (status)
return svn_error_wrap_apr(status, _("Can't lock cache mutex"));
#endif
return SVN_NO_ERROR;
}
static svn_error_t *
unlock_cache(inprocess_cache_t *cache,
svn_error_t *err)
{
#if APR_HAS_THREADS
apr_status_t status;
if (! cache->mutex)
return err;
status = apr_thread_mutex_unlock(cache->mutex);
if (status && !err)
return svn_error_wrap_apr(status, _("Can't unlock cache mutex"));
#endif
return err;
}
static svn_error_t *
inprocess_cache_get(void **value_p,
svn_boolean_t *found,
void *cache_void,
const void *key,
apr_pool_t *pool)
{
inprocess_cache_t *cache = cache_void;
struct cache_entry *entry;
svn_error_t *err;
SVN_ERR(lock_cache(cache));
entry = apr_hash_get(cache->hash, key, cache->klen);
if (! entry)
{
*found = FALSE;
return unlock_cache(cache, SVN_NO_ERROR);
}
move_page_to_front(cache, entry->page);
*found = TRUE;
err = duplicate_value(value_p, cache, entry->value, pool);
return unlock_cache(cache, err);
}
static void
erase_page(inprocess_cache_t *cache,
struct cache_page *page)
{
struct cache_entry *e;
remove_page_from_list(page);
for (e = page->first_entry;
e;
e = e->next_entry)
{
apr_hash_set(cache->hash, e->key, cache->klen, NULL);
}
svn_pool_clear(page->page_pool);
page->first_entry = NULL;
page->prev = NULL;
page->next = NULL;
cache->partial_page = page;
cache->partial_page_number_filled = 0;
}
static svn_error_t *
inprocess_cache_set(void *cache_void,
const void *key,
void *value,
apr_pool_t *pool)
{
inprocess_cache_t *cache = cache_void;
struct cache_entry *existing_entry;
svn_error_t *err = SVN_NO_ERROR;
SVN_ERR(lock_cache(cache));
existing_entry = apr_hash_get(cache->hash, key, cache->klen);
if (existing_entry && cache->items_per_page == 1)
{
struct cache_page *page = existing_entry->page;
SVN_ERR_ASSERT(page->next != NULL);
SVN_ERR_ASSERT(cache->partial_page == NULL);
erase_page(cache, page);
existing_entry = NULL;
}
if (existing_entry)
{
struct cache_page *page = existing_entry->page;
move_page_to_front(cache, page);
err = duplicate_value(&(existing_entry->value), cache,
value, page->page_pool);
goto cleanup;
}
if (cache->partial_page == NULL && cache->unallocated_pages > 0)
{
cache->partial_page = apr_pcalloc(cache->cache_pool,
sizeof(*(cache->partial_page)));
cache->partial_page->page_pool = svn_pool_create(cache->cache_pool);
cache->partial_page_number_filled = 0;
(cache->unallocated_pages)--;
}
if (cache->partial_page == NULL)
{
struct cache_page *oldest_page = cache->sentinel->prev;
SVN_ERR_ASSERT(oldest_page != cache->sentinel);
erase_page(cache, oldest_page);
}
SVN_ERR_ASSERT(cache->partial_page != NULL);
{
struct cache_page *page = cache->partial_page;
struct cache_entry *new_entry = apr_pcalloc(page->page_pool,
sizeof(*new_entry));
new_entry->key = duplicate_key(cache, key, page->page_pool);
err = duplicate_value(&(new_entry->value), cache, value,
page->page_pool);
if (err)
goto cleanup;
new_entry->page = page;
new_entry->next_entry = page->first_entry;
page->first_entry = new_entry;
apr_hash_set(cache->hash, new_entry->key, cache->klen, new_entry);
(cache->partial_page_number_filled)++;
if (cache->partial_page_number_filled >= cache->items_per_page)
{
insert_page(cache, page);
cache->partial_page = NULL;
}
}
cleanup:
return unlock_cache(cache, err);
}
struct cache_iter_baton {
svn_iter_apr_hash_cb_t user_cb;
void *user_baton;
};
static svn_error_t *
iter_cb(void *baton,
const void *key,
apr_ssize_t klen,
void *val,
apr_pool_t *pool)
{
struct cache_iter_baton *b = baton;
struct cache_entry *entry = val;
return (b->user_cb)(b->user_baton, key, klen, entry->value, pool);
}
static svn_error_t *
inprocess_cache_iter(svn_boolean_t *completed,
void *cache_void,
svn_iter_apr_hash_cb_t user_cb,
void *user_baton,
apr_pool_t *pool)
{
inprocess_cache_t *cache = cache_void;
struct cache_iter_baton b;
b.user_cb = user_cb;
b.user_baton = user_baton;
SVN_ERR(lock_cache(cache));
return unlock_cache(cache,
svn_iter_apr_hash(completed, cache->hash, iter_cb, &b,
pool));
}
static svn_cache__vtable_t inprocess_cache_vtable = {
inprocess_cache_get,
inprocess_cache_set,
inprocess_cache_iter
};
svn_error_t *
svn_cache__create_inprocess(svn_cache__t **cache_p,
svn_cache__dup_func_t dup_func,
apr_ssize_t klen,
apr_int64_t pages,
apr_int64_t items_per_page,
svn_boolean_t thread_safe,
apr_pool_t *pool)
{
svn_cache__t *wrapper = apr_pcalloc(pool, sizeof(*wrapper));
inprocess_cache_t *cache = apr_pcalloc(pool, sizeof(*cache));
cache->hash = apr_hash_make(pool);
cache->klen = klen;
cache->dup_func = dup_func;
SVN_ERR_ASSERT(pages >= 1);
cache->unallocated_pages = pages;
SVN_ERR_ASSERT(items_per_page >= 1);
cache->items_per_page = items_per_page;
cache->sentinel = apr_pcalloc(pool, sizeof(*(cache->sentinel)));
cache->sentinel->prev = cache->sentinel;
cache->sentinel->next = cache->sentinel;
#if APR_HAS_THREADS
if (thread_safe)
{
apr_status_t status = apr_thread_mutex_create(&(cache->mutex),
APR_THREAD_MUTEX_DEFAULT,
pool);
if (status)
return svn_error_wrap_apr(status,
_("Can't create cache mutex"));
}
#endif
cache->cache_pool = pool;
wrapper->vtable = &inprocess_cache_vtable;
wrapper->cache_internal = cache;
*cache_p = wrapper;
return SVN_NO_ERROR;
}