Commit c57175cf authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Polish the object/storage allocation code a bit, in preparation for

some significant changes to the policy.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@4228 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent c4b1e714
...@@ -335,7 +335,7 @@ cnt_error(struct sess *sp) ...@@ -335,7 +335,7 @@ cnt_error(struct sess *sp)
if (sp->obj == NULL) { if (sp->obj == NULL) {
HSH_Prealloc(sp); HSH_Prealloc(sp);
sp->wrk->cacheable = 0; sp->wrk->cacheable = 0;
sp->obj = STV_NewObject(sp, 0); sp->obj = STV_NewObject(sp, 0, 0);
sp->obj->xid = sp->xid; sp->obj->xid = sp->xid;
sp->obj->entered = sp->t_req; sp->obj->entered = sp->t_req;
} else { } else {
...@@ -526,7 +526,8 @@ cnt_fetch(struct sess *sp) ...@@ -526,7 +526,8 @@ cnt_fetch(struct sess *sp)
* XXX: If we have a Length: header, we should allocate the body * XXX: If we have a Length: header, we should allocate the body
* XXX: also. * XXX: also.
*/ */
sp->obj = STV_NewObject(sp, l);
sp->obj = STV_NewObject(sp, l, sp->wrk->ttl);
if (sp->objhead != NULL) { if (sp->objhead != NULL) {
CHECK_OBJ_NOTNULL(sp->objhead, OBJHEAD_MAGIC); CHECK_OBJ_NOTNULL(sp->objhead, OBJHEAD_MAGIC);
......
...@@ -65,42 +65,60 @@ LRU_Alloc(void) ...@@ -65,42 +65,60 @@ LRU_Alloc(void)
return (l); return (l);
} }
/*********************************************************************
* XXX: trust pointer writes to be atomic
*/
static struct stevedore *
stv_pick_stevedore(void)
{
struct stevedore *stv;
/* pick a stevedore and bump the head along */
stv = VTAILQ_NEXT(stv_next, list);
if (stv == NULL)
stv = VTAILQ_FIRST(&stevedores);
AN(stv);
AN(stv->name);
stv_next = stv;
return (stv);
}
/*********************************************************************/ /*********************************************************************/
struct object * struct object *
STV_NewObject(struct sess *sp, unsigned l) STV_NewObject(struct sess *sp, unsigned l, double ttl)
{ {
struct object *o; struct object *o;
struct storage *st; struct storage *st = NULL;
void *p;
(void)ttl;
if (l == 0) if (l == 0)
l = 1024; l = 1024;
if (params->obj_workspace > 0 && params->obj_workspace > l) if (params->obj_workspace > 0 && params->obj_workspace > l)
l = params->obj_workspace; l = params->obj_workspace;
if (!sp->wrk->cacheable) { if (!sp->wrk->cacheable) {
p = malloc(sizeof *o + l); o = malloc(sizeof *o + l);
XXXAN(p); XXXAN(o);
o = p;
p = o + 1;
memset(o, 0, sizeof *o);
o->magic = OBJECT_MAGIC;
WS_Init(o->ws_o, "obj", p, l);
} else { } else {
st = STV_alloc(sp, sizeof *o + l); st = STV_alloc(sp, sizeof *o + l);
XXXAN(st); XXXAN(st);
assert(st->space > sizeof *o); xxxassert(st->space >= (sizeof *o + l));
o = (void *)st->ptr; /* XXX: align ? */
st->len = sizeof *o;
memset(o, 0, sizeof *o);
o->magic = OBJECT_MAGIC;
o->objstore = st;
WS_Init(o->ws_o, "obj",
st->ptr + st->len, st->space - st->len);
st->len = st->space; st->len = st->space;
l = st->space - sizeof *o;
o = (void *)st->ptr; /* XXX: align ? */
} }
memset(o, 0, sizeof *o);
o->objstore = st;
o->magic = OBJECT_MAGIC;
WS_Init(o->ws_o, "obj", (o + 1), l);
WS_Assert(o->ws_o); WS_Assert(o->ws_o);
http_Setup(o->http, o->ws_o); http_Setup(o->http, o->ws_o);
o->http->magic = HTTP_MAGIC; o->http->magic = HTTP_MAGIC;
o->refcnt = 1; o->refcnt = 1;
...@@ -135,13 +153,7 @@ STV_alloc(struct sess *sp, size_t size) ...@@ -135,13 +153,7 @@ STV_alloc(struct sess *sp, size_t size)
for (;;) { for (;;) {
if (stv == NULL) { if (stv == NULL) {
/* pick a stevedore and bump the head along */ stv = stv_pick_stevedore();
stv = VTAILQ_NEXT(stv_next, list);
if (stv == NULL)
stv = VTAILQ_FIRST(&stevedores);
AN(stv);
AN(stv->name);
stv_next = stv;
fail = 0; fail = 0;
} }
......
...@@ -37,6 +37,7 @@ struct object; ...@@ -37,6 +37,7 @@ struct object;
typedef void storage_init_f(struct stevedore *, int ac, char * const *av); typedef void storage_init_f(struct stevedore *, int ac, char * const *av);
typedef void storage_open_f(const struct stevedore *); typedef void storage_open_f(const struct stevedore *);
typedef struct storage *storage_alloc_f(struct stevedore *, size_t size); typedef struct storage *storage_alloc_f(struct stevedore *, size_t size);
typedef struct object *storage_alloc_obj_f(struct stevedore *, size_t size, double ttl);
typedef void storage_trim_f(struct storage *, size_t size); typedef void storage_trim_f(struct storage *, size_t size);
typedef void storage_free_f(struct storage *); typedef void storage_free_f(struct storage *);
typedef void storage_object_f(const struct sess *sp); typedef void storage_object_f(const struct sess *sp);
...@@ -50,6 +51,7 @@ struct stevedore { ...@@ -50,6 +51,7 @@ struct stevedore {
storage_init_f *init; /* called by mgt process */ storage_init_f *init; /* called by mgt process */
storage_open_f *open; /* called by cache process */ storage_open_f *open; /* called by cache process */
storage_alloc_f *alloc; storage_alloc_f *alloc;
storage_alloc_obj_f *allocobj;
storage_trim_f *trim; storage_trim_f *trim;
storage_free_f *free; storage_free_f *free;
storage_object_f *object; storage_object_f *object;
...@@ -63,7 +65,7 @@ struct stevedore { ...@@ -63,7 +65,7 @@ struct stevedore {
VTAILQ_ENTRY(stevedore) list; VTAILQ_ENTRY(stevedore) list;
}; };
struct object *STV_NewObject(struct sess *sp, unsigned len); struct object *STV_NewObject(struct sess *sp, unsigned len, double ttl);
struct storage *STV_alloc(struct sess *sp, size_t size); struct storage *STV_alloc(struct sess *sp, size_t size);
void STV_trim(struct storage *st, size_t size); void STV_trim(struct storage *st, size_t size);
void STV_free(struct storage *st); void STV_free(struct storage *st);
......
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