]> mj.ucw.cz Git - libucw.git/commitdiff
- changed the format of struct oattr to allow 0-copy: byte val[] -> byte *val
authorRobert Spalek <robert@ucw.cz>
Fri, 25 Jun 2004 11:00:59 +0000 (11:00 +0000)
committerRobert Spalek <robert@ucw.cz>
Fri, 25 Jun 2004 11:00:59 +0000 (11:00 +0000)
- added obj_add_attr_ref() as a replacement for obj_add_attr()

lib/object.c
lib/object.h

index db471c4b0257e25d58e61e4ce9ec6bb9f1879eae..47024c11eaaa7ce6f26f08544a475e6c4ca7f62c 100644 (file)
@@ -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)
 {
index 4f6cffbc0737296d0f782446d0466ceea8faa72f..5f43414072d091b61823ef8244c07a368f5f53e3 100644 (file)
@@ -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);