[Libsysio-commit] HEAD: libsysio/src init.c
Brought to you by:
lward
From: Lee W. <lw...@us...> - 2006-02-27 17:26:58
|
Update of /cvsroot/libsysio/libsysio/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21374/src Modified Files: init.c Log Message: Tracing now can reference user-supplied data. The callbacks are supplied the data pointer originally given at registration. A destructor, given at registration, is called with a copy of the user-supplied data pointer when the the trace remove function is invoked if it wasn't NULL. Index: init.c =================================================================== RCS file: /cvsroot/libsysio/libsysio/src/init.c,v retrieving revision 1.25 retrieving revision 1.26 diff -u -w -b -B -p -r1.25 -r1.26 --- init.c 18 Jan 2006 00:47:41 -0000 1.25 +++ init.c 27 Feb 2006 17:26:55 -0000 1.26 @@ -85,16 +85,23 @@ * Tracing callback record. */ struct trace_callback { - TAILQ_ENTRY(trace_callback) links; - void (*f)(const char *file, const char *func, int line); + TAILQ_ENTRY(trace_callback) links; /* trace list links */ + void (*f)(const char *file, /* callback function */ + const char *func, + int line, + void *data); + void *data; /* callback data */ + void (*destructor)(void *data); /* data destructor */ }; /* * Initialize a tracing callback record. */ -#define TCB_INIT(__tcb, __f) \ +#define TCB_INIT(__tcb, __f, __d, __destroy) \ do { \ (__tcb)->f = (__f); \ + (__tcb)->data = (__d); \ + (__tcb)->destructor = (__destroy); \ } while (0); /* @@ -210,14 +217,10 @@ _sysio_shutdown() /* * Empty the trace queues and free the entries. */ - while ((tcb = _sysio_entry_trace_head.tqh_first) != NULL) { - TAILQ_REMOVE(&_sysio_entry_trace_head, tcb, links); - free(tcb); - } - while ((tcb = _sysio_exit_trace_head.tqh_first) != NULL) { - TAILQ_REMOVE(&_sysio_exit_trace_head, tcb, links); - free(tcb); - } + while ((tcb = _sysio_entry_trace_head.tqh_first) != NULL) + _sysio_remove_trace(&_sysio_entry_trace_head, tcb); + while ((tcb = _sysio_exit_trace_head.tqh_first) != NULL) + _sysio_remove_trace(&_sysio_exit_trace_head, tcb); } #endif #endif @@ -316,14 +319,17 @@ void * _sysio_register_trace(void *q, void (*f)(const char *file, const char *func, - int line)) + int line, + void *data), + void *data, + void (*destructor)(void *data)) { struct trace_callback *tcb; tcb = malloc(sizeof(struct trace_callback)); if (!tcb) return NULL; - TCB_INIT(tcb, f); + TCB_INIT(tcb, f, data, destructor); TAILQ_INSERT_TAIL((struct trace_q *)q, tcb, links); return tcb; } @@ -334,9 +340,14 @@ _sysio_register_trace(void *q, void _sysio_remove_trace(void *q, void *p) { + struct trace_callback *tcb; - TAILQ_REMOVE((struct trace_q *)q, (struct trace_callback *)p, links); - free(p); + tcb = (struct trace_callback *)p; + + if (tcb->destructor) + (*tcb->destructor)(tcb->data); + TAILQ_REMOVE((struct trace_q *)q, tcb, links); + free(tcb); } void @@ -352,7 +363,7 @@ _sysio_run_trace_q(void *q, tcb = ((struct trace_q *)q)->tqh_first; while (tcb) { - (*tcb->f)(file, func, line); + (*tcb->f)(file, func, line, tcb->data); tcb = tcb->links.tqe_next; } } @@ -360,7 +371,8 @@ _sysio_run_trace_q(void *q, static void _sysio_trace_entry(const char *file __IS_UNUSED, const char *func, - int line __IS_UNUSED) + int line __IS_UNUSED, + void *data __IS_UNUSED) { _sysio_cprintf("+ENTER+ %s\n", func); @@ -369,7 +381,8 @@ _sysio_trace_entry(const char *file __IS static void _sysio_trace_exit(const char *file __IS_UNUSED, const char *func, - int line __IS_UNUSED) + int line __IS_UNUSED, + void *data __IS_UNUSED) { _sysio_cprintf("+EXIT+ %s\n", func); @@ -917,13 +930,17 @@ _sysio_boot_tracing(const char *arg) if (entcb == NULL) entcb = _sysio_register_trace(_sysio_entry_trace_q, - _sysio_trace_entry); + _sysio_trace_entry, + NULL, + NULL); if (entcb == NULL) return -errno; if (exitcb == NULL) exitcb = _sysio_register_trace(_sysio_exit_trace_q, - _sysio_trace_exit); + _sysio_trace_exit, + NULL, + NULL); if (exitcb == NULL) return -errno; } else { |