| Petr Machata | a11cbed | 2012-05-05 00:07:38 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * This file is part of ltrace. |
| 3 | * Copyright (C) 2011,2012 Petr Machata |
| 4 | * Copyright (C) 2003,2004,2008,2009 Juan Cespedes |
| 5 | * Copyright (C) 2006 Ian Wienand |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of the |
| 10 | * License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 20 | * 02110-1301 USA |
| 21 | */ |
| 22 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 25 | #include <string.h> |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 26 | #include <assert.h> |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 27 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 28 | #include "common.h" |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 29 | |
| 30 | /* |
| Petr Machata | a11cbed | 2012-05-05 00:07:38 +0200 | [diff] [blame^] | 31 | * Dictionary based on code by Morten Eriksen <mortene@sim.no>. |
| 32 | */ |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 33 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 34 | struct dict_entry { |
| 35 | unsigned int hash; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 36 | void *key; |
| 37 | void *value; |
| 38 | struct dict_entry *next; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | /* #define DICTTABLESIZE 97 */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 42 | #define DICTTABLESIZE 997 /* Semi-randomly selected prime number. */ |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 43 | /* #define DICTTABLESIZE 9973 */ |
| 44 | /* #define DICTTABLESIZE 99991 */ |
| 45 | /* #define DICTTABLESIZE 999983 */ |
| 46 | |
| 47 | struct dict { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 48 | struct dict_entry *buckets[DICTTABLESIZE]; |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 49 | unsigned int (*key2hash) (const void *); |
| 50 | int (*key_cmp) (const void *, const void *); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 53 | Dict * |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 54 | dict_init(unsigned int (*key2hash) (const void *), |
| 55 | int (*key_cmp) (const void *, const void *)) |
| 56 | { |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 57 | Dict *d; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 58 | int i; |
| 59 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 60 | debug(DEBUG_FUNCTION, "dict_init()"); |
| 61 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 62 | d = malloc(sizeof(Dict)); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 63 | if (!d) { |
| 64 | perror("malloc()"); |
| 65 | exit(1); |
| 66 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 67 | for (i = 0; i < DICTTABLESIZE; i++) { /* better use memset()? */ |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 68 | d->buckets[i] = NULL; |
| 69 | } |
| 70 | d->key2hash = key2hash; |
| 71 | d->key_cmp = key_cmp; |
| 72 | return d; |
| 73 | } |
| 74 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 75 | void |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 76 | dict_clear(Dict *d) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 77 | int i; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 78 | struct dict_entry *entry, *nextentry; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 79 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 80 | debug(DEBUG_FUNCTION, "dict_clear()"); |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 81 | assert(d); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 82 | for (i = 0; i < DICTTABLESIZE; i++) { |
| 83 | for (entry = d->buckets[i]; entry != NULL; entry = nextentry) { |
| 84 | nextentry = entry->next; |
| 85 | free(entry); |
| 86 | } |
| 87 | d->buckets[i] = NULL; |
| 88 | } |
| 89 | free(d); |
| 90 | } |
| 91 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 92 | int |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 93 | dict_enter(Dict *d, void *key, void *value) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 94 | struct dict_entry *entry, *newentry; |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 95 | unsigned int hash; |
| 96 | unsigned int bucketpos; |
| 97 | |
| 98 | debug(DEBUG_FUNCTION, "dict_enter()"); |
| 99 | |
| 100 | hash = d->key2hash(key); |
| 101 | bucketpos = hash % DICTTABLESIZE; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 102 | |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 103 | assert(d); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 104 | newentry = malloc(sizeof(struct dict_entry)); |
| 105 | if (!newentry) { |
| 106 | perror("malloc"); |
| 107 | exit(1); |
| 108 | } |
| 109 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 110 | newentry->hash = hash; |
| 111 | newentry->key = key; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 112 | newentry->value = value; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 113 | newentry->next = NULL; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 114 | |
| 115 | entry = d->buckets[bucketpos]; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 116 | while (entry && entry->next) |
| 117 | entry = entry->next; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 118 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 119 | if (entry) |
| 120 | entry->next = newentry; |
| 121 | else |
| 122 | d->buckets[bucketpos] = newentry; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 123 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 124 | debug(3, "new dict entry at %p[%d]: (%p,%p)", d, bucketpos, key, value); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 128 | void * |
| Petr Machata | 77fbb8f | 2012-04-19 16:57:57 +0200 | [diff] [blame] | 129 | dict_remove(Dict *d, void *key) |
| 130 | { |
| 131 | assert(d != NULL); |
| 132 | debug(DEBUG_FUNCTION, "dict_remove(%p)", key); |
| 133 | |
| 134 | unsigned int hash = d->key2hash(key); |
| 135 | unsigned int bucketpos = hash % DICTTABLESIZE; |
| 136 | |
| 137 | struct dict_entry **entryp; |
| 138 | for (entryp = &d->buckets[bucketpos]; (*entryp) != NULL; |
| 139 | entryp = &(*entryp)->next) { |
| 140 | struct dict_entry *entry = *entryp; |
| 141 | if (hash != entry->hash) |
| 142 | continue; |
| 143 | if (d->key_cmp(key, entry->key) == 0) { |
| 144 | *entryp = entry->next; |
| 145 | return entry->value; |
| 146 | } |
| 147 | } |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | void * |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 152 | dict_find_entry(Dict *d, const void *key) |
| 153 | { |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 154 | unsigned int hash; |
| 155 | unsigned int bucketpos; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 156 | struct dict_entry *entry; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 157 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 158 | debug(DEBUG_FUNCTION, "dict_find_entry()"); |
| 159 | |
| 160 | hash = d->key2hash(key); |
| 161 | bucketpos = hash % DICTTABLESIZE; |
| 162 | |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 163 | assert(d); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 164 | for (entry = d->buckets[bucketpos]; entry; entry = entry->next) { |
| 165 | if (hash != entry->hash) { |
| 166 | continue; |
| 167 | } |
| 168 | if (!d->key_cmp(key, entry->key)) { |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | return entry ? entry->value : NULL; |
| 173 | } |
| 174 | |
| 175 | void |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 176 | dict_apply_to_all(Dict *d, |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 177 | void (*func) (void *key, void *value, void *data), void *data) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 178 | int i; |
| 179 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 180 | debug(DEBUG_FUNCTION, "dict_apply_to_all()"); |
| 181 | |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 182 | if (!d) { |
| 183 | return; |
| 184 | } |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 185 | for (i = 0; i < DICTTABLESIZE; i++) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 186 | struct dict_entry *entry = d->buckets[i]; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 187 | while (entry) { |
| 188 | func(entry->key, entry->value, data); |
| 189 | entry = entry->next; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /*****************************************************************************/ |
| 195 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 196 | unsigned int |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 197 | dict_key2hash_string(const void *key) |
| 198 | { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 199 | const char *s = (const char *)key; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 200 | unsigned int total = 0, shift = 0; |
| 201 | |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 202 | assert(key); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 203 | while (*s) { |
| 204 | total = total ^ ((*s) << shift); |
| 205 | shift += 5; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 206 | if (shift > 24) |
| 207 | shift -= 24; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 208 | s++; |
| 209 | } |
| 210 | return total; |
| 211 | } |
| 212 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 213 | int |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 214 | dict_key_cmp_string(const void *key1, const void *key2) |
| 215 | { |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 216 | assert(key1); |
| 217 | assert(key2); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 218 | return strcmp((const char *)key1, (const char *)key2); |
| 219 | } |
| 220 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 221 | unsigned int |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 222 | dict_key2hash_int(const void *key) |
| 223 | { |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 224 | return (unsigned long)key; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 225 | } |
| 226 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 227 | int |
| Petr Machata | 345c9b5 | 2012-02-10 13:10:03 +0100 | [diff] [blame] | 228 | dict_key_cmp_int(const void *key1, const void *key2) |
| 229 | { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 230 | return key1 - key2; |
| 231 | } |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 232 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 233 | Dict * |
| Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 234 | dict_clone2(Dict * old, void * (*key_clone)(void *, void *), |
| 235 | void * (*value_clone)(void *, void *), void * data) |
| 236 | { |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 237 | Dict *d; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 238 | int i; |
| 239 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 240 | debug(DEBUG_FUNCTION, "dict_clone()"); |
| 241 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 242 | d = malloc(sizeof(Dict)); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 243 | if (!d) { |
| 244 | perror("malloc()"); |
| 245 | exit(1); |
| 246 | } |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 247 | memcpy(d, old, sizeof(Dict)); |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 248 | for (i = 0; i < DICTTABLESIZE; i++) { /* better use memset()? */ |
| 249 | struct dict_entry *de_old; |
| 250 | struct dict_entry **de_new; |
| 251 | |
| 252 | de_old = old->buckets[i]; |
| 253 | de_new = &d->buckets[i]; |
| 254 | while (de_old) { |
| Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 255 | void * nkey, * nval; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 256 | *de_new = malloc(sizeof(struct dict_entry)); |
| 257 | if (!*de_new) { |
| 258 | perror("malloc()"); |
| 259 | exit(1); |
| 260 | } |
| 261 | memcpy(*de_new, de_old, sizeof(struct dict_entry)); |
| Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 262 | |
| 263 | /* The error detection is rather weak :-/ */ |
| 264 | nkey = key_clone(de_old->key, data); |
| 265 | if (nkey == NULL && de_old->key != NULL) { |
| 266 | perror("key_clone"); |
| 267 | err: |
| 268 | /* XXX Will this actually work? We |
| 269 | * simply memcpy the old dictionary |
| 270 | * over up there. */ |
| 271 | dict_clear(d); |
| 272 | free(de_new); |
| 273 | return NULL; |
| 274 | } |
| 275 | |
| 276 | nval = value_clone(de_old->value, data); |
| 277 | if (nval == NULL && de_old->value != NULL) { |
| 278 | perror("value_clone"); |
| 279 | goto err; |
| 280 | } |
| 281 | |
| 282 | (*de_new)->key = nkey; |
| 283 | (*de_new)->value = nval; |
| Juan Cespedes | bc8caf0 | 2009-05-07 19:38:38 +0200 | [diff] [blame] | 284 | de_new = &(*de_new)->next; |
| 285 | de_old = de_old->next; |
| 286 | } |
| 287 | } |
| 288 | return d; |
| 289 | } |
| Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 290 | |
| 291 | struct wrap_clone_cb |
| 292 | { |
| 293 | void * (*key_clone)(void *); |
| 294 | void * (*value_clone)(void *); |
| 295 | }; |
| 296 | |
| 297 | static void * |
| 298 | value_clone_1(void * arg, void * data) |
| 299 | { |
| 300 | return ((struct wrap_clone_cb *)data)->value_clone(arg); |
| 301 | } |
| 302 | |
| 303 | static void * |
| 304 | key_clone_1(void * arg, void * data) |
| 305 | { |
| 306 | return ((struct wrap_clone_cb *)data)->key_clone(arg); |
| 307 | } |
| 308 | |
| 309 | Dict * |
| 310 | dict_clone(Dict * old, void * (*key_clone)(void *), |
| 311 | void * (*value_clone)(void *)) |
| 312 | { |
| 313 | struct wrap_clone_cb cb = { key_clone, value_clone }; |
| 314 | return dict_clone2(old, &key_clone_1, &value_clone_1, &cb); |
| 315 | } |