fix unconditional check of first GParamSpec in
[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 #ifndef __G_NOTIFY_H__
20 #define __G_NOTIFY_H__
21
22 #include        <string.h> /* memset */
23 #include        <gobject/gobject.h>
24
25 G_BEGIN_DECLS
26
27
28 /* --- typedefs --- */
29 typedef struct _GObjectNotifyContext          GObjectNotifyContext;
30 typedef struct _GObjectNotifyQueue            GObjectNotifyQueue;
31 typedef void (*GObjectNotifyQueueDispatcher) (GObject     *object,
32                                               guint        n_pspecs,
33                                               GParamSpec **pspecs);
34
35
36 /* --- structures --- */
37 struct _GObjectNotifyContext
38 {
39   GQuark                       quark_notify_queue;
40   GObjectNotifyQueueDispatcher dispatcher;
41   GTrashStack                 *nqueue_trash;
42 };
43 struct _GObjectNotifyQueue
44 {
45   GObjectNotifyContext *context;
46   GSList               *pspecs;
47   guint                 n_pspecs;
48   guint                 freeze_count;
49 };
50
51
52 /* --- functions --- */
53 static void
54 g_object_notify_queue_free (gpointer data)
55 {
56   GObjectNotifyQueue *nqueue = data;
57
58   g_slist_free (nqueue->pspecs);
59   g_trash_stack_push (&nqueue->context->nqueue_trash, nqueue);
60 }
61
62 static inline GObjectNotifyQueue*
63 g_object_notify_queue_freeze (GObject              *object,
64                               GObjectNotifyContext *context)
65 {
66   GObjectNotifyQueue *nqueue;
67
68   nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
69   if (!nqueue)
70     {
71       nqueue = g_trash_stack_pop (&context->nqueue_trash);
72       if (!nqueue)
73         {
74           guint i;
75
76           nqueue = g_new (GObjectNotifyQueue, 16);
77           for (i = 0; i < 15; i++)
78             g_trash_stack_push (&context->nqueue_trash, nqueue++);
79         }
80       memset (nqueue, 0, sizeof (*nqueue));
81       nqueue->context = context;
82       g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
83                                    nqueue, g_object_notify_queue_free);
84     }
85   nqueue->freeze_count++;
86
87   return nqueue;
88 }
89
90 static inline void
91 g_object_notify_queue_thaw (GObject            *object,
92                             GObjectNotifyQueue *nqueue)
93 {
94   GObjectNotifyContext *context = nqueue->context;
95   GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
96   GSList *slist;
97   guint n_pspecs = 0;
98
99   g_return_if_fail (nqueue->freeze_count > 0);
100
101   nqueue->freeze_count--;
102   if (nqueue->freeze_count)
103     return;
104   g_return_if_fail (object->ref_count > 0);
105
106   pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
107   /* set first entry to NULL since it's checked unconditionally */
108   pspecs[0] = NULL;
109   for (slist = nqueue->pspecs; slist; slist = slist->next)
110     {
111       GParamSpec *pspec = slist->data;
112       gint i = 0;
113
114       /* dedup, make pspecs in the list unique */
115     redo_dedup_check:
116       if (pspecs[i] == pspec)
117         continue;
118       if (++i < n_pspecs)
119         goto redo_dedup_check;
120
121       pspecs[n_pspecs++] = pspec;
122     }
123   g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
124
125   if (n_pspecs)
126     context->dispatcher (object, n_pspecs, pspecs);
127   g_free (free_me);
128 }
129
130 static inline void
131 g_object_notify_queue_clear (GObject            *object,
132                              GObjectNotifyQueue *nqueue)
133 {
134   g_return_if_fail (nqueue->freeze_count > 0);
135
136   g_slist_free (nqueue->pspecs);
137   nqueue->pspecs = NULL;
138   nqueue->n_pspecs = 0;
139 }
140
141 static inline void
142 g_object_notify_queue_add (GObject            *object,
143                            GObjectNotifyQueue *nqueue,
144                            GParamSpec         *pspec)
145 {
146   if (pspec->flags & G_PARAM_READABLE)
147     {
148       /* we do the deduping in _thaw */
149       nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
150       nqueue->n_pspecs++;
151     }
152 }
153
154 static inline GObjectNotifyQueue*
155 g_object_notify_queue_from_object (GObject              *object,
156                                    GObjectNotifyContext *context)
157 {
158   return g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
159 }
160
161
162 G_END_DECLS
163
164 #endif /* __G_OBJECT_H__ */