Skip to content

Commit 4bbba78

Browse files
committed
Fix unused
1 parent 1f283b3 commit 4bbba78

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

src/util/imap.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -739,33 +739,38 @@ vec_t * imap_vec_ref(imap_t * imap)
739739
static uint64_t imap__unused_id(imap_node_t * node, uint64_t max)
740740
{
741741
imap_node_t * nd = node->nodes;
742-
size_t i, n, m, r;
742+
size_t i, n, m, r, low;
743743

744744
if (node->key != IMAP_NODE_SZ)
745-
return (node->key+1) % IMAP_NODE_SZ;
745+
// it is not enough to test just for free, it must be the lowest
746+
// because a higher value might be out the range of max
747+
return (node->key % IMAP_NODE_SZ) == 0 ? 1 : 0;
746748

747749
for (i = 0; i < IMAP_NODE_SZ; ++i, ++nd)
748750
if (!nd->data)
749751
return i; /* we don't care if larger than max */
750752

751753
n = IMAP_NODE_SZ + IMAP_NODE_SZ;
752754
n = n < max ? n : max;
753-
m = max / IMAP_NODE_SZ - 1;
755+
m = max / IMAP_NODE_SZ;
754756
nd = node->nodes;
755-
757+
low = max;
756758
for (i = IMAP_NODE_SZ; i < n; ++i, ++nd)
757759
{
758760
if (!nd->nodes)
759761
return i;
760762

761763
if ((r = imap__unused_id(nd, m)) < m)
762764
{
765+
if (r == 0)
766+
return i;
763767
r *= IMAP_NODE_SZ;
764768
r += i;
765-
return r;
769+
if (r < low)
770+
low = r;
766771
}
767772
}
768-
return max;
773+
return low;
769774
}
770775

771776
/*

test/test_imap/test_imap.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,25 @@
11
#include "../test.h"
22
#include <util/imap.h>
33

4-
static const unsigned int num_batches = 5;
5-
static const unsigned int num_entries = 8;
6-
7-
84
int main()
95
{
106
test_start("imap");
117

128
imap_t * imap = imap_create();
139
uint64_t id = 0;
10+
uint64_t x = 0;
11+
uint64_t t = 0x1fff;
1412

1513
_assert (imap);
1614

17-
/* test adding values */
18-
for (size_t j = 0; j < num_batches; ++j)
19-
{
20-
for (size_t i = 1; i <= num_entries ; ++i)
21-
{
22-
id = (j*100) + i + (i*j);
23-
_assert (imap_add(
24-
imap, id,
25-
(void *) (uintptr_t)
26-
&id) == IMAP_SUCCESS);
27-
}
28-
}
29-
30-
_assert (imap->n == num_batches * num_entries);
31-
32-
while ((id = imap_unused_id(imap, 0x1000)) != 0x1000)
15+
while ((id = imap_unused_id(imap, t)) != t)
3316
{
17+
_assert (x < t);
18+
_assert (id < t);
3419
_assert (imap_add(imap, id, (void *) (uintptr_t) &id) == IMAP_SUCCESS);
20+
x += 1;
3521
}
36-
37-
_assert (imap->n == 0x1000);
22+
_assert (imap->n == t);
3823

3924
imap_destroy(imap, NULL);
4025

0 commit comments

Comments
 (0)