x-hook.c revision 706f2543
1/* x-hook.c 2 3 Copyright (c) 2003 Apple Computer, Inc. All rights reserved. 4 5 Permission is hereby granted, free of charge, to any person 6 obtaining a copy of this software and associated documentation files 7 (the "Software"), to deal in the Software without restriction, 8 including without limitation the rights to use, copy, modify, merge, 9 publish, distribute, sublicense, and/or sell copies of the Software, 10 and to permit persons to whom the Software is furnished to do so, 11 subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT 20 HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 DEALINGS IN THE SOFTWARE. 24 25 Except as contained in this notice, the name(s) of the above 26 copyright holders shall not be used in advertising or otherwise to 27 promote the sale, use or other dealings in this Software without 28 prior written authorization. */ 29 30#ifdef HAVE_DIX_CONFIG_H 31#include <dix-config.h> 32#endif 33 34#include "x-hook.h" 35#include <stdlib.h> 36#include <assert.h> 37#include "os.h" 38 39#define CELL_NEW(f,d) X_PFX (list_prepend) ((x_list *) (f), (d)) 40#define CELL_FREE(c) X_PFX (list_free_1) (c) 41#define CELL_FUN(c) ((x_hook_function *) ((c)->next)) 42#define CELL_DATA(c) ((c)->data) 43 44X_EXTERN x_list * 45X_PFX (hook_add) (x_list *lst, x_hook_function *fun, void *data) 46{ 47 return X_PFX (list_prepend) (lst, CELL_NEW (fun, data)); 48} 49 50X_EXTERN x_list * 51X_PFX (hook_remove) (x_list *lst, x_hook_function *fun, void *data) 52{ 53 x_list *node, *cell; 54 x_list *to_delete = NULL; 55 56 for (node = lst; node != NULL; node = node->next) 57 { 58 cell = node->data; 59 if (CELL_FUN (cell) == fun && CELL_DATA (cell) == data) 60 to_delete = X_PFX (list_prepend) (to_delete, cell); 61 } 62 63 for (node = to_delete; node != NULL; node = node->next) 64 { 65 cell = node->data; 66 lst = X_PFX (list_remove) (lst, cell); 67 CELL_FREE (cell); 68 } 69 70 X_PFX (list_free) (to_delete); 71 return lst; 72} 73 74X_EXTERN void 75X_PFX (hook_run) (x_list *lst, void *arg) 76{ 77 x_list *node, *cell; 78 x_hook_function **fun; 79 void **data; 80 int length, i; 81 82 if(!lst) 83 return; 84 85 length = X_PFX (list_length) (lst); 86 fun = malloc(sizeof (x_hook_function *) * length); 87 data = malloc(sizeof (void *) * length); 88 89 if(!fun || !data) { 90 FatalError("Failed to allocate memory in %s\n", __func__); 91 } 92 93 for (i = 0, node = lst; node != NULL; node = node->next, i++) 94 { 95 cell = node->data; 96 fun[i] = CELL_FUN (cell); 97 data[i] = CELL_DATA (cell); 98 } 99 100 for (i = 0; i < length; i++) 101 { 102 (*fun[i]) (arg, data[i]); 103 } 104 105 free(fun); 106 free(data); 107} 108 109X_EXTERN void 110X_PFX (hook_free) (x_list *lst) 111{ 112 x_list *node; 113 114 for (node = lst; node != NULL; node = node->next) 115 { 116 CELL_FREE (node->data); 117 } 118 119 X_PFX (list_free) (lst); 120} 121