#include <lttv/hook.h>
-
+#include <ltt/compiler.h>
typedef struct _LttvHookClosure {
LttvHook hook;
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;
}
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;
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;
/* 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);
{
LttvHookClosure *c;
- if(i >= h->len)
+ if(unlikely(i >= h->len))
{
*f = NULL;
*hook_data = NULL;
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);
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;
}
+/* 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.
+ */
gboolean lttv_hooks_call_merge(LttvHooks *h1, void *call_data1,
LttvHooks *h2, void *call_data2)
{
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++;
}
- 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++;
+ }
+ } 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;
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;
}
g_ptr_array_free(h, 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();
+ if(unlikely(h->len <= id)) g_ptr_array_set_size(h, id + 1);
+ if(unlikely(h->pdata[id] == NULL)) h->pdata[id] = lttv_hooks_new();
return h->pdata[id];
}
return h->len;
}
-
-LttvHooks *lttv_hooks_by_id_get(LttvHooksById *h, unsigned id)
-{
- if(id < h->len) return h->pdata[id];
- return NULL;
-}
-
-
void lttv_hooks_by_id_remove(LttvHooksById *h, unsigned id)
{
- if(id < h->len && h->pdata[id] != NULL) {
+ if(likely(id < h->len && h->pdata[id] != NULL)) {
lttv_hooks_destroy((LttvHooks *)h->pdata[id]);
h->pdata[id] = NULL;
}