[pango/harfbuzz-ng: 14/57] [HB] Simplify object creation



commit 308259613a34f54151c20d616cd5c940d69de980
Author: Behdad Esfahbod <behdad behdad org>
Date:   Sat Aug 1 20:29:22 2009 -0400

    [HB] Simplify object creation

 pango/opentype/Makefile.am                         |    2 +-
 pango/opentype/hb-blob.c                           |    6 +---
 pango/opentype/hb-font-private.h                   |    1 -
 pango/opentype/hb-font.c                           |   37 ++++++--------------
 .../{hb-refcount-private.h => hb-object-private.h} |   14 ++++++-
 pango/opentype/hb-private.h                        |    2 +
 6 files changed, 27 insertions(+), 35 deletions(-)
---
diff --git a/pango/opentype/Makefile.am b/pango/opentype/Makefile.am
index c33c019..f941046 100644
--- a/pango/opentype/Makefile.am
+++ b/pango/opentype/Makefile.am
@@ -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 = \
diff --git a/pango/opentype/hb-blob.c b/pango/opentype/hb-blob.c
index 8edb221..512bec9 100644
--- a/pango/opentype/hb-blob.c
+++ b/pango/opentype/hb-blob.c
@@ -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;
diff --git a/pango/opentype/hb-font-private.h b/pango/opentype/hb-font-private.h
index 7c35406..8e0fe72 100644
--- a/pango/opentype/hb-font-private.h
+++ b/pango/opentype/hb-font-private.h
@@ -30,7 +30,6 @@
 #include "hb-private.h"
 
 #include "hb-font.h"
-#include "hb-refcount-private.h"
 
 HB_BEGIN_DECLS
 
diff --git a/pango/opentype/hb-font.c b/pango/opentype/hb-font.c
index 0d0d620..b2cf440 100644
--- a/pango/opentype/hb-font.c
+++ b/pango/opentype/hb-font.c
@@ -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/pango/opentype/hb-refcount-private.h b/pango/opentype/hb-object-private.h
similarity index 91%
rename from pango/opentype/hb-refcount-private.h
rename to pango/opentype/hb-object-private.h
index c9b350b..541bad9 100644
--- a/pango/opentype/hb-refcount-private.h
+++ b/pango/opentype/hb-object-private.h
@@ -60,11 +60,21 @@ typedef struct {
 #define HB_OBJECT_IS_INERT(obj) \
     ((obj) == NULL || HB_REFERENCE_COUNT_IS_INVALID ((obj)->ref_count))
 
-#define HB_OBJECT_DO_CREATE(obj) \
+#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_REFERENCE_COUNT_INIT (obj->ref_count, 1); \
+    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)) \
diff --git a/pango/opentype/hb-private.h b/pango/opentype/hb-private.h
index 2f482db..001aa09 100644
--- a/pango/opentype/hb-private.h
+++ b/pango/opentype/hb-private.h
@@ -104,4 +104,6 @@ _hb_popcount32 (uint32_t mask)
 #endif
 }
 
+#include "hb-object-private.h"
+
 #endif /* HB_PRIVATE_H */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]