[serialize] Simplify copy
authorBehdad Esfahbod <behdad@behdad.org>
Sun, 31 Mar 2019 02:01:23 +0000 (19:01 -0700)
committerBehdad Esfahbod <behdad@behdad.org>
Sun, 31 Mar 2019 02:01:23 +0000 (19:01 -0700)
src/hb-serialize.hh

index 805ac49..7335361 100644 (file)
@@ -246,16 +246,6 @@ struct hb_serialize_context_t
   Type *extend (Type &obj) { return extend_size (obj, obj.get_size ()); }
 
   /* Output routines. */
-  template <typename Type>
-  Type *copy () const
-  {
-    assert (this->successful);
-    unsigned int len = this->head - this->start;
-    void *p = malloc (len);
-    if (p)
-      memcpy (p, this->start, len);
-    return reinterpret_cast<Type *> (p);
-  }
   hb_bytes_t copy_bytes () const
   {
     assert (this->successful);
@@ -267,13 +257,15 @@ struct hb_serialize_context_t
       return hb_bytes_t ();
     return hb_bytes_t ((char *) p, len);
   }
+  template <typename Type>
+  Type *copy () const
+  { return reinterpret_cast<Type *> ((char *) copy_bytes ().arrayZ); }
   hb_blob_t *copy_blob () const
   {
-    assert (this->successful);
-    return hb_blob_create (this->start,
-                          this->head - this->start,
-                          HB_MEMORY_MODE_DUPLICATE,
-                          nullptr, nullptr);
+    hb_bytes_t b = copy_bytes ();
+    return hb_blob_create (b.arrayZ, b.length,
+                          HB_MEMORY_MODE_WRITABLE,
+                          (char *) b.arrayZ, free);
   }
 
   public: /* TODO Make private. */