From: Robert Spalek Date: Fri, 25 Jun 2004 11:00:59 +0000 (+0000) Subject: - changed the format of struct oattr to allow 0-copy: byte val[] -> byte *val X-Git-Tag: holmes-import~1026 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=b97a7ac3e6a7c332202d113015d5dfb2d24bcd8b;p=libucw.git - changed the format of struct oattr to allow 0-copy: byte val[] -> byte *val - added obj_add_attr_ref() as a replacement for obj_add_attr() --- diff --git a/lib/object.c b/lib/object.c index db471c4b..47024c11 100644 --- a/lib/object.c +++ b/lib/object.c @@ -26,14 +26,26 @@ obj_dump(struct odes *o) static struct oattr * oa_new(struct odes *o, uns x, byte *v) { - struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr) + strlen(v)); + struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr) + strlen(v)+1); a->next = a->same = NULL; a->attr = x; + a->val = (byte*) (a+1); strcpy(a->val, v); return a; } +static struct oattr * +oa_new_ref(struct odes *o, uns x, byte *v) +{ + struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr)); + + a->next = a->same = NULL; + a->attr = x; + a->val = v; + return a; +} + struct odes * obj_new(struct mempool *pool) { @@ -199,12 +211,15 @@ obj_set_attr_num(struct odes *o, uns a, uns v) return obj_set_attr(o, a, x); } -struct oattr * -obj_add_attr(struct odes *o, uns x, byte *v) +static inline struct oattr * +obj_add_attr_internal(struct odes *o, uns x, byte *v, uns just_ref) { struct oattr *a, *b; - b = oa_new(o, x, v); + if (just_ref) + b = oa_new_ref(o, x, v); + else + b = oa_new(o, x, v); if (!(a = o->cached_attr) || a->attr != x) { if (!(a = obj_find_attr(o, x))) @@ -222,6 +237,18 @@ obj_add_attr(struct odes *o, uns x, byte *v) return b; } +struct oattr * +obj_add_attr(struct odes *o, uns x, byte *v) +{ + return obj_add_attr_internal(o, x, v, 0); +} + +struct oattr * +obj_add_attr_ref(struct odes *o, uns x, byte *v) +{ + return obj_add_attr_internal(o, x, v, 1); +} + struct oattr * obj_prepend_attr(struct odes *o, uns x, byte *v) { diff --git a/lib/object.h b/lib/object.h index 4f6cffbc..5f434140 100644 --- a/lib/object.h +++ b/lib/object.h @@ -23,7 +23,7 @@ struct odes { /* Object description */ struct oattr { /* Object attribute */ struct oattr *next, *same; byte attr; - byte val[1]; + byte *val; }; void obj_dump(struct odes *); @@ -39,6 +39,7 @@ byte *obj_find_aval(struct odes *, uns); struct oattr *obj_set_attr(struct odes *, uns, byte *); struct oattr *obj_set_attr_num(struct odes *, uns, uns); struct oattr *obj_add_attr(struct odes *, uns, byte *); +struct oattr *obj_add_attr_ref(struct odes *o, uns x, byte *v); // no strdup() struct oattr *obj_prepend_attr(struct odes *, uns, byte *); struct oattr *obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v); void obj_move_attr_to_head(struct odes *o, uns);