Commit e55460bf authored by Pekka Pessi's avatar Pekka Pessi

sres_cache.c: using updated heap

darcs-hash:20070704094250-65a35-68a88c76d91187e57751d3b11a745bc6246116eb.gz
parent d756db75
...@@ -87,10 +87,9 @@ typedef struct sres_rr_hash_entry_s sres_rr_hash_entry_t; ...@@ -87,10 +87,9 @@ typedef struct sres_rr_hash_entry_s sres_rr_hash_entry_t;
HTABLE_DECLARE_WITH(sres_htable, ht, sres_rr_hash_entry_t, unsigned, size_t); HTABLE_DECLARE_WITH(sres_htable, ht, sres_rr_hash_entry_t, unsigned, size_t);
typedef struct sres_heap sres_heap_t; typedef HEAP_TYPE sres_heap_t;
HEAP_DECLARE(sres_heap, he_, sres_rr_hash_entry_t *);
HEAP_PROTOS(static inline, sres_heap_t, sres_heap_, sres_rr_hash_entry_t *); HEAP_DECLARE(static inline, sres_heap_t, sres_heap_, sres_rr_hash_entry_t *);
struct sres_rr_hash_entry_s { struct sres_rr_hash_entry_s {
sres_record_t *rr; sres_record_t *rr;
...@@ -109,7 +108,7 @@ struct sres_cache ...@@ -109,7 +108,7 @@ struct sres_cache
su_home_t cache_home[1]; su_home_t cache_home[1];
time_t cache_cleaned; time_t cache_cleaned;
sres_htable_t cache_hash[1]; sres_htable_t cache_hash[1];
sres_heap_t cache_heap[1]; sres_heap_t cache_heap;
}; };
#define sr_refcount sr_record->r_refcount #define sr_refcount sr_record->r_refcount
...@@ -136,7 +135,6 @@ static unsigned sres_hash_key(const char *string); ...@@ -136,7 +135,6 @@ static unsigned sres_hash_key(const char *string);
HTABLE_PROTOS_WITH(sres_htable, ht, sres_rr_hash_entry_t, unsigned, size_t); HTABLE_PROTOS_WITH(sres_htable, ht, sres_rr_hash_entry_t, unsigned, size_t);
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
/* Public functions */ /* Public functions */
...@@ -151,8 +149,7 @@ sres_cache_t *sres_cache_new(int n) ...@@ -151,8 +149,7 @@ sres_cache_t *sres_cache_new(int n)
if (cache) { if (cache) {
su_home_threadsafe(cache->cache_home); su_home_threadsafe(cache->cache_home);
if (sres_htable_resize(cache->cache_home, cache->cache_hash, n) < 0 || if (sres_htable_resize(cache->cache_home, cache->cache_hash, n) < 0 ||
sres_heap_resize(cache->cache_home, cache->cache_heap, sres_heap_resize(cache->cache_home, &cache->cache_heap, 0) < 0)
cache->cache_hash->ht_size) < 0)
su_home_unref(cache->cache_home), cache = NULL; su_home_unref(cache->cache_home), cache = NULL;
} }
...@@ -319,7 +316,7 @@ sres_cache_store(sres_cache_t *cache, sres_record_t *rr, time_t now) ...@@ -319,7 +316,7 @@ sres_cache_store(sres_cache_t *cache, sres_record_t *rr, time_t now)
sres_htable_resize(cache->cache_home, cache->cache_hash, 0); sres_htable_resize(cache->cache_home, cache->cache_hash, 0);
if (sres_heap_is_full(cache->cache_heap)) if (sres_heap_is_full(cache->cache_heap))
if (sres_heap_resize(cache->cache_home, cache->cache_heap, 0) < 0) { if (sres_heap_resize(cache->cache_home, &cache->cache_heap, 0) < 0) {
UNLOCK(cache); UNLOCK(cache);
return; return;
} }
...@@ -448,49 +445,40 @@ void sres_cache_clean(sres_cache_t *cache, time_t now) ...@@ -448,49 +445,40 @@ void sres_cache_clean(sres_cache_t *cache, time_t now)
if (now < cache->cache_cleaned + SRES_CACHE_TIMER_INTERVAL) if (now < cache->cache_cleaned + SRES_CACHE_TIMER_INTERVAL)
return; return;
if (!LOCK(cache))
return;
/* Clean cache from old entries */ /* Clean cache from old entries */
cache->cache_cleaned = now;
while (cache->cache_heap->he_used > 0 && for (;;) {
cache->cache_heap->he_heap[0]->rr_expires < now) { if (!LOCK(cache))
return;
cache->cache_cleaned = now;
for (i = 0; i < 100; i++) { for (i = 0; i < 100; i++) {
sres_rr_hash_entry_t *e = cache->cache_heap->he_heap[0]; sres_rr_hash_entry_t *e = sres_heap_get(cache->cache_heap, 1);
if (cache->cache_heap->he_used == 0 || e->rr_expires >= now) { if (e == NULL || e->rr_expires >= now) {
UNLOCK(cache); UNLOCK(cache);
return; return;
} }
sres_heap_remove(cache->cache_heap, 0); sres_heap_remove(cache->cache_heap, 1);
sres_htable_remove(cache->cache_hash, e); sres_htable_remove(cache->cache_hash, e);
_sres_cache_free_one(cache, e->rr); _sres_cache_free_one(cache, e->rr);
su_free(cache->cache_home, e); su_free(cache->cache_home, e);
} }
UNLOCK(cache); UNLOCK(cache);
if (!LOCK(cache))
return;
} }
UNLOCK(cache);
} }
HTABLE_BODIES_WITH(sres_htable, ht, sres_rr_hash_entry_t, SRES_HENTRY_HASH, HTABLE_BODIES_WITH(sres_htable, ht, sres_rr_hash_entry_t, SRES_HENTRY_HASH,
unsigned, size_t); unsigned, size_t);
static inline static inline
int sres_heap_cmp_entry(sres_rr_hash_entry_t const *a, int sres_heap_earlier_entry(sres_rr_hash_entry_t const *a,
sres_rr_hash_entry_t *b) sres_rr_hash_entry_t const *b)
{ {
if (a->rr_expires < b->rr_expires) return a->rr_expires < b->rr_expires;
return -1;
else if (a->rr_expires > b->rr_expires)
return 1;
else
return 0;
} }
static inline static inline
...@@ -502,8 +490,11 @@ void sres_heap_set_entry(sres_rr_hash_entry_t **heap, ...@@ -502,8 +490,11 @@ void sres_heap_set_entry(sres_rr_hash_entry_t **heap,
heap[index] = entry; heap[index] = entry;
} }
HEAP_BODIES(static inline, sres_heap_t, sres_heap_, he_, HEAP_BODIES(static inline,
sres_heap_t,
sres_heap_,
sres_rr_hash_entry_t *, sres_rr_hash_entry_t *,
sres_heap_cmp_entry, sres_heap_earlier_entry,
sres_heap_set_entry, sres_heap_set_entry,
su_realloc); su_realloc,
NULL);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment