notify: Remove g_object_notify_queue_from_object()
[platform/upstream/glib.git] / gobject / gobjectnotifyqueue.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifndef __G_OBJECT_NOTIFY_QUEUE_H__
21 #define __G_OBJECT_NOTIFY_QUEUE_H__
22
23 #include <string.h> /* memset */
24
25 #include <glib-object.h>
26
27 G_BEGIN_DECLS
28
29
30 /* --- typedefs --- */
31 typedef struct _GObjectNotifyContext          GObjectNotifyContext;
32 typedef struct _GObjectNotifyQueue            GObjectNotifyQueue;
33 typedef void (*GObjectNotifyQueueDispatcher) (GObject     *object,
34                                               guint        n_pspecs,
35                                               GParamSpec **pspecs);
36
37
38 /* --- structures --- */
39 struct _GObjectNotifyContext
40 {
41   GQuark                       quark_notify_queue;
42   GObjectNotifyQueueDispatcher dispatcher;
43   GTrashStack                 *_nqueue_trash; /* unused */
44 };
45 struct _GObjectNotifyQueue
46 {
47   GObjectNotifyContext *context;
48   GSList               *pspecs;
49   guint16               n_pspecs;
50   guint16               freeze_count;
51 };
52
53 /* --- functions --- */
54 static void
55 g_object_notify_queue_free (gpointer data)
56 {
57   GObjectNotifyQueue *nqueue = data;
58
59   g_slist_free (nqueue->pspecs);
60   g_slice_free (GObjectNotifyQueue, nqueue);
61 }
62
63 static inline GObjectNotifyQueue*
64 g_object_notify_queue_freeze (GObject              *object,
65                               GObjectNotifyContext *context)
66 {
67   GObjectNotifyQueue *nqueue;
68
69   nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
70   if (!nqueue)
71     {
72       nqueue = g_slice_new0 (GObjectNotifyQueue);
73       nqueue->context = context;
74       g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
75                                    nqueue, g_object_notify_queue_free);
76     }
77
78   if (nqueue->freeze_count >= 65535)
79     g_critical("Free queue for %s (%p) is larger than 65535,"
80                " called g_object_freeze_notify() too often."
81                " Forgot to call g_object_thaw_notify() or infinite loop",
82                G_OBJECT_TYPE_NAME (object), object);
83   else
84     nqueue->freeze_count++;
85
86   return nqueue;
87 }
88
89 static inline void
90 g_object_notify_queue_thaw (GObject            *object,
91                             GObjectNotifyQueue *nqueue)
92 {
93   GObjectNotifyContext *context = nqueue->context;
94   GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
95   GSList *slist;
96   guint n_pspecs = 0;
97
98   g_return_if_fail (nqueue->freeze_count > 0);
99   g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
100
101   /* Just make sure we never get into some nasty race condition */
102   if (G_UNLIKELY(nqueue->freeze_count == 0)) {
103     g_warning ("%s: property-changed notification for %s(%p) is not frozen",
104                G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
105     return;
106   }
107
108   nqueue->freeze_count--;
109   if (nqueue->freeze_count) {
110     return;
111   }
112
113   pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
114   /* set first entry to NULL since it's checked unconditionally */
115   pspecs[0] = NULL;
116   for (slist = nqueue->pspecs; slist; slist = slist->next)
117     {
118       GParamSpec *pspec = slist->data;
119       guint i = 0;
120
121       /* dedup, make pspecs in the list unique */
122     redo_dedup_check:
123       if (pspecs[i] == pspec)
124         continue;
125       if (++i < n_pspecs)
126         goto redo_dedup_check;
127
128       pspecs[n_pspecs++] = pspec;
129     }
130   g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
131
132   if (n_pspecs)
133     context->dispatcher (object, n_pspecs, pspecs);
134   g_free (free_me);
135 }
136
137 static inline void
138 g_object_notify_queue_add (GObject            *object,
139                            GObjectNotifyQueue *nqueue,
140                            GParamSpec         *pspec)
141 {
142   if (pspec->flags & G_PARAM_READABLE)
143     {
144       GParamSpec *redirect;
145
146       g_return_if_fail (nqueue->n_pspecs < 65535);
147
148       redirect = g_param_spec_get_redirect_target (pspec);
149       if (redirect)
150         pspec = redirect;
151             
152       /* we do the deduping in _thaw */
153       nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
154       nqueue->n_pspecs++;
155     }
156 }
157
158
159 G_END_DECLS
160
161 #endif /* __G_OBJECT_NOTIFY_QUEUE_H__ */