Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / src / hb-blob.cc
index fb48f03..b5291f6 100644 (file)
 
 /* http://www.oracle.com/technetwork/articles/servers-storage-dev/standardheaderfiles-453865.html */
 #ifndef _POSIX_C_SOURCE
-#define _POSIX_C_SOURCE 199309L
+#define _POSIX_C_SOURCE 200809L
 #endif
 
 #include "hb-private.hh"
+#include "hb-debug.hh"
 
 #include "hb-object-private.hh"
 
 #include <errno.h>
 
 
-
-#ifndef HB_DEBUG_BLOB
-#define HB_DEBUG_BLOB (HB_DEBUG+0)
-#endif
-
-
 struct hb_blob_t {
   hb_object_header_t header;
   ASSERT_POD ();
@@ -72,8 +67,8 @@ _hb_blob_destroy_user_data (hb_blob_t *blob)
 {
   if (blob->destroy) {
     blob->destroy (blob->user_data);
-    blob->user_data = NULL;
-    blob->destroy = NULL;
+    blob->user_data = nullptr;
+    blob->destroy = nullptr;
   }
 }
 
@@ -128,6 +123,12 @@ hb_blob_create (const char        *data,
   return blob;
 }
 
+static void
+_hb_blob_destroy (void *data)
+{
+  hb_blob_destroy ((hb_blob_t *) data);
+}
+
 /**
  * hb_blob_create_sub_blob:
  * @parent: Parent blob.
@@ -164,7 +165,32 @@ hb_blob_create_sub_blob (hb_blob_t    *parent,
                         MIN (length, parent->length - offset),
                         HB_MEMORY_MODE_READONLY,
                         hb_blob_reference (parent),
-                        (hb_destroy_func_t) hb_blob_destroy);
+                        _hb_blob_destroy);
+
+  return blob;
+}
+
+/**
+ * hb_blob_copy_writable_or_fail:
+ * @blob: A blob.
+ *
+ * Makes a writable copy of @blob.
+ *
+ * Return value: New blob, or nullptr if allocation failed.
+ *
+ * Since: 1.8.0
+ **/
+hb_blob_t *
+hb_blob_copy_writable_or_fail (hb_blob_t *blob)
+{
+  blob = hb_blob_create (blob->data,
+                        blob->length,
+                        HB_MEMORY_MODE_DUPLICATE,
+                        nullptr,
+                        nullptr);
+
+  if (unlikely (blob == hb_blob_get_empty ()))
+    blob = nullptr;
 
   return blob;
 }
@@ -188,12 +214,12 @@ hb_blob_get_empty (void)
 
     true, /* immutable */
 
-    NULL, /* data */
+    nullptr, /* data */
     0, /* length */
     HB_MEMORY_MODE_READONLY, /* mode */
 
-    NULL, /* user_data */
-    NULL  /* destroy */
+    nullptr, /* user_data */
+    nullptr  /* destroy */
   };
 
   return const_cast<hb_blob_t *> (&_hb_blob_nil);
@@ -221,7 +247,7 @@ hb_blob_reference (hb_blob_t *blob)
  * hb_blob_destroy: (skip)
  * @blob: a blob.
  *
- * Descreases the reference count on @blob, and if it reaches zero, destroys
+ * Decreases the reference count on @blob, and if it reaches zero, destroys
  * @blob, freeing all memory, possibly calling the destroy-callback the blob
  * was created for if it has not been called already.
  *
@@ -373,7 +399,7 @@ hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
     if (length)
       *length = 0;
 
-    return NULL;
+    return nullptr;
   }
 
   if (length)