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.
34 void init_seglocations(segtab
* root
)
39 void descend_tree_add(struct segtabnode
* * node
,
40 int localseg
, int destseg
, long offset
)
42 struct segtabnode
* n
;
45 *node
= malloc (sizeof (**node
));
47 fprintf(stderr
, "segment table: out of memory\n");
50 (*node
)->localseg
= localseg
;
51 (*node
)->offset
= offset
;
53 (*node
)->leftcount
= 0;
54 (*node
)->right
= NULL
;
55 (*node
)->rightcount
= 0;
56 (*node
)->destseg
= destseg
;
60 if (localseg
< (*node
)->localseg
)
63 descend_tree_add(&(*node
)->left
, localseg
, destseg
, offset
);
65 if ((*node
)->leftcount
> (*node
)->rightcount
+ 2) {
68 n
->left
= (*node
)->right
;
69 n
->leftcount
= (*node
)->rightcount
;
71 (*node
)->rightcount
= n
->leftcount
+ n
->rightcount
+ 1;
76 (*node
)->rightcount
++;
77 descend_tree_add(&(*node
)->right
, localseg
, destseg
, offset
);
79 if ((*node
)->rightcount
> (*node
)->leftcount
+ 2) {
82 n
->right
= (*node
)->left
;
83 n
->rightcount
= (*node
)->leftcount
;
85 (*node
)->leftcount
= n
->leftcount
+ n
->rightcount
+ 1;
90 void add_seglocation(segtab
* root
, int localseg
, int destseg
, long offset
)
92 descend_tree_add((struct segtabnode
**) root
, localseg
, destseg
, offset
);
95 int get_seglocation(segtab
* root
, int localseg
, int * destseg
, long * offset
)
97 struct segtabnode
* n
= (struct segtabnode
*) *root
;
99 while (n
&& n
->localseg
!= localseg
)
101 if (localseg
< n
->localseg
)
107 *destseg
= n
->destseg
;
115 void freenode(struct segtabnode
* n
)
123 void done_seglocations(segtab
* root
)
130 void printnode(int i
, struct segtabnode
* n
)
133 printnode(i
+ 1, n
->left
);
134 printf ("%*s%d %d %ld\n", i
, "", n
->localseg
, n
->destseg
, n
->offset
);
135 printnode(i
+ 1, n
->right
);