X-Git-Url: http://git.liburcu.org/?a=blobdiff_plain;f=ltt%2Fbranches%2Fpoly%2Flttv%2Flttv%2Fhook.c;h=c7b43a50b956c5517538ef28b6a63dd4db04d8ce;hb=a5cc07d804005b34454e9b828dd9bc2cc88f936b;hp=f0e535d9e6823c9b83bd3971b72fc0dce4f8b61a;hpb=8436038a4e160b8916eee165b2a897d05d4bca2f;p=lttv.git diff --git a/ltt/branches/poly/lttv/lttv/hook.c b/ltt/branches/poly/lttv/lttv/hook.c index f0e535d9..c7b43a50 100644 --- a/ltt/branches/poly/lttv/lttv/hook.c +++ b/ltt/branches/poly/lttv/lttv/hook.c @@ -16,9 +16,13 @@ * MA 02111-1307, USA. */ +#ifdef HAVE_CONFIG_H +#include +#endif #include - +#include +#include typedef struct _LttvHookClosure { LttvHook hook; @@ -29,9 +33,10 @@ typedef struct _LttvHookClosure { gint lttv_hooks_prio_compare(LttvHookClosure *a, LttvHookClosure *b) { - if(a->prio < b->prio) return -1; - if(a->prio > b->prio) return 1; - return 0; + gint ret=0; + if(a->prio < b->prio) ret = -1; + else if(a->prio > b->prio) ret = 1; + return ret; } @@ -53,11 +58,12 @@ void lttv_hooks_add(LttvHooks *h, LttvHook f, void *hook_data, LttvHookPrio p) LttvHookClosure *c, new_c; guint i; - if(h == NULL)g_error("Null hook added"); + if(unlikely(h == NULL))g_error("Null hook added"); new_c.hook = f; new_c.hook_data = hook_data; new_c.prio = p; + new_c.ref_count = 1; /* Preliminary check for duplication */ /* only hook and hook data is checked */ @@ -98,7 +104,8 @@ void lttv_hooks_add_list(LttvHooks *h, const LttvHooks *list) LttvHookClosure *c; const LttvHookClosure *new_c; - if(list == NULL) return; + if(unlikely(list == NULL)) return; + for(i = 0, j = 0 ; i < list->len; i++) { new_c = &g_array_index(list, LttvHookClosure, i); gboolean found=FALSE; @@ -211,7 +218,7 @@ void lttv_hooks_remove_list(LttvHooks *h, LttvHooks *list) /* Normally the hooks in h are ordered as in list. If this is not the case, try harder here. */ - if(j < list->len) { + if(unlikely(j < list->len)) { for(; j < list->len ; j++) { c_list = &g_array_index(list, LttvHookClosure, j); lttv_hooks_remove_data(h, c_list->hook, c_list->hook_data); @@ -231,7 +238,7 @@ void lttv_hooks_get(LttvHooks *h, unsigned i, LttvHook *f, void **hook_data, { LttvHookClosure *c; - if(i >= h->len) + if(unlikely(i >= h->len)) { *f = NULL; *hook_data = NULL; @@ -259,7 +266,7 @@ gboolean lttv_hooks_call(LttvHooks *h, void *call_data) guint i; - if(h != NULL) { + if(likely(h != NULL)) { for(i = 0 ; i < h->len ; i++) { c = &g_array_index(h, LttvHookClosure, i); ret = c->hook(c->hook_data,call_data); @@ -278,59 +285,66 @@ gboolean lttv_hooks_call_check(LttvHooks *h, void *call_data) for(i = 0 ; i < h->len ; i++) { c = &g_array_index(h, LttvHookClosure, i); - if(c->hook(c->hook_data,call_data)) return TRUE; + if(unlikely(c->hook(c->hook_data,call_data))) return TRUE; } return FALSE; } -gboolean lttv_hooks_call_merge(LttvHooks *h1, void *call_data1, +/* Optimised for h1 == NULL, h2 != NULL. This is the case + * for optimised computation (with specific by id hooks, but + * no main hooks). + * + * The second case that should occur the most often is + * h1 != NULL , h2 == NULL. + */ +gint lttv_hooks_call_merge(LttvHooks *h1, void *call_data1, LttvHooks *h2, void *call_data2) { - gboolean ret, sum_ret = FALSE; + gint ret, sum_ret = 0; LttvHookClosure *c1, *c2; guint i, j; - if(h1 != NULL && h2 != NULL) { - for(i = 0, j = 0 ; i < h1->len && j < h2->len ;) { - c1 = &g_array_index(h1, LttvHookClosure, i); - c2 = &g_array_index(h2, LttvHookClosure, j); - if(c1->prio <= c2->prio) { + if(unlikely(h1 != NULL)) { + if(unlikely(h2 != NULL)) { + for(i = 0, j = 0 ; i < h1->len && j < h2->len ;) { + c1 = &g_array_index(h1, LttvHookClosure, i); + c2 = &g_array_index(h2, LttvHookClosure, j); + if(c1->prio <= c2->prio) { + ret = c1->hook(c1->hook_data,call_data1); + sum_ret = sum_ret | ret; + i++; + } + else { + ret = c2->hook(c2->hook_data,call_data2); + sum_ret = sum_ret | ret; + j++; + } + } + /* Finish the last list with hooks left */ + for(;i < h1->len; i++) { + c1 = &g_array_index(h1, LttvHookClosure, i); ret = c1->hook(c1->hook_data,call_data1); - sum_ret = sum_ret || ret; - i++; + sum_ret = sum_ret | ret; } - else { + for(;j < h2->len; j++) { + c2 = &g_array_index(h2, LttvHookClosure, j); ret = c2->hook(c2->hook_data,call_data2); - sum_ret = sum_ret || ret; - j++; + sum_ret = sum_ret | ret; + } + } else { /* h1 != NULL && h2 == NULL */ + for(i = 0 ; i < h1->len ; i++) { + c1 = &g_array_index(h1, LttvHookClosure, i); + ret = c1->hook(c1->hook_data,call_data1); + sum_ret = sum_ret | ret; } } - /* Finish the last list with hooks left */ - for(;i < h1->len; i++) { - c1 = &g_array_index(h1, LttvHookClosure, i); - ret = c1->hook(c1->hook_data,call_data1); - sum_ret = sum_ret || ret; - } - for(;j < h2->len; j++) { - c2 = &g_array_index(h2, LttvHookClosure, j); - ret = c2->hook(c2->hook_data,call_data2); - sum_ret = sum_ret || ret; - } - } - else if(h1 != NULL && h2 == NULL) { - for(i = 0 ; i < h1->len ; i++) { - c1 = &g_array_index(h1, LttvHookClosure, i); - ret = c1->hook(c1->hook_data,call_data1); - sum_ret = sum_ret || ret; - } - } - else if(h1 == NULL && h2 != NULL) { - for(j = 0 ; j < h2->len ; j++) { + } else if(likely(h2 != NULL)) { /* h1 == NULL && h2 != NULL */ + for(j = 0 ; j < h2->len ; j++) { c2 = &g_array_index(h2, LttvHookClosure, j); ret = c2->hook(c2->hook_data,call_data2); - sum_ret = sum_ret || ret; + sum_ret = sum_ret | ret; } } @@ -344,50 +358,60 @@ gboolean lttv_hooks_call_check_merge(LttvHooks *h1, void *call_data1, guint i, j; - if(h1 != NULL && h2 != NULL) { - for(i = 0, j = 0 ; i < h1->len && j < h2->len ;) { - c1 = &g_array_index(h1, LttvHookClosure, i); - c2 = &g_array_index(h2, LttvHookClosure, j); - if(c1->prio <= c2->prio) { + if(unlikely(h1 != NULL)) { + if(unlikely(h2 != NULL)) { + for(i = 0, j = 0 ; i < h1->len && j < h2->len ;) { + c1 = &g_array_index(h1, LttvHookClosure, i); + c2 = &g_array_index(h2, LttvHookClosure, j); + if(c1->prio <= c2->prio) { + if(c1->hook(c1->hook_data,call_data1)) return TRUE; + i++; + } + else { + if(c2->hook(c2->hook_data,call_data2)) return TRUE; + j++; + } + } + /* Finish the last list with hooks left */ + for(;i < h1->len; i++) { + c1 = &g_array_index(h1, LttvHookClosure, i); if(c1->hook(c1->hook_data,call_data1)) return TRUE; - i++; } - else { + for(;j < h2->len; j++) { + c2 = &g_array_index(h2, LttvHookClosure, j); if(c2->hook(c2->hook_data,call_data2)) return TRUE; - j++; + } + } else { /* h2 == NULL && h1 != NULL */ + for(i = 0 ; i < h1->len ; i++) { + c1 = &g_array_index(h1, LttvHookClosure, i); + if(c1->hook(c1->hook_data,call_data1)) return TRUE; } } - /* Finish the last list with hooks left */ - for(;i < h1->len; i++) { - c1 = &g_array_index(h1, LttvHookClosure, i); - if(c1->hook(c1->hook_data,call_data1)) return TRUE; - } - for(;j < h2->len; j++) { - c2 = &g_array_index(h2, LttvHookClosure, j); - if(c2->hook(c2->hook_data,call_data2)) return TRUE; - } - } - else if(h1 != NULL && h2 == NULL) { - for(i = 0 ; i < h1->len ; i++) { - c1 = &g_array_index(h1, LttvHookClosure, i); - if(c1->hook(c1->hook_data,call_data1)) return TRUE; - } - } - else if(h1 == NULL && h2 != NULL) { + } else if(likely(h2 != NULL)) { /* h1 == NULL && h2 != NULL */ for(j = 0 ; j < h2->len ; j++) { c2 = &g_array_index(h2, LttvHookClosure, j); if(c2->hook(c2->hook_data,call_data2)) return TRUE; } } - + return FALSE; } +/* Two pointer arrays : + * * one indexed by id for quick search : + * size : max id + * typically 4 bytes * 256 facilities * 10 events = 10kbytes + * * another array that keeps a list of used numbers (for later deletion) + * size : number of ids used. + */ LttvHooksById *lttv_hooks_by_id_new() { - return g_ptr_array_new(); + LttvHooksById *h = g_new(LttvHooksById, 1); + h->index = g_ptr_array_sized_new(NUM_FACILITIES * AVG_EVENTS_PER_FACILITIES); + h->array = g_array_sized_new(FALSE, FALSE, sizeof(guint), 50); + return h; } @@ -395,39 +419,42 @@ void lttv_hooks_by_id_destroy(LttvHooksById *h) { guint i; - for(i = 0 ; i < h->len ; i++) { - if(h->pdata[i] != NULL) lttv_hooks_destroy((LttvHooks *)(h->pdata[i])); + for(i = 0 ; i < h->array->len ; i++) { + guint index = g_array_index(h->array, guint, i); + if(h->index->pdata[index] != NULL) { /* hook may have been removed */ + lttv_hooks_destroy(h->index->pdata[index]); + h->index->pdata[index] = NULL; /* Must be there in case of + multiple addition of the same index */ + } } - g_ptr_array_free(h, TRUE); + g_ptr_array_free(h->index, TRUE); + g_array_free(h->array, TRUE); } - +/* Optimised for searching an existing hook */ LttvHooks *lttv_hooks_by_id_find(LttvHooksById *h, unsigned id) { - if(h->len <= id) g_ptr_array_set_size(h, id + 1); - if(h->pdata[id] == NULL) h->pdata[id] = lttv_hooks_new(); - return h->pdata[id]; + if(unlikely(h->index->len <= id)) g_ptr_array_set_size(h->index, id + 1); + if(unlikely(h->index->pdata[id] == NULL)) { + h->index->pdata[id] = lttv_hooks_new(); + g_array_append_val(h->array, id); + } + return h->index->pdata[id]; } unsigned lttv_hooks_by_id_max_id(LttvHooksById *h) { - return h->len; + return h->index->len; } - -LttvHooks *lttv_hooks_by_id_get(LttvHooksById *h, unsigned id) -{ - if(id < h->len) return h->pdata[id]; - return NULL; -} - - +/* We don't bother removing the used slot array id : lttv_hooks_by_id_destroy is + * almost never called and is able to deal with used slot repetition. */ void lttv_hooks_by_id_remove(LttvHooksById *h, unsigned id) { - if(id < h->len && h->pdata[id] != NULL) { - lttv_hooks_destroy((LttvHooks *)h->pdata[id]); - h->pdata[id] = NULL; + if(likely(id < h->index->len && h->index->pdata[id] != NULL)) { + lttv_hooks_destroy((LttvHooks *)h->index->pdata[id]); + h->index->pdata[id] = NULL; } }