Pass capn_ptr by value, better handling of tags
This commit is contained in:
parent
3a235fe8c6
commit
b8da11676a
10 changed files with 1236 additions and 438 deletions
452
capn.c
452
capn.c
|
|
@ -8,6 +8,7 @@
|
|||
#define STRUCT_PTR 0
|
||||
#define LIST_PTR 1
|
||||
#define FAR_PTR 2
|
||||
#define DOUBLE_PTR 6
|
||||
|
||||
#define VOID_LIST 0
|
||||
#define BIT_1_LIST 1
|
||||
|
|
@ -228,6 +229,42 @@ static struct capn_segment *lookup_segment(struct capn* c, struct capn_segment *
|
|||
return s;
|
||||
}
|
||||
|
||||
static uint64_t lookup_double(struct capn_segment **s, char **d, uint64_t val) {
|
||||
uint64_t far, tag;
|
||||
uint32_t off = (U32(val) >> 3) * 8;
|
||||
char *p;
|
||||
|
||||
if ((*s = lookup_segment((*s)->capn, *s, U32(val >> 32))) == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
p = (*s)->data + off;
|
||||
if (off + 16 > (*s)->len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
far = capn_flip64(*(uint64_t*) p);
|
||||
tag = capn_flip64(*(uint64_t*) (p+8));
|
||||
|
||||
/* the far tag should not be another double, and the tag
|
||||
* should be struct/list and have no offset */
|
||||
if ((far&7) != FAR_PTR || U32(tag) > LIST_PTR) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((*s = lookup_segment((*s)->capn, *s, U32(far >> 32))) == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* -8 because far pointers reference from the start of
|
||||
* the segment, but offsets reference the end of the
|
||||
* pointer data. Here *d points to where an equivalent
|
||||
* ptr would be.
|
||||
*/
|
||||
*d = (*s)->data - 8;
|
||||
return U64(U32(far) >> 3 << 2) | tag;
|
||||
}
|
||||
|
||||
static uint64_t lookup_far(struct capn_segment **s, char **d, uint64_t val) {
|
||||
uint32_t off = (U32(val) >> 3) * 8;
|
||||
|
||||
|
|
@ -235,50 +272,25 @@ static uint64_t lookup_far(struct capn_segment **s, char **d, uint64_t val) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
if (val & 4) {
|
||||
/* Double far pointer */
|
||||
uint64_t far, tag;
|
||||
char *p = (*s)->data + off;
|
||||
if (off + 16 > (*s)->len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
far = capn_flip64(*(uint64_t*) p);
|
||||
tag = capn_flip64(*(uint64_t*) (p+8));
|
||||
|
||||
/* the far tag should not be another double, and the tag
|
||||
* should be struct/list and have no offset */
|
||||
if ((far&7) != FAR_PTR || U32(tag) > LIST_PTR) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((*s = lookup_segment((*s)->capn, *s, U32(far >> 32))) == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* -8 because far pointers reference from the start of
|
||||
* the segment, but offsets reference the end of the
|
||||
* pointer data. Here *d points to where an equivalent
|
||||
* ptr would be.
|
||||
*/
|
||||
*d = (*s)->data - 8;
|
||||
return U64(U32(far) >> 3 << 2) | tag;
|
||||
} else {
|
||||
if (off + 8 > (*s)->len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*d = (*s)->data + off;
|
||||
return capn_flip64(*(uint64_t*) *d);
|
||||
if (off + 8 > (*s)->len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*d = (*s)->data + off;
|
||||
return capn_flip64(*(uint64_t*)*d);
|
||||
}
|
||||
|
||||
static char *struct_ptr(struct capn_segment *s, char *d, int minsz) {
|
||||
uint64_t val = capn_flip64(*(uint64_t*)d);
|
||||
uint16_t datasz;
|
||||
|
||||
if ((val&3) == FAR_PTR) {
|
||||
switch (val&7) {
|
||||
case FAR_PTR:
|
||||
val = lookup_far(&s, &d, val);
|
||||
break;
|
||||
case DOUBLE_PTR:
|
||||
val = lookup_double(&s, &d, val);
|
||||
break;
|
||||
}
|
||||
|
||||
datasz = U16(val >> 32);
|
||||
|
|
@ -291,15 +303,21 @@ static char *struct_ptr(struct capn_segment *s, char *d, int minsz) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static struct capn_ptr read_ptr(struct capn_segment *s, char *d) {
|
||||
char *e;
|
||||
struct capn_ptr ret;
|
||||
static capn_ptr read_ptr(struct capn_segment *s, char *d) {
|
||||
capn_ptr ret = {CAPN_NULL};
|
||||
uint64_t val;
|
||||
char *e;
|
||||
|
||||
val = capn_flip64(*(uint64_t*) d);
|
||||
|
||||
if ((val&3) == FAR_PTR) {
|
||||
switch (val&7) {
|
||||
case FAR_PTR:
|
||||
val = lookup_far(&s, &d, val);
|
||||
ret.has_ptr_tag = (U32(val) >> 2) == 0;
|
||||
break;
|
||||
case DOUBLE_PTR:
|
||||
val = lookup_double(&s, &d, val);
|
||||
break;
|
||||
}
|
||||
|
||||
d += (I32(U32(val)) >> 2) * 8 + 8;
|
||||
|
|
@ -310,15 +328,12 @@ static struct capn_ptr read_ptr(struct capn_segment *s, char *d) {
|
|||
|
||||
if ((val&3) == STRUCT_PTR) {
|
||||
ret.type = CAPN_STRUCT;
|
||||
ret.size = 0;
|
||||
ret.datasz = U32(U16(val >> 32)) * 8;
|
||||
ret.ptrsz = U32(U16(val >> 48)) * 8;
|
||||
e = d + ret.size * (ret.datasz + ret.ptrsz);
|
||||
} else {
|
||||
ret.type = CAPN_LIST;
|
||||
ret.size = val >> 35;
|
||||
ret.datasz = 0;
|
||||
ret.ptrsz = 0;
|
||||
|
||||
switch ((val >> 32) & 7) {
|
||||
case VOID_LIST:
|
||||
|
|
@ -362,6 +377,7 @@ static struct capn_ptr read_ptr(struct capn_segment *s, char *d) {
|
|||
ret.datasz = U32(U16(val >> 32)) * 8;
|
||||
ret.ptrsz = U32(U16(val >> 48)) * 8;
|
||||
ret.size = U32(val) >> 2;
|
||||
ret.has_composite_tag = 1;
|
||||
|
||||
if ((ret.datasz + ret.ptrsz) * ret.size != e - d) {
|
||||
goto err;
|
||||
|
|
@ -381,76 +397,77 @@ err:
|
|||
return ret;
|
||||
}
|
||||
|
||||
struct capn_ptr capn_getp(const struct capn_ptr *p, int off) {
|
||||
struct capn_ptr ret;
|
||||
|
||||
switch (p->type) {
|
||||
capn_ptr capn_getp(capn_ptr p, int off) {
|
||||
switch (p.type) {
|
||||
case CAPN_LIST:
|
||||
/* Return an inner pointer */
|
||||
if (off >= p->size) {
|
||||
if (off < p.size) {
|
||||
capn_ptr ret = {CAPN_NULL};
|
||||
ret.type = CAPN_STRUCT;
|
||||
ret.is_list_member = 1;
|
||||
ret.data = p.data + off * (p.datasz + p.ptrsz);
|
||||
ret.seg = p.seg;
|
||||
ret.datasz = p.datasz;
|
||||
ret.ptrsz = p.ptrsz;
|
||||
return ret;
|
||||
} else {
|
||||
goto err;
|
||||
}
|
||||
ret = *p;
|
||||
ret.type = CAPN_LIST_MEMBER;
|
||||
ret.data += off * (p->datasz + p->ptrsz);
|
||||
ret.size = 0;
|
||||
return ret;
|
||||
|
||||
case CAPN_STRUCT:
|
||||
case CAPN_LIST_MEMBER:
|
||||
off *= 8;
|
||||
if (off >= p->ptrsz) {
|
||||
if (off >= p.ptrsz) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
return read_ptr(p->seg, p->data + p->datasz + off);
|
||||
return read_ptr(p.seg, p.data + p.datasz + off);
|
||||
|
||||
case CAPN_PTR_LIST:
|
||||
if (off >= p->size) {
|
||||
if (off >= p.size) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
return read_ptr(p->seg, p->data + off * 8);
|
||||
return read_ptr(p.seg, p.data + off * 8);
|
||||
|
||||
default:
|
||||
goto err;
|
||||
}
|
||||
|
||||
err:
|
||||
memset(&ret, 0, sizeof(ret));
|
||||
return ret;
|
||||
memset(&p, 0, sizeof(p));
|
||||
return p;
|
||||
}
|
||||
|
||||
static uint64_t ptr_value(const struct capn_ptr *p, int off) {
|
||||
static uint64_t ptr_value(capn_ptr p, int off) {
|
||||
uint64_t val = U64(U32(I32(off/8) << 2));
|
||||
|
||||
switch (p->type) {
|
||||
switch (p.type) {
|
||||
case CAPN_STRUCT:
|
||||
val |= STRUCT_PTR | (U64(p->datasz/8) << 32) | (U64(p->ptrsz/8) << 48);
|
||||
val |= STRUCT_PTR | (U64(p.datasz/8) << 32) | (U64(p.ptrsz/8) << 48);
|
||||
break;
|
||||
|
||||
case CAPN_LIST:
|
||||
if (p->ptrsz || p->datasz > 8) {
|
||||
val |= LIST_PTR | (U64(COMPOSITE_LIST) << 32) | (U64(p->size * (p->datasz + p->ptrsz)/8) << 35);
|
||||
} else if (p->datasz == 8) {
|
||||
val |= LIST_PTR | (U64(BYTE_8_LIST) << 32) | (U64(p->size) << 35);
|
||||
} else if (p->datasz == 4) {
|
||||
val |= LIST_PTR | (U64(BYTE_4_LIST) << 32) | (U64(p->size) << 35);
|
||||
} else if (p->datasz == 2) {
|
||||
val |= LIST_PTR | (U64(BYTE_2_LIST) << 32) | (U64(p->size) << 35);
|
||||
} else if (p->datasz == 1) {
|
||||
val |= LIST_PTR | (U64(BYTE_1_LIST) << 32) | (U64(p->size) << 35);
|
||||
if (p.has_composite_tag) {
|
||||
val |= LIST_PTR | (U64(COMPOSITE_LIST) << 32) | (U64(p.size * (p.datasz + p.ptrsz)/8) << 35);
|
||||
} else if (p.datasz == 8) {
|
||||
val |= LIST_PTR | (U64(BYTE_8_LIST) << 32) | (U64(p.size) << 35);
|
||||
} else if (p.datasz == 4) {
|
||||
val |= LIST_PTR | (U64(BYTE_4_LIST) << 32) | (U64(p.size) << 35);
|
||||
} else if (p.datasz == 2) {
|
||||
val |= LIST_PTR | (U64(BYTE_2_LIST) << 32) | (U64(p.size) << 35);
|
||||
} else if (p.datasz == 1) {
|
||||
val |= LIST_PTR | (U64(BYTE_1_LIST) << 32) | (U64(p.size) << 35);
|
||||
} else {
|
||||
val |= LIST_PTR | (U64(VOID_LIST) << 32) | (U64(p->size) << 35);
|
||||
val |= LIST_PTR | (U64(VOID_LIST) << 32) | (U64(p.size) << 35);
|
||||
}
|
||||
break;
|
||||
|
||||
case CAPN_BIT_LIST:
|
||||
val |= LIST_PTR | (U64(BIT_1_LIST) << 32) | (U64(p->size) << 35);
|
||||
val |= LIST_PTR | (U64(BIT_1_LIST) << 32) | (U64(p.size) << 35);
|
||||
break;
|
||||
|
||||
case CAPN_PTR_LIST:
|
||||
val |= LIST_PTR | (U64(PTR_LIST) << 32) | (U64(p->size) << 35);
|
||||
val |= LIST_PTR | (U64(PTR_LIST) << 32) | (U64(p.size) << 35);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
@ -466,57 +483,50 @@ static void write_far_ptr(char *d, struct capn_segment *s, char *tgt) {
|
|||
}
|
||||
|
||||
static void write_double_far(char *d, struct capn_segment *s, char *tgt) {
|
||||
*(uint64_t*) d = capn_flip64(FAR_PTR | 4 | U64(tgt - s->data) | (U64(s->id) << 32));
|
||||
*(uint64_t*) d = capn_flip64(DOUBLE_PTR | U64(tgt - s->data) | (U64(s->id) << 32));
|
||||
}
|
||||
|
||||
static void write_ptr_tag(char *d, const struct capn_ptr *p, int off) {
|
||||
static void write_ptr_tag(char *d, capn_ptr p, int off) {
|
||||
*(uint64_t*) d = ptr_value(p, off);
|
||||
}
|
||||
|
||||
static int has_tag(const struct capn_ptr* p, const char *pdata) {
|
||||
const char *d = pdata - 8;
|
||||
return d >= p->seg->data && ptr_value(p, 0) == *(uint64_t*) d;
|
||||
}
|
||||
|
||||
#define NEED_TO_COPY 1
|
||||
|
||||
static int write_ptr_no_copy(struct capn_segment *s, char *d, const struct capn_ptr *p) {
|
||||
/* note p->seg can be NULL if its a ptr to static data */
|
||||
char *pdata = p->data;
|
||||
static int write_ptr_no_copy(struct capn_segment *s, char *d, capn_ptr p) {
|
||||
/* note p.seg can be NULL if its a ptr to static data */
|
||||
char *pdata = p.data;
|
||||
|
||||
/* TODO: how to distinguish between 8 byte list and a composite
|
||||
* list with ptrs == 0, data == 1? */
|
||||
if (p->type == CAPN_LIST && (p->ptrsz || p->datasz > 8)) {
|
||||
if (p.has_composite_tag) {
|
||||
pdata -= 8;
|
||||
}
|
||||
|
||||
if (!p || p->type == CAPN_NULL) {
|
||||
if (p.type == CAPN_NULL) {
|
||||
*(uint64_t*) d = 0;
|
||||
return 0;
|
||||
|
||||
} else if (!p->seg || p->seg->capn != s->capn || p->type == CAPN_LIST_MEMBER) {
|
||||
} else if (!p.seg || p.seg->capn != s->capn || p.is_list_member) {
|
||||
return NEED_TO_COPY;
|
||||
|
||||
} else if (p->seg == s) {
|
||||
} else if (p.seg == s) {
|
||||
write_ptr_tag(d, p, pdata - d - 8);
|
||||
return 0;
|
||||
|
||||
} else {
|
||||
/* if its in the same context we can create a far pointer */
|
||||
|
||||
if (has_tag(p, pdata)) {
|
||||
if (p.has_ptr_tag) {
|
||||
/* By lucky chance, the data has a tag in front
|
||||
* of it. This happens when new_object had to move
|
||||
* the data to a new segment. */
|
||||
write_far_ptr(d, p->seg, pdata-8);
|
||||
write_far_ptr(d, p.seg, pdata-8);
|
||||
return 0;
|
||||
|
||||
} else if (p->seg->len + 8 <= p->seg->cap) {
|
||||
} else if (p.seg->len + 8 <= p.seg->cap) {
|
||||
/* The target segment has enough room for tag */
|
||||
char *t = p->seg->data + p->seg->len;
|
||||
char *t = p.seg->data + p.seg->len;
|
||||
write_ptr_tag(t, p, pdata - t - 8);
|
||||
write_far_ptr(d, p->seg, t);
|
||||
p->seg->len += 8;
|
||||
write_far_ptr(d, p.seg, t);
|
||||
p.seg->len += 8;
|
||||
return 0;
|
||||
|
||||
} else {
|
||||
|
|
@ -535,7 +545,7 @@ static int write_ptr_no_copy(struct capn_segment *s, char *d, const struct capn_
|
|||
if (!t) return -1;
|
||||
}
|
||||
|
||||
write_far_ptr(t, p->seg, pdata);
|
||||
write_far_ptr(t, p.seg, pdata);
|
||||
write_ptr_tag(t+8, p, 0);
|
||||
write_double_far(d, s, t);
|
||||
return 0;
|
||||
|
|
@ -546,6 +556,7 @@ static int write_ptr_no_copy(struct capn_segment *s, char *d, const struct capn_
|
|||
struct copy {
|
||||
struct capn_tree hdr;
|
||||
struct capn_ptr to, from;
|
||||
char *fdata;
|
||||
int fsize;
|
||||
};
|
||||
|
||||
|
|
@ -556,7 +567,6 @@ static int data_size(const struct capn_ptr *p) {
|
|||
case CAPN_PTR_LIST:
|
||||
return p->size*8;
|
||||
case CAPN_STRUCT:
|
||||
case CAPN_LIST_MEMBER:
|
||||
return p->datasz + p->ptrsz;
|
||||
case CAPN_LIST:
|
||||
return p->size * (p->datasz + p->ptrsz);
|
||||
|
|
@ -565,31 +575,41 @@ static int data_size(const struct capn_ptr *p) {
|
|||
}
|
||||
}
|
||||
|
||||
static struct capn_ptr new_clone(struct capn_segment *s, const struct capn_ptr *p) {
|
||||
switch (p->type) {
|
||||
case CAPN_LIST_MEMBER:
|
||||
static capn_ptr new_clone(struct capn_segment *s, capn_ptr p) {
|
||||
switch (p.type) {
|
||||
case CAPN_STRUCT:
|
||||
return capn_new_struct(s, p->datasz, p->ptrsz);
|
||||
return capn_new_struct(s, p.datasz, p.ptrsz);
|
||||
case CAPN_PTR_LIST:
|
||||
return capn_new_ptr_list(s, p->size);
|
||||
return capn_new_ptr_list(s, p.size);
|
||||
case CAPN_BIT_LIST:
|
||||
return capn_new_bit_list(s, p->size);
|
||||
return capn_new_bit_list(s, p.size);
|
||||
case CAPN_LIST:
|
||||
return capn_new_list(s, p->size, p->datasz, p->ptrsz);
|
||||
return capn_new_list(s, p.size, p.datasz, p.ptrsz);
|
||||
default:
|
||||
return *p;
|
||||
return p;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static int is_ptr_equal(const struct capn_ptr *a, const struct capn_ptr *b) {
|
||||
return a->data == b->data && a->type == b->type && a->size == b->size && a->datasz == b->datasz && a->ptrsz == b->ptrsz;
|
||||
return a->data == b->data
|
||||
&& a->type == b->type
|
||||
&& a->size == b->size
|
||||
&& a->datasz == b->datasz
|
||||
&& a->ptrsz == b->ptrsz
|
||||
&& a->has_composite_tag == b->has_composite_tag;
|
||||
}
|
||||
|
||||
static int write_copy(struct capn_segment *seg, char *data, struct capn_ptr *t, struct capn_ptr *f, int *dep, int zeros) {
|
||||
struct capn *c = seg->capn;
|
||||
struct copy *cp = (struct copy*) c->copy;
|
||||
int sz = data_size(f);
|
||||
int fsize = data_size(f);
|
||||
char *fdata = f->data;
|
||||
|
||||
if (f->has_composite_tag) {
|
||||
fsize += 8;
|
||||
fdata -= 8;
|
||||
}
|
||||
|
||||
/* We always copy list members as it would otherwise be an
|
||||
* overlapped pointer (the data is owned by the inclosing list).
|
||||
|
|
@ -599,14 +619,14 @@ static int write_copy(struct capn_segment *seg, char *data, struct capn_ptr *t,
|
|||
* pointers.
|
||||
*/
|
||||
|
||||
while (c && sz) {
|
||||
if (f->data + sz <= cp->from.data) {
|
||||
while (c && fsize) {
|
||||
if (fdata + fsize <= cp->fdata) {
|
||||
cp = (struct copy*) cp->hdr.link[0];
|
||||
} else if (cp->from.data + cp->fsize <= f->data) {
|
||||
} else if (cp->fdata + cp->fsize <= fdata) {
|
||||
cp = (struct copy*) cp->hdr.link[1];
|
||||
} else if (is_ptr_equal(f, &cp->from)) {
|
||||
/* we already have a copy so just point to that */
|
||||
return write_ptr_no_copy(seg, data, &cp->from);
|
||||
return write_ptr_no_copy(seg, data, cp->from);
|
||||
} else {
|
||||
/* pointer to overlapped data */
|
||||
return -1;
|
||||
|
|
@ -614,11 +634,11 @@ static int write_copy(struct capn_segment *seg, char *data, struct capn_ptr *t,
|
|||
}
|
||||
|
||||
/* no copy - have to copy */
|
||||
*t = new_clone(seg, f);
|
||||
*t = new_clone(seg, *f);
|
||||
|
||||
/* add the copy to the copy tree so we can look for overlapping
|
||||
* source pointers and handle recursive structures */
|
||||
if (sz && f->type != CAPN_LIST_MEMBER) {
|
||||
if (fsize && !f->is_list_member) {
|
||||
struct copy *n;
|
||||
struct capn_segment *cs = c->copylist;
|
||||
|
||||
|
|
@ -636,18 +656,15 @@ static int write_copy(struct capn_segment *seg, char *data, struct capn_ptr *t,
|
|||
n = (struct copy*) (cs->data + cs->len);
|
||||
cs->len += sizeof(*n);
|
||||
|
||||
n->hdr.parent = &cp->hdr;
|
||||
n->from = *f;
|
||||
n->to = *t;
|
||||
n->fsize = sz;
|
||||
n->fdata = fdata;
|
||||
n->fsize = fsize;
|
||||
|
||||
if (f->data < cp->from.data) {
|
||||
cp->hdr.link[0] = &n->hdr;
|
||||
} else {
|
||||
cp->hdr.link[1] = &n->hdr;
|
||||
}
|
||||
n->hdr.parent = &cp->hdr;
|
||||
cp->hdr.link[cp->fdata < f->data] = &n->hdr;
|
||||
|
||||
seg->capn->copy = insert_rebalance(seg->capn->copy, &n->hdr);
|
||||
seg->capn->copy = capn_tree_insert(seg->capn->copy, &n->hdr);
|
||||
}
|
||||
|
||||
/* minimize the number of types the main copy routine has to
|
||||
|
|
@ -656,7 +673,6 @@ static int write_copy(struct capn_segment *seg, char *data, struct capn_ptr *t,
|
|||
* be valid */
|
||||
switch (t->type) {
|
||||
case CAPN_STRUCT:
|
||||
case CAPN_LIST_MEMBER:
|
||||
if (t->datasz) {
|
||||
memcpy(t->data, f->data, t->datasz - zeros);
|
||||
t->data += t->datasz;
|
||||
|
|
@ -700,28 +716,28 @@ static int write_copy(struct capn_segment *seg, char *data, struct capn_ptr *t,
|
|||
|
||||
#define MAX_COPY_DEPTH 32
|
||||
|
||||
int write_ptr(struct capn_ptr *p, int off, const struct capn_ptr *tgt, int zeros) {
|
||||
int write_ptr(capn_ptr p, int off, struct capn_ptr tgt, int zeros) {
|
||||
struct capn_ptr to[MAX_COPY_DEPTH], from[MAX_COPY_DEPTH];
|
||||
char *data;
|
||||
int err, dep;
|
||||
|
||||
switch (p->type) {
|
||||
switch (p.type) {
|
||||
case CAPN_LIST:
|
||||
if (off < p->size && (tgt->type == CAPN_STRUCT || tgt->type == CAPN_LIST_MEMBER)) {
|
||||
if (off < p.size && tgt.type == CAPN_STRUCT) {
|
||||
struct capn_ptr *f, *t;
|
||||
char *d;
|
||||
int sz;
|
||||
|
||||
/* copy struct data */
|
||||
d = p->data + off * (p->datasz + p->ptrsz);
|
||||
sz = min(p->datasz, tgt->datasz);
|
||||
memcpy(d, tgt->data, sz);
|
||||
memset(d + sz, 0, p->datasz - sz);
|
||||
d = p.data + off * (p.datasz + p.ptrsz);
|
||||
sz = min(p.datasz, tgt.datasz);
|
||||
memcpy(d, tgt.data, sz);
|
||||
memset(d + sz, 0, p.datasz - sz);
|
||||
|
||||
/* reset excess pointers */
|
||||
d += p->datasz;
|
||||
sz = min(p->ptrsz, tgt->ptrsz);
|
||||
memset(d + sz, 0, p->ptrsz - sz);
|
||||
d += p.datasz;
|
||||
sz = min(p.ptrsz, tgt.ptrsz);
|
||||
memset(d + sz, 0, p.ptrsz - sz);
|
||||
|
||||
/* create a pointer list for the main loop to copy */
|
||||
dep = 1;
|
||||
|
|
@ -729,14 +745,14 @@ int write_ptr(struct capn_ptr *p, int off, const struct capn_ptr *tgt, int zeros
|
|||
/* main copy loop doesn't need the other fields
|
||||
* for ptr lists */
|
||||
f = &from[0];
|
||||
f->data = tgt->data + tgt->datasz;
|
||||
f->seg = tgt->seg;
|
||||
f->data = tgt.data + tgt.datasz;
|
||||
f->seg = tgt.seg;
|
||||
|
||||
t = &to[0];
|
||||
t->type = CAPN_PTR_LIST;
|
||||
t->data = d;
|
||||
t->size = sz/8;
|
||||
t->seg = p->seg;
|
||||
t->seg = p.seg;
|
||||
|
||||
goto copy_loop;
|
||||
} else {
|
||||
|
|
@ -744,24 +760,23 @@ int write_ptr(struct capn_ptr *p, int off, const struct capn_ptr *tgt, int zeros
|
|||
}
|
||||
|
||||
case CAPN_PTR_LIST:
|
||||
if (off >= p->size)
|
||||
if (off >= p.size)
|
||||
return -1;
|
||||
data = p->data + off * 8;
|
||||
data = p.data + off * 8;
|
||||
break;
|
||||
|
||||
case CAPN_STRUCT:
|
||||
case CAPN_LIST_MEMBER:
|
||||
off *= 8;
|
||||
if (off >= p->ptrsz)
|
||||
if (off >= p.ptrsz)
|
||||
return -1;
|
||||
data = p->data + p->datasz + off;
|
||||
data = p.data + p.datasz + off;
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
err = write_ptr_no_copy(p->seg, data, tgt);
|
||||
err = write_ptr_no_copy(p.seg, data, tgt);
|
||||
if (err != NEED_TO_COPY)
|
||||
return err;
|
||||
|
||||
|
|
@ -774,8 +789,8 @@ int write_ptr(struct capn_ptr *p, int off, const struct capn_ptr *tgt, int zeros
|
|||
*/
|
||||
|
||||
dep = 0;
|
||||
from[0] = *tgt;
|
||||
if (write_copy(p->seg, data, to, from, &dep, zeros))
|
||||
from[0] = tgt;
|
||||
if (write_copy(p.seg, data, to, from, &dep, zeros))
|
||||
return -1;
|
||||
|
||||
copy_loop:
|
||||
|
|
@ -797,7 +812,8 @@ copy_loop:
|
|||
case CAPN_LIST:
|
||||
*fn = *fc;
|
||||
*tn = *tc;
|
||||
fn->type = tn->type = CAPN_LIST_MEMBER;
|
||||
fn->type = tn->type = CAPN_STRUCT;
|
||||
fn->is_list_member = tn->is_list_member = 1;
|
||||
fn->size = tn->size = 0;
|
||||
|
||||
if (write_copy(tc->seg, tc->data, tn, fn, &dep, 0))
|
||||
|
|
@ -825,42 +841,42 @@ copy_loop:
|
|||
return 0;
|
||||
}
|
||||
|
||||
int capn_setp(struct capn_ptr *p, int off, const struct capn_ptr *tgt) {
|
||||
int capn_setp(capn_ptr p, int off, capn_ptr tgt) {
|
||||
return write_ptr(p, off, tgt, 0);
|
||||
}
|
||||
|
||||
int capn_read1(const struct capn_ptr *p, int off, uint8_t *data, int sz) {
|
||||
int capn_read1(capn_ptr p, int off, uint8_t *data, int sz) {
|
||||
/* Note we only support aligned reads */
|
||||
int bsz;
|
||||
if (p->type != CAPN_BIT_LIST || (off & 7) != 0)
|
||||
if (p.type != CAPN_BIT_LIST || (off & 7) != 0)
|
||||
return -1;
|
||||
|
||||
bsz = (sz + 7) / 8;
|
||||
off /= 8;
|
||||
|
||||
if (off + sz > p->datasz) {
|
||||
memcpy(data, p->data + off, p->datasz - off);
|
||||
return p->size - off*8;
|
||||
if (off + sz > p.datasz) {
|
||||
memcpy(data, p.data + off, p.datasz - off);
|
||||
return p.size - off*8;
|
||||
} else {
|
||||
memcpy(data, p->data + off, bsz);
|
||||
memcpy(data, p.data + off, bsz);
|
||||
return sz;
|
||||
}
|
||||
}
|
||||
|
||||
int capn_write1(struct capn_ptr *p, int off, const uint8_t *data, int sz) {
|
||||
int capn_write1(capn_ptr p, int off, const uint8_t *data, int sz) {
|
||||
/* Note we only support aligned writes */
|
||||
int bsz;
|
||||
if (p->type != CAPN_BIT_LIST || (off & 7) != 0)
|
||||
if (p.type != CAPN_BIT_LIST || (off & 7) != 0)
|
||||
return -1;
|
||||
|
||||
bsz = (sz + 7) / 8;
|
||||
off /= 8;
|
||||
|
||||
if (off + sz > p->datasz) {
|
||||
memcpy(p->data + off, data, p->datasz - off);
|
||||
return p->size - off*8;
|
||||
if (off + sz > p.datasz) {
|
||||
memcpy(p.data + off, data, p.datasz - off);
|
||||
return p.size - off*8;
|
||||
} else {
|
||||
memcpy(p->data + off, data, bsz);
|
||||
memcpy(p.data + off, data, bsz);
|
||||
return sz;
|
||||
}
|
||||
}
|
||||
|
|
@ -887,7 +903,7 @@ int capn_write1(struct capn_ptr *p, int off, const uint8_t *data, int sz) {
|
|||
#define ADD_TAG 1
|
||||
#endif
|
||||
|
||||
static void new_object(struct capn_ptr *p, int bytes) {
|
||||
static void new_object(capn_ptr *p, int bytes) {
|
||||
struct capn_segment *s = p->seg;
|
||||
|
||||
/* all allocations are 8 byte aligned */
|
||||
|
|
@ -908,54 +924,57 @@ static void new_object(struct capn_ptr *p, int bytes) {
|
|||
}
|
||||
|
||||
if (ADD_TAG) {
|
||||
write_ptr_tag(p->data, p, 0);
|
||||
write_ptr_tag(p->data, *p, 0);
|
||||
p->data += 8;
|
||||
p->has_ptr_tag = 1;
|
||||
}
|
||||
}
|
||||
|
||||
struct capn_ptr capn_root(struct capn* c) {
|
||||
struct capn_ptr p;
|
||||
p.seg = lookup_segment(c, NULL, 0);
|
||||
capn_ptr capn_get_root(struct capn* c) {
|
||||
struct capn_segment* s = lookup_segment(c, NULL, 0);
|
||||
if (s->len < 8) {
|
||||
capn_ptr ret = {CAPN_NULL};
|
||||
return ret;
|
||||
} else {
|
||||
return read_ptr(s, s->data);
|
||||
}
|
||||
}
|
||||
|
||||
capn_ptr capn_new_root(struct capn *c) {
|
||||
capn_ptr p = {CAPN_NULL};
|
||||
struct capn_segment *s = lookup_segment(c, NULL, 0);
|
||||
|
||||
/* don't use new_object as we don't want the tag */
|
||||
if (!p.seg && new_data(c, 8, &p.seg) == NULL)
|
||||
goto err;
|
||||
if ((s || new_data(c, 8, &s) != NULL) && s->len >= 8) {
|
||||
p.seg = s;
|
||||
p.data = p.seg->data;
|
||||
p.size = 1;
|
||||
p.type = CAPN_PTR_LIST;
|
||||
}
|
||||
|
||||
if (p.seg->len < 8)
|
||||
goto err;
|
||||
|
||||
p.data = p.seg->data;
|
||||
p.size = 1;
|
||||
p.type = CAPN_PTR_LIST;
|
||||
p.datasz = 0;
|
||||
p.ptrsz = 0;
|
||||
return p;
|
||||
err:
|
||||
memset(&p, 0, sizeof(p));
|
||||
return p;
|
||||
}
|
||||
|
||||
struct capn_ptr capn_new_struct(struct capn_segment *seg, int datasz, int ptrs) {
|
||||
struct capn_ptr p;
|
||||
capn_ptr capn_new_struct(struct capn_segment *seg, int datasz, int ptrs) {
|
||||
capn_ptr p = {CAPN_NULL};
|
||||
p.seg = seg;
|
||||
p.type = CAPN_STRUCT;
|
||||
p.size = 0;
|
||||
p.datasz = (datasz + 7) & ~7;
|
||||
p.ptrsz = ptrs * 8;
|
||||
new_object(&p, p.datasz + p.ptrsz);
|
||||
return p;
|
||||
}
|
||||
|
||||
struct capn_ptr capn_new_list(struct capn_segment *seg, int sz, int datasz, int ptrs) {
|
||||
struct capn_ptr p;
|
||||
capn_ptr capn_new_list(struct capn_segment *seg, int sz, int datasz, int ptrs) {
|
||||
capn_ptr p = {CAPN_NULL};
|
||||
p.seg = seg;
|
||||
p.type = CAPN_LIST;
|
||||
p.size = sz;
|
||||
p.ptrsz = 0;
|
||||
|
||||
if (ptrs || datasz > 8) {
|
||||
p.datasz = (datasz + 7) & ~7;
|
||||
p.ptrsz = ptrs*8;
|
||||
p.has_composite_tag = 1;
|
||||
new_object(&p, p.size * (p.datasz + p.ptrsz) + 8);
|
||||
if (p.data) {
|
||||
uint64_t hdr = STRUCT_PTR | (U64(p.size) << 2) | (U64(p.datasz/8) << 32) | (U64(ptrs) << 48);
|
||||
|
|
@ -976,19 +995,18 @@ struct capn_ptr capn_new_list(struct capn_segment *seg, int sz, int datasz, int
|
|||
return p;
|
||||
}
|
||||
|
||||
struct capn_ptr capn_new_bit_list(struct capn_segment *seg, int sz) {
|
||||
struct capn_ptr p;
|
||||
capn_ptr capn_new_bit_list(struct capn_segment *seg, int sz) {
|
||||
capn_ptr p = {CAPN_NULL};
|
||||
p.seg = seg;
|
||||
p.type = CAPN_BIT_LIST;
|
||||
p.datasz = (sz+7)/8;
|
||||
p.ptrsz = 0;
|
||||
p.size = sz;
|
||||
new_object(&p, p.datasz);
|
||||
return p;
|
||||
}
|
||||
|
||||
struct capn_ptr capn_new_ptr_list(struct capn_segment *seg, int sz) {
|
||||
struct capn_ptr p;
|
||||
capn_ptr capn_new_ptr_list(struct capn_segment *seg, int sz) {
|
||||
capn_ptr p = {CAPN_NULL};
|
||||
p.seg = seg;
|
||||
p.type = CAPN_PTR_LIST;
|
||||
p.size = sz;
|
||||
|
|
@ -998,13 +1016,12 @@ struct capn_ptr capn_new_ptr_list(struct capn_segment *seg, int sz) {
|
|||
return p;
|
||||
}
|
||||
|
||||
struct capn_ptr capn_new_string(struct capn_segment *seg, const char *str, int sz) {
|
||||
struct capn_ptr p;
|
||||
capn_ptr capn_new_string(struct capn_segment *seg, const char *str, int sz) {
|
||||
capn_ptr p = {CAPN_NULL};
|
||||
p.seg = seg;
|
||||
p.type = CAPN_LIST;
|
||||
p.size = ((sz >= 0) ? sz : strlen(str)) + 1;
|
||||
p.datasz = 1;
|
||||
p.ptrsz = 0;
|
||||
new_object(&p, p.size);
|
||||
if (p.data) {
|
||||
memcpy(p.data, str, p.size-1);
|
||||
|
|
@ -1012,80 +1029,53 @@ struct capn_ptr capn_new_string(struct capn_segment *seg, const char *str, int s
|
|||
return p;
|
||||
}
|
||||
|
||||
char *capn_to_string(const struct capn_ptr *p, int *psz) {
|
||||
if (p->type != CAPN_LIST || p->size < 1 || p->data[p->size - 1] != 0) {
|
||||
if (psz) *psz = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (psz) *psz = p->size - 1;
|
||||
return p->data;
|
||||
}
|
||||
|
||||
struct capn_text capn_get_text(const struct capn_ptr *p, int off) {
|
||||
struct capn_ptr m = capn_getp(p, off);
|
||||
struct capn_text ret;
|
||||
capn_text capn_get_text(capn_ptr p, int off) {
|
||||
capn_ptr m = capn_getp(p, off);
|
||||
capn_text ret = {CAPN_NULL};
|
||||
if (m.type == CAPN_LIST && m.datasz == 1 && m.size && m.data[m.size - 1] == 0) {
|
||||
ret.seg = m.seg;
|
||||
ret.str = m.data;
|
||||
ret.size = m.size - 1;
|
||||
} else {
|
||||
ret.seg = NULL;
|
||||
ret.str = NULL;
|
||||
ret.size = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct capn_data capn_get_data(const struct capn_ptr *p, int off) {
|
||||
struct capn_ptr m = capn_getp(p, off);
|
||||
struct capn_data ret;
|
||||
capn_data capn_get_data(capn_ptr p, int off) {
|
||||
capn_ptr m = capn_getp(p, off);
|
||||
capn_data ret = {CAPN_NULL};
|
||||
if (m.type == CAPN_LIST && m.datasz == 1) {
|
||||
ret.seg = m.seg;
|
||||
ret.data = (uint8_t*) m.data;
|
||||
ret.size = m.size;
|
||||
} else {
|
||||
ret.seg = NULL;
|
||||
ret.data = NULL;
|
||||
ret.size = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int capn_set_text(struct capn_ptr *p, int off, struct capn_text tgt) {
|
||||
struct capn_ptr m;
|
||||
int capn_set_text(capn_ptr p, int off, capn_text tgt) {
|
||||
capn_ptr m = {CAPN_NULL};
|
||||
if (tgt.str) {
|
||||
m.type = CAPN_LIST;
|
||||
m.seg = tgt.seg;
|
||||
m.data = (char*)tgt.str;
|
||||
m.size = (tgt.size >= 0 ? tgt.size : strlen(tgt.str)) + 1;
|
||||
m.datasz = 1;
|
||||
m.ptrsz = 0;
|
||||
} else {
|
||||
m.type = CAPN_NULL;
|
||||
}
|
||||
/* in the case that the size is specified we need to be careful
|
||||
* that we don't read the extra byte as it may be not be null or
|
||||
* may be in a different page and cause a segfault
|
||||
*/
|
||||
return write_ptr(p, off, &m, 1);
|
||||
return write_ptr(p, off, m, 1);
|
||||
}
|
||||
|
||||
int capn_set_data(struct capn_ptr *p, int off, struct capn_data tgt) {
|
||||
struct capn_ptr m;
|
||||
int capn_set_data(capn_ptr p, int off, capn_data tgt) {
|
||||
capn_ptr m = {CAPN_NULL};
|
||||
if (tgt.data) {
|
||||
m.type = CAPN_LIST;
|
||||
m.seg = tgt.seg;
|
||||
m.data = (char*)tgt.data;
|
||||
m.size = tgt.size;
|
||||
m.datasz = 1;
|
||||
m.ptrsz = 0;
|
||||
} else {
|
||||
m.type = CAPN_NULL;
|
||||
}
|
||||
return write_ptr(p, off, &m, 0);
|
||||
return write_ptr(p, off, m, 0);
|
||||
}
|
||||
|
||||
int capn_marshal_iptr(const union capn_iptr *ip, struct capn_ptr *p, int off) {
|
||||
return write_ptr(p, off, &ip->c, 0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue