1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/*
* ggcov - A GTK frontend for exploring gcov coverage data
* Derived from ggui code
* Copyright (c) 2000-2003 Greg Banks <gnb@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "mvc.h"
CVSID("$Id: mvc.c,v 1.4 2010-05-09 05:37:15 gnb Exp $");
typedef struct mvc_listener_s mvc_listener_t;
typedef struct mvc_batch_s mvc_batch_t;
struct mvc_listener_s
{
mvc_listener_t *next; /* chain for same object */
unsigned int features; /* interested only in these features */
mvc_callback_t callback;
void *closure;
};
struct mvc_batch_s
{
unsigned int features;
};
static GHashTable *listeners;
static gboolean batching;
static GHashTable *batch;
static enum { NONE, LISTEN, UNLISTEN, CHANGED, UNBATCH } state = NONE;
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
void
mvc_listen(void *obj, unsigned int feat, mvc_callback_t proc, void *closure)
{
mvc_listener_t *ml;
// assert(state == NONE);
state = LISTEN;
ml = new(mvc_listener_t);
ml->features = feat;
ml->callback = proc;
ml->closure = closure;
if (listeners == 0)
{
listeners = g_hash_table_new(g_direct_hash, g_direct_equal);
assert(listeners != 0);
}
else
{
mvc_listener_t *chain = (mvc_listener_t *)g_hash_table_lookup(
listeners, obj);
if (chain != 0)
{
g_hash_table_remove(listeners, obj);
ml->next = chain;
}
}
g_hash_table_insert(listeners, obj, ml);
state = NONE;
}
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
static void
mvc_do_unlisten(
void *obj,
gboolean all,
unsigned int feat,
mvc_callback_t proc,
void *closure)
{
mvc_listener_t *ml, *head, *next, *prev, *orig_head;
// assert(state == NONE);
state = UNLISTEN;
orig_head = head = (mvc_listener_t *)g_hash_table_lookup(listeners, obj);
prev = 0;
for (ml = head ; ml != 0 ; prev = ml, ml = next)
{
next = ml->next;
if (all ||
(ml->callback == proc &&
ml->closure == closure))
{
ml->features &= ~feat;
if (all || ml->features == 0)
{
/* remove from chain */
if (prev == 0)
head = ml->next;
else
prev->next = ml->next;
g_free(ml);
}
}
}
if (head != orig_head)
{
g_hash_table_remove(listeners, obj);
g_hash_table_insert(listeners, obj, head);
}
state = NONE;
}
void
mvc_unlisten(void *obj, unsigned int feat, mvc_callback_t proc, void *closure)
{
mvc_do_unlisten(obj, /*all*/FALSE, feat, proc, closure);
}
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
static void
mvc_dispatch(void *obj, unsigned int features)
{
mvc_listener_t *ml;
if (listeners == 0)
return; /* no listeners on any object */
for (ml = (mvc_listener_t *)g_hash_table_lookup(listeners, obj) ;
ml != 0 ;
ml = ml->next)
{
if ((ml->features & features))
(*ml->callback)(obj, features, ml->closure);
}
}
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
void
mvc_changed(void *obj, unsigned int features)
{
// assert(state == NONE);
state = CHANGED;
if (batching)
{
mvc_batch_t *mb;
if ((mb = (mvc_batch_t *)g_hash_table_lookup(batch, obj)) == 0)
{
mb = new(mvc_batch_t);
mb->features = features;
g_hash_table_insert(batch, obj, mb);
}
else
{
mb->features |= features;
}
}
else
{
mvc_dispatch(obj, features);
}
state = NONE;
}
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
void
mvc_deleted(void *obj)
{
if (batching)
{
mvc_batch_t *mb;
if ((mb = (mvc_batch_t *)g_hash_table_lookup(batch, obj)) != 0)
{
fprintf(stderr, "mvc_deleted: object 0x%p deleted with pending 0x%x\n",
obj, mb->features);
g_hash_table_remove(batch, obj);
g_free(mb);
}
}
mvc_do_unlisten(obj, /*all*/TRUE, 0, 0, 0);
}
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
void
mvc_batch(void)
{
if (!batching)
{
if (batch == 0)
batch = g_hash_table_new(g_direct_hash, g_direct_equal);
batching = TRUE;
}
}
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
static gboolean
mvc_unbatch_1(gpointer key, gpointer value, gpointer user_data)
{
void *object = (void *)key;
mvc_batch_t *mb = (mvc_batch_t *)value;
mvc_dispatch(object, mb->features);
g_free(mb);
return TRUE; /* remove me */
}
void
mvc_unbatch(void)
{
// assert(state == NONE);
state = UNBATCH;
assert(batching);
batching = FALSE;
g_hash_table_foreach_remove(batch, mvc_unbatch_1, 0);
state = NONE;
}
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
/*END*/
|