1 #include <unistd.h>
2 #include <glib.h>
3 #include <glib-object.h>
4
5 #define G_TYPE_TEST (my_test_get_type ())
6 #define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
7 #define MY_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
8 #define MY_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
9 #define MY_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
10 #define MY_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
11
12 typedef struct _GTest GTest;
13 typedef struct _GTestClass GTestClass;
14
15 struct _GTest
16 {
17 GObject object;
18 };
19
20 struct _GTestClass
21 {
22 GObjectClass parent_class;
23 };
24
25 static GType my_test_get_type (void);
26
27 static void my_test_class_init (GTestClass * klass);
28 static void my_test_init (GTest * test);
29 static void my_test_dispose (GObject * object);
30
31 static GObjectClass *parent_class = NULL;
32
33 static GType
my_test_get_type(void)34 my_test_get_type (void)
35 {
36 static GType test_type = 0;
37
38 if (!test_type) {
39 static const GTypeInfo test_info = {
40 sizeof (GTestClass),
41 NULL,
42 NULL,
43 (GClassInitFunc) my_test_class_init,
44 NULL,
45 NULL,
46 sizeof (GTest),
47 0,
48 (GInstanceInitFunc) my_test_init,
49 NULL
50 };
51
52 test_type = g_type_register_static (G_TYPE_OBJECT, "GTest",
53 &test_info, 0);
54 }
55 return test_type;
56 }
57
58 static void
my_test_class_init(GTestClass * klass)59 my_test_class_init (GTestClass * klass)
60 {
61 GObjectClass *gobject_class;
62
63 gobject_class = (GObjectClass *) klass;
64
65 parent_class = g_type_class_ref (G_TYPE_OBJECT);
66
67 gobject_class->dispose = my_test_dispose;
68 }
69
70 static void
my_test_init(GTest * test)71 my_test_init (GTest * test)
72 {
73 g_print ("init %p\n", test);
74 }
75
76 static void
my_test_dispose(GObject * object)77 my_test_dispose (GObject * object)
78 {
79 GTest *test;
80
81 test = MY_TEST (object);
82
83 g_print ("dispose %p!\n", object);
84
85 G_OBJECT_CLASS (parent_class)->dispose (object);
86 }
87
88 static void
my_test_do_refcount(GTest * test)89 my_test_do_refcount (GTest * test)
90 {
91 static guint i = 1;
92 if (i++ % 100000 == 0)
93 g_print (".");
94 g_object_ref (test);
95 g_object_unref (test);
96 }
97
98 int
main(int argc,char ** argv)99 main (int argc, char **argv)
100 {
101 gint i;
102 GTest *test;
103
104 g_thread_init (NULL);
105 g_print ("START: %s\n", argv[0]);
106 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
107 g_type_init ();
108
109 test = g_object_new (G_TYPE_TEST, NULL);
110
111 for (i=0; i<100000000; i++) {
112 my_test_do_refcount (test);
113 }
114
115 g_print ("\n");
116
117 return 0;
118 }
119