diff options
| -rw-r--r-- | src/core/endpt.c | 4 | ||||
| -rw-r--r-- | src/core/idhash.c | 34 | ||||
| -rw-r--r-- | src/core/idhash.h | 11 | ||||
| -rw-r--r-- | src/core/pipe.c | 4 | ||||
| -rw-r--r-- | src/core/socket.c | 4 | ||||
| -rw-r--r-- | tests/idhash.c | 241 |
6 files changed, 162 insertions, 136 deletions
diff --git a/src/core/endpt.c b/src/core/endpt.c index f90bd068..5b56b784 100644 --- a/src/core/endpt.c +++ b/src/core/endpt.c @@ -18,7 +18,7 @@ struct nni_ep { nni_tran_ep ep_ops; // transport ops nni_tran * ep_tran; // transport pointer void * ep_data; // transport private - uint32_t ep_id; // endpoint id + uint64_t ep_id; // endpoint id nni_list_node ep_node; // per socket list nni_sock * ep_sock; char ep_addr[NNG_MAXADDRLEN]; @@ -78,7 +78,7 @@ nni_ep_sys_fini(void) uint32_t nni_ep_id(nni_ep *ep) { - return (ep->ep_id); + return ((uint32_t) ep->ep_id); } static void diff --git a/src/core/idhash.c b/src/core/idhash.c index 03854fc8..08352ea3 100644 --- a/src/core/idhash.c +++ b/src/core/idhash.c @@ -13,9 +13,9 @@ #include <string.h> struct nni_idhash_entry { - uint32_t ihe_key; - uint32_t ihe_skips; + uint64_t ihe_key; void * ihe_val; + uint32_t ihe_skips; }; struct nni_idhash { @@ -25,9 +25,9 @@ struct nni_idhash { size_t ih_minload; // considers placeholders size_t ih_maxload; uint32_t ih_walkers; - uint32_t ih_minval; - uint32_t ih_maxval; - uint32_t ih_dynval; + uint64_t ih_minval; + uint64_t ih_maxval; + uint64_t ih_dynval; nni_idhash_entry *ih_entries; nni_mtx ih_mtx; }; @@ -73,7 +73,7 @@ nni_idhash_fini(nni_idhash *h) void nni_idhash_set_limits( - nni_idhash *h, uint32_t minval, uint32_t maxval, uint32_t start) + nni_idhash *h, uint64_t minval, uint64_t maxval, uint64_t start) { if (start < minval) { start = minval; @@ -97,7 +97,7 @@ nni_idhash_set_limits( #define NNI_IDHASH_NEXTPROBE(h, j) ((((j) *5) + 1) & (h->ih_cap - 1)) static int -nni_hash_find(nni_idhash *h, uint32_t id, void **valp) +nni_hash_find(nni_idhash *h, uint64_t id, void **valp) { uint32_t index = id & (h->ih_cap - 1); @@ -119,7 +119,7 @@ nni_hash_find(nni_idhash *h, uint32_t id, void **valp) } int -nni_idhash_find(nni_idhash *h, uint32_t id, void **valp) +nni_idhash_find(nni_idhash *h, uint64_t id, void **valp) { int rv; @@ -195,11 +195,11 @@ nni_hash_resize(nni_idhash *h) } int -nni_idhash_remove(nni_idhash *h, uint32_t id) +nni_idhash_remove(nni_idhash *h, uint64_t id) { - int rv; - void * val; - uint32_t index; + int rv; + void * val; + size_t index; nni_mtx_lock(&h->ih_mtx); // First check that it is in the table. This may double the @@ -240,9 +240,9 @@ nni_idhash_remove(nni_idhash *h, uint32_t id) } static int -nni_hash_insert(nni_idhash *h, uint32_t id, void *val) +nni_hash_insert(nni_idhash *h, uint64_t id, void *val) { - uint32_t index; + size_t index; if ((id < h->ih_minval) || (id > h->ih_maxval)) { return (NNG_EINVAL); @@ -280,7 +280,7 @@ nni_hash_insert(nni_idhash *h, uint32_t id, void *val) } int -nni_idhash_insert(nni_idhash *h, uint32_t id, void *val) +nni_idhash_insert(nni_idhash *h, uint64_t id, void *val) { int rv; @@ -291,9 +291,9 @@ nni_idhash_insert(nni_idhash *h, uint32_t id, void *val) } int -nni_idhash_alloc(nni_idhash *h, uint32_t *idp, void *val) +nni_idhash_alloc(nni_idhash *h, uint64_t *idp, void *val) { - uint32_t id; + uint64_t id; void * scrap; int rv; nni_mtx_lock(&h->ih_mtx); diff --git a/src/core/idhash.h b/src/core/idhash.h index b21a681b..c2cecf81 100644 --- a/src/core/idhash.h +++ b/src/core/idhash.h @@ -28,13 +28,14 @@ typedef struct nni_idhash_entry nni_idhash_entry; extern int nni_idhash_init(nni_idhash **); extern void nni_idhash_fini(nni_idhash *); -extern void nni_idhash_set_limits(nni_idhash *, uint32_t, uint32_t, uint32_t); +extern void nni_idhash_set_limits(nni_idhash *, uint64_t, uint64_t, uint64_t); extern int nni_idhash_create(nni_idhash **); extern void nni_idhash_destroy(nni_idhash *); -extern int nni_idhash_find(nni_idhash *, uint32_t, void **); -extern int nni_idhash_remove(nni_idhash *, uint32_t); -extern int nni_idhash_insert(nni_idhash *, uint32_t, void *); -extern int nni_idhash_alloc(nni_idhash *, uint32_t *, void *); +extern int nni_idhash_find(nni_idhash *, uint64_t, void **); +extern int nni_idhash_remove(nni_idhash *, uint64_t); +extern int nni_idhash_insert(nni_idhash *, uint64_t, void *); +extern int nni_idhash_alloc(nni_idhash *, uint64_t *, void *); + extern size_t nni_idhash_count(nni_idhash *); #endif // CORE_IDHASH_H diff --git a/src/core/pipe.c b/src/core/pipe.c index 94524da4..0c024c66 100644 --- a/src/core/pipe.c +++ b/src/core/pipe.c @@ -16,7 +16,7 @@ // performed in the context of the protocol. struct nni_pipe { - uint32_t p_id; + uint64_t p_id; nni_tran_pipe p_tran_ops; void * p_tran_data; void * p_proto_data; @@ -135,7 +135,7 @@ nni_pipe_destroy(nni_pipe *p) uint32_t nni_pipe_id(nni_pipe *p) { - return (p->p_id); + return ((uint32_t) p->p_id); } void diff --git a/src/core/socket.c b/src/core/socket.c index 79c1602b..765a6d0c 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -31,7 +31,7 @@ struct nni_socket { nni_cv s_cv; nni_cv s_close_cv; - uint32_t s_id; + uint64_t s_id; uint32_t s_flags; unsigned s_refcnt; // protected by global lock void * s_data; // Protocol private @@ -81,7 +81,7 @@ nni_free_opt(nni_sockopt *opt) uint32_t nni_sock_id(nni_sock *s) { - return (s->s_id); + return ((uint32_t) s->s_id); } // nni_sock_sendq and nni_sock_recvq are called by the protocol to obtain diff --git a/tests/idhash.c b/tests/idhash.c index 45c0439a..21f66c58 100644 --- a/tests/idhash.c +++ b/tests/idhash.c @@ -15,117 +15,144 @@ Main({ nni_init(); - Test("General ID Hash", - { - int rv; - - Convey("Given an id hash", { - nni_idhash *h = NULL; - - So(nni_idhash_init(&h) == 0); - So(h != NULL); - So(nni_idhash_count(h) == 0); - - Reset({ nni_idhash_fini(h); }); - - Convey("We can insert an element", { - char *five = "five"; - char *four = "four"; - rv = nni_idhash_insert(h, 5, five); - So(nni_idhash_count(h) == 1); - So(rv == 0); - - Convey("And we can find it", { - void *ptr; - rv = nni_idhash_find(h, 5, &ptr); - So(rv == 0); - So(ptr == five); - }); - Convey("We can delete it", { - void *ptr; - rv = nni_idhash_remove(h, 5); - So(rv == 0); - rv = nni_idhash_find(h, 5, &ptr); - So(rv == NNG_ENOENT); - }); - Convey("We can change the value", { - void *ptr; - So(nni_idhash_insert(h, 5, four) == - 0); - So(nni_idhash_count(h) == 1); - So(nni_idhash_find(h, 5, &ptr) == - 0); - So(ptr == four); - }); - Convey("We can insert a hash collision", { - void *ptr; - So(nni_idhash_insert( - h, 13, four) == 0); - So(nni_idhash_count(h) == 2); - So(nni_idhash_find(h, 5, &ptr) == - 0); - So(ptr == five); - So(nni_idhash_find(h, 13, &ptr) == - 0); - So(ptr == four); - Convey("And delete intermediate", { - So(nni_idhash_remove( - h, 5) == 0); - ptr = NULL; - So(nni_idhash_find( - h, 13, &ptr) == 0); - So(ptr == four); - }); - }); - - }); - Convey("We cannot find bogus values", { - void *ptr; - ptr = NULL; - rv = nni_idhash_find(h, 42, &ptr); - So(rv == NNG_ENOENT); - So(ptr == NULL); - }); - }); - }) - - Test("Resize ID Hash", { - int expect[1024]; - int i; - - for (i = 0; i < 1024; i++) { - expect[i] = i; - } - Convey("Given an id hash", { - nni_idhash *h; - - So(nni_idhash_init(&h) == 0); - So(nni_idhash_count(h) == 0); - - Reset({ nni_idhash_fini(h); }); - - Convey("We can insert 1024 items", { - for (i = 0; i < 1024; i++) { - nni_idhash_insert( - h, i, &expect[i]); - } - So(nni_idhash_count(h) == 1024); - - Convey("We can remove them", { - for (i = 0; i < 1024; i++) { - nni_idhash_remove(h, i); - } - So(nni_idhash_count(h) == 0); - }); - }); - }); - }); + atexit(nni_fini); + Test("General ID Hash", { + int rv; + + Convey("Given an id hash", { + nni_idhash *h = NULL; + + So(nni_idhash_init(&h) == 0); + So(h != NULL); + So(nni_idhash_count(h) == 0); + + Reset({ nni_idhash_fini(h); }); + + Convey("We can insert an element", { + char *five = "five"; + char *four = "four"; + rv = nni_idhash_insert(h, 5, five); + So(nni_idhash_count(h) == 1); + So(rv == 0); + + Convey("And we can find it", { + void *ptr; + rv = nni_idhash_find(h, 5, &ptr); + So(rv == 0); + So(ptr == five); + }); + Convey("We can delete it", { + void *ptr; + rv = nni_idhash_remove(h, 5); + So(rv == 0); + rv = nni_idhash_find(h, 5, &ptr); + So(rv == NNG_ENOENT); + }); + Convey("We can change the value", { + void *ptr; + So(nni_idhash_insert(h, 5, four) == 0); + So(nni_idhash_count(h) == 1); + So(nni_idhash_find(h, 5, &ptr) == 0); + So(ptr == four); + }); + Convey("We can insert a hash collision", { + void *ptr; + So(nni_idhash_insert(h, 13, four) == + 0); + So(nni_idhash_count(h) == 2); + So(nni_idhash_find(h, 5, &ptr) == 0); + So(ptr == five); + So(nni_idhash_find(h, 13, &ptr) == 0); + So(ptr == four); + Convey("And delete intermediate", { + So(nni_idhash_remove(h, 5) == + 0); + ptr = NULL; + So(nni_idhash_find( + h, 13, &ptr) == 0); + So(ptr == four); + }); + }); + + }); + Convey("We cannot find bogus values", { + void *ptr; + ptr = NULL; + rv = nni_idhash_find(h, 42, &ptr); + So(rv == NNG_ENOENT); + So(ptr == NULL); + }); + + Convey("Range checks work", { + char *bad = "bad"; + + nni_idhash_set_limits(h, 1, 10, 1); + So(nni_idhash_insert(h, 20, bad) == + NNG_EINVAL); + }); + + Convey("64-bit hash values work", { + char * huge = "huge"; + void * ptr = NULL; + uint64_t hugenum = 0x1234567890ULL; + + nni_idhash_set_limits(h, 1, 1ULL << 63, 1); + So(nni_idhash_insert(h, hugenum, huge) == 0); + So(nni_idhash_find(h, hugenum, &ptr) == 0); + So((char *) ptr == huge); + }); + + Convey("64-bit dynvals work", { + char * huge = "dynhuge"; + void * ptr = NULL; + uint64_t id; + + nni_idhash_set_limits( + h, 1ULL << 32, 1ULL << 63, 1); + So(nni_idhash_alloc(h, &id, huge) == 0); + So(id > 0xffffffff); + So(nni_idhash_find(h, id, &ptr) == 0); + So((char *) ptr == huge); + }); + }); + }); + + Test("Resize ID Hash", { + int expect[1024]; + int i; + + for (i = 0; i < 1024; i++) { + expect[i] = i; + } + Convey("Given an id hash", { + nni_idhash *h; + + So(nni_idhash_init(&h) == 0); + So(nni_idhash_count(h) == 0); + + Reset({ nni_idhash_fini(h); }); + + Convey("We can insert 1024 items", { + for (i = 0; i < 1024; i++) { + nni_idhash_insert(h, i, &expect[i]); + } + So(nni_idhash_count(h) == 1024); + + Convey("We can remove them", { + for (i = 0; i < 1024; i++) { + nni_idhash_remove(h, i); + } + So(nni_idhash_count(h) == 0); + }); + }); + }); + }); Test("Dynamic ID generation", { Convey("Given a small ID hash", { nni_idhash *h; int expect[5]; - uint32_t id; + uint64_t id; int i; So(nni_idhash_init(&h) == 0); Reset({ nni_idhash_fini(h); }); @@ -156,6 +183,4 @@ Main({ }); }); }); - - nni_fini(); }); |
