| 1 |
|
| #include "xh_config.h" |
| 2 |
|
| #include "xh_core.h" |
| 3 |
|
| |
| 4 |
|
| static int |
| 5 |
25 |
| xh_sort_hash_cmp(const void *p1, const void *p2) |
| 6 |
|
| { |
| 7 |
25 |
| return xh_strcmp(((xh_sort_hash_t *) p1)->key, ((xh_sort_hash_t *) p2)->key); |
| 8 |
|
| } |
| 9 |
|
| |
| 10 |
|
| xh_sort_hash_t * |
| 11 |
13 |
| xh_sort_hash(HV *hash, size_t len) |
| 12 |
|
| { |
| 13 |
|
| xh_sort_hash_t *sorted_hash; |
| 14 |
|
| size_t i; |
| 15 |
|
| |
| 16 |
13 |
| sorted_hash = malloc(sizeof(xh_sort_hash_t) * len); |
| 17 |
13 |
▶ | if (sorted_hash == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sorted_hash == NULL) { |
0 |
13 |
13 |
|
| 18 |
0 |
| croak("Memory allocation error"); |
| 19 |
|
| } |
| 20 |
|
| |
| 21 |
13 |
| hv_iterinit(hash); |
| 22 |
|
| |
| 23 |
45 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
32 |
13 |
45 |
|
| 24 |
32 |
| sorted_hash[i].value = hv_iternextsv(hash, (char **) &sorted_hash[i].key, &sorted_hash[i].key_len); |
| 25 |
|
| } |
| 26 |
|
| |
| 27 |
13 |
| qsort(sorted_hash, len, sizeof(xh_sort_hash_t), xh_sort_hash_cmp); |
| 28 |
|
| |
| 29 |
13 |
| return sorted_hash; |
| 30 |
|
| } |
| 31 |
|
| |
| 32 |
|
| /* Callers register this with SAVEDESTRUCTOR_X so that the sorted hash is |
| 33 |
|
| * released both on the normal path and when a perl callback dies in the |
| 34 |
|
| * middle of the loop and longjmps past the end of the block. */ |
| 35 |
|
| void |
| 36 |
13 |
| xh_sort_hash_free(pTHX_ void *sorted_hash) |
| 37 |
|
| { |
| 38 |
|
| PERL_UNUSED_CONTEXT; |
| 39 |
13 |
| free(sorted_hash); |
| 40 |
13 |
| } |