r234 - trunk/varnish-cache/bin/varnishd
phk at projects.linpro.no
phk at projects.linpro.no
Mon Jun 26 16:00:41 CEST 2006
Author: phk
Date: 2006-06-26 16:00:40 +0200 (Mon, 26 Jun 2006)
New Revision: 234
Modified:
trunk/varnish-cache/bin/varnishd/cache.h
trunk/varnish-cache/bin/varnishd/cache_hash.c
trunk/varnish-cache/bin/varnishd/hash_simple_list.c
Log:
Move a bit more responsibility into the hash-slinger to get a cleaner
interface.
Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h 2006-06-26 08:58:08 UTC (rev 233)
+++ trunk/varnish-cache/bin/varnishd/cache.h 2006-06-26 14:00:40 UTC (rev 234)
@@ -29,16 +29,14 @@
/* Hashing -----------------------------------------------------------*/
typedef void hash_init_f(void);
-typedef struct objhead *hash_lookup_f(unsigned char *key, struct objhead *nobj);
+typedef struct objhead *hash_lookup_f(const char *key, struct objhead *nobj);
typedef void hash_deref_f(struct objhead *obj);
-typedef void hash_purge_f(struct objhead *obj);
struct hash_slinger {
const char *name;
hash_init_f *init;
hash_lookup_f *lookup;
hash_deref_f *deref;
- hash_purge_f *purge;
};
extern struct hash_slinger hsl_slinger;
@@ -66,14 +64,13 @@
/* -------------------------------------------------------------------*/
struct object {
- unsigned char hash[16];
unsigned refcnt;
- unsigned valid;
- unsigned cacheable;
-
struct objhead *objhead;
pthread_cond_t cv;
+ unsigned valid;
+ unsigned cacheable;
+
unsigned busy;
unsigned len;
time_t ttl;
@@ -85,8 +82,7 @@
};
struct objhead {
- unsigned char hash[16];
- unsigned refcnt;
+ void *hashpriv;
pthread_mutex_t mtx;
TAILQ_HEAD(,object) objects;
Modified: trunk/varnish-cache/bin/varnishd/cache_hash.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_hash.c 2006-06-26 08:58:08 UTC (rev 233)
+++ trunk/varnish-cache/bin/varnishd/cache_hash.c 2006-06-26 14:00:40 UTC (rev 234)
@@ -8,7 +8,6 @@
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
-#include <md5.h>
#include <event.h>
#include <pthread.h>
@@ -23,8 +22,6 @@
{
struct objhead *oh;
struct object *o;
- unsigned char key[16];
- MD5_CTX ctx;
char *b;
assert(hash != NULL);
@@ -43,10 +40,7 @@
}
assert(http_GetURL(h, &b));
- MD5Init(&ctx);
- MD5Update(&ctx, b, strlen(b));
- MD5Final(key, &ctx);
- oh = hash->lookup(key, w->nobjhead);
+ oh = hash->lookup(b, w->nobjhead);
if (oh == w->nobjhead)
w->nobjhead = NULL;
AZ(pthread_mutex_lock(&oh->mtx));
Modified: trunk/varnish-cache/bin/varnishd/hash_simple_list.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/hash_simple_list.c 2006-06-26 08:58:08 UTC (rev 233)
+++ trunk/varnish-cache/bin/varnishd/hash_simple_list.c 2006-06-26 14:00:40 UTC (rev 234)
@@ -1,5 +1,7 @@
/*
* $Id$
+ *
+ * This is the reference hash(/lookup) implementation
*/
#include <assert.h>
@@ -12,14 +14,23 @@
#include <libvarnish.h>
#include <cache.h>
+/*--------------------------------------------------------------------*/
+
struct hsl_entry {
TAILQ_ENTRY(hsl_entry) list;
+ char *key;
struct objhead *obj;
+ unsigned refcnt;
};
static TAILQ_HEAD(, hsl_entry) hsl_head = TAILQ_HEAD_INITIALIZER(hsl_head);
static pthread_mutex_t hsl_mutex;
+/*--------------------------------------------------------------------
+ * The ->init method is called during process start and allows
+ * initialization to happen before the first lookup.
+ */
+
static void
hsl_init(void)
{
@@ -27,23 +38,31 @@
AZ(pthread_mutex_init(&hsl_mutex, NULL));
}
+/*--------------------------------------------------------------------
+ * Lookup and possibly insert element.
+ * If nobj != NULL and the lookup does not find key, nobj is inserted.
+ * If nobj == NULL and the lookup does not find key, NULL is returned.
+ * A reference to the returned object is held.
+ */
+
static struct objhead *
-hsl_lookup(unsigned char *key, struct objhead *nobj)
+hsl_lookup(const char *key, struct objhead *nobj)
{
struct hsl_entry *he, *he2;
int i;
AZ(pthread_mutex_lock(&hsl_mutex));
TAILQ_FOREACH(he, &hsl_head, list) {
- i = memcmp(key, he->obj->hash, sizeof he->obj->hash);
+ i = strcmp(key, he->key);
+ if (i < 0)
+ continue;
if (i == 0) {
- he->obj->refcnt++;
+ he->refcnt++;
nobj = he->obj;
+ nobj->hashpriv = he;
AZ(pthread_mutex_unlock(&hsl_mutex));
return (nobj);
}
- if (i < 0)
- continue;
if (nobj == NULL) {
AZ(pthread_mutex_unlock(&hsl_mutex));
return (NULL);
@@ -53,8 +72,10 @@
he2 = calloc(sizeof *he2, 1);
assert(he2 != NULL);
he2->obj = nobj;
- nobj->refcnt++;
- memcpy(nobj->hash, key, sizeof nobj->hash);
+ he2->refcnt = 1;
+ he2->key = strdup(key);
+ assert(he2->key != NULL);
+ nobj->hashpriv = he2;
if (he != NULL)
TAILQ_INSERT_BEFORE(he, he2, list);
else
@@ -63,37 +84,31 @@
return (nobj);
}
+/*--------------------------------------------------------------------
+ * Dereference and if no references are left, free.
+ */
+
static void
hsl_deref(struct objhead *obj)
{
+ struct hsl_entry *he;
+ assert(obj->hashpriv != NULL);
+ he = obj->hashpriv;
AZ(pthread_mutex_lock(&hsl_mutex));
- obj->refcnt--;
+ if (--he->refcnt == 0) {
+ free(he->key);
+ TAILQ_REMOVE(&hsl_head, he, list);
+ free(he);
+ }
AZ(pthread_mutex_unlock(&hsl_mutex));
}
-static void
-hsl_purge(struct objhead *obj)
-{
- struct hsl_entry *he;
+/*--------------------------------------------------------------------*/
- assert(obj->refcnt > 0);
- AZ(pthread_mutex_lock(&hsl_mutex));
- TAILQ_FOREACH(he, &hsl_head, list) {
- if (he->obj == obj) {
- TAILQ_REMOVE(&hsl_head, he, list);
- AZ(pthread_mutex_unlock(&hsl_mutex));
- free(he);
- return;
- }
- }
- assert(he != NULL);
-}
-
struct hash_slinger hsl_slinger = {
"simple_list",
hsl_init,
hsl_lookup,
hsl_deref,
- hsl_purge
};
More information about the varnish-commit
mailing list