2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 * Copyright (c) 1988, 1989, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)radix.c 8.4 (Berkeley) 11/2/94
41 * DEBUG: section 53 Radix Tree data structure implementation
55 #include <sys/types.h>
70 #include <gnumalloc.h>
78 #include <sys/param.h>
85 struct squid_radix_mask
*squid_rn_mkfreelist
;
86 struct squid_radix_node_head
*squid_mask_rnhead
;
87 static char *addmask_key
;
88 static unsigned char normal_chars
[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xFF};
89 static char *rn_zeros
, *rn_ones
;
92 #define rn_masktop (squid_mask_rnhead->rnh_treetop)
93 #define rn_dupedkey rn_u.rn_leaf.rn_Dupedkey
94 #define rn_off rn_u.rn_node.rn_Off
95 #define rn_l rn_u.rn_node.rn_L
96 #define rn_r rn_u.rn_node.rn_R
97 #define rm_mask rm_rmu.rmu_mask
98 #define rm_leaf rm_rmu.rmu_leaf /* extra field would make 32 bytes */
101 #define squid_Bcmp(a, b, l) (l == 0 ? 0 : memcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
102 #define squid_R_Malloc(p, t, n) (p = (t) xmalloc((unsigned int)(n)))
103 #define squid_Free(p) xfree((char *)p)
104 #define squid_MKGet(m) {\
105 if (squid_rn_mkfreelist) {\
106 m = squid_rn_mkfreelist; \
107 squid_rn_mkfreelist = (m)->rm_mklist; \
109 squid_R_Malloc(m, struct squid_radix_mask *, sizeof (*(m)));\
112 #define squid_MKFree(m) { (m)->rm_mklist = squid_rn_mkfreelist; squid_rn_mkfreelist = (m);}
115 #define min(x,y) ((x)<(y)? (x) : (y))
118 * The data structure for the keys is a radix tree with one way
119 * branching removed. The index rn_b at an internal node n represents a bit
120 * position to be tested. The tree is arranged so that all descendants
121 * of a node n have keys whose bits all agree up to position rn_b - 1.
122 * (We say the index of n is rn_b.)
124 * There is at least one descendant which has a one bit at position rn_b,
125 * and at least one with a zero there.
127 * A route is determined by a pair of key and mask. We require that the
128 * bit-wise logical and of the key and mask to be the key.
129 * We define the index of a route to associated with the mask to be
130 * the first bit number in the mask where 0 occurs (with bit number 0
131 * representing the highest order bit).
133 * We say a mask is normal if every bit is 0, past the index of the mask.
134 * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
135 * and m is a normal mask, then the route applies to every descendant of n.
136 * If the index(m) < rn_b, this implies the trailing last few bits of k
137 * before bit b are all 0, (and hence consequently true of every descendant
138 * of n), so the route applies to all descendants of the node as well.
140 * Similar logic shows that a non-normal mask m such that
141 * index(m) <= index(n) could potentially apply to many children of n.
142 * Thus, for each non-host route, we attach its mask to a list at an internal
143 * node as high in the tree as we can go.
145 * The present version of the code makes use of normal routes in short-
146 * circuiting an explict mask and compare operation when testing whether
147 * a key satisfies a normal route, and also in remembering the unique leaf
148 * that governs a subtree.
151 struct squid_radix_node
*
152 squid_rn_search(void *v_arg
, struct squid_radix_node
*head
) {
153 register struct squid_radix_node
*x
;
156 for (x
= head
, v
= v_arg
; x
->rn_b
>= 0;) {
157 if (x
->rn_bmask
& v
[x
->rn_off
])
165 struct squid_radix_node
*
166 squid_rn_search_m(void *v_arg
, struct squid_radix_node
*head
, void *m_arg
) {
167 register struct squid_radix_node
*x
;
168 register caddr_t v
= v_arg
, m
= m_arg
;
170 for (x
= head
; x
->rn_b
>= 0;) {
171 if ((x
->rn_bmask
& m
[x
->rn_off
]) &&
172 (x
->rn_bmask
& v
[x
->rn_off
]))
181 squid_rn_refines(void *m_arg
, void *n_arg
)
183 register caddr_t m
= m_arg
, n
= n_arg
;
184 register caddr_t lim
, lim2
= lim
= n
+ *(u_char
*) n
;
185 int longer
= (*(u_char
*) n
++) - (int) (*(u_char
*) m
++);
186 int masks_are_equal
= 1;
199 if (masks_are_equal
&& (longer
< 0))
200 for (lim2
= m
- longer
; m
< lim2
;)
203 return (!masks_are_equal
);
206 struct squid_radix_node
*
207 squid_rn_lookup(void *v_arg
, void *m_arg
, struct squid_radix_node_head
*head
) {
208 register struct squid_radix_node
*x
;
212 if ((x
= squid_rn_addmask(m_arg
, 1, head
->rnh_treetop
->rn_off
)) == 0)
216 x
= squid_rn_match(v_arg
, head
);
218 while (x
&& x
->rn_mask
!= netmask
)
225 rn_satsifies_leaf(char *trial
, register struct squid_radix_node
*leaf
, int skip
)
227 register char *cp
= trial
, *cp2
= leaf
->rn_key
, *cp3
= leaf
->rn_mask
;
229 int length
= min(*(u_char
*) cp
, *(u_char
*) cp2
);
234 length
= min(length
, *(u_char
*) cp3
);
238 for (cp
+= skip
; cp
< cplim
; cp
++, cp2
++, cp3
++)
239 if ((*cp
^ *cp2
) & *cp3
)
244 struct squid_radix_node
*
245 squid_rn_match(void *v_arg
, struct squid_radix_node_head
*head
) {
247 register struct squid_radix_node
*t
= head
->rnh_treetop
, *x
;
248 register caddr_t cp
= v
, cp2
;
250 struct squid_radix_node
*saved_t
, *top
= t
;
251 int off
= t
->rn_off
, vlen
= *(u_char
*) cp
, matched_off
;
252 register int test
, b
, rn_b
;
255 * Open code squid_rn_search(v, top) to avoid overhead of extra
258 for (; t
->rn_b
>= 0;) {
259 if (t
->rn_bmask
& cp
[t
->rn_off
])
265 * See if we match exactly as a host destination
266 * or at least learn how many bits match, for normal mask finesse.
268 * It doesn't hurt us to limit how many bytes to check
269 * to the length of the mask, since if it matches we had a genuine
270 * match and the leaf we have is the most specific one anyway;
271 * if it didn't match with a shorter length it would fail
272 * with a long one. This wins big for class B&C netmasks which
273 * are probably the most common case...
276 vlen
= *(u_char
*) t
->rn_mask
;
278 cp2
= t
->rn_key
+ off
;
280 for (; cp
< cplim
; cp
++, cp2
++)
284 * This extra grot is in case we are explicitly asked
285 * to look up the default. Ugh!
287 if ((t
->rn_flags
& RNF_ROOT
) && t
->rn_dupedkey
)
291 test
= (*cp
^ *cp2
) & 0xff; /* find first bit that differs */
292 for (b
= 7; (test
>>= 1) > 0;)
294 matched_off
= cp
- v
;
295 b
+= matched_off
<< 3;
298 * If there is a host route in a duped-key chain, it will be first.
300 if ((saved_t
= t
)->rn_mask
== 0)
302 for (; t
; t
= t
->rn_dupedkey
)
304 * Even if we don't match exactly as a host,
305 * we may match if the leaf we wound up at is
308 if (t
->rn_flags
& RNF_NORMAL
) {
311 } else if (rn_satsifies_leaf(v
, t
, matched_off
))
314 /* start searching up the tree */
316 register struct squid_radix_mask
*m
;
318 if ((m
= t
->rn_mklist
)) {
320 * If non-contiguous masks ever become important
321 * we can restore the masking and open coding of
322 * the search and satisfaction test and put the
323 * calculation of "off" back before the "do".
326 if (m
->rm_flags
& RNF_NORMAL
) {
330 off
= min(t
->rn_off
, matched_off
);
331 x
= squid_rn_search_m(v
, t
, m
->rm_mask
);
332 while (x
&& x
->rn_mask
!= m
->rm_mask
)
334 if (x
&& rn_satsifies_leaf(v
, x
, off
))
337 } while ((m
= m
->rm_mklist
));
345 struct squid_radix_node
*rn_clist
;
350 struct squid_radix_node
*
351 squid_rn_newpair(void *v
, int b
, struct squid_radix_node nodes
[2]) {
352 register struct squid_radix_node
*tt
= nodes
, *t
= tt
+ 1;
354 t
->rn_bmask
= 0x80 >> (b
& 7);
358 tt
->rn_key
= (caddr_t
) v
;
360 tt
->rn_flags
= t
->rn_flags
= RNF_ACTIVE
;
362 tt
->rn_info
= rn_nodenum
++;
363 t
->rn_info
= rn_nodenum
++;
365 tt
->rn_ybro
= rn_clist
;
371 struct squid_radix_node
*
372 squid_rn_insert(void *v_arg
, struct squid_radix_node_head
*head
, int *dupentry
, struct squid_radix_node nodes
[2]) {
374 struct squid_radix_node
*top
= head
->rnh_treetop
;
375 int head_off
= top
->rn_off
, vlen
= (int) *((u_char
*) v
);
376 register struct squid_radix_node
*t
= squid_rn_search(v_arg
, top
);
377 register caddr_t cp
= v
+ head_off
;
379 struct squid_radix_node
*tt
;
381 * Find first bit at which v and t->rn_key differ
384 register caddr_t cp2
= t
->rn_key
+ head_off
;
385 register int cmp_res
;
386 caddr_t cplim
= v
+ vlen
;
395 cmp_res
= (cp
[-1] ^ cp2
[-1]) & 0xff;
396 for (b
= (cp
- v
) << 3; cmp_res
; b
--)
400 register struct squid_radix_node
*p
, *x
= top
;
404 if (cp
[x
->rn_off
] & x
->rn_bmask
)
408 } while (b
> (unsigned) x
->rn_b
); /* x->rn_b < b && x->rn_b >= 0 */
411 fprintf(stderr
, "squid_rn_insert: Going In:\n");
414 t
= squid_rn_newpair(v_arg
, b
, nodes
);
416 if ((cp
[p
->rn_off
] & p
->rn_bmask
) == 0)
421 t
->rn_p
= p
; /* frees x, p as temp vars below */
422 if ((cp
[t
->rn_off
] & t
->rn_bmask
) == 0) {
430 log(LOG_DEBUG
, "squid_rn_insert: Coming Out:\n"), traverse(p
);
436 struct squid_radix_node
*
437 squid_rn_addmask(void *n_arg
, int search
, int skip
) {
438 caddr_t netmask
= (caddr_t
) n_arg
;
439 register struct squid_radix_node
*x
;
440 register caddr_t cp
, cplim
;
441 register int b
= 0, mlen
, j
;
442 int maskduplicated
, m0
, isnormal
;
443 struct squid_radix_node
*saved_x
;
444 static int last_zeroed
= 0;
446 if ((mlen
= *(u_char
*) netmask
) > squid_max_keylen
)
447 mlen
= squid_max_keylen
;
451 return (squid_mask_rnhead
->rnh_nodes
);
453 memcpy(addmask_key
+ 1, rn_ones
+ 1, skip
- 1);
454 if ((m0
= mlen
) > skip
)
455 memcpy(addmask_key
+ skip
, netmask
+ skip
, mlen
- skip
);
457 * Trim trailing zeroes.
459 for (cp
= addmask_key
+ mlen
; (cp
> addmask_key
) && cp
[-1] == 0;)
461 mlen
= cp
- addmask_key
;
463 if (m0
>= last_zeroed
)
465 return (squid_mask_rnhead
->rnh_nodes
);
467 if (m0
< last_zeroed
)
468 memset(addmask_key
+ m0
, '\0', last_zeroed
- m0
);
469 *addmask_key
= last_zeroed
= mlen
;
470 x
= squid_rn_search(addmask_key
, rn_masktop
);
471 if (memcmp(addmask_key
, x
->rn_key
, mlen
) != 0)
475 squid_R_Malloc(x
, struct squid_radix_node
*, squid_max_keylen
+ 2 * sizeof(*x
));
476 if ((saved_x
= x
) == 0)
478 memset(x
, '\0', squid_max_keylen
+ 2 * sizeof(*x
));
479 netmask
= cp
= (caddr_t
) (x
+ 2);
480 memcpy(cp
, addmask_key
, mlen
);
481 x
= squid_rn_insert(cp
, squid_mask_rnhead
, &maskduplicated
, x
);
482 if (maskduplicated
) {
483 fprintf(stderr
, "squid_rn_addmask: mask impossibly already in tree");
488 * Calculate index of mask, and check for normalcy.
490 cplim
= netmask
+ mlen
;
492 for (cp
= netmask
+ skip
; (cp
< cplim
) && *(u_char
*) cp
== 0xff;)
495 for (j
= 0x80; (j
& *cp
) != 0; j
>>= 1)
497 if (*cp
!= normal_chars
[b
] || cp
!= (cplim
- 1))
500 b
+= (cp
- netmask
) << 3;
503 x
->rn_flags
|= RNF_NORMAL
;
507 static int /* XXX: arbitrary ordering for non-contiguous masks */
508 rn_lexobetter(void *m_arg
, void *n_arg
)
510 register u_char
*mp
= m_arg
, *np
= n_arg
, *lim
;
513 return 1; /* not really, but need to check longer one first */
515 for (lim
= mp
+ *mp
; mp
< lim
;)
521 static struct squid_radix_mask
*
522 rn_new_radix_mask(struct squid_radix_node
*tt
, struct squid_radix_mask
*next
) {
523 register struct squid_radix_mask
*m
;
527 fprintf(stderr
, "Mask for route not entered\n");
530 memset(m
, '\0', sizeof *m
);
532 m
->rm_flags
= tt
->rn_flags
;
533 if (tt
->rn_flags
& RNF_NORMAL
)
536 m
->rm_mask
= tt
->rn_mask
;
542 struct squid_radix_node
*
543 squid_rn_addroute(void *v_arg
, void *n_arg
, struct squid_radix_node_head
*head
, struct squid_radix_node treenodes
[2]) {
544 caddr_t v
= (caddr_t
) v_arg
, netmask
= (caddr_t
) n_arg
;
545 register struct squid_radix_node
*t
, *x
= NULL
, *tt
;
546 struct squid_radix_node
*saved_tt
, *top
= head
->rnh_treetop
;
547 short b
= 0, b_leaf
= 0;
550 struct squid_radix_mask
*m
, **mp
;
553 * In dealing with non-contiguous masks, there may be
554 * many different routes which have the same mask.
555 * We will find it useful to have a unique pointer to
556 * the mask to speed avoiding duplicate references at
557 * nodes and possibly save time in calculating indices.
560 if ((x
= squid_rn_addmask(netmask
, 0, top
->rn_off
)) == 0)
567 * Deal with duplicated keys: attach node to previous instance
569 saved_tt
= tt
= squid_rn_insert(v
, head
, &keyduplicated
, treenodes
);
571 for (t
= tt
; tt
; t
= tt
, tt
= tt
->rn_dupedkey
) {
572 if (tt
->rn_mask
== netmask
)
576 ((b_leaf
< tt
->rn_b
) || /* index(netmask) > node */
577 squid_rn_refines(netmask
, tt
->rn_mask
) ||
578 rn_lexobetter(netmask
, tt
->rn_mask
))))
582 * If the mask is not duplicated, we wouldn't
583 * find it among possible duplicate key entries
584 * anyway, so the above test doesn't hurt.
586 * We sort the masks for a duplicated key the same way as
587 * in a masklist -- most specific to least specific.
588 * This may require the unfortunate nuisance of relocating
589 * the head of the list.
591 if (tt
== saved_tt
) {
592 struct squid_radix_node
*xx
= x
;
593 /* link in at head of list */
596 tt
->rn_flags
= t
->rn_flags
;
597 tt
->rn_p
= x
= t
->rn_p
;
606 tt
->rn_dupedkey
= t
->rn_dupedkey
;
611 tt
->rn_info
= rn_nodenum
++;
612 t
->rn_info
= rn_nodenum
++;
614 tt
->rn_ybro
= rn_clist
;
617 tt
->rn_key
= (caddr_t
) v
;
619 tt
->rn_flags
= RNF_ACTIVE
;
625 tt
->rn_mask
= netmask
;
627 tt
->rn_flags
|= x
->rn_flags
& RNF_NORMAL
;
632 b_leaf
= -1 - t
->rn_b
;
633 if (t
->rn_r
== saved_tt
)
637 /* Promote general routes from below */
639 for (mp
= &t
->rn_mklist
; x
; x
= x
->rn_dupedkey
)
640 if (x
->rn_mask
&& (x
->rn_b
>= b_leaf
) && x
->rn_mklist
== 0) {
641 if ((*mp
= m
= rn_new_radix_mask(x
, 0)))
644 } else if (x
->rn_mklist
) {
646 * Skip over masks whose index is > that of new node
648 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
)
649 if (m
->rm_b
>= b_leaf
)
655 /* Add new route to highest possible ancestor's list */
656 if ((netmask
== 0) || (b
> t
->rn_b
))
657 return tt
; /* can't lift at all */
662 } while (b
<= t
->rn_b
&& x
!= top
);
664 * Search through routes associated with node to
665 * insert new route according to index.
666 * Need same criteria as when sorting dupedkeys to avoid
667 * double loop on deletion.
669 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
) {
670 if (m
->rm_b
< b_leaf
)
672 if (m
->rm_b
> b_leaf
)
674 if (m
->rm_flags
& RNF_NORMAL
) {
675 mmask
= m
->rm_leaf
->rn_mask
;
676 if (tt
->rn_flags
& RNF_NORMAL
) {
678 "Non-unique normal route, mask not entered");
683 if (mmask
== netmask
) {
688 if (squid_rn_refines(netmask
, mmask
) || rn_lexobetter(netmask
, mmask
))
691 *mp
= rn_new_radix_mask(tt
, *mp
);
695 struct squid_radix_node
*
696 squid_rn_delete(void *v_arg
, void *netmask_arg
, struct squid_radix_node_head
*head
) {
697 register struct squid_radix_node
*t
, *p
, *x
, *tt
;
698 struct squid_radix_mask
*m
, *saved_m
, **mp
;
699 struct squid_radix_node
*dupedkey
, *saved_tt
, *top
;
701 int b
, head_off
, vlen
;
704 netmask
= netmask_arg
;
705 x
= head
->rnh_treetop
;
706 tt
= squid_rn_search(v
, x
);
707 head_off
= x
->rn_off
;
708 vlen
= *(u_char
*) v
;
712 memcmp(v
+ head_off
, tt
->rn_key
+ head_off
, vlen
- head_off
))
715 * Delete our route from mask lists.
718 if ((x
= squid_rn_addmask(netmask
, 1, head_off
)) == 0)
721 while (tt
->rn_mask
!= netmask
)
722 if ((tt
= tt
->rn_dupedkey
) == 0)
725 if (tt
->rn_mask
== 0 || (saved_m
= m
= tt
->rn_mklist
) == 0)
727 if (tt
->rn_flags
& RNF_NORMAL
) {
728 if (m
->rm_leaf
!= tt
|| m
->rm_refs
> 0) {
729 fprintf(stderr
, "squid_rn_delete: inconsistent annotation\n");
730 return 0; /* dangling ref could cause disaster */
733 if (m
->rm_mask
!= tt
->rn_mask
) {
734 fprintf(stderr
, "squid_rn_delete: inconsistent annotation\n");
737 if (--m
->rm_refs
>= 0)
743 goto on1
; /* Wasn't lifted at all */
747 } while (b
<= t
->rn_b
&& x
!= top
);
748 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
)
755 fprintf(stderr
, "squid_rn_delete: couldn't find our annotation\n");
756 if (tt
->rn_flags
& RNF_NORMAL
)
757 return (0); /* Dangling ref to us */
761 * Eliminate us from tree
763 if (tt
->rn_flags
& RNF_ROOT
)
766 /* Get us out of the creation list */
767 for (t
= rn_clist
; t
&& t
->rn_ybro
!= tt
; t
= t
->rn_ybro
) {
770 t
->rn_ybro
= tt
->rn_ybro
;
773 if ((dupedkey
= saved_tt
->rn_dupedkey
)) {
774 if (tt
== saved_tt
) {
782 for (x
= p
= saved_tt
; p
&& p
->rn_dupedkey
!= tt
;)
785 p
->rn_dupedkey
= tt
->rn_dupedkey
;
787 fprintf(stderr
, "squid_rn_delete: couldn't find us\n");
790 if (t
->rn_flags
& RNF_ACTIVE
) {
820 * Demote routes attached to us.
824 for (mp
= &x
->rn_mklist
; (m
= *mp
);)
828 /* If there are any key,mask pairs in a sibling
829 * duped-key chain, some subset will appear sorted
830 * in the same order attached to our mklist */
831 for (m
= t
->rn_mklist
; m
&& x
; x
= x
->rn_dupedkey
)
832 if (m
== x
->rn_mklist
) {
833 struct squid_radix_mask
*mm
= m
->rm_mklist
;
835 if (--(m
->rm_refs
) < 0)
841 fprintf(stderr
, "%s %x at %x\n",
842 "squid_rn_delete: Orphaned Mask", (int) m
, (int) x
);
849 * We may be holding an active internal node in the tree.
869 tt
->rn_flags
&= ~RNF_ACTIVE
;
870 tt
[1].rn_flags
&= ~RNF_ACTIVE
;
875 squid_rn_walktree(struct squid_radix_node_head
*h
, int (*f
) (struct squid_radix_node
*, void *), void *w
)
878 struct squid_radix_node
*base
, *next
;
879 register struct squid_radix_node
*rn
= h
->rnh_treetop
;
881 * This gets complicated because we may delete the node
882 * while applying the function f to it, so we need to calculate
883 * the successor node in advance.
885 /* First time through node, go left */
886 while (rn
->rn_b
>= 0)
890 /* If at right child go back up, otherwise, go right */
891 while (rn
->rn_p
->rn_r
== rn
&& (rn
->rn_flags
& RNF_ROOT
) == 0)
893 /* Find the next *leaf* since next node might vanish, too */
894 for (rn
= rn
->rn_p
->rn_r
; rn
->rn_b
>= 0;)
898 while ((rn
= base
)) {
899 base
= rn
->rn_dupedkey
;
900 if (!(rn
->rn_flags
& RNF_ROOT
) && (error
= (*f
) (rn
, w
)))
904 if (rn
->rn_flags
& RNF_ROOT
)
911 squid_rn_inithead(struct squid_radix_node_head
**head
, int off
)
913 register struct squid_radix_node_head
*rnh
;
914 register struct squid_radix_node
*t
, *tt
, *ttt
;
917 squid_R_Malloc(rnh
, struct squid_radix_node_head
*, sizeof(*rnh
));
920 memset(rnh
, '\0', sizeof(*rnh
));
922 t
= squid_rn_newpair(rn_zeros
, off
, rnh
->rnh_nodes
);
923 ttt
= rnh
->rnh_nodes
+ 2;
927 tt
->rn_flags
= t
->rn_flags
= RNF_ROOT
| RNF_ACTIVE
;
930 ttt
->rn_key
= rn_ones
;
931 rnh
->rnh_addaddr
= squid_rn_addroute
;
932 rnh
->rnh_deladdr
= squid_rn_delete
;
933 rnh
->rnh_matchaddr
= squid_rn_match
;
934 rnh
->rnh_lookup
= squid_rn_lookup
;
935 rnh
->rnh_walktree
= squid_rn_walktree
;
936 rnh
->rnh_treetop
= t
;
947 for (dom
= domains
; dom
; dom
= dom
->dom_next
)
948 if (dom
->dom_maxrtkey
> squid_max_keylen
)
949 squid_max_keylen
= dom
->dom_maxrtkey
;
951 if (squid_max_keylen
== 0) {
953 "squid_rn_init: radix functions require squid_max_keylen be set\n");
956 squid_R_Malloc(rn_zeros
, char *, 3 * squid_max_keylen
);
957 if (rn_zeros
== NULL
) {
958 fprintf(stderr
, "squid_rn_init failed.\n");
961 memset(rn_zeros
, '\0', 3 * squid_max_keylen
);
962 rn_ones
= cp
= rn_zeros
+ squid_max_keylen
;
963 addmask_key
= cplim
= rn_ones
+ squid_max_keylen
;
966 if (squid_rn_inithead(&squid_mask_rnhead
, 0) == 0) {
967 fprintf(stderr
, "rn_init2 failed.\n");