]> mj.ucw.cz Git - libucw.git/blobdiff - lib/object.c
Minor optimizations.
[libucw.git] / lib / object.c
index db471c4b0257e25d58e61e4ce9ec6bb9f1879eae..371d9b3647d7e6443f09dd838d34716acc78b84f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     Sherlock Library -- Object Functions
  *
- *     (c) 1997--2003 Martin Mares <mj@ucw.cz>
+ *     (c) 1997--2004 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
@@ -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)
 {
@@ -157,7 +169,6 @@ byte *
 obj_find_aval(struct odes *o, uns x)
 {
   struct oattr *a = obj_find_attr(o, x);
-
   return a ? a->val : NULL;
 }
 
@@ -199,15 +210,14 @@ 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, struct oattr *b)
 {
-  struct oattr *a, *b;
+  struct oattr *a;
 
-  b = oa_new(o, x, v);
-  if (!(a = o->cached_attr) || a->attr != x)
+  if (!(a = o->cached_attr) || a->attr != b->attr)
     {
-      if (!(a = obj_find_attr(o, x)))
+      if (!(a = obj_find_attr(o, b->attr)))
        {
          b->next = o->attrs;
          o->attrs = b;
@@ -222,6 +232,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, oa_new(o, x, v));
+}
+
+struct oattr *
+obj_add_attr_ref(struct odes *o, uns x, byte *v)
+{
+  return obj_add_attr_internal(o, oa_new_ref(o, x, v));
+}
+
 struct oattr *
 obj_prepend_attr(struct odes *o, uns x, byte *v)
 {