[master] 849a3ff Use VSL_cursor->priv members instead of struct overloading, keeping in line with miniobj coding convention.
Martin Blix Grydeland
martin at varnish-cache.org
Tue Jul 2 14:55:57 CEST 2013
commit 849a3ffc6fe6cfedd54014c7f5855bbd0a0390e0
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date: Tue Jul 2 14:04:44 2013 +0200
Use VSL_cursor->priv members instead of struct overloading, keeping in
line with miniobj coding convention.
diff --git a/include/vapi/vsl.h b/include/vapi/vsl.h
index daf5366..d718398 100644
--- a/include/vapi/vsl.h
+++ b/include/vapi/vsl.h
@@ -59,6 +59,10 @@ struct VSLC_ptr {
struct VSL_cursor {
/* The record this cursor points to */
struct VSLC_ptr rec;
+
+ /* Private data */
+ const void *priv_tbl;
+ void *priv_data;
};
enum VSL_transaction_e {
diff --git a/lib/libvarnishapi/vsl_api.h b/lib/libvarnishapi/vsl_api.h
index e5a7506..51e3766 100644
--- a/lib/libvarnishapi/vsl_api.h
+++ b/lib/libvarnishapi/vsl_api.h
@@ -41,13 +41,16 @@ int vsl_diag(struct VSL_data *vsl, const char *fmt, ...)
__printflike(2, 3);
int vsl_skip(struct VSL_cursor *c, ssize_t words);
-typedef void vslc_delete_f(void *);
-typedef int vslc_next_f(void *);
-typedef int vslc_reset_f(void *);
-typedef int vslc_skip_f(void *, ssize_t words);
-typedef int vslc_check_f(const void *, const struct VSLC_ptr *ptr);
+typedef void vslc_delete_f(struct VSL_cursor *);
+typedef int vslc_next_f(struct VSL_cursor *);
+typedef int vslc_reset_f(struct VSL_cursor *);
+typedef int vslc_skip_f(struct VSL_cursor *, ssize_t words);
+typedef int vslc_check_f(const struct VSL_cursor *, const struct VSLC_ptr *);
struct vslc_tbl {
+ unsigned magic;
+#define VSLC_TBL_MAGIC 0x5007C0DE
+
vslc_delete_f *delete;
vslc_next_f *next;
vslc_reset_f *reset;
@@ -55,14 +58,6 @@ struct vslc_tbl {
vslc_check_f *check;
};
-struct vslc {
- struct VSL_cursor c;
- unsigned magic;
-#define VSLC_MAGIC 0x5007C0DE
-
- const struct vslc_tbl *tbl;
-};
-
struct vslf {
unsigned magic;
#define VSLF_MAGIC 0x08650B39
diff --git a/lib/libvarnishapi/vsl_cursor.c b/lib/libvarnishapi/vsl_cursor.c
index 7e72e07..c4f3cf1 100644
--- a/lib/libvarnishapi/vsl_cursor.c
+++ b/lib/libvarnishapi/vsl_cursor.c
@@ -41,16 +41,18 @@
#include "vas.h"
#include "miniobj.h"
+#include "vdef.h"
#include "vapi/vsm.h"
#include "vsm_api.h"
#include "vapi/vsl.h"
#include "vsl_api.h"
struct vslc_vsm {
- struct vslc c;
unsigned magic;
#define VSLC_VSM_MAGIC 0x4D3903A6
+ struct VSL_cursor cursor;
+
struct VSM_data *vsm;
struct VSM_fantom vf;
@@ -61,21 +63,23 @@ struct vslc_vsm {
};
static void
-vslc_vsm_delete(void *cursor)
+vslc_vsm_delete(struct VSL_cursor *cursor)
{
struct vslc_vsm *c;
- CAST_OBJ_NOTNULL(c, (void *)cursor, VSLC_VSM_MAGIC);
+ CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_VSM_MAGIC);
+ assert(&c->cursor == cursor);
FREE_OBJ(c);
}
static int
-vslc_vsm_check(const void *cursor, const struct VSLC_ptr *ptr)
+vslc_vsm_check(const struct VSL_cursor *cursor, const struct VSLC_ptr *ptr)
{
const struct vslc_vsm *c;
unsigned seqdiff, segment, segdiff;
- CAST_OBJ_NOTNULL(c, cursor, VSLC_VSM_MAGIC);
+ CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_VSM_MAGIC);
+ assert(&c->cursor == cursor);
if (ptr->ptr == NULL)
return (0);
@@ -109,13 +113,14 @@ vslc_vsm_check(const void *cursor, const struct VSLC_ptr *ptr)
}
static int
-vslc_vsm_next(void *cursor)
+vslc_vsm_next(struct VSL_cursor *cursor)
{
struct vslc_vsm *c;
int i;
uint32_t t;
- CAST_OBJ_NOTNULL(c, cursor, VSLC_VSM_MAGIC);
+ CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_VSM_MAGIC);
+ assert(&c->cursor == cursor);
CHECK_OBJ_NOTNULL(c->vsm, VSM_MAGIC);
/* Assert pointers */
@@ -123,7 +128,7 @@ vslc_vsm_next(void *cursor)
assert(c->next.ptr >= c->head->log);
assert(c->next.ptr < c->end);
- i = vslc_vsm_check(c, &c->next);
+ i = vslc_vsm_check(&c->cursor, &c->next);
if (i <= 0)
/* Overrun */
return (-3);
@@ -163,19 +168,20 @@ vslc_vsm_next(void *cursor)
if (c->next.ptr == c->head->log)
c->next.priv = c->head->seq;
- c->c.c.rec = c->next;
+ c->cursor.rec = c->next;
c->next.ptr = VSL_NEXT(c->next.ptr);
return (1);
}
}
static int
-vslc_vsm_reset(void *cursor)
+vslc_vsm_reset(struct VSL_cursor *cursor)
{
struct vslc_vsm *c;
unsigned segment;
- CAST_OBJ_NOTNULL(c, cursor, VSLC_VSM_MAGIC);
+ CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_VSM_MAGIC);
+ assert(&c->cursor == cursor);
/*
* Starting (VSL_SEGMENTS - 3) behind varnishd. This way
@@ -189,29 +195,31 @@ vslc_vsm_reset(void *cursor)
assert(c->head->segments[segment] >= 0);
c->next.ptr = c->head->log + c->head->segments[segment];
c->next.priv = c->head->seq;
- c->c.c.rec.ptr = NULL;
+ c->cursor.rec.ptr = NULL;
return (0);
}
static int
-vslc_vsm_skip(void *cursor, ssize_t words)
+vslc_vsm_skip(struct VSL_cursor *cursor, ssize_t words)
{
struct vslc_vsm *c;
- CAST_OBJ_NOTNULL(c, cursor, VSLC_VSM_MAGIC);
+ CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_VSM_MAGIC);
+ assert(&c->cursor == cursor);
if (words < 0)
return (-1);
c->next.ptr += words;
assert(c->next.ptr >= c->head->log);
assert(c->next.ptr < c->end);
- c->c.c.rec.ptr = NULL;
+ c->cursor.rec.ptr = NULL;
return (0);
}
static const struct vslc_tbl vslc_vsm_tbl = {
+ .magic = VSLC_TBL_MAGIC,
.delete = vslc_vsm_delete,
.next = vslc_vsm_next,
.reset = vslc_vsm_reset,
@@ -249,8 +257,8 @@ VSL_CursorVSM(struct VSL_data *vsl, struct VSM_data *vsm, int tail)
vsl_diag(vsl, "Out of memory\n");
return (NULL);
}
- c->c.magic = VSLC_MAGIC;
- c->c.tbl = & vslc_vsm_tbl;
+ c->cursor.priv_tbl = &vslc_vsm_tbl;
+ c->cursor.priv_data = c;
c->vsm = vsm;
c->vf = vf;
@@ -267,17 +275,17 @@ VSL_CursorVSM(struct VSL_data *vsl, struct VSM_data *vsm, int tail)
c->next.ptr = VSL_NEXT(c->next.ptr);
c->next.priv = c->head->seq;
} else
- AZ(vslc_vsm_reset(&c->c));
+ AZ(vslc_vsm_reset(&c->cursor));
- /* XXX: How does 'c' ever get freed ? */
- return (&c->c.c);
+ return (&c->cursor);
}
struct vslc_file {
- struct vslc c;
unsigned magic;
#define VSLC_FILE_MAGIC 0x1D65FFEF
+ struct VSL_cursor cursor;
+
int error;
int fd;
ssize_t buflen;
@@ -285,11 +293,12 @@ struct vslc_file {
};
static void
-vslc_file_delete(void *cursor)
+vslc_file_delete(struct VSL_cursor *cursor)
{
struct vslc_file *c;
- CAST_OBJ_NOTNULL(c, cursor, VSLC_FILE_MAGIC);
+ CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_FILE_MAGIC);
+ assert(&c->cursor == cursor);
if (c->fd > STDIN_FILENO)
(void)close(c->fd);
if (c->buf != NULL)
@@ -313,18 +322,19 @@ vslc_file_readn(int fd, void *buf, size_t n)
}
static int
-vslc_file_next(void *cursor)
+vslc_file_next(struct VSL_cursor *cursor)
{
struct vslc_file *c;
ssize_t i, l;
- CAST_OBJ_NOTNULL(c, cursor, VSLC_FILE_MAGIC);
+ CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_FILE_MAGIC);
+ assert(&c->cursor == cursor);
if (c->error)
return (c->error);
do {
- c->c.c.rec.ptr = NULL;
+ c->cursor.rec.ptr = NULL;
assert(c->buflen >= VSL_BYTES(2));
i = vslc_file_readn(c->fd, c->buf, VSL_BYTES(2));
if (i < 0)
@@ -347,13 +357,13 @@ vslc_file_next(void *cursor)
return (-1); /* EOF */
assert(i == l - VSL_BYTES(2));
}
- c->c.c.rec.ptr = c->buf;
- } while (VSL_TAG(c->c.c.rec.ptr) == SLT__Batch);
+ c->cursor.rec.ptr = c->buf;
+ } while (VSL_TAG(c->cursor.rec.ptr) == SLT__Batch);
return (1);
}
static int
-vslc_file_reset(void *cursor)
+vslc_file_reset(struct VSL_cursor *cursor)
{
(void)cursor;
/* XXX: Implement me */
@@ -361,6 +371,7 @@ vslc_file_reset(void *cursor)
}
static const struct vslc_tbl vslc_file_tbl = {
+ .magic = VSLC_TBL_MAGIC,
.delete = vslc_file_delete,
.next = vslc_file_next,
.reset = vslc_file_reset,
@@ -410,67 +421,67 @@ VSL_CursorFile(struct VSL_data *vsl, const char *name)
vsl_diag(vsl, "Out of memory\n");
return (NULL);
}
- c->c.magic = VSLC_MAGIC;
- c->c.tbl = &vslc_file_tbl;
+ c->cursor.priv_tbl = &vslc_file_tbl;
+ c->cursor.priv_data = c;
c->fd = fd;
c->buflen = BUFSIZ;
c->buf = malloc(c->buflen);
AN(c->buf);
- return (&c->c.c);
+ return (&c->cursor);
}
void
VSL_DeleteCursor(struct VSL_cursor *cursor)
{
- struct vslc *c;
+ const struct vslc_tbl *tbl;
- CAST_OBJ_NOTNULL(c, (void *)cursor, VSLC_MAGIC);
- if (c->tbl->delete == NULL)
+ CAST_OBJ_NOTNULL(tbl, cursor->priv_tbl, VSLC_TBL_MAGIC);
+ if (tbl->delete == NULL)
return;
- (c->tbl->delete)(c);
+ (tbl->delete)(cursor);
}
int
VSL_ResetCursor(struct VSL_cursor *cursor)
{
- struct vslc *c;
+ const struct vslc_tbl *tbl;
- CAST_OBJ_NOTNULL(c, (void *)cursor, VSLC_MAGIC);
- if (c->tbl->reset == NULL)
+ CAST_OBJ_NOTNULL(tbl, cursor->priv_tbl, VSLC_TBL_MAGIC);
+ if (tbl->reset == NULL)
return (-1);
- return ((c->tbl->reset)(c));
+ return ((tbl->reset)(cursor));
}
int
VSL_Next(struct VSL_cursor *cursor)
{
- struct vslc *c;
+ const struct vslc_tbl *tbl;
- CAST_OBJ_NOTNULL(c, (void *)cursor, VSLC_MAGIC);
- AN(c->tbl->next);
- return ((c->tbl->next)(c));
+ CAST_OBJ_NOTNULL(tbl, cursor->priv_tbl, VSLC_TBL_MAGIC);
+ AN(tbl->next);
+ return ((tbl->next)(cursor));
}
int
vsl_skip(struct VSL_cursor *cursor, ssize_t words)
{
- struct vslc *c;
+ const struct vslc_tbl *tbl;
- CAST_OBJ_NOTNULL(c, (void *)cursor, VSLC_MAGIC);
- if (c->tbl->skip == NULL)
+ CAST_OBJ_NOTNULL(tbl, cursor->priv_tbl, VSLC_TBL_MAGIC);
+ if (tbl->skip == NULL)
return (-1);
- return ((c->tbl->skip)(c, words));
+ return ((tbl->skip)(cursor, words));
}
int
VSL_Check(const struct VSL_cursor *cursor, const struct VSLC_ptr *ptr)
{
- const struct vslc *c;
+ const struct vslc_tbl *tbl;
- CAST_OBJ_NOTNULL(c, (const void *)cursor, VSLC_MAGIC);
- if (c->tbl->check == NULL)
+ CAST_OBJ_NOTNULL(tbl, cursor->priv_tbl, VSLC_TBL_MAGIC);
+ if (tbl->check == NULL)
return (-1);
- return ((c->tbl->check)(c, ptr));
+ return ((tbl->check)(cursor, ptr));
}
diff --git a/lib/libvarnishapi/vsl_dispatch.c b/lib/libvarnishapi/vsl_dispatch.c
index 592e4b8..bb2da3f 100644
--- a/lib/libvarnishapi/vsl_dispatch.c
+++ b/lib/libvarnishapi/vsl_dispatch.c
@@ -51,10 +51,11 @@
struct vtx;
struct vslc_raw {
- struct vslc c;
unsigned magic;
#define VSLC_RAW_MAGIC 0x247EBD44
+ struct VSL_cursor cursor;
+
const uint32_t *start;
ssize_t len;
const uint32_t *next;
@@ -67,10 +68,11 @@ struct vtx_chunk {
};
struct vslc_vtx {
- struct vslc c;
unsigned magic;
#define VSLC_VTX_MAGIC 0x74C6523F
+ struct VSL_cursor cursor;
+
struct vtx *vtx;
unsigned chunk; /* Current chunk */
@@ -155,16 +157,17 @@ VRB_PROTOTYPE_STATIC(vtx_tree, vtx_key, entry, vtx_keycmp)
VRB_GENERATE_STATIC(vtx_tree, vtx_key, entry, vtx_keycmp)
static int
-vslc_raw_next(void *cursor)
+vslc_raw_next(struct VSL_cursor *cursor)
{
struct vslc_raw *c;
- CAST_OBJ_NOTNULL(c, cursor, VSLC_RAW_MAGIC);
+ CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_RAW_MAGIC);
+ assert(&c->cursor == cursor);
assert(c->next >= c->start);
assert(c->next <= c->start + c->len);
if (c->next < c->start + c->len) {
- c->c.c.rec.ptr = c->next;
+ c->cursor.rec.ptr = c->next;
c->next = VSL_NEXT(c->next);
return (1);
}
@@ -172,21 +175,23 @@ vslc_raw_next(void *cursor)
}
static int
-vslc_raw_reset(void *cursor)
+vslc_raw_reset(struct VSL_cursor *cursor)
{
struct vslc_raw *c;
- CAST_OBJ_NOTNULL(c, cursor, VSLC_RAW_MAGIC);
+ CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_RAW_MAGIC);
+ assert(&c->cursor == cursor);
assert(c->next >= c->start);
assert(c->next <= c->start + c->len);
c->next = c->start;
- c->c.c.rec.ptr = NULL;
+ c->cursor.rec.ptr = NULL;
return (0);
}
static const struct vslc_tbl vslc_raw_tbl = {
+ .magic = VSLC_TBL_MAGIC,
.delete = NULL,
.next = vslc_raw_next,
.reset = vslc_raw_reset,
@@ -195,12 +200,14 @@ static const struct vslc_tbl vslc_raw_tbl = {
};
static int
-vslc_vtx_next(void *cursor)
+vslc_vtx_next(struct VSL_cursor *cursor)
{
struct vslc_vtx *c;
struct vtx_chunk *chunk;
- CAST_OBJ_NOTNULL(c, cursor, VSLC_VTX_MAGIC);
+ CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_VTX_MAGIC);
+ assert(&c->cursor == cursor);
+ CHECK_OBJ_NOTNULL(c->vtx, VTX_MAGIC);
assert (c->offset <= c->vtx->len);
if (c->offset == c->vtx->len)
@@ -210,8 +217,8 @@ vslc_vtx_next(void *cursor)
/* Buffer */
AN(c->vtx->buf);
assert(c->offset < c->vtx->bufsize);
- c->c.c.rec.ptr = c->vtx->buf + c->offset;
- c->offset += VSL_NEXT(c->c.c.rec.ptr) - c->c.c.rec.ptr;
+ c->cursor.rec.ptr = c->vtx->buf + c->offset;
+ c->offset += VSL_NEXT(c->cursor.rec.ptr) - c->cursor.rec.ptr;
return (1);
}
@@ -225,26 +232,28 @@ vslc_vtx_next(void *cursor)
chunk = &c->vtx->chunk[c->chunk];
}
AN(chunk->start.ptr);
- c->c.c.rec.ptr = chunk->start.ptr + c->offset - chunk->offset;
- c->offset += VSL_NEXT(c->c.c.rec.ptr) - c->c.c.rec.ptr;
+ c->cursor.rec.ptr = chunk->start.ptr + c->offset - chunk->offset;
+ c->offset += VSL_NEXT(c->cursor.rec.ptr) - c->cursor.rec.ptr;
return (1);
}
static int
-vslc_vtx_reset(void *cursor)
+vslc_vtx_reset(struct VSL_cursor *cursor)
{
struct vslc_vtx *c;
- CAST_OBJ_NOTNULL(c, cursor, VSLC_VTX_MAGIC);
+ CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_VTX_MAGIC);
+ assert(&c->cursor == cursor);
c->chunk = 0;
c->offset = 0;
- c->c.c.rec.ptr = NULL;
+ c->cursor.rec.ptr = NULL;
return (0);
}
static const struct vslc_tbl vslc_vtx_tbl = {
+ .magic = VSLC_TBL_MAGIC,
.delete = NULL,
.next = vslc_vtx_next,
.reset = vslc_vtx_reset,
@@ -266,10 +275,10 @@ vtx_new(struct VSLQ *vslq)
} else {
ALLOC_OBJ(vtx, VTX_MAGIC);
AN(vtx);
- vtx->c.c.magic = VSLC_MAGIC;
- vtx->c.c.tbl = &vslc_vtx_tbl;
vtx->c.magic = VSLC_VTX_MAGIC;
vtx->c.vtx = vtx;
+ vtx->c.cursor.priv_tbl = &vslc_vtx_tbl;
+ vtx->c.cursor.priv_data = &vtx->c;
}
vtx->key.vxid = 0;
@@ -281,7 +290,7 @@ vtx_new(struct VSLQ *vslq)
vtx->n_child = 0;
vtx->n_childready = 0;
vtx->n_descend = 0;
- (void)vslc_vtx_reset(&vtx->c);
+ (void)vslc_vtx_reset(&vtx->c.cursor);
vtx->len = 0;
memset(vtx->chunk, 0, sizeof vtx->chunk);
vtx->n_chunk = 0;
@@ -625,8 +634,8 @@ vtx_scan(struct VSLQ *vslq, struct vtx *vtx)
enum VSL_tag_e tag;
struct vtx *ret = NULL;
- while (vslc_vtx_next(&vtx->c) == 1) {
- ptr = vtx->c.c.c.rec.ptr;
+ while (vslc_vtx_next(&vtx->c.cursor) == 1) {
+ ptr = vtx->c.cursor.rec.ptr;
tag = VSL_TAG(ptr);
if (tag == SLT__Batch)
@@ -691,9 +700,9 @@ vslq_callback(const struct VSLQ *vslq, struct vtx *vtx, VSLQ_dispatch_f *func,
void *priv)
{
unsigned n = vtx->n_descend + 1;
+ struct vtx *vtxs[n];
struct VSL_transaction trans[n];
struct VSL_transaction *ptrans[n + 1];
- struct vslc_vtx *c;
unsigned i, j;
AN(vslq);
@@ -709,22 +718,23 @@ vslq_callback(const struct VSLQ *vslq, struct vtx *vtx, VSLQ_dispatch_f *func,
return (0);
/* Build transaction array */
- (void)vslc_vtx_reset(&vtx->c);
+ (void)vslc_vtx_reset(&vtx->c.cursor);
+ vtxs[0] = vtx;
trans[0].level = 1;
trans[0].vxid = vtx->key.vxid;
trans[0].type = vtx->type;
- trans[0].c = &vtx->c.c.c;
+ trans[0].c = &vtx->c.cursor;
i = 1;
j = 0;
while (j < i) {
- CAST_OBJ_NOTNULL(c, (void *)trans[j].c, VSLC_VTX_MAGIC);
- VTAILQ_FOREACH(vtx, &c->vtx->child, list_child) {
+ VTAILQ_FOREACH(vtx, &vtxs[j]->child, list_child) {
assert(i < n);
- (void)vslc_vtx_reset(&vtx->c);
+ (void)vslc_vtx_reset(&vtx->c.cursor);
+ vtxs[i] = vtx;
trans[i].level = trans[j].level + 1;
trans[i].vxid = vtx->key.vxid;
trans[i].type = vtx->type;
- trans[i].c = &vtx->c.c.c;
+ trans[i].c = &vtx->c.cursor;
i++;
}
j++;
@@ -856,12 +866,12 @@ vslq_raw(const struct VSLQ *vslq, VSLQ_dispatch_f *func, void *priv)
c = vslq->c;
memset(&rawc, 0, sizeof rawc);
- rawc.c.magic = VSLC_MAGIC;
- rawc.c.tbl = &vslc_raw_tbl;
rawc.magic = VSLC_RAW_MAGIC;
+ rawc.cursor.priv_tbl = &vslc_raw_tbl;
+ rawc.cursor.priv_data = &rawc;
trans.level = 0;
trans.type = VSL_t_raw;
- trans.c = &rawc.c.c;
+ trans.c = &rawc.cursor;
ptrans[0] = &trans;
ptrans[1] = NULL;
@@ -875,7 +885,7 @@ vslq_raw(const struct VSLQ *vslq, VSLQ_dispatch_f *func, void *priv)
rawc.start = c->rec.ptr;
rawc.len = VSL_NEXT(c->rec.ptr) - c->rec.ptr;
rawc.next = rawc.start;
- rawc.c.c.rec.ptr = NULL;
+ rawc.cursor.rec.ptr = NULL;
trans.vxid = VSL_ID(c->rec.ptr);
/* Query check goes here */
More information about the varnish-commit
mailing list