* xmlHashGrow: * @hash: hash table * @size: new size of the hash table * * Resize the hash table. * * Returns 0 in case of success, -1 if a memory allocation failed. */
| 351 | * Returns 0 in case of success, -1 if a memory allocation failed. |
| 352 | */ |
| 353 | static int |
| 354 | xmlHashGrow(xmlHashTablePtr hash, unsigned size) { |
| 355 | const xmlHashEntry *oldentry, *oldend, *end; |
| 356 | xmlHashEntry *table; |
| 357 | unsigned oldsize, i; |
| 358 | |
| 359 | /* Add 0 to avoid spurious -Wtype-limits warning on 64-bit GCC */ |
| 360 | if ((size_t) size + 0 > SIZE_MAX / sizeof(table[0])) |
| 361 | return(-1); |
| 362 | table = xmlMalloc(size * sizeof(table[0])); |
| 363 | if (table == NULL) |
| 364 | return(-1); |
| 365 | memset(table, 0, size * sizeof(table[0])); |
| 366 | |
| 367 | oldsize = hash->size; |
| 368 | if (oldsize == 0) |
| 369 | goto done; |
| 370 | |
| 371 | oldend = &hash->table[oldsize]; |
| 372 | end = &table[size]; |
| 373 | |
| 374 | /* |
| 375 | * Robin Hood sorting order is maintained if we |
| 376 | * |
| 377 | * - compute hash indices with modulo |
| 378 | * - resize by an integer factor |
| 379 | * - start to copy from the beginning of a probe sequence |
| 380 | */ |
| 381 | oldentry = hash->table; |
| 382 | while (oldentry->hashValue != 0) { |
| 383 | if (++oldentry >= oldend) |
| 384 | oldentry = hash->table; |
| 385 | } |
| 386 | |
| 387 | for (i = 0; i < oldsize; i++) { |
| 388 | if (oldentry->hashValue != 0) { |
| 389 | xmlHashEntry *entry = &table[oldentry->hashValue & (size - 1)]; |
| 390 | |
| 391 | while (entry->hashValue != 0) { |
| 392 | if (++entry >= end) |
| 393 | entry = table; |
| 394 | } |
| 395 | *entry = *oldentry; |
| 396 | } |
| 397 | |
| 398 | if (++oldentry >= oldend) |
| 399 | oldentry = hash->table; |
| 400 | } |
| 401 | |
| 402 | xmlFree(hash->table); |
| 403 | |
| 404 | done: |
| 405 | hash->table = table; |
| 406 | hash->size = size; |
| 407 | |
| 408 | return(0); |
| 409 | } |
| 410 |
no outgoing calls
no test coverage detected