From 07a8c468e699f459d6dc3cd60c5ab0e0c052f7dc Mon Sep 17 00:00:00 2001 From: phk Date: Thu, 26 Feb 2009 12:22:50 +0000 Subject: [PATCH] Add a alloc/free function. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@3829 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/stevedore.c | 3 + .../bin/varnishd/storage_persistent.c | 100 +++++++++++++++--- 2 files changed, 87 insertions(+), 16 deletions(-) diff --git a/varnish-cache/bin/varnishd/stevedore.c b/varnish-cache/bin/varnishd/stevedore.c index f1432ddc..fc55295b 100644 --- a/varnish-cache/bin/varnishd/stevedore.c +++ b/varnish-cache/bin/varnishd/stevedore.c @@ -55,6 +55,7 @@ STV_alloc(struct sess *sp, size_t size) if (stv == NULL) stv = VTAILQ_FIRST(&stevedores); AN(stv); + AN(stv->name); /* XXX: only safe as long as pointer writes are atomic */ stv_next = stv; @@ -103,6 +104,8 @@ STV_add(const struct stevedore *stv2, int ac, char * const *av) AN(stv); *stv = *stv2; + AN(stv->name); + AN(stv->alloc); if (stv->init != NULL) stv->init(stv, ac, av); diff --git a/varnish-cache/bin/varnishd/storage_persistent.c b/varnish-cache/bin/varnishd/storage_persistent.c index 1614abeb..0cf52392 100644 --- a/varnish-cache/bin/varnishd/storage_persistent.c +++ b/varnish-cache/bin/varnishd/storage_persistent.c @@ -64,6 +64,7 @@ struct smp_seg { VTAILQ_ENTRY(smp_seg) list; uint64_t offset; uint64_t length; + struct smp_segment segment; }; struct smp_sc { @@ -81,6 +82,8 @@ struct smp_sc { struct smp_ident *ident; VTAILQ_HEAD(, smp_seg) segments; + struct smp_seg *cur_seg; + uint64_t next_addr; }; /*-------------------------------------------------------------------- @@ -311,7 +314,7 @@ smp_init(struct stevedore *parent, int ac, char * const *av) "sizeof(%s) = %zu = 0x%zx\n", #foo, sizeof(foo), sizeof(foo)); SIZOF(struct smp_ident); SIZOF(struct smp_sign); - SIZOF(struct smp_segment); + SIZOF(struct smp_segptr); SIZOF(struct smp_object); #undef SIZOF @@ -375,7 +378,7 @@ smp_init(struct stevedore *parent, int ac, char * const *av) static void smp_save_seg(struct smp_sc *sc, uint64_t adr, const char *id) { - struct smp_segment *ss; + struct smp_segptr *ss; struct smp_seg *sg; void *ptr; uint64_t length; @@ -411,7 +414,7 @@ smp_open_segs(struct smp_sc *sc, int stuff, const char *id) { void *ptr; uint64_t length; - struct smp_segment *ss; + struct smp_segptr *ss; struct smp_seg *sg; if (smp_open_sign(sc, sc->ident->stuff[stuff], &ptr, &length, id)) @@ -426,18 +429,44 @@ smp_open_segs(struct smp_sc *sc, int stuff, const char *id) VTAILQ_INSERT_TAIL(&sc->segments, sg, list); fprintf(stderr, "RD SEG %jx %jx\n", sg->offset, sg->length); } - if (VTAILQ_EMPTY(&sc->segments)) { - ALLOC_OBJ(sg, SMP_SEG_MAGIC); - AN(sg); - sg->offset = sc->ident->stuff[SMP_SPC_STUFF]; - sg->length = sc->ident->stuff[SMP_END_STUFF] - sg->offset; - VTAILQ_INSERT_TAIL(&sc->segments, sg, list); + return (0); +} + +/*-------------------------------------------------------------------- + * Create a new segment + */ + +static void +smp_new_seg(struct smp_sc *sc) +{ + struct smp_seg *sg; + void *ptr; + uint64_t length; + + ALLOC_OBJ(sg, SMP_SEG_MAGIC); + AN(sg); + /* XXX: find where */ + sg->offset = sc->ident->stuff[SMP_SPC_STUFF]; + sg->length = sc->ident->stuff[SMP_END_STUFF] - sg->offset; + VTAILQ_INSERT_TAIL(&sc->segments, sg, list); fprintf(stderr, "MK SEG %jx %jx\n", sg->offset, sg->length); - } - /* XXX: sanity check pointer+length for validity and non-overlap */ + /* Neuter the new segment in case there is an old one there */ + (void)smp_open_sign(sc, sg->offset, &ptr, &length, "SEGMENT"); + memcpy(ptr, &sg->segment, sizeof sg->segment); + smp_create_sign(sc, sg->offset, sizeof sg->segment, "SEGMENT"); + smp_sync_sign(sc, sg->offset, sizeof sg->segment); - return (0); + /* Then add it to the segment list. */ + smp_save_segs(sc); + + /* Set up our allocation point */ + sc->cur_seg = sg; + sc->next_addr = sg->offset + + sizeof (struct smp_sign) + + sizeof (struct smp_segment) + + SHA256_LEN; + memcpy(sc->ptr + sc->next_addr, "HERE", 4); } /*-------------------------------------------------------------------- @@ -463,9 +492,49 @@ smp_open(const struct stevedore *st) */ if (smp_open_segs(sc, SMP_SEG1_STUFF, "SEG 1")) AZ(smp_open_segs(sc, SMP_SEG2_STUFF, "SEG 2")); - smp_save_segs(sc); + + smp_new_seg(sc); } +/*-------------------------------------------------------------------- + * Allocate a bite + */ + +static struct storage * +smp_alloc(struct stevedore *st, size_t size) +{ + struct smp_sc *sc; + struct storage *ss; + + CAST_OBJ_NOTNULL(sc, st->priv, SMP_SC_MAGIC); + + /* XXX: size fit check */ + AN(sc->next_addr); + + /* Grab and fill a storage structure */ + ss = (void *)(sc->ptr + sc->next_addr); + memset(ss, 0, sizeof *ss); + ss->magic = STORAGE_MAGIC; + ss->space = size; + ss->ptr = (void *)(ss + 1); + ss->priv = sc->cur_seg; /* XXX ? */ + ss->stevedore = st; + ss->fd = sc->fd; + ss->where = sc->next_addr + sizeof *ss; + + sc->next_addr += size + sizeof *ss; + return (ss); +} + +static void +smp_free(struct storage *st) +{ + + /* XXX */ + (void)st; +} + + /*--------------------------------------------------------------------*/ struct stevedore smp_stevedore = { @@ -473,7 +542,6 @@ struct stevedore smp_stevedore = { .name = "persistent", .init = smp_init, .open = smp_open, - // .alloc = smf_alloc, - // .trim = smf_trim, - // .free = smf_free, + .alloc = smp_alloc, + .free = smp_free, }; -- 2.39.5