Fix problem where if g_mem_chunk_reset() is called on an alloc-only
[glib.git] / gobject / gobjectnotifyqueue.c
blob03db5c2c7a0a6362d777bcd9e9c91a8790f760cc
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
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.
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.
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.
19 #ifndef __G_NOTIFY_H__
20 #define __G_NOTIFY_H__
22 #include <string.h> /* memset */
23 #include <glib-object.h>
25 G_BEGIN_DECLS
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);
36 /* --- structures --- */
37 struct _GObjectNotifyContext
39 GQuark quark_notify_queue;
40 GObjectNotifyQueueDispatcher dispatcher;
41 GTrashStack *_nqueue_trash; /* unused */
43 struct _GObjectNotifyQueue
45 GObjectNotifyContext *context;
46 GSList *pspecs;
47 guint16 n_pspecs;
48 guint16 freeze_count;
49 /* currently, this structure abuses the GList allocation chain and thus
50 * must be <= sizeof (GList)
55 /* --- functions --- */
56 static void
57 g_object_notify_queue_free (gpointer data)
59 GObjectNotifyQueue *nqueue = data;
61 g_slist_free (nqueue->pspecs);
62 g_list_free_1 ((void*) nqueue);
65 static inline GObjectNotifyQueue*
66 g_object_notify_queue_freeze (GObject *object,
67 GObjectNotifyContext *context)
69 GObjectNotifyQueue *nqueue;
71 nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
72 if (!nqueue)
74 nqueue = (void*) g_list_alloc ();
75 memset (nqueue, 0, sizeof (*nqueue));
76 nqueue->context = context;
77 g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
78 nqueue, g_object_notify_queue_free);
81 g_return_val_if_fail (nqueue->freeze_count < 65535, nqueue);
82 nqueue->freeze_count++;
84 return nqueue;
87 static inline void
88 g_object_notify_queue_thaw (GObject *object,
89 GObjectNotifyQueue *nqueue)
91 GObjectNotifyContext *context = nqueue->context;
92 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
93 GSList *slist;
94 guint n_pspecs = 0;
96 g_return_if_fail (nqueue->freeze_count > 0);
98 nqueue->freeze_count--;
99 if (nqueue->freeze_count)
100 return;
101 g_return_if_fail (object->ref_count > 0);
103 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
104 /* set first entry to NULL since it's checked unconditionally */
105 pspecs[0] = NULL;
106 for (slist = nqueue->pspecs; slist; slist = slist->next)
108 GParamSpec *pspec = slist->data;
109 gint i = 0;
111 /* dedup, make pspecs in the list unique */
112 redo_dedup_check:
113 if (pspecs[i] == pspec)
114 continue;
115 if (++i < n_pspecs)
116 goto redo_dedup_check;
118 pspecs[n_pspecs++] = pspec;
120 g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
122 if (n_pspecs)
123 context->dispatcher (object, n_pspecs, pspecs);
124 g_free (free_me);
127 static inline void
128 g_object_notify_queue_clear (GObject *object,
129 GObjectNotifyQueue *nqueue)
131 g_return_if_fail (nqueue->freeze_count > 0);
133 g_slist_free (nqueue->pspecs);
134 nqueue->pspecs = NULL;
135 nqueue->n_pspecs = 0;
138 static inline void
139 g_object_notify_queue_add (GObject *object,
140 GObjectNotifyQueue *nqueue,
141 GParamSpec *pspec)
143 if (pspec->flags & G_PARAM_READABLE)
145 g_return_if_fail (nqueue->n_pspecs < 65535);
147 /* we do the deduping in _thaw */
148 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
149 nqueue->n_pspecs++;
153 static inline GObjectNotifyQueue*
154 g_object_notify_queue_from_object (GObject *object,
155 GObjectNotifyContext *context)
157 return g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
161 G_END_DECLS
163 #endif /* __G_OBJECT_H__ */