10 struct segtabnode
*left
;
11 struct segtabnode
*right
;
13 * counts of how many are left or right, for use in reorganising
26 * functions used by write_output() to manipulate associations
27 * between segment numbers and locations (which are built up on a per
28 * module basis, but we only need one module at a time...)
30 * implementation: we build a binary tree.
33 void init_seglocations(segtab
* root
)
38 void descend_tree_add(struct segtabnode
**node
,
39 int localseg
, int destseg
, long offset
)
44 *node
= malloc(sizeof(**node
));
46 fprintf(stderr
, "segment table: out of memory\n");
49 (*node
)->localseg
= localseg
;
50 (*node
)->offset
= offset
;
52 (*node
)->leftcount
= 0;
53 (*node
)->right
= NULL
;
54 (*node
)->rightcount
= 0;
55 (*node
)->destseg
= destseg
;
59 if (localseg
< (*node
)->localseg
) {
61 descend_tree_add(&(*node
)->left
, localseg
, destseg
, offset
);
63 if ((*node
)->leftcount
> (*node
)->rightcount
+ 2) {
66 n
->left
= (*node
)->right
;
67 n
->leftcount
= (*node
)->rightcount
;
69 (*node
)->rightcount
= n
->leftcount
+ n
->rightcount
+ 1;
72 (*node
)->rightcount
++;
73 descend_tree_add(&(*node
)->right
, localseg
, destseg
, offset
);
75 if ((*node
)->rightcount
> (*node
)->leftcount
+ 2) {
78 n
->right
= (*node
)->left
;
79 n
->rightcount
= (*node
)->leftcount
;
81 (*node
)->leftcount
= n
->leftcount
+ n
->rightcount
+ 1;
86 void add_seglocation(segtab
* root
, int localseg
, int destseg
, long offset
)
88 descend_tree_add((struct segtabnode
**)root
, localseg
, destseg
,
92 int get_seglocation(segtab
* root
, int localseg
, int *destseg
,
95 struct segtabnode
*n
= (struct segtabnode
*)*root
;
97 while (n
&& n
->localseg
!= localseg
) {
98 if (localseg
< n
->localseg
)
104 *destseg
= n
->destseg
;
111 void freenode(struct segtabnode
*n
)
120 void done_seglocations(segtab
* root
)
127 void printnode(int i
, struct segtabnode
*n
)
131 printnode(i
+ 1, n
->left
);
132 printf("%*s%d %d %ld\n", i
, "", n
->localseg
, n
->destseg
, n
->offset
);
133 printnode(i
+ 1, n
->right
);