Intern type names before registering the type to avoid unnecessary copies.
[platform/upstream/glib.git] / gobject / gvaluetypes.c
index dde820b..cb3de7e 100644 (file)
@@ -24,6 +24,7 @@
 #include       "gvaluetypes.h"
 
 #include       "gvaluecollector.h"
+#include       "gobjectalias.h"
 #include       <string.h>
 #include       <stdlib.h>      /* qsort() */
 
@@ -129,6 +130,46 @@ value_lcopy_long (const GValue *value,
 }
 
 static void
+value_init_int64 (GValue *value)
+{
+  value->data[0].v_int64 = 0;
+}
+
+static void
+value_copy_int64 (const GValue *src_value,
+                 GValue       *dest_value)
+{
+  dest_value->data[0].v_int64 = src_value->data[0].v_int64;
+}
+
+static gchar*
+value_collect_int64 (GValue      *value,
+                    guint        n_collect_values,
+                    GTypeCValue *collect_values,
+                    guint        collect_flags)
+{
+  value->data[0].v_int64 = collect_values[0].v_int64;
+  
+  return NULL;
+}
+
+static gchar*
+value_lcopy_int64 (const GValue *value,
+                  guint         n_collect_values,
+                  GTypeCValue  *collect_values,
+                  guint         collect_flags)
+{
+  gint64 *int64_p = collect_values[0].v_pointer;
+  
+  if (!int64_p)
+    return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
+  
+  *int64_p = value->data[0].v_int64;
+  
+  return NULL;
+}
+
+static void
 value_init_float (GValue *value)
 {
   value->data[0].v_float = 0.0;
@@ -317,7 +358,7 @@ value_lcopy_pointer (const GValue *value,
 
 /* --- type initialization --- */
 void
-g_value_types_init (void)  /* sync with gtype.c */
+g_value_types_init (void)
 {
   GTypeInfo info = {
     0,                         /* class_size */
@@ -348,9 +389,9 @@ g_value_types_init (void)  /* sync with gtype.c */
       value_lcopy_char,                /* lcopy_value */
     };
     info.value_table = &value_table;
-    type = g_type_register_fundamental (G_TYPE_CHAR, "gchar", &info, &finfo, 0);
+    type = g_type_register_fundamental (G_TYPE_CHAR, g_intern_static_string ("gchar"), &info, &finfo, 0);
     g_assert (type == G_TYPE_CHAR);
-    type = g_type_register_fundamental (G_TYPE_UCHAR, "guchar", &info, &finfo, 0);
+    type = g_type_register_fundamental (G_TYPE_UCHAR, g_intern_static_string ("guchar"), &info, &finfo, 0);
     g_assert (type == G_TYPE_UCHAR);
   }
 
@@ -368,7 +409,7 @@ g_value_types_init (void)  /* sync with gtype.c */
       value_lcopy_boolean,      /* lcopy_value */
     };
     info.value_table = &value_table;
-    type = g_type_register_fundamental (G_TYPE_BOOLEAN, "gboolean", &info, &finfo, 0);
+    type = g_type_register_fundamental (G_TYPE_BOOLEAN, g_intern_static_string ("gboolean"), &info, &finfo, 0);
     g_assert (type == G_TYPE_BOOLEAN);
   }
   
@@ -386,9 +427,9 @@ g_value_types_init (void)  /* sync with gtype.c */
       value_lcopy_int,         /* lcopy_value */
     };
     info.value_table = &value_table;
-    type = g_type_register_fundamental (G_TYPE_INT, "gint", &info, &finfo, 0);
+    type = g_type_register_fundamental (G_TYPE_INT, g_intern_static_string ("gint"), &info, &finfo, 0);
     g_assert (type == G_TYPE_INT);
-    type = g_type_register_fundamental (G_TYPE_UINT, "guint", &info, &finfo, 0);
+    type = g_type_register_fundamental (G_TYPE_UINT, g_intern_static_string ("guint"), &info, &finfo, 0);
     g_assert (type == G_TYPE_UINT);
   }
 
@@ -406,12 +447,32 @@ g_value_types_init (void)  /* sync with gtype.c */
       value_lcopy_long,                /* lcopy_value */
     };
     info.value_table = &value_table;
-    type = g_type_register_fundamental (G_TYPE_LONG, "glong", &info, &finfo, 0);
+    type = g_type_register_fundamental (G_TYPE_LONG, g_intern_static_string ("glong"), &info, &finfo, 0);
     g_assert (type == G_TYPE_LONG);
-    type = g_type_register_fundamental (G_TYPE_ULONG, "gulong", &info, &finfo, 0);
+    type = g_type_register_fundamental (G_TYPE_ULONG, g_intern_static_string ("gulong"), &info, &finfo, 0);
     g_assert (type == G_TYPE_ULONG);
   }
   
+  /* G_TYPE_INT64 / G_TYPE_UINT64
+   */
+  {
+    static const GTypeValueTable value_table = {
+      value_init_int64,                /* value_init */
+      NULL,                    /* value_free */
+      value_copy_int64,                /* value_copy */
+      NULL,                     /* value_peek_pointer */
+      "q",                     /* collect_format */
+      value_collect_int64,     /* collect_value */
+      "p",                     /* lcopy_format */
+      value_lcopy_int64,       /* lcopy_value */
+    };
+    info.value_table = &value_table;
+    type = g_type_register_fundamental (G_TYPE_INT64, g_intern_static_string ("gint64"), &info, &finfo, 0);
+    g_assert (type == G_TYPE_INT64);
+    type = g_type_register_fundamental (G_TYPE_UINT64, g_intern_static_string ("guint64"), &info, &finfo, 0);
+    g_assert (type == G_TYPE_UINT64);
+  }
+  
   /* G_TYPE_FLOAT
    */
   {
@@ -426,7 +487,7 @@ g_value_types_init (void)  /* sync with gtype.c */
       value_lcopy_float,        /* lcopy_value */
     };
     info.value_table = &value_table;
-    type = g_type_register_fundamental (G_TYPE_FLOAT, "gfloat", &info, &finfo, 0);
+    type = g_type_register_fundamental (G_TYPE_FLOAT, g_intern_static_string ("gfloat"), &info, &finfo, 0);
     g_assert (type == G_TYPE_FLOAT);
   }
   
@@ -444,7 +505,7 @@ g_value_types_init (void)  /* sync with gtype.c */
       value_lcopy_double,      /* lcopy_value */
     };
     info.value_table = &value_table;
-    type = g_type_register_fundamental (G_TYPE_DOUBLE, "gdouble", &info, &finfo, 0);
+    type = g_type_register_fundamental (G_TYPE_DOUBLE, g_intern_static_string ("gdouble"), &info, &finfo, 0);
     g_assert (type == G_TYPE_DOUBLE);
   }
 
@@ -462,7 +523,7 @@ g_value_types_init (void)  /* sync with gtype.c */
       value_lcopy_string,      /* lcopy_value */
     };
     info.value_table = &value_table;
-    type = g_type_register_fundamental (G_TYPE_STRING, "gchararray", &info, &finfo, 0);
+    type = g_type_register_fundamental (G_TYPE_STRING, g_intern_static_string ("gchararray"), &info, &finfo, 0);
     g_assert (type == G_TYPE_STRING);
   }
 
@@ -480,7 +541,7 @@ g_value_types_init (void)  /* sync with gtype.c */
       value_lcopy_pointer,     /* lcopy_value */
     };
     info.value_table = &value_table;
-    type = g_type_register_fundamental (G_TYPE_POINTER, "gpointer", &info, &finfo, 0);
+    type = g_type_register_fundamental (G_TYPE_POINTER, g_intern_static_string ("gpointer"), &info, &finfo, 0);
     g_assert (type == G_TYPE_POINTER);
   }
 }
@@ -607,6 +668,40 @@ g_value_get_ulong (const GValue *value)
 }
 
 void
+g_value_set_int64 (GValue *value,
+                  gint64  v_int64)
+{
+  g_return_if_fail (G_VALUE_HOLDS_INT64 (value));
+  
+  value->data[0].v_int64 = v_int64;
+}
+
+gint64
+g_value_get_int64 (const GValue *value)
+{
+  g_return_val_if_fail (G_VALUE_HOLDS_INT64 (value), 0);
+  
+  return value->data[0].v_int64;
+}
+
+void
+g_value_set_uint64 (GValue *value,
+                   guint64 v_uint64)
+{
+  g_return_if_fail (G_VALUE_HOLDS_UINT64 (value));
+  
+  value->data[0].v_uint64 = v_uint64;
+}
+
+guint64
+g_value_get_uint64 (const GValue *value)
+{
+  g_return_val_if_fail (G_VALUE_HOLDS_UINT64 (value), 0);
+  
+  return value->data[0].v_uint64;
+}
+
+void
 g_value_set_float (GValue *value,
                   gfloat  v_float)
 {
@@ -644,13 +739,18 @@ void
 g_value_set_string (GValue     *value,
                    const gchar *v_string)
 {
+  gchar *new_val;
+
   g_return_if_fail (G_VALUE_HOLDS_STRING (value));
-  
+
+  new_val = g_strdup (v_string);
+
   if (value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)
     value->data[1].v_uint = 0;
   else
     g_free (value->data[0].v_pointer);
-  value->data[0].v_pointer = g_strdup (v_string);
+
+  value->data[0].v_pointer = new_val;
 }
 
 void
@@ -669,6 +769,13 @@ void
 g_value_set_string_take_ownership (GValue *value,
                                   gchar  *v_string)
 {
+  g_value_take_string (value, v_string);
+}
+
+void
+g_value_take_string (GValue *value,
+                    gchar  *v_string)
+{
   g_return_if_fail (G_VALUE_HOLDS_STRING (value));
   
   if (value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)
@@ -711,7 +818,6 @@ g_value_get_pointer (const GValue *value)
   return value->data[0].v_pointer;
 }
 
-
 /* need extra includes for g_strdup_value_contents() ;( */
 #include "gobject.h"
 #include "gparam.h"
@@ -779,3 +885,31 @@ g_strdup_value_contents (const GValue *value)
 
   return contents;
 }
+
+GType
+g_pointer_type_register_static (const gchar *name)
+{
+  static const GTypeInfo type_info = {
+    0,                 /* class_size */
+    NULL,              /* base_init */
+    NULL,              /* base_finalize */
+    NULL,              /* class_init */
+    NULL,              /* class_finalize */
+    NULL,              /* class_data */
+    0,                 /* instance_size */
+    0,                 /* n_preallocs */
+    NULL,              /* instance_init */
+    NULL               /* value_table */
+  };
+  GType type;
+
+  g_return_val_if_fail (name != NULL, 0);
+  g_return_val_if_fail (g_type_from_name (name) == 0, 0);
+
+  type = g_type_register_static (G_TYPE_POINTER, name, &type_info, 0);
+
+  return type;
+}
+
+#define __G_VALUETYPES_C__
+#include "gobjectaliasdef.c"