[HB] Simplify object creation
authorBehdad Esfahbod <behdad@behdad.org>
Sun, 2 Aug 2009 00:29:22 +0000 (20:29 -0400)
committerBehdad Esfahbod <behdad@behdad.org>
Mon, 2 Nov 2009 19:40:25 +0000 (14:40 -0500)
src/Makefile.am
src/hb-blob.c
src/hb-font-private.h
src/hb-font.c
src/hb-object-private.h [new file with mode: 0644]
src/hb-private.h
src/hb-refcount-private.h [deleted file]

index c33c01926ea593b4a95317d4b838c5341e1445c6..f941046ddb74660885f30a5edcded468c6421be4 100644 (file)
@@ -25,7 +25,7 @@ HBSOURCES =  \
        hb-ot-layout-gsub-private.h \
        hb-ot-layout-open-private.h \
        hb-ot-layout-private.h \
-       hb-refcount-private.h \
+       hb-object-private.h \
        $(NULL)
 
 HBHEADERS = \
index 8edb221acc1211901b53be6fc38f7c99387a8a05..512bec98dd602114ec13711fc7e89ec32512cfaa 100644 (file)
@@ -27,7 +27,6 @@
 #include "hb-private.h"
 
 #include "hb-blob.h"
-#include "hb-refcount-private.h"
 
 struct _hb_blob_t {
   hb_reference_count_t ref_count;
@@ -69,15 +68,12 @@ hb_blob_create (const char        *data,
 {
   hb_blob_t *blob;
 
-  blob = calloc (1, sizeof (hb_blob_t));
-  if (!blob) {
+  if (!HB_OBJECT_DO_CREATE (blob)) {
     if (destroy)
       destroy (user_data);
     return &_hb_blob_nil;
   }
 
-  HB_OBJECT_DO_CREATE (blob);
-
   blob->data = data;
   blob->len = len;
   blob->mode = mode;
index 7c354060aa9432d8d1a2c8bcc1f1a47231e3959a..8e0fe7205e6c9c7ea50a0cff15114121b2f87d55 100644 (file)
@@ -30,7 +30,6 @@
 #include "hb-private.h"
 
 #include "hb-font.h"
-#include "hb-refcount-private.h"
 
 HB_BEGIN_DECLS
 
index 0d0d6209bab9d687746310e24cba432cb87f3525..b2cf440e64ada110816e3f5e26c250319e919b0f 100644 (file)
@@ -50,12 +50,9 @@ hb_font_callbacks_create (void)
 {
   hb_font_callbacks_t *fcallbacks;
 
-  fcallbacks = calloc (1, sizeof (hb_font_callbacks_t));
-  if (!fcallbacks)
+  if (!HB_OBJECT_DO_CREATE (fcallbacks))
     return &_hb_font_callbacks_nil;
 
-  HB_OBJECT_DO_CREATE (fcallbacks);
-
   return fcallbacks;
 }
 
@@ -78,12 +75,13 @@ hb_font_callbacks_duplicate (hb_font_callbacks_t *other_fcallbacks)
 {
   hb_font_callbacks_t *fcallbacks;
 
-  fcallbacks = calloc (1, sizeof (hb_font_callbacks_t));
-  if (!fcallbacks)
+  if (!HB_OBJECT_DO_CREATE (fcallbacks))
     return &_hb_font_callbacks_nil;
 
   *fcallbacks = *other_fcallbacks;
-  HB_OBJECT_DO_CREATE (fcallbacks);
+
+  /* re-init refcount */
+  HB_OBJECT_DO_INIT (fcallbacks);
 
   return fcallbacks;
 }
@@ -110,12 +108,9 @@ hb_unicode_callbacks_create (void)
 {
   hb_unicode_callbacks_t *ucallbacks;
 
-  ucallbacks = calloc (1, sizeof (hb_unicode_callbacks_t));
-  if (!ucallbacks)
+  if (!HB_OBJECT_DO_CREATE (ucallbacks))
     return &_hb_unicode_callbacks_nil;
 
-  HB_OBJECT_DO_CREATE (ucallbacks);
-
   return ucallbacks;
 }
 
@@ -138,12 +133,11 @@ hb_unicode_callbacks_duplicate (hb_unicode_callbacks_t *other_ucallbacks)
 {
   hb_unicode_callbacks_t *ucallbacks;
 
-  ucallbacks = calloc (1, sizeof (hb_unicode_callbacks_t));
-  if (!ucallbacks)
+  if (!HB_OBJECT_DO_CREATE (ucallbacks))
     return &_hb_unicode_callbacks_nil;
 
   *ucallbacks = *other_ucallbacks;
-  HB_OBJECT_DO_CREATE (ucallbacks);
+  HB_OBJECT_DO_INIT (ucallbacks);
 
   return ucallbacks;
 }
@@ -174,12 +168,9 @@ hb_face_create_for_data (hb_blob_t    *blob,
 {
   hb_face_t *face;
 
-  face = calloc (1, sizeof (hb_face_t));
-  if (!face)
+  if (!HB_OBJECT_DO_CREATE (face))
     return &_hb_face_nil;
 
-  HB_OBJECT_DO_CREATE (face);
-
   face->blob = hb_blob_reference (blob);
   face->index = index;
 
@@ -193,15 +184,12 @@ hb_face_create_for_tables (hb_get_table_func_t  get_table,
 {
   hb_face_t *face;
 
-  face = calloc (1, sizeof (hb_face_t));
-  if (!face) {
+  if (!HB_OBJECT_DO_CREATE (face)) {
     if (destroy)
       destroy (user_data);
     return &_hb_face_nil;
   }
 
-  HB_OBJECT_DO_CREATE (face);
-
   face->get_table = get_table;
   face->destroy = destroy;
   face->user_data = user_data;
@@ -278,12 +266,9 @@ hb_font_create (hb_face_t *face)
 {
   hb_font_t *font;
 
-  font = calloc (1, sizeof (hb_font_t));
-  if (!font)
+  if (!HB_OBJECT_DO_CREATE (font))
     return &_hb_font_nil;
 
-  HB_OBJECT_DO_CREATE (font);
-
   font->face = hb_face_reference (face);
 
   return font;
diff --git a/src/hb-object-private.h b/src/hb-object-private.h
new file mode 100644 (file)
index 0000000..541bad9
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2007 Chris Wilson
+ * Copyright (C) 2009  Red Hat, Inc.
+ *
+ *  This is part of HarfBuzz, an OpenType Layout engine library.
+ *
+ * Permission is hereby granted, without written agreement and without
+ * license or royalty fees, to use, copy, modify, and distribute this
+ * software and its documentation for any purpose, provided that the
+ * above copyright notice and the following two paragraphs appear in
+ * all copies of this software.
+ *
+ * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
+ * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
+ * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
+ * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ *
+ * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
+ * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
+ * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
+ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ *
+ * Contributor(s):
+ *     Chris Wilson <chris@chris-wilson.co.uk>
+ * Red Hat Author(s): Behdad Esfahbod
+ */
+
+#ifndef HB_REFCOUNT_PRIVATE_H
+#define HB_REFCOUNT_PRIVATE_H
+
+typedef int hb_atomic_int_t;
+
+/* Encapsulate operations on the object's reference count */
+typedef struct {
+  hb_atomic_int_t ref_count;
+} hb_reference_count_t;
+
+/* XXX add real atomic ops */
+#define _hb_reference_count_inc(RC) ((RC).ref_count++)
+#define _hb_reference_count_dec_and_test(RC) ((RC).ref_count-- == 1)
+
+#define HB_REFERENCE_COUNT_INIT(RC, VALUE) ((RC).ref_count = (VALUE))
+
+#define HB_REFERENCE_COUNT_GET_VALUE(RC) ((RC).ref_count+0)
+#define HB_REFERENCE_COUNT_SET_VALUE(RC, VALUE) ((RC).ref_count = (VALUE), 0)
+
+#define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1)
+#define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE}
+
+#define HB_REFERENCE_COUNT_IS_INVALID(RC) (HB_REFERENCE_COUNT_GET_VALUE (RC) == HB_REFERENCE_COUNT_INVALID_VALUE)
+
+#define HB_REFERENCE_COUNT_HAS_REFERENCE(RC) (HB_REFERENCE_COUNT_GET_VALUE (RC) > 0)
+
+
+
+/* Helper macros */
+
+#define HB_OBJECT_IS_INERT(obj) \
+    ((obj) == NULL || HB_REFERENCE_COUNT_IS_INVALID ((obj)->ref_count))
+
+#define HB_OBJECT_DO_INIT_EXPR(obj) \
+    HB_REFERENCE_COUNT_INIT (obj->ref_count, 1)
+
+#define HB_OBJECT_DO_INIT(obj) \
+  HB_STMT_START { \
+    HB_OBJECT_DO_INIT_EXPR (obj); \
+  } HB_STMT_END
+
+#define HB_OBJECT_DO_CREATE(obj) \
+  HB_LIKELY (( \
+            (obj) = calloc (1, sizeof (*(obj))), \
+            HB_OBJECT_DO_INIT_EXPR (obj), \
+            (obj) \
+            ))
+
+#define HB_OBJECT_DO_REFERENCE(obj) \
+  HB_STMT_START { \
+    if (HB_OBJECT_IS_INERT (obj)) \
+      return obj; \
+    assert (HB_REFERENCE_COUNT_HAS_REFERENCE (obj->ref_count)); \
+    _hb_reference_count_inc (obj->ref_count); \
+    return obj; \
+  } HB_STMT_END
+
+#define HB_OBJECT_DO_DESTROY(obj) \
+  HB_STMT_START { \
+    if (HB_OBJECT_IS_INERT (obj)) \
+      return; \
+    assert (HB_REFERENCE_COUNT_HAS_REFERENCE (obj->ref_count)); \
+    if (!_hb_reference_count_dec_and_test (obj->ref_count)) \
+      return; \
+  } HB_STMT_END
+
+
+#endif /* HB_REFCOUNT_PRIVATE_H */
index 2f482db9b90556b17e6ffcce99733c7881a2594d..001aa09282cef6d39ea818706066bdbf9069c2da 100644 (file)
@@ -104,4 +104,6 @@ _hb_popcount32 (uint32_t mask)
 #endif
 }
 
+#include "hb-object-private.h"
+
 #endif /* HB_PRIVATE_H */
diff --git a/src/hb-refcount-private.h b/src/hb-refcount-private.h
deleted file mode 100644 (file)
index c9b350b..0000000
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Copyright (C) 2007 Chris Wilson
- * Copyright (C) 2009  Red Hat, Inc.
- *
- *  This is part of HarfBuzz, an OpenType Layout engine library.
- *
- * Permission is hereby granted, without written agreement and without
- * license or royalty fees, to use, copy, modify, and distribute this
- * software and its documentation for any purpose, provided that the
- * above copyright notice and the following two paragraphs appear in
- * all copies of this software.
- *
- * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
- * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
- * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
- * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- *
- * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
- * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
- * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
- * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
- *
- * Contributor(s):
- *     Chris Wilson <chris@chris-wilson.co.uk>
- * Red Hat Author(s): Behdad Esfahbod
- */
-
-#ifndef HB_REFCOUNT_PRIVATE_H
-#define HB_REFCOUNT_PRIVATE_H
-
-typedef int hb_atomic_int_t;
-
-/* Encapsulate operations on the object's reference count */
-typedef struct {
-  hb_atomic_int_t ref_count;
-} hb_reference_count_t;
-
-/* XXX add real atomic ops */
-#define _hb_reference_count_inc(RC) ((RC).ref_count++)
-#define _hb_reference_count_dec_and_test(RC) ((RC).ref_count-- == 1)
-
-#define HB_REFERENCE_COUNT_INIT(RC, VALUE) ((RC).ref_count = (VALUE))
-
-#define HB_REFERENCE_COUNT_GET_VALUE(RC) ((RC).ref_count+0)
-#define HB_REFERENCE_COUNT_SET_VALUE(RC, VALUE) ((RC).ref_count = (VALUE), 0)
-
-#define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1)
-#define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE}
-
-#define HB_REFERENCE_COUNT_IS_INVALID(RC) (HB_REFERENCE_COUNT_GET_VALUE (RC) == HB_REFERENCE_COUNT_INVALID_VALUE)
-
-#define HB_REFERENCE_COUNT_HAS_REFERENCE(RC) (HB_REFERENCE_COUNT_GET_VALUE (RC) > 0)
-
-
-
-/* Helper macros */
-
-#define HB_OBJECT_IS_INERT(obj) \
-    ((obj) == NULL || HB_REFERENCE_COUNT_IS_INVALID ((obj)->ref_count))
-
-#define HB_OBJECT_DO_CREATE(obj) \
-  HB_STMT_START { \
-    HB_REFERENCE_COUNT_INIT (obj->ref_count, 1); \
-  } HB_STMT_END
-
-#define HB_OBJECT_DO_REFERENCE(obj) \
-  HB_STMT_START { \
-    if (HB_OBJECT_IS_INERT (obj)) \
-      return obj; \
-    assert (HB_REFERENCE_COUNT_HAS_REFERENCE (obj->ref_count)); \
-    _hb_reference_count_inc (obj->ref_count); \
-    return obj; \
-  } HB_STMT_END
-
-#define HB_OBJECT_DO_DESTROY(obj) \
-  HB_STMT_START { \
-    if (HB_OBJECT_IS_INERT (obj)) \
-      return; \
-    assert (HB_REFERENCE_COUNT_HAS_REFERENCE (obj->ref_count)); \
-    if (!_hb_reference_count_dec_and_test (obj->ref_count)) \
-      return; \
-  } HB_STMT_END
-
-
-#endif /* HB_REFCOUNT_PRIVATE_H */