xref: /csrg-svn/sys/net/radix.h (revision 36211)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)radix.h	7.1 (Berkeley) 11/09/88
18  */
19 
20 /*
21  * Radix search tree node layout.
22  */
23 
24 struct radix_node {
25 	struct	radix_mask *rn_mklist;	/* list of masks contained in subtree */
26 	struct	radix_node *rn_p;	/* parent */
27 	short	rn_b;			/* bit offset; -1-index(netmask) */
28 	char	rn_bmask;		/* node: mask for bit test*/
29 	u_char	rn_flags;		/* enumerated next */
30 #define RNF_NORMAL	1		/* leaf contains normal route */
31 #define RNF_ROOT	2		/* leaf is root leaf for tree */
32 #define RNF_ACTIVE	4		/* This node is alive (for rtfree) */
33 	union {
34 		struct {			/* leaf only data: */
35 			char 	*rn_Key;	/* object of search */
36 			char	*rn_Mask;	/* netmask, if present */
37 			struct	radix_node *rn_Dupedkey;
38 		} rn_leaf;
39 		struct {			/* node only data: */
40 			int	rn_Off;		/* where to start compare */
41 			struct	radix_node *rn_L;/* progeny */
42 			struct	radix_node *rn_R;/* progeny */
43 		}rn_node;
44 	}		rn_u;
45 #ifdef RN_DEBUG
46 	int rn_info;
47 	struct radix_node *rn_twin;
48 	struct radix_node *rn_ybro;
49 #endif
50 };
51 
52 #define rn_dupedkey rn_u.rn_leaf.rn_Dupedkey
53 #define rn_key rn_u.rn_leaf.rn_Key
54 #define rn_mask rn_u.rn_leaf.rn_Mask
55 #define rn_off rn_u.rn_node.rn_Off
56 #define rn_l rn_u.rn_node.rn_L
57 #define rn_r rn_u.rn_node.rn_R
58 /*
59  * Annotations to tree concerning potential routes applying to subtrees.
60  */
61 struct radix_mask {
62 	short	rm_b;			/* bit offset; -1-index(netmask) */
63 	char	rm_unused;		/* cf. rn_bmask */
64 	u_char	rm_flags;		/* cf. rn_flags */
65 	struct	radix_mask *rm_mklist;	/* more masks to try */
66 	char	*rm_mask;		/* the mask */
67 	int	rm_refs;		/* # of references to this struct */
68 };
69 
70 #ifndef KERNEL
71 char *malloc();
72 #define Bcmp(a, b, n) bcmp(((char *)(a)), ((char *)(b)), (n))
73 #define Malloc(p, t, n) (p = (t) malloc((unsigned int)(n)))
74 #define Bzero(p, n) bzero((char *)(p), (int)(n));
75 #define Free(p) free((char *)p);
76 #define min(a, b) ((a) < (b) ? (a) : (b))
77 #ifndef RTF_UP
78 /*
79  * probably just testing here . . .
80  */
81 struct rtentry {
82 	int	rt_unused;
83 };
84 #endif
85 #else
86 #define Bcmp(a, b, n) bcmp(((caddr_t)(a)), ((caddr_t)(b)), (n))
87 #define Malloc(p, t, n) (p = (t) malloc((n), M_RTABLE, M_DONTWAIT))
88 #define Bzero(p, n) bzero((caddr_t)(p), (int)(n));
89 #define Free(p) free((caddr_t)p);
90 #endif KERNEL
91 
92 struct nrtentry {
93 	struct	radix_node nrt_nodes[2];
94 	struct	rtentry nrt_rt;
95 };
96 
97 #define MAXKEYLEN 32
98 
99 extern struct radix_node_head {
100 	struct	radix_node_head *rnh_next;
101 	struct	radix_node *rnh_treetop;
102 	int	rnh_af;
103 	struct	radix_node rnh_upper;
104 	struct	nrtentry rnh_nrt;
105 } *radix_node_head;
106 
107 extern struct radix_mask *rn_mkfreelist;
108 
109 #define MKGet(m) {\
110 	if (rn_mkfreelist) {\
111 		m = rn_mkfreelist; \
112 		rn_mkfreelist = (m)->rm_mklist; \
113 	} else \
114 		Malloc(m, struct radix_mask *, sizeof (*(m))); }\
115 
116 #define MKFree(m) { (m)->rm_mklist = rn_mkfreelist; rn_mkfreelist = (m);}
117