1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
/*
* Copyright 1993-1999 Bruno Haible, <haible@clisp.cons.org>
*
* This is free software distributed under the GNU General Public Licence
* described in the file COPYING. Contact the author if you don't have this
* or can't live with it. There is ABSOLUTELY NO WARRANTY, explicit or implied,
* on this software.
*/
#include "sigsegv.h"
#include <stddef.h> /* needed for NULL on SunOS4 */
#include <stdlib.h>
#ifdef _WIN32
#include <malloc.h>
#endif
/* C++ doesn't allow us to define a function called `delete'. */
#ifdef __cplusplus
#define delete my_delete
#endif
/*
* A dispatcher contains an AVL tree of non-empty intervals, sorted according
* to their starting address.
*/
typedef
struct node_t {
/* AVL tree management */
struct node_t * left;
struct node_t * right;
unsigned int height;
/* Representation of interval */
unsigned long address;
unsigned long len;
/* User handler */
sigsegv_area_handler_t handler;
void* handler_arg;
}
node_t;
#define empty ((node_t *) 0)
#define heightof(tree) ((tree)==empty ? 0 : (tree)->height)
#define MAXHEIGHT 41
static void rebalance (node_t*** nodeplaces_ptr, unsigned int count)
{
if (count > 0)
do {
node_t** nodeplace = *--nodeplaces_ptr;
node_t* node = *nodeplace;
node_t* nodeleft = node->left;
node_t* noderight = node->right;
unsigned int heightleft = heightof(nodeleft);
unsigned int heightright = heightof(noderight);
if (heightright + 1 < heightleft) {
node_t* nodeleftleft = nodeleft->left;
node_t* nodeleftright = nodeleft->right;
unsigned int heightleftright = heightof(nodeleftright);
if (heightof(nodeleftleft) >= heightleftright) {
node->left = nodeleftright; nodeleft->right = node;
nodeleft->height = 1 + (node->height = 1 + heightleftright);
*nodeplace = nodeleft;
} else {
nodeleft->right = nodeleftright->left;
node->left = nodeleftright->right;
nodeleftright->left = nodeleft;
nodeleftright->right = node;
nodeleft->height = node->height = heightleftright;
nodeleftright->height = heightleft;
*nodeplace = nodeleftright;
}
} else if (heightleft + 1 < heightright) {
node_t* noderightright = noderight->right;
node_t* noderightleft = noderight->left;
unsigned int heightrightleft = heightof(noderightleft);
if (heightof(noderightright) >= heightrightleft) {
node->right = noderightleft; noderight->left = node;
noderight->height = 1 + (node->height = 1 + heightrightleft);
*nodeplace = noderight;
} else {
noderight->left = noderightleft->right;
node->right = noderightleft->left;
noderightleft->right = noderight;
noderightleft->left = node;
noderight->height = node->height = heightrightleft;
noderightleft->height = heightright;
*nodeplace = noderightleft;
}
} else {
unsigned int height = (heightleft<heightright ? heightright : heightleft) + 1;
if (height == node->height) break;
node->height = height;
}
} while (--count > 0);
}
static node_t* insert (node_t* new_node, node_t* tree)
{
unsigned int key = new_node->address;
node_t** nodeplace = &tree;
node_t** stack[MAXHEIGHT];
unsigned int stack_count = 0;
node_t*** stack_ptr = &stack[0];
for (;;) {
node_t* node = *nodeplace;
if (node == empty) break;
*stack_ptr++ = nodeplace; stack_count++;
if (key < node->address)
nodeplace = &node->left;
else
nodeplace = &node->right;
}
new_node->left = empty;
new_node->right = empty;
new_node->height = 1;
*nodeplace = new_node;
rebalance(stack_ptr,stack_count);
return tree;
}
static node_t* delete (node_t* node_to_delete, node_t* tree)
{
unsigned long key = node_to_delete->address;
node_t** nodeplace = &tree;
node_t** stack[MAXHEIGHT];
unsigned int stack_count = 0;
node_t*** stack_ptr = &stack[0];
for (;;) {
node_t* node = *nodeplace;
if (node == empty) return tree;
*stack_ptr++ = nodeplace; stack_count++;
if (key == node->address) {
if (node != node_to_delete)
abort();
break;
}
if (key < node->address)
nodeplace = &node->left;
else
nodeplace = &node->right;
}
{
node_t** nodeplace_to_delete = nodeplace;
if (node_to_delete->left == empty) {
*nodeplace_to_delete = node_to_delete->right;
stack_ptr--; stack_count--;
} else {
node_t*** stack_ptr_to_delete = stack_ptr;
node_t** nodeplace = &node_to_delete->left;
node_t* node;
for (;;) {
node = *nodeplace;
if (node->right == empty) break;
*stack_ptr++ = nodeplace; stack_count++;
nodeplace = &node->right;
}
*nodeplace = node->left;
node->left = node_to_delete->left;
node->right = node_to_delete->right;
node->height = node_to_delete->height;
*nodeplace_to_delete = node;
*stack_ptr_to_delete = &node->left;
}
}
rebalance(stack_ptr,stack_count);
return tree;
}
void sigsegv_init (sigsegv_dispatcher* dispatcher)
{
dispatcher->tree = empty;
}
void* sigsegv_register (sigsegv_dispatcher* dispatcher,
void* address, unsigned long len,
sigsegv_area_handler_t handler, void* handler_arg)
{
if (len == 0)
return NULL;
else {
node_t* new_node = (node_t*) malloc(sizeof(node_t));
new_node->address = (unsigned long) address;
new_node->len = len;
new_node->handler = handler;
new_node->handler_arg = handler_arg;
dispatcher->tree = insert(new_node,(node_t*)(dispatcher->tree));
return new_node;
}
}
void sigsegv_unregister (sigsegv_dispatcher* dispatcher, void* ticket)
{
if (ticket != NULL) {
node_t* node_to_delete = (node_t*)ticket;
dispatcher->tree = delete(node_to_delete,(node_t*)(dispatcher->tree));
free(node_to_delete);
}
}
int sigsegv_dispatch (sigsegv_dispatcher* dispatcher, void* fault_address)
{
unsigned long key = (unsigned long) fault_address;
node_t* tree = (node_t*)(dispatcher->tree);
for (;;) {
if (tree == empty) return 0;
if (key < tree->address)
tree = tree->left;
else if (key - tree->address >= tree->len)
tree = tree->right;
else
break;
}
return (*tree->handler)(fault_address,tree->handler_arg);
}
|