2 Copyright (C) 1998-2020 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions.
5 This file is part of the GNU Simulators.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #ifndef _SIM_ARANGE_C_
21 #define _SIM_ARANGE_C_
23 #include "libiberty.h"
24 #include "sim-basics.h"
25 #include "sim-arange.h"
38 insert_range (ADDR_SUBRANGE
**pos
, ADDR_SUBRANGE
*asr
)
47 delete_range (ADDR_SUBRANGE
**thisasrp
)
49 ADDR_SUBRANGE
*thisasr
;
52 *thisasrp
= thisasr
->next
;
57 /* Add or delete an address range.
58 This code was borrowed from linux's locks.c:posix_lock_file().
59 ??? Todo: Given our simpler needs this could be simplified
60 (split into two fns). */
63 frob_range (ADDR_RANGE
*ar
, address_word start
, address_word end
, int delete_p
)
66 ADDR_SUBRANGE
*new_asr
, *new_asr2
;
67 ADDR_SUBRANGE
*left
= NULL
;
68 ADDR_SUBRANGE
*right
= NULL
;
69 ADDR_SUBRANGE
**before
;
70 ADDR_SUBRANGE init_caller
;
71 ADDR_SUBRANGE
*caller
= &init_caller
;
74 memset (caller
, 0, sizeof (ADDR_SUBRANGE
));
75 new_asr
= ZALLOC (ADDR_SUBRANGE
);
76 new_asr2
= ZALLOC (ADDR_SUBRANGE
);
78 caller
->start
= start
;
82 while ((asr
= *before
) != NULL
)
86 /* Try next range if current range preceeds new one and not
87 adjacent or overlapping. */
88 if (asr
->end
< caller
->start
- 1)
91 /* Break out if new range preceeds current one and not
92 adjacent or overlapping. */
93 if (asr
->start
> caller
->end
+ 1)
96 /* If we come here, the new and current ranges are adjacent or
97 overlapping. Make one range yielding from the lower start address
98 of both ranges to the higher end address. */
99 if (asr
->start
> caller
->start
)
100 asr
->start
= caller
->start
;
102 caller
->start
= asr
->start
;
103 if (asr
->end
< caller
->end
)
104 asr
->end
= caller
->end
;
106 caller
->end
= asr
->end
;
110 delete_range (before
);
116 else /* deleting a range */
118 /* Try next range if current range preceeds new one. */
119 if (asr
->end
< caller
->start
)
122 /* Break out if new range preceeds current one. */
123 if (asr
->start
> caller
->end
)
128 if (asr
->start
< caller
->start
)
131 /* If the next range in the list has a higher end
132 address than the new one, insert the new one here. */
133 if (asr
->end
> caller
->end
)
138 if (asr
->start
>= caller
->start
)
140 /* The new range completely replaces an old
141 one (This may happen several times). */
144 delete_range (before
);
148 /* Replace the old range with the new one. */
149 asr
->start
= caller
->start
;
150 asr
->end
= caller
->end
;
156 /* Go on to next range. */
165 new_asr
->start
= caller
->start
;
166 new_asr
->end
= caller
->end
;
167 insert_range (before
, new_asr
);
174 /* The new range breaks the old one in two pieces,
175 so we have to use the second new range. */
176 new_asr2
->start
= right
->start
;
177 new_asr2
->end
= right
->end
;
179 insert_range (before
, left
);
182 right
->start
= caller
->end
+ 1;
186 left
->end
= caller
->start
- 1;
196 /* Free T and all subtrees. */
199 free_search_tree (ADDR_RANGE_TREE
*t
)
203 free_search_tree (t
->lower
);
204 free_search_tree (t
->higher
);
209 /* Subroutine of build_search_tree to recursively build a balanced tree.
210 ??? It's not an optimum tree though. */
212 static ADDR_RANGE_TREE
*
213 build_tree_1 (ADDR_SUBRANGE
**asrtab
, unsigned int n
)
215 unsigned int mid
= n
/ 2;
220 t
= (ADDR_RANGE_TREE
*) xmalloc (sizeof (ADDR_RANGE_TREE
));
221 t
->start
= asrtab
[mid
]->start
;
222 t
->end
= asrtab
[mid
]->end
;
224 t
->lower
= build_tree_1 (asrtab
, mid
);
228 t
->higher
= build_tree_1 (asrtab
+ mid
+ 1, n
- mid
- 1);
234 /* Build a search tree for address range AR. */
237 build_search_tree (ADDR_RANGE
*ar
)
239 /* ??? Simple version for now. */
240 ADDR_SUBRANGE
*asr
,**asrtab
;
243 for (n
= 0, asr
= ar
->ranges
; asr
!= NULL
; ++n
, asr
= asr
->next
)
245 asrtab
= (ADDR_SUBRANGE
**) xmalloc (n
* sizeof (ADDR_SUBRANGE
*));
246 for (i
= 0, asr
= ar
->ranges
; i
< n
; ++i
, asr
= asr
->next
)
248 ar
->range_tree
= build_tree_1 (asrtab
, n
);
254 sim_addr_range_add (ADDR_RANGE
*ar
, address_word start
, address_word end
)
256 frob_range (ar
, start
, end
, 0);
258 /* Rebuild the search tree. */
259 /* ??? Instead of rebuilding it here it could be done in a module resume
260 handler, say by first checking for a `changed' flag, assuming of course
261 this would never be done while the simulation is running. */
262 free_search_tree (ar
->range_tree
);
263 build_search_tree (ar
);
268 sim_addr_range_delete (ADDR_RANGE
*ar
, address_word start
, address_word end
)
270 frob_range (ar
, start
, end
, 1);
272 /* Rebuild the search tree. */
273 /* ??? Instead of rebuilding it here it could be done in a module resume
274 handler, say by first checking for a `changed' flag, assuming of course
275 this would never be done while the simulation is running. */
276 free_search_tree (ar
->range_tree
);
277 build_search_tree (ar
);
282 sim_addr_range_hit_p (ADDR_RANGE
*ar
, address_word addr
)
284 ADDR_RANGE_TREE
*t
= ar
->range_tree
;
290 else if (addr
> t
->end
)
298 #endif /* _SIM_ARANGE_C_ */