1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
|
/*
* Copyright (c) mjh-EDV Beratung, 1996-1999
* mjh-EDV Beratung - 63263 Neu-Isenburg - Rosenstrasse 12
* Tel +49 6102 328279 - Fax +49 6102 328278
* Email info@mjh.teddy-net.com
*
* Author: Jordan Hrycaj <jordan@mjh.teddy-net.com>
*
* $Id: hlst.c,v 1.33 2003/02/27 10:09:57 renaud Exp $
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* HLST - a simple hash list manager
*/
#include <includes.h>
#define __HLST_INTERNAL__
#include "hlst.h"
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
/* ------------------------------------------------------------------------- *
* private definitions *
* ------------------------------------------------------------------------- */
/* XMALLOC returns memory initialized to zero */
#define XMALLOC(x) emalloc(x)
#define XFREE(x) efree(&(x))
/* default number of hash buckets per list */
#define DEFAULT_BUCKETS 53
/* global factor to be applied internally to any estimated_size_hint */
#define DEFAULT_PERCENTAGE_COMPRESSOR 80
typedef
struct _sorter {
int dirty ;
unsigned size ;
struct _hashqueue *inx [1] ;
/* varable length, pointer aligned */
} sorter ;
typedef
struct _hashqueue { /* linked list of bucket entries */
void *contents;
struct _hashqueue *next;
unsigned keylen; /* length of current key */
int locked; /* currently visited my some hash walk */
struct _sorter *backlink; /* there might be an index on that list */
# ifdef ENABLE_RHLST
int tranum; /* transaction id, used for caching */
# endif /* ENABLE_RHLST */
char key [1]; /* varable size key */
/* varable length, pointer aligned */
} hashqueue ;
/* get the byte offset of a field */
#define STRUCT_OFFSET(type, field) \
((char*)(&((type*)0)->field)-(char*)0)
/* given a pointer to field "field" from a structure of type "type",
get the structure pointer */
#define REVERT_FIELD_PTR(p, type, field) \
((type*)(((char*)p) - STRUCT_OFFSET (type,field)))
/* ------------------------------------------------------------------------- *
* private variables *
* ------------------------------------------------------------------------- */
/* non-empty, 0-terminated sorted list in the first entry:
possible table sizes and hash factors (rel prime to the table size) */
/* FIXME: parameters need to be double ckecked, here */
static const hash_defs hints [] = {
{ 11, 7},
{ 23, 11},
{ 53, 31},
{ 73, 37},
{101, 40},
{151, 43},
{269, 97},
{509, 101},
{577, 107},
{0,0}
};
static unsigned
size_hint_percentage_compressor = DEFAULT_PERCENTAGE_COMPRESSOR ;
/* used for custum sorting -- not thread safe */
static void *sorter_desc ;
static int (*sorter_fn)(void*,const char*,unsigned,const char*,unsigned);
#ifdef USE_PTHREADS
/* some global sync for logging etc -- make custum sorting thread safe */
static int glob_mutex_initialized = 0;
static pthread_mutex_t glob_mutex;
#endif
/* ------------------------------------------------------------------------- *
* hashed index ank key length calculation *
* ------------------------------------------------------------------------- */
/* make a hash from the argument key string */
#define _GET_HASH_AND_STRLEN( h, H, L, key) { \
const char *s = (key) ; \
(H) = *s ; \
(L) = 1 ; \
goto S; do { \
(H) *= (h)->z.fac ; /* shift accu */ \
(H) += * s ; /* get char */ \
(L) ++ ; /* word length */ \
S:(H) %= (h)->z.mod ; /* module size */ \
} while (*s ++) ; }
/* make a hash from a generic argument key of given length */
#define _GET_HASH( h, H, len, key) { \
const char *s = (key) ; \
int l = (len) ; \
(H) = *s ; \
goto T; do { \
(H) *= (h)->z.fac ; /* shift accu */ \
(H) += * ++ s ; /* get char */ \
T:(H) %= (h)->z.mod ; /* module size */ \
} while (-- l); }
/* combine that methods, above */
#define GET_HASH_AND_LEN( h, H, len, key) \
{ if (len) _GET_HASH (h,H,len,key) else _GET_HASH_AND_STRLEN(h,H,len,key) }
#ifdef USE_PTHREADS
# define mutex_init(x) pthread_mutex_init (x, 0)
# define mutex_destroy(x) pthread_mutex_destroy (x)
# define mutex_lock(x) pthread_mutex_lock (x)
# define mutex_unlock(x) pthread_mutex_unlock (x)
# define globally_lock() _glob_lock ()
# define globally_unlock() mutex_unlock (&glob_mutex)
static void
_glob_lock
(void)
{
if (glob_mutex_initialized == 0) {
mutex_init (&glob_mutex);
glob_mutex_initialized = 1;
}
mutex_lock (&glob_mutex);
}
#else
# define mutex_init(x) /* empty */
# define mutex_lock(x) /* empty */
# define mutex_unlock(x) /* empty */
# define mutex_destroy(x) /* empty */
# define globally_lock() /* empty */
# define globally_unlock() /* empty */
#endif
/* ------------------------------------------------------------------------- *
* private functions *
* ------------------------------------------------------------------------- */
#define XMCOPY(p,len) memcpy (XMALLOC (len), (p), (len))
static hashqueue **
find_bucket_ptr
(hashqueue **Q,
const char *key,
unsigned len)
{
hashqueue *q ;
while (q = *Q, q != 0) {
if (len == q->keylen && memcmp (q->key, key, len) == 0)
return Q ;
Q = &q->next ;
}
errno = ENOENT ;
return 0;
}
/* qsort call back functions */
static int
__comp
(hashqueue** left,
hashqueue** right)
{
int n, min ;
if ((min = (*left)->keylen) > (*right)->keylen)
min = (*right)->keylen ;
if ((n = memcmp ((*left)->key, (*right)->key, min)) != 0)
return n;
return (*left)->keylen - (*right)->keylen ;
}
static int
__comp_custom
(hashqueue** left,
hashqueue** right)
{
return (*sorter_fn) (sorter_desc,
(*left)->key,
(*left)->keylen,
(*right)->key,
(*right)->keylen);
}
/* ------------------------------------------------------------------------- *
* public functions: open/close management *
* ------------------------------------------------------------------------- */
hlst *
create_hlst
(unsigned estimated_size_hint,
void (*clup)(void*,void*,char*,unsigned),
void *state)
{
const hash_defs *hd = hints ;
hlst *h ;
if (estimated_size_hint == 0)
estimated_size_hint = DEFAULT_BUCKETS ;
/* adjust accoording to policy */
estimated_size_hint *= size_hint_percentage_compressor ;
estimated_size_hint /= 100 ;
/* find appropriate list size, will stop at the last entry */
# ifdef _WIN32
for (;;) {
const hash_defs *hd1 = hd+1 ;
if (hd1->mod == 0 || hd1->mod > estimated_size_hint)
break ;
++ hd ;
}
# else
while (hd [1].mod != 0 && hd [1].mod <= estimated_size_hint)
++ hd ;
# endif
h = XMALLOC (sizeof (hlst) + (hd->mod - 1) * sizeof (void*));
h->z = *hd ;
h->clup = clup ;
h->clup_state = state ;
return h;
}
hlst *
copy_hlst
(hlst *h,
unsigned estimated_size_hint,
void *(*copy)(void*,void*,char*,unsigned),
void *cpstate,
void (*clup)(void*,void*,char*,unsigned),
void *state)
{
const hash_defs *hd = hints ;
hlst *new ;
unsigned i, copy_only ;
/* sanity check */
if (h == 0) {
errno = EINVAL;
return 0 ;
}
if (estimated_size_hint == 0)
/* get default from list to copy */
hd = &h->z ;
else {
/* adjust accoording to policy */
estimated_size_hint *= size_hint_percentage_compressor ;
estimated_size_hint /= 100 ;
if (estimated_size_hint != h->z.mod) {
/* find appropriate list size, will stop at the last entry */
# ifdef _WIN32
for (;;) {
const hash_defs *hd1 = hd+1 ;
if (hd1->mod == 0 || hd1->mod > estimated_size_hint)
break ;
++ hd ;
}
# else
while (hd [1].mod != 0 && hd [1].mod <= estimated_size_hint)
++ hd ;
# endif
}
}
new = (copy_only = (hd->mod == h->z.mod && copy == 0)) == 0
/* create a new list */
? XMALLOC (sizeof (hlst) + (hd->mod - 1) * sizeof (void*))
/* in this case, we can simply copy blocks, later on */
: XMCOPY (h, sizeof (hlst) + (h->z.mod - 1) * sizeof (void*))
;
new->walk = 0 ;
new->clup = clup ;
new->clup_state = state ;
new->total_entries = h->total_entries ;
/* we organize the new list while looping over the old one */
for (i = 0; i < h->z.mod; i ++) {
/* get hash queue */
hashqueue *p = h->bucket [i] ;
new->bucket [i] = 0 ;
while (p != 0) {
hashqueue *q ;
void **Q ;
if (copy_only) {
/* copy entry */
q = XMCOPY (p, sizeof (hashqueue) + p->keylen-1) ;
q->locked = 0 ;
/* link to bucket queue */
q->next = ((hashqueue*)new->bucket [i]) ;
new->bucket [i] = q ;
/* get contents pointer */
Q = &q->contents ;
} else {
/* create new entry */
if ((Q = make_hlst ((hlst*)new, p->key, p->keylen)) == 0) {
fprintf (stderr, __FILE__
"(%d): [make_hlst() == 0] serious bug, "
"corrupt target list -- please report, aborting.\n",
__LINE__);
exit (2) ;
}
}
if (copy != 0 && /* duplicate user contents */
(*Q = (*copy) (cpstate, p->contents, p->key, p->keylen)) == 0 &&
errno != 0) {
int e = errno ;
/* stop: clean up */
destroy_hlst (new);
errno = e ;
return 0;
}
/* get next template */
p = p->next ;
}
}
return new ;
}
void
flush_hlst
(hlst *h,
void (*clup)(void*desc,void*,char*,unsigned),
void* desc)
{
unsigned i;
hsrch *s ;
/* sanity check */
if (h == 0) return ;
if (clup == 0) {
clup = h->clup ;
desc = h->clup_state ;
}
/* remove sorter */
if (h->access != 0) {
XFREE (h->access);
h->access = 0;
}
for (i = 0; i < h->z.mod; i ++) {
/* do with this bucket */
hashqueue *p, **P = (hashqueue**)h->bucket + i ;
while ((p = *P) != 0) {
/* unlink that node, so even circular sublists would not loop */
*P = p->next ;
if (clup != 0 && p->contents != 0)
(*clup) (desc, p->contents, p->key, p->keylen);
XFREE (p);
}
} /* for */
/* cannot visit any node, anymore */
for (s = h->walk; s != 0; s = s->next) {
s->hlist = 0 ; /* next_hlst_search() will stop, that way */
# ifdef ENABLE_RHLST
if (s->clup != 0) { /* clean up by call back as early as possible */
(*s->clup)(s->clup_state);
s->clup = 0 ;
}
# endif
}
/* statistics */
h->total_entries = 0 ;
}
void
destroy_hlst
(hlst *h)
{
/* sanity check */
if (h == 0) return ;
flush_hlst (h, 0, 0);
if (h->clup != 0)
(*h->clup) (h->clup_state,0,0,0);
XFREE (h);
}
/* ------------------------------------------------------------------------- *
* public functions: manipulate slots *
* ------------------------------------------------------------------------- */
void**
find_hlst
(hlst *h,
const char *key,
unsigned len)
{
hashqueue **Q;
int inx ;
/* sanity check */
if (h == 0 || key == 0) {
errno = EINVAL;
return 0;
}
GET_HASH_AND_LEN (h, inx, len, key);
if ((Q = find_bucket_ptr ((hashqueue**)h->bucket + inx, key, len)) != 0)
return &(*Q)->contents ;
errno = ENOENT;
return 0;
}
void**
make_hlst
(hlst *_h,
const char *key,
unsigned len)
{
hlst *h = (hlst *)_h ;
hashqueue *q ;
int inx ;
/* sanity check */
if (h == 0 || key == 0) {
errno = EINVAL;
return 0;
}
GET_HASH_AND_LEN (h, inx, len, key);
/* cannot have duplicate entries */
if (find_bucket_ptr ((hashqueue**)h->bucket + inx, key, len) != 0) {
errno = EEXIST;
return 0;
}
/* make entry */
q = XMALLOC (sizeof (hashqueue) + len - 1);
memcpy (q->key, key, q->keylen = len);
/* link entry */
q->next = h->bucket [inx] ;
h->bucket [inx] = q ;
/* statistics */
h->total_entries ++ ;
if (h->access != 0)
/* mark the sorter ready for rebuilt */
h->access->dirty = 1 ;
/* always returns some pointer */
return &q->contents ;
}
int
delete_hlst
(hlst *h,
const char *key,
unsigned len)
{
hashqueue *q, **Q;
hsrch *s;
unsigned inx ;
/* sanity check */
if (h == 0 || key == 0) {
errno = EINVAL;
return -1;
}
GET_HASH_AND_LEN (h, inx, len, key);
if ((Q = find_bucket_ptr ((hashqueue**)h->bucket + inx, key, len)) == 0) {
errno = ENOENT ;
return -1 ;
}
q = *Q ;
if (q->locked)
/* cannot visit that node, anymore so update the walk decriptors */
for (s = h->walk; s != 0; s = s->next)
if (s->ntry == q) /* find find descriptor */
s->ntry = q->next ; /* visit successor, instead */
/* set that index link idle */
if (h->access != 0)
if (q->backlink != 0) {
*q->backlink->inx = 0 ;
h->access->dirty = 1;
}
/* unlink */
*Q = q->next ;
/* statistics */
h->total_entries -- ;
if (h->clup != 0 && q->contents != 0) /* clean up call back */
(*h->clup) (h->clup_state, q->contents, q->key, q->keylen);
XFREE (q);
return 0;
}
/* ------------------------------------------------------------------------- *
* public functions: misc *
* ------------------------------------------------------------------------- */
/* global factor to be applied internally to any estimated_size_hint */
unsigned
compress_hlst_index
(unsigned size_hint_percentage)
{
unsigned o_index = size_hint_percentage_compressor ;
if ((size_hint_percentage_compressor = size_hint_percentage) > 100)
size_hint_percentage_compressor = 100 ;
return o_index;
}
char *
query_key_hlst
(void **t)
{
if (t == 0) {
errno = EINVAL;
return 0;
}
return REVERT_FIELD_PTR (t, hashqueue, contents)->key ;
}
unsigned
query_keylen_hlst
(void **t)
{
if (t == 0) {
errno = EINVAL;
return 0;
}
return REVERT_FIELD_PTR (t, hashqueue, contents)->keylen ;
}
#ifdef ENABLE_RHLST
int
query_tranum_hlst
(void **t)
{
if (t == 0) {
errno = EINVAL;
return 0;
}
errno = 0 ;
return REVERT_FIELD_PTR (t, hashqueue, contents)->tranum ;
}
int
set_tranum_hlst
(void **t,
int n)
{
int last ;
if (t == 0) {
errno = EINVAL;
return 0;
}
errno = 0 ;
last = REVERT_FIELD_PTR (t, hashqueue, contents)->tranum ;
REVERT_FIELD_PTR (t, hashqueue, contents)->tranum = n ;
return last;
}
#endif /* ENABLE_RHLST */
unsigned
query_hlst_size
(hlst *h)
{
if (h == 0) {
errno = EINVAL;
return 0;
}
errno = 0 ;
return h->total_entries ;
}
/* ------------------------------------------------------------------------- *
* public functions: search that list, itemwise *
* ------------------------------------------------------------------------- */
hsrch*
open_hlst_search
(hlst *h)
{
hsrch *s ;
/* sanity check */
if (h == 0) {
errno = EINVAL;
return 0;
}
s = XMALLOC (sizeof (hsrch));
s->hlist = h ; /* current hash list, to walk on */
s->bucket_id = -1 ;
s->ntry = 0 ; /* before the first entry */
s->next = h->walk ; /* more such entries */
h->walk = s ;
return s;
}
void **
next_hlst_search
(hsrch *s)
{
hlst *h ;
void ** V ;
/* sanity check */
if (s == 0) {
errno = EINVAL;
return 0;
}
/* get the hash list */
if ((h = s->hlist) == 0) {
/* list has been flushed */
errno = ENOENT ;
return 0;
}
if (s->ntry != 0)
s->ntry->locked -- ; /* release that node */
else
do { /* find next node */
/* get bucket, check for end-of-list */
if (++ s->bucket_id >= h->z.mod) {
errno = 0;
return 0 ;
}
s->ntry = h->bucket [s->bucket_id] ;
} while (s->ntry == 0) ;
/* get node contents as return value */
V = &s->ntry->contents ;
/* set to next value */
if ((s->ntry = s->ntry->next) != 0)
s->ntry->locked ++ ; /* mark it visited */
return V;
}
void
close_hlst_search
(hsrch *s)
{
hsrch **U, *u;
/* sanity check */
if (s == 0) return ;
/* is this a stale walk descriptor? */
if (s->hlist == 0) {
XFREE (s);
return ;
}
/* unlink current walk descriptor */
U = &s->hlist->walk ;
while (u = *U, u != 0) {
if (u == s) { /* find the link pointer for that record */
if (u->ntry != 0) /* release that particular node */
u->ntry->locked -- ;
*U = u->next ; /* unlink the walk descriptor */
# ifdef ENABLE_RHLST
if (u->clup != 0) /* clean up peripheral my call back fn */
(*u->clup)(u->clup_state);
# endif
XFREE (u); /* done */
return ;
}
if (u->next == u) { /* XXXXXXXXXXXXX Grrr - should not happen (jh) */
fprintf (stderr,
"%s (%d): [u->next == u] serious bug -- please report\n",
__FILE__,
__LINE__);
u->next = 0 ;
return;
}
U = &u->next; /* set to successor link */
}
}
int
for_hlst_do
(hlst *h,
int (*fn)(void*,void*,char*,unsigned),
void *state)
{
unsigned i ;
int n ;
/* sanity check */
if (h == 0 || fn == 0) {
errno = EINVAL;
return -1;
}
/* looping over the buckets */
for (i = 0; i < h->z.mod; i ++) {
/* get hash queue */
hashqueue *p = h->bucket [i] ;
while (p != 0) {
/* get next, the cb function could delete the current entry */
hashqueue *q = p->next ;
if ((n = (*fn) (state, p->contents, p->key, p->keylen)) < 0)
return -1;
if (n)
return n;
p = q ;
}
}
return 0;
}
/* ------------------------------------------------------------------------- *
* public functions: sorter index *
* ------------------------------------------------------------------------- */
void
sort_hlst
(hlst *h)
{
unsigned i;
hashqueue **ix ;
int (*sorter_cb)(const void*,const void*);
if (h == 0) return ;
/* create an access array with entry pointers */
if (h->access != 0) {
/* nothing has changed, yet */
if (h->access->dirty == 0)
return ;
XFREE (h->access) ;
}
h->access = XMALLOC
(sizeof (sorter) + (h->total_entries - 1) * sizeof (hashqueue*));
h->access->size = h->total_entries ;
/* link that array somehow to the list entries */
ix = h->access->inx ;
/* looping over the buckets */
for (i = 0; i < h->z.mod; i ++) {
/* looping over the hash queue */
hashqueue *p = h->bucket [i] ;
while (p != 0) {
/* link into access array */
* ix ++ = p ;
/* get next */
p = p->next ;
}
}
/* check comparison function */
if (h->sorter_fn != 0) {
globally_lock () ; /* not thread safe, otherwise */
sorter_fn = h->sorter_fn ;
sorter_desc = h->sorter_desc ;
sorter_cb = (int(*)(const void*, const void*))__comp_custom ;
} else {
sorter_cb = (int(*)(const void*, const void*))__comp ;
}
/* sort that access array */
qsort (h->access->inx, h->total_entries, sizeof (hashqueue*), sorter_cb);
if (h->sorter_fn != 0)
{
globally_unlock () ;
}
}
int
csort_hlst
(hlst *h,
int (*fn)(void*,const char*,unsigned,const char*,unsigned),
void *fn_desc)
{
if (h == 0) {
errno = EINVAL;
return 0;
}
h->sorter_fn = fn;
h->sorter_desc = fn_desc;
return 0;
}
void **
inx_hlst
(hlst *h,
unsigned n)
{
hashqueue *p ;
if (h == 0) {
errno = EINVAL;
return 0;
}
if (h->access == 0) {
errno = ESRCH;
return 0;
}
if (n < h->access->size && (p = h->access->inx [n]) != 0)
return &p->contents ;
errno = ENOENT;
return 0 ;
}
void
unsort_hlst
(hlst *h)
{
if (h == 0 || h->access == 0) return ;
XFREE (h->access) ;
h->access = 0;
}
/* ------------------------------------------------------------------------- *
* public functions: statistics *
* ------------------------------------------------------------------------- */
int
hlst_buckets
(hlst *h)
{
if (h == 0) {
errno = EINVAL;
return -1;
}
return h->z.mod;
}
typedef
struct _hstatistics {
struct {
unsigned busy, idle ;
struct { unsigned entries, squares ; } sum ;
} buckets ;
struct {
unsigned min, max ;
} fill ;
} hstatistics ;
static void
__hstatistics_fn
(hstatistics *state,
unsigned fill)
{
if (fill == 0) {
state->buckets.idle ++ ;
return ;
}
state->buckets.busy ++ ;
state->buckets.sum.entries += fill ;
fill *= fill ;
state->buckets.sum.squares += fill ;
if (fill > state->fill.max)
state->fill.max = fill ;
if (fill < state->fill.min)
state->fill.min = fill ;
}
void
hlst_statistics
(hlst *h,
void (*fn) (void*, unsigned),
void *state)
{
unsigned i, sum;
float var, mu ;
hstatistics hs;
/* sanity check */
if (h == 0) return ;
if (fn == 0) {
fn = (void(*)(void*,unsigned))__hstatistics_fn ;
hs.fill.min = -1 ;
state = &hs ;
}
/* looping over the buckets */
for (i = 0; i < h->z.mod; i ++) {
/* get hash queue */
hashqueue *p = h->bucket [i] ;
unsigned n = 0;
while (p != 0)
++ n, p = p->next ;
(*fn) (state, n);
}
if (fn != (void(*)(void*,unsigned))__hstatistics_fn)
return ;
sum = hs.buckets.idle + hs.buckets.busy ;
if (hs.buckets.busy <= 1) {
return ;
}
sum = hs.buckets.sum.entries + hs.buckets.idle ;
fprintf (stderr, "Buckets: %u out of %u are busy, min/max fill: %u/%u\n",
hs.buckets.busy, sum, hs.fill.min, hs.fill.max);
/* busy buckets */
mu = hs.buckets.sum.entries / hs.buckets.busy ;
var = hs.buckets.sum.squares / hs.buckets.busy - mu * mu ;
fprintf (stderr, "Busy statistics (mean/stddev): %f/%f\n", mu, var);
/* all buckets */
mu = hs.buckets.sum.entries / sum ;
var = hs.buckets.sum.squares / sum - mu * mu ;
fprintf (stderr, "Total statistics (mean/stddev): %f/%f\n", mu, var);
}
|