14642e01fSmrg/* x-hook.c
235c4bbdfSmrg *
335c4bbdfSmrg * Copyright (c) 2002-2012 Apple Inc. All rights reserved.
435c4bbdfSmrg *
535c4bbdfSmrg * Permission is hereby granted, free of charge, to any person
635c4bbdfSmrg * obtaining a copy of this software and associated documentation files
735c4bbdfSmrg * (the "Software"), to deal in the Software without restriction,
835c4bbdfSmrg * including without limitation the rights to use, copy, modify, merge,
935c4bbdfSmrg * publish, distribute, sublicense, and/or sell copies of the Software,
1035c4bbdfSmrg * and to permit persons to whom the Software is furnished to do so,
1135c4bbdfSmrg * subject to the following conditions:
1235c4bbdfSmrg *
1335c4bbdfSmrg * The above copyright notice and this permission notice shall be
1435c4bbdfSmrg * included in all copies or substantial portions of the Software.
1535c4bbdfSmrg *
1635c4bbdfSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1735c4bbdfSmrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1835c4bbdfSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1935c4bbdfSmrg * NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
2035c4bbdfSmrg * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
2135c4bbdfSmrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2235c4bbdfSmrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2335c4bbdfSmrg * DEALINGS IN THE SOFTWARE.
2435c4bbdfSmrg *
2535c4bbdfSmrg * Except as contained in this notice, the name(s) of the above
2635c4bbdfSmrg * copyright holders shall not be used in advertising or otherwise to
2735c4bbdfSmrg * promote the sale, use or other dealings in this Software without
2835c4bbdfSmrg * prior written authorization.
2935c4bbdfSmrg */
304642e01fSmrg
314642e01fSmrg#ifdef HAVE_DIX_CONFIG_H
324642e01fSmrg#include <dix-config.h>
334642e01fSmrg#endif
344642e01fSmrg
354642e01fSmrg#include "x-hook.h"
364642e01fSmrg#include <stdlib.h>
374642e01fSmrg#include <assert.h>
386747b715Smrg#include "os.h"
394642e01fSmrg
4035c4bbdfSmrg#define CELL_NEW(f, d) X_PFX(list_prepend) ((x_list *)(f), (d))
4135c4bbdfSmrg#define CELL_FREE(c)   X_PFX(list_free_1) (c)
4235c4bbdfSmrg#define CELL_FUN(c)    ((x_hook_function *)((c)->next))
4335c4bbdfSmrg#define CELL_DATA(c)   ((c)->data)
444642e01fSmrg
454642e01fSmrgX_EXTERN x_list *
4635c4bbdfSmrgX_PFX(hook_add) (x_list * lst, x_hook_function * fun, void *data) {
4735c4bbdfSmrg    return X_PFX(list_prepend) (lst, CELL_NEW(fun, data));
484642e01fSmrg}
494642e01fSmrg
504642e01fSmrgX_EXTERN x_list *
5135c4bbdfSmrgX_PFX(hook_remove) (x_list * lst, x_hook_function * fun, void *data) {
524642e01fSmrg    x_list *node, *cell;
534642e01fSmrg    x_list *to_delete = NULL;
544642e01fSmrg
5535c4bbdfSmrg    for (node = lst; node != NULL; node = node->next) {
5635c4bbdfSmrg        cell = node->data;
5735c4bbdfSmrg        if (CELL_FUN(cell) == fun && CELL_DATA(cell) == data)
5835c4bbdfSmrg            to_delete = X_PFX(list_prepend) (to_delete, cell);
594642e01fSmrg    }
604642e01fSmrg
6135c4bbdfSmrg    for (node = to_delete; node != NULL; node = node->next) {
6235c4bbdfSmrg        cell = node->data;
6335c4bbdfSmrg        lst = X_PFX(list_remove) (lst, cell);
6435c4bbdfSmrg        CELL_FREE(cell);
654642e01fSmrg    }
664642e01fSmrg
6735c4bbdfSmrg    X_PFX(list_free) (to_delete);
684642e01fSmrg    return lst;
694642e01fSmrg}
704642e01fSmrg
714642e01fSmrgX_EXTERN void
7235c4bbdfSmrgX_PFX(hook_run) (x_list * lst, void *arg) {
7335c4bbdfSmrg    x_list *node;
744642e01fSmrg
7535c4bbdfSmrg    if (!lst)
766747b715Smrg        return;
776747b715Smrg
7835c4bbdfSmrg    for (node = lst; node != NULL; node = node->next) {
7935c4bbdfSmrg        x_list *cell = node->data;
804642e01fSmrg
8135c4bbdfSmrg        x_hook_function *fun = CELL_FUN(cell);
8235c4bbdfSmrg        void *data = CELL_DATA(cell);
834642e01fSmrg
8435c4bbdfSmrg        (*fun)(arg, data);
854642e01fSmrg    }
864642e01fSmrg}
874642e01fSmrg
884642e01fSmrgX_EXTERN void
8935c4bbdfSmrgX_PFX(hook_free) (x_list * lst) {
904642e01fSmrg    x_list *node;
914642e01fSmrg
9235c4bbdfSmrg    for (node = lst; node != NULL; node = node->next) {
9335c4bbdfSmrg        CELL_FREE(node->data);
944642e01fSmrg    }
954642e01fSmrg
9635c4bbdfSmrg    X_PFX(list_free) (lst);
974642e01fSmrg}
98