📄 gclosure.c
字号:
/* GObject - GLib Type, Object, Parameter and Signal Library * Copyright (C) 2000-2001 Red Hat, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */#include "gclosure.h"#include "gvalue.h"#include <string.h>/* FIXME: need caching allocators */#define CLOSURE_MAX_REF_COUNT ((1 << 15) - 1)#define CLOSURE_MAX_N_GUARDS ((1 << 1) - 1)#define CLOSURE_MAX_N_FNOTIFIERS ((1 << 2) - 1)#define CLOSURE_MAX_N_INOTIFIERS ((1 << 8) - 1)#define CLOSURE_N_MFUNCS(cl) ((cl)->meta_marshal + \ ((cl)->n_guards << 1L))/* same as G_CLOSURE_N_NOTIFIERS() (keep in sync) */#define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \ (cl)->n_fnotifiers + \ (cl)->n_inotifiers)enum { FNOTIFY, INOTIFY, PRE_NOTIFY, POST_NOTIFY};/* --- functions --- */GClosure*g_closure_new_simple (guint sizeof_closure, gpointer data){ GClosure *closure; g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL); closure = g_malloc (sizeof_closure); closure->ref_count = 1; closure->meta_marshal = 0; closure->n_guards = 0; closure->n_fnotifiers = 0; closure->n_inotifiers = 0; closure->in_inotify = FALSE; closure->floating = TRUE; closure->derivative_flag = 0; closure->in_marshal = FALSE; closure->is_invalid = FALSE; closure->marshal = NULL; closure->data = data; closure->notifiers = NULL; memset (G_STRUCT_MEMBER_P (closure, sizeof (*closure)), 0, sizeof_closure - sizeof (*closure)); return closure;}static inline voidclosure_invoke_notifiers (GClosure *closure, guint notify_type){ /* notifier layout: * meta_marshal n_guards n_guards n_fnotif. n_inotifiers * ->[[meta_marshal][pre_guards][post_guards][fnotifiers][inotifiers]] * * CLOSURE_N_MFUNCS(cl) = meta_marshal + n_guards + n_guards; * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers * * constrains/catches: * - closure->notifiers may be reloacted during callback * - closure->n_fnotifiers and closure->n_inotifiers may change during callback * - i.e. callbacks can be removed/added during invocation * - have to prepare for callback removal during invocation (->marshal & ->data) * - have to distinguish (->marshal & ->data) for INOTIFY/FNOTIFY (->in_inotify) * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY * + closure->meta_marshal is const for all cases * + none of the callbacks can cause recursion * + closure->n_inotifiers is const 0 during FNOTIFY */ switch (notify_type) { GClosureNotifyData *ndata; guint i, offs; case FNOTIFY: while (closure->n_fnotifiers) { register guint n = --closure->n_fnotifiers; ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n; closure->marshal = (GClosureMarshal) ndata->notify; closure->data = ndata->data; ndata->notify (ndata->data, closure); } closure->marshal = NULL; closure->data = NULL; break; case INOTIFY: closure->in_inotify = TRUE; while (closure->n_inotifiers) { register guint n = --closure->n_inotifiers; ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n; closure->marshal = (GClosureMarshal) ndata->notify; closure->data = ndata->data; ndata->notify (ndata->data, closure); } closure->marshal = NULL; closure->data = NULL; closure->in_inotify = FALSE; break; case PRE_NOTIFY: i = closure->n_guards; offs = closure->meta_marshal; while (i--) { ndata = closure->notifiers + offs + i; ndata->notify (ndata->data, closure); } break; case POST_NOTIFY: i = closure->n_guards; offs = closure->meta_marshal + i; while (i--) { ndata = closure->notifiers + offs + i; ndata->notify (ndata->data, closure); } break; }}voidg_closure_set_meta_marshal (GClosure *closure, gpointer marshal_data, GClosureMarshal meta_marshal){ GClosureNotifyData *notifiers; guint n; g_return_if_fail (closure != NULL); g_return_if_fail (meta_marshal != NULL); g_return_if_fail (closure->is_invalid == FALSE); g_return_if_fail (closure->in_marshal == FALSE); g_return_if_fail (closure->meta_marshal == 0); n = CLOSURE_N_NOTIFIERS (closure); notifiers = closure->notifiers; closure->notifiers = g_renew (GClosureNotifyData, NULL, CLOSURE_N_NOTIFIERS (closure) + 1); if (notifiers) { /* usually the meta marshal will be setup right after creation, so the * g_memmove() should be rare-case scenario */ g_memmove (closure->notifiers + 1, notifiers, CLOSURE_N_NOTIFIERS (closure) * sizeof (notifiers[0])); g_free (notifiers); } closure->notifiers[0].data = marshal_data; closure->notifiers[0].notify = (GClosureNotify) meta_marshal; closure->meta_marshal = 1;}voidg_closure_add_marshal_guards (GClosure *closure, gpointer pre_marshal_data, GClosureNotify pre_marshal_notify, gpointer post_marshal_data, GClosureNotify post_marshal_notify){ guint i; g_return_if_fail (closure != NULL); g_return_if_fail (pre_marshal_notify != NULL); g_return_if_fail (post_marshal_notify != NULL); g_return_if_fail (closure->is_invalid == FALSE); g_return_if_fail (closure->in_marshal == FALSE); g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS); closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2); if (closure->n_inotifiers) closure->notifiers[(CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + 0)]; if (closure->n_inotifiers > 1) closure->notifiers[(CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + 1)]; if (closure->n_fnotifiers) closure->notifiers[(CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0]; if (closure->n_fnotifiers > 1) closure->notifiers[(CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1]; if (closure->n_guards) closure->notifiers[(closure->meta_marshal + closure->n_guards + closure->n_guards + 1)] = closure->notifiers[closure->meta_marshal + closure->n_guards]; i = closure->n_guards++; closure->notifiers[closure->meta_marshal + i].data = pre_marshal_data; closure->notifiers[closure->meta_marshal + i].notify = pre_marshal_notify; closure->notifiers[closure->meta_marshal + i + 1].data = post_marshal_data; closure->notifiers[closure->meta_marshal + i + 1].notify = post_marshal_notify;}voidg_closure_add_finalize_notifier (GClosure *closure, gpointer notify_data, GClosureNotify notify_func){ guint i; g_return_if_fail (closure != NULL); g_return_if_fail (notify_func != NULL); g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS); closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1); if (closure->n_inotifiers) closure->notifiers[(CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + 0)]; i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers++; closure->notifiers[i].data = notify_data; closure->notifiers[i].notify = notify_func;}voidg_closure_add_invalidate_notifier (GClosure *closure, gpointer notify_data, GClosureNotify notify_func){ guint i; g_return_if_fail (closure != NULL); g_return_if_fail (notify_func != NULL); g_return_if_fail (closure->is_invalid == FALSE); g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS); closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1); i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers++; closure->notifiers[i].data = notify_data; closure->notifiers[i].notify = notify_func;}static inline gbooleanclosure_try_remove_inotify (GClosure *closure, gpointer notify_data, GClosureNotify notify_func){ GClosureNotifyData *ndata, *nlast; nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1; for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++) if (ndata->notify == notify_func && ndata->data == notify_data) { closure->n_inotifiers -= 1; if (ndata < nlast) *ndata = *nlast; return TRUE; } return FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -