4 * DEBUG: section 53 Radix Tree data structure implementation
5 * AUTHOR: NetBSD Derived
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
37 * Copyright (c) 1988, 1989, 1993
38 * The Regents of the University of California. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)radix.c 8.4 (Berkeley) 11/2/94
79 #include <sys/types.h>
94 #include <gnumalloc.h>
102 #include <sys/param.h>
112 int squid_max_keylen
;
113 struct squid_radix_mask
*squid_rn_mkfreelist
;
114 struct squid_radix_node_head
*squid_mask_rnhead
;
115 static char *addmask_key
;
116 static unsigned char normal_chars
[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xFF};
117 static char *rn_zeros
, *rn_ones
;
120 #define rn_masktop (squid_mask_rnhead->rnh_treetop)
121 #define rn_dupedkey rn_u.rn_leaf.rn_Dupedkey
122 #define rn_off rn_u.rn_node.rn_Off
123 #define rn_l rn_u.rn_node.rn_L
124 #define rn_r rn_u.rn_node.rn_R
125 #define rm_mask rm_rmu.rmu_mask
126 #define rm_leaf rm_rmu.rmu_leaf /* extra field would make 32 bytes */
130 #define squid_Bcmp(a, b, l) (l == 0 ? 0 : memcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
131 #define squid_R_Malloc(p, t, n) (p = (t) xmalloc((unsigned int)(n)))
132 #define squid_Free(p) xfree((char *)p)
133 #define squid_MKGet(m) {\
134 if (squid_rn_mkfreelist) {\
135 m = squid_rn_mkfreelist; \
136 squid_rn_mkfreelist = (m)->rm_mklist; \
138 squid_R_Malloc(m, struct squid_radix_mask *, sizeof (*(m)));\
141 #define squid_MKFree(m) { (m)->rm_mklist = squid_rn_mkfreelist; squid_rn_mkfreelist = (m);}
144 #define min(x,y) ((x)<(y)? (x) : (y))
147 * The data structure for the keys is a radix tree with one way
148 * branching removed. The index rn_b at an internal node n represents a bit
149 * position to be tested. The tree is arranged so that all descendants
150 * of a node n have keys whose bits all agree up to position rn_b - 1.
151 * (We say the index of n is rn_b.)
153 * There is at least one descendant which has a one bit at position rn_b,
154 * and at least one with a zero there.
156 * A route is determined by a pair of key and mask. We require that the
157 * bit-wise logical and of the key and mask to be the key.
158 * We define the index of a route to associated with the mask to be
159 * the first bit number in the mask where 0 occurs (with bit number 0
160 * representing the highest order bit).
162 * We say a mask is normal if every bit is 0, past the index of the mask.
163 * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
164 * and m is a normal mask, then the route applies to every descendant of n.
165 * If the index(m) < rn_b, this implies the trailing last few bits of k
166 * before bit b are all 0, (and hence consequently true of every descendant
167 * of n), so the route applies to all descendants of the node as well.
169 * Similar logic shows that a non-normal mask m such that
170 * index(m) <= index(n) could potentially apply to many children of n.
171 * Thus, for each non-host route, we attach its mask to a list at an internal
172 * node as high in the tree as we can go.
174 * The present version of the code makes use of normal routes in short-
175 * circuiting an explict mask and compare operation when testing whether
176 * a key satisfies a normal route, and also in remembering the unique leaf
177 * that governs a subtree.
180 struct squid_radix_node
*
181 squid_rn_search(void *v_arg
, struct squid_radix_node
*head
) {
182 register struct squid_radix_node
*x
;
185 for (x
= head
, v
= v_arg
; x
->rn_b
>= 0;) {
186 if (x
->rn_bmask
& v
[x
->rn_off
])
194 struct squid_radix_node
*
195 squid_rn_search_m(void *v_arg
, struct squid_radix_node
*head
, void *m_arg
) {
196 register struct squid_radix_node
*x
;
197 register caddr_t v
= v_arg
, m
= m_arg
;
199 for (x
= head
; x
->rn_b
>= 0;) {
200 if ((x
->rn_bmask
& m
[x
->rn_off
]) &&
201 (x
->rn_bmask
& v
[x
->rn_off
]))
210 squid_rn_refines(void *m_arg
, void *n_arg
)
212 register caddr_t m
= m_arg
, n
= n_arg
;
213 register caddr_t lim
, lim2
= lim
= n
+ *(u_char
*) n
;
214 int longer
= (*(u_char
*) n
++) - (int) (*(u_char
*) m
++);
215 int masks_are_equal
= 1;
228 if (masks_are_equal
&& (longer
< 0))
229 for (lim2
= m
- longer
; m
< lim2
;)
232 return (!masks_are_equal
);
235 struct squid_radix_node
*
236 squid_rn_lookup(void *v_arg
, void *m_arg
, struct squid_radix_node_head
*head
) {
237 register struct squid_radix_node
*x
;
241 if ((x
= squid_rn_addmask(m_arg
, 1, head
->rnh_treetop
->rn_off
)) == 0)
245 x
= squid_rn_match(v_arg
, head
);
247 while (x
&& x
->rn_mask
!= netmask
)
254 rn_satsifies_leaf(char *trial
, register struct squid_radix_node
*leaf
, int skip
)
256 register char *cp
= trial
, *cp2
= leaf
->rn_key
, *cp3
= leaf
->rn_mask
;
258 int length
= min(*(u_char
*) cp
, *(u_char
*) cp2
);
263 length
= min(length
, *(u_char
*) cp3
);
267 for (cp
+= skip
; cp
< cplim
; cp
++, cp2
++, cp3
++)
268 if ((*cp
^ *cp2
) & *cp3
)
273 struct squid_radix_node
*
274 squid_rn_match(void *v_arg
, struct squid_radix_node_head
*head
) {
276 register struct squid_radix_node
*t
= head
->rnh_treetop
, *x
;
277 register caddr_t cp
= v
, cp2
;
279 struct squid_radix_node
*saved_t
, *top
= t
;
280 int off
= t
->rn_off
, vlen
= *(u_char
*) cp
, matched_off
;
281 register int test
, b
, rn_b
;
284 * Open code squid_rn_search(v, top) to avoid overhead of extra
287 for (; t
->rn_b
>= 0;) {
288 if (t
->rn_bmask
& cp
[t
->rn_off
])
294 * See if we match exactly as a host destination
295 * or at least learn how many bits match, for normal mask finesse.
297 * It doesn't hurt us to limit how many bytes to check
298 * to the length of the mask, since if it matches we had a genuine
299 * match and the leaf we have is the most specific one anyway;
300 * if it didn't match with a shorter length it would fail
301 * with a long one. This wins big for class B&C netmasks which
302 * are probably the most common case...
305 vlen
= *(u_char
*) t
->rn_mask
;
307 cp2
= t
->rn_key
+ off
;
309 for (; cp
< cplim
; cp
++, cp2
++)
313 * This extra grot is in case we are explicitly asked
314 * to look up the default. Ugh!
316 if ((t
->rn_flags
& RNF_ROOT
) && t
->rn_dupedkey
)
320 test
= (*cp
^ *cp2
) & 0xff; /* find first bit that differs */
321 for (b
= 7; (test
>>= 1) > 0;)
323 matched_off
= cp
- v
;
324 b
+= matched_off
<< 3;
327 * If there is a host route in a duped-key chain, it will be first.
329 if ((saved_t
= t
)->rn_mask
== 0)
331 for (; t
; t
= t
->rn_dupedkey
)
333 * Even if we don't match exactly as a host,
334 * we may match if the leaf we wound up at is
337 if (t
->rn_flags
& RNF_NORMAL
) {
340 } else if (rn_satsifies_leaf(v
, t
, matched_off
))
343 /* start searching up the tree */
345 register struct squid_radix_mask
*m
;
347 if ((m
= t
->rn_mklist
)) {
349 * If non-contiguous masks ever become important
350 * we can restore the masking and open coding of
351 * the search and satisfaction test and put the
352 * calculation of "off" back before the "do".
355 if (m
->rm_flags
& RNF_NORMAL
) {
359 off
= min(t
->rn_off
, matched_off
);
360 x
= squid_rn_search_m(v
, t
, m
->rm_mask
);
361 while (x
&& x
->rn_mask
!= m
->rm_mask
)
363 if (x
&& rn_satsifies_leaf(v
, x
, off
))
366 } while ((m
= m
->rm_mklist
));
374 struct squid_radix_node
*rn_clist
;
379 struct squid_radix_node
*
380 squid_rn_newpair(void *v
, int b
, struct squid_radix_node nodes
[2]) {
381 register struct squid_radix_node
*tt
= nodes
, *t
= tt
+ 1;
383 t
->rn_bmask
= 0x80 >> (b
& 7);
387 tt
->rn_key
= (caddr_t
) v
;
389 tt
->rn_flags
= t
->rn_flags
= RNF_ACTIVE
;
391 tt
->rn_info
= rn_nodenum
++;
392 t
->rn_info
= rn_nodenum
++;
394 tt
->rn_ybro
= rn_clist
;
400 struct squid_radix_node
*
401 squid_rn_insert(void *v_arg
, struct squid_radix_node_head
*head
, int *dupentry
, struct squid_radix_node nodes
[2]) {
403 struct squid_radix_node
*top
= head
->rnh_treetop
;
404 int head_off
= top
->rn_off
, vlen
= (int) *((u_char
*) v
);
405 register struct squid_radix_node
*t
= squid_rn_search(v_arg
, top
);
406 register caddr_t cp
= v
+ head_off
;
408 struct squid_radix_node
*tt
;
410 * Find first bit at which v and t->rn_key differ
413 register caddr_t cp2
= t
->rn_key
+ head_off
;
414 register int cmp_res
;
415 caddr_t cplim
= v
+ vlen
;
424 cmp_res
= (cp
[-1] ^ cp2
[-1]) & 0xff;
425 for (b
= (cp
- v
) << 3; cmp_res
; b
--)
429 register struct squid_radix_node
*p
, *x
= top
;
433 if (cp
[x
->rn_off
] & x
->rn_bmask
)
437 } while (b
> (unsigned) x
->rn_b
); /* x->rn_b < b && x->rn_b >= 0 */
440 fprintf(stderr
, "squid_rn_insert: Going In:\n");
443 t
= squid_rn_newpair(v_arg
, b
, nodes
);
445 if ((cp
[p
->rn_off
] & p
->rn_bmask
) == 0)
450 t
->rn_p
= p
; /* frees x, p as temp vars below */
451 if ((cp
[t
->rn_off
] & t
->rn_bmask
) == 0) {
459 log(LOG_DEBUG
, "squid_rn_insert: Coming Out:\n"), traverse(p
);
465 struct squid_radix_node
*
466 squid_rn_addmask(void *n_arg
, int search
, int skip
) {
467 caddr_t netmask
= (caddr_t
) n_arg
;
468 register struct squid_radix_node
*x
;
469 register caddr_t cp
, cplim
;
470 register int b
= 0, mlen
, j
;
471 int maskduplicated
, m0
, isnormal
;
472 struct squid_radix_node
*saved_x
;
473 static int last_zeroed
= 0;
475 if ((mlen
= *(u_char
*) netmask
) > squid_max_keylen
)
476 mlen
= squid_max_keylen
;
480 return (squid_mask_rnhead
->rnh_nodes
);
482 memcpy(addmask_key
+ 1, rn_ones
+ 1, skip
- 1);
483 if ((m0
= mlen
) > skip
)
484 memcpy(addmask_key
+ skip
, netmask
+ skip
, mlen
- skip
);
486 * Trim trailing zeroes.
488 for (cp
= addmask_key
+ mlen
; (cp
> addmask_key
) && cp
[-1] == 0;)
490 mlen
= cp
- addmask_key
;
492 if (m0
>= last_zeroed
)
494 return (squid_mask_rnhead
->rnh_nodes
);
496 if (m0
< last_zeroed
)
497 memset(addmask_key
+ m0
, '\0', last_zeroed
- m0
);
498 *addmask_key
= last_zeroed
= mlen
;
499 x
= squid_rn_search(addmask_key
, rn_masktop
);
500 if (memcmp(addmask_key
, x
->rn_key
, mlen
) != 0)
504 squid_R_Malloc(x
, struct squid_radix_node
*, squid_max_keylen
+ 2 * sizeof(*x
));
505 if ((saved_x
= x
) == 0)
507 memset(x
, '\0', squid_max_keylen
+ 2 * sizeof(*x
));
508 netmask
= cp
= (caddr_t
) (x
+ 2);
509 memcpy(cp
, addmask_key
, mlen
);
510 x
= squid_rn_insert(cp
, squid_mask_rnhead
, &maskduplicated
, x
);
511 if (maskduplicated
) {
512 fprintf(stderr
, "squid_rn_addmask: mask impossibly already in tree");
517 * Calculate index of mask, and check for normalcy.
519 cplim
= netmask
+ mlen
;
521 for (cp
= netmask
+ skip
; (cp
< cplim
) && *(u_char
*) cp
== 0xff;)
524 for (j
= 0x80; (j
& *cp
) != 0; j
>>= 1)
526 if (*cp
!= normal_chars
[b
] || cp
!= (cplim
- 1))
529 b
+= (cp
- netmask
) << 3;
532 x
->rn_flags
|= RNF_NORMAL
;
536 static int /* XXX: arbitrary ordering for non-contiguous masks */
537 rn_lexobetter(void *m_arg
, void *n_arg
)
539 register u_char
*mp
= m_arg
, *np
= n_arg
, *lim
;
542 return 1; /* not really, but need to check longer one first */
544 for (lim
= mp
+ *mp
; mp
< lim
;)
550 static struct squid_radix_mask
*
551 rn_new_radix_mask(struct squid_radix_node
*tt
, struct squid_radix_mask
*next
) {
552 register struct squid_radix_mask
*m
;
556 fprintf(stderr
, "Mask for route not entered\n");
559 memset(m
, '\0', sizeof *m
);
561 m
->rm_flags
= tt
->rn_flags
;
562 if (tt
->rn_flags
& RNF_NORMAL
)
565 m
->rm_mask
= tt
->rn_mask
;
571 struct squid_radix_node
*
572 squid_rn_addroute(void *v_arg
, void *n_arg
, struct squid_radix_node_head
*head
, struct squid_radix_node treenodes
[2]) {
573 caddr_t v
= (caddr_t
) v_arg
, netmask
= (caddr_t
) n_arg
;
574 register struct squid_radix_node
*t
, *x
= NULL
, *tt
;
575 struct squid_radix_node
*saved_tt
, *top
= head
->rnh_treetop
;
576 short b
= 0, b_leaf
= 0;
579 struct squid_radix_mask
*m
, **mp
;
582 * In dealing with non-contiguous masks, there may be
583 * many different routes which have the same mask.
584 * We will find it useful to have a unique pointer to
585 * the mask to speed avoiding duplicate references at
586 * nodes and possibly save time in calculating indices.
589 if ((x
= squid_rn_addmask(netmask
, 0, top
->rn_off
)) == 0)
596 * Deal with duplicated keys: attach node to previous instance
598 saved_tt
= tt
= squid_rn_insert(v
, head
, &keyduplicated
, treenodes
);
600 for (t
= tt
; tt
; t
= tt
, tt
= tt
->rn_dupedkey
) {
601 if (tt
->rn_mask
== netmask
)
605 ((b_leaf
< tt
->rn_b
) || /* index(netmask) > node */
606 squid_rn_refines(netmask
, tt
->rn_mask
) ||
607 rn_lexobetter(netmask
, tt
->rn_mask
))))
611 * If the mask is not duplicated, we wouldn't
612 * find it among possible duplicate key entries
613 * anyway, so the above test doesn't hurt.
615 * We sort the masks for a duplicated key the same way as
616 * in a masklist -- most specific to least specific.
617 * This may require the unfortunate nuisance of relocating
618 * the head of the list.
620 if (tt
== saved_tt
) {
621 struct squid_radix_node
*xx
= x
;
622 /* link in at head of list */
623 (tt
= treenodes
)->rn_dupedkey
= t
;
624 tt
->rn_flags
= t
->rn_flags
;
625 tt
->rn_p
= x
= t
->rn_p
;
633 (tt
= treenodes
)->rn_dupedkey
= t
->rn_dupedkey
;
638 tt
->rn_info
= rn_nodenum
++;
639 t
->rn_info
= rn_nodenum
++;
641 tt
->rn_ybro
= rn_clist
;
644 tt
->rn_key
= (caddr_t
) v
;
646 tt
->rn_flags
= RNF_ACTIVE
;
652 tt
->rn_mask
= netmask
;
654 tt
->rn_flags
|= x
->rn_flags
& RNF_NORMAL
;
659 b_leaf
= -1 - t
->rn_b
;
660 if (t
->rn_r
== saved_tt
)
664 /* Promote general routes from below */
666 for (mp
= &t
->rn_mklist
; x
; x
= x
->rn_dupedkey
)
667 if (x
->rn_mask
&& (x
->rn_b
>= b_leaf
) && x
->rn_mklist
== 0) {
668 if ((*mp
= m
= rn_new_radix_mask(x
, 0)))
671 } else if (x
->rn_mklist
) {
673 * Skip over masks whose index is > that of new node
675 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
)
676 if (m
->rm_b
>= b_leaf
)
682 /* Add new route to highest possible ancestor's list */
683 if ((netmask
== 0) || (b
> t
->rn_b
))
684 return tt
; /* can't lift at all */
689 } while (b
<= t
->rn_b
&& x
!= top
);
691 * Search through routes associated with node to
692 * insert new route according to index.
693 * Need same criteria as when sorting dupedkeys to avoid
694 * double loop on deletion.
696 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
) {
697 if (m
->rm_b
< b_leaf
)
699 if (m
->rm_b
> b_leaf
)
701 if (m
->rm_flags
& RNF_NORMAL
) {
702 mmask
= m
->rm_leaf
->rn_mask
;
703 if (tt
->rn_flags
& RNF_NORMAL
) {
705 "Non-unique normal route, mask not entered");
710 if (mmask
== netmask
) {
715 if (squid_rn_refines(netmask
, mmask
) || rn_lexobetter(netmask
, mmask
))
718 *mp
= rn_new_radix_mask(tt
, *mp
);
722 struct squid_radix_node
*
723 squid_rn_delete(void *v_arg
, void *netmask_arg
, struct squid_radix_node_head
*head
) {
724 register struct squid_radix_node
*t
, *p
, *x
, *tt
;
725 struct squid_radix_mask
*m
, *saved_m
, **mp
;
726 struct squid_radix_node
*dupedkey
, *saved_tt
, *top
;
728 int b
, head_off
, vlen
;
731 netmask
= netmask_arg
;
732 x
= head
->rnh_treetop
;
733 tt
= squid_rn_search(v
, x
);
734 head_off
= x
->rn_off
;
735 vlen
= *(u_char
*) v
;
739 memcmp(v
+ head_off
, tt
->rn_key
+ head_off
, vlen
- head_off
))
742 * Delete our route from mask lists.
745 if ((x
= squid_rn_addmask(netmask
, 1, head_off
)) == 0)
748 while (tt
->rn_mask
!= netmask
)
749 if ((tt
= tt
->rn_dupedkey
) == 0)
752 if (tt
->rn_mask
== 0 || (saved_m
= m
= tt
->rn_mklist
) == 0)
754 if (tt
->rn_flags
& RNF_NORMAL
) {
755 if (m
->rm_leaf
!= tt
|| m
->rm_refs
> 0) {
756 fprintf(stderr
, "squid_rn_delete: inconsistent annotation\n");
757 return 0; /* dangling ref could cause disaster */
760 if (m
->rm_mask
!= tt
->rn_mask
) {
761 fprintf(stderr
, "squid_rn_delete: inconsistent annotation\n");
764 if (--m
->rm_refs
>= 0)
770 goto on1
; /* Wasn't lifted at all */
774 } while (b
<= t
->rn_b
&& x
!= top
);
775 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
)
782 fprintf(stderr
, "squid_rn_delete: couldn't find our annotation\n");
783 if (tt
->rn_flags
& RNF_NORMAL
)
784 return (0); /* Dangling ref to us */
788 * Eliminate us from tree
790 if (tt
->rn_flags
& RNF_ROOT
)
793 /* Get us out of the creation list */
794 for (t
= rn_clist
; t
&& t
->rn_ybro
!= tt
; t
= t
->rn_ybro
) {
797 t
->rn_ybro
= tt
->rn_ybro
;
800 if ((dupedkey
= saved_tt
->rn_dupedkey
)) {
801 if (tt
== saved_tt
) {
809 for (x
= p
= saved_tt
; p
&& p
->rn_dupedkey
!= tt
;)
812 p
->rn_dupedkey
= tt
->rn_dupedkey
;
814 fprintf(stderr
, "squid_rn_delete: couldn't find us\n");
817 if (t
->rn_flags
& RNF_ACTIVE
) {
847 * Demote routes attached to us.
851 for (mp
= &x
->rn_mklist
; (m
= *mp
);)
855 /* If there are any key,mask pairs in a sibling
856 * duped-key chain, some subset will appear sorted
857 * in the same order attached to our mklist */
858 for (m
= t
->rn_mklist
; m
&& x
; x
= x
->rn_dupedkey
)
859 if (m
== x
->rn_mklist
) {
860 struct squid_radix_mask
*mm
= m
->rm_mklist
;
862 if (--(m
->rm_refs
) < 0)
868 fprintf(stderr
, "%s %x at %x\n",
869 "squid_rn_delete: Orphaned Mask", (int) m
, (int) x
);
876 * We may be holding an active internal node in the tree.
896 tt
->rn_flags
&= ~RNF_ACTIVE
;
897 tt
[1].rn_flags
&= ~RNF_ACTIVE
;
902 squid_rn_walktree(struct squid_radix_node_head
*h
, int (*f
) (struct squid_radix_node
*, void *), void *w
)
905 struct squid_radix_node
*base
, *next
;
906 register struct squid_radix_node
*rn
= h
->rnh_treetop
;
908 * This gets complicated because we may delete the node
909 * while applying the function f to it, so we need to calculate
910 * the successor node in advance.
912 /* First time through node, go left */
913 while (rn
->rn_b
>= 0)
917 /* If at right child go back up, otherwise, go right */
918 while (rn
->rn_p
->rn_r
== rn
&& (rn
->rn_flags
& RNF_ROOT
) == 0)
920 /* Find the next *leaf* since next node might vanish, too */
921 for (rn
= rn
->rn_p
->rn_r
; rn
->rn_b
>= 0;)
925 while ((rn
= base
)) {
926 base
= rn
->rn_dupedkey
;
927 if (!(rn
->rn_flags
& RNF_ROOT
) && (error
= (*f
) (rn
, w
)))
931 if (rn
->rn_flags
& RNF_ROOT
)
938 squid_rn_inithead(struct squid_radix_node_head
**head
, int off
)
940 register struct squid_radix_node_head
*rnh
;
941 register struct squid_radix_node
*t
, *tt
, *ttt
;
944 squid_R_Malloc(rnh
, struct squid_radix_node_head
*, sizeof(*rnh
));
947 memset(rnh
, '\0', sizeof(*rnh
));
949 t
= squid_rn_newpair(rn_zeros
, off
, rnh
->rnh_nodes
);
950 ttt
= rnh
->rnh_nodes
+ 2;
954 tt
->rn_flags
= t
->rn_flags
= RNF_ROOT
| RNF_ACTIVE
;
957 ttt
->rn_key
= rn_ones
;
958 rnh
->rnh_addaddr
= squid_rn_addroute
;
959 rnh
->rnh_deladdr
= squid_rn_delete
;
960 rnh
->rnh_matchaddr
= squid_rn_match
;
961 rnh
->rnh_lookup
= squid_rn_lookup
;
962 rnh
->rnh_walktree
= squid_rn_walktree
;
963 rnh
->rnh_treetop
= t
;
974 for (dom
= domains
; dom
; dom
= dom
->dom_next
)
975 if (dom
->dom_maxrtkey
> squid_max_keylen
)
976 squid_max_keylen
= dom
->dom_maxrtkey
;
978 if (squid_max_keylen
== 0) {
980 "squid_rn_init: radix functions require squid_max_keylen be set\n");
983 squid_R_Malloc(rn_zeros
, char *, 3 * squid_max_keylen
);
984 if (rn_zeros
== NULL
) {
985 fprintf(stderr
, "squid_rn_init failed.\n");
988 memset(rn_zeros
, '\0', 3 * squid_max_keylen
);
989 rn_ones
= cp
= rn_zeros
+ squid_max_keylen
;
990 addmask_key
= cplim
= rn_ones
+ squid_max_keylen
;
993 if (squid_rn_inithead(&squid_mask_rnhead
, 0) == 0) {
994 fprintf(stderr
, "rn_init2 failed.\n");