1 /* $OpenBSD: ohash_do.c,v 1.4 2004/06/22 20:00:16 espie Exp $ */
5 /* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "ohash_int.h"
22 static void ohash_resize(struct ohash
*);
25 ohash_resize(struct ohash
*h
)
27 struct _ohash_record
*n
;
31 if (4 * h
->deleted
< h
->total
)
33 else if (3 * h
->deleted
> 2 * h
->total
)
41 STAT_HASH_SIZE
+= ns
- h
->size
;
43 n
= (h
->info
.halloc
)(sizeof(struct _ohash_record
) * ns
, h
->info
.data
);
47 for (j
= 0; j
< h
->size
; j
++) {
48 if (h
->t
[j
].p
!= NULL
&& h
->t
[j
].p
!= DELETED
) {
50 incr
= ((h
->t
[j
].hv
% (ns
- 2)) & ~1) + 1;
51 while (n
[i
].p
!= NULL
) {
60 (h
->info
.hfree
)(h
->t
, sizeof(struct _ohash_record
) * h
->size
,
64 h
->total
-= h
->deleted
;
69 ohash_remove(struct ohash
*h
, unsigned int i
)
71 void *result
= __UNCONST(h
->t
[i
].p
);
73 if (result
== NULL
|| result
== DELETED
)
81 if (h
->deleted
>= MINDELETED
&& 4 * h
->deleted
> h
->total
)
87 ohash_find(struct ohash
*h
, unsigned int i
)
89 if (h
->t
[i
].p
== DELETED
)
92 return __UNCONST(h
->t
[i
].p
);
96 ohash_insert(struct ohash
*h
, unsigned int i
, void *p
)
101 if (h
->t
[i
].p
== DELETED
) {
106 /* Arbitrary resize boundary. Tweak if not efficient enough. */
107 if (++h
->total
* 4 > h
->size
* 3)