2 Copyright (C) 1998-2021 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"
33 insert_range (ADDR_SUBRANGE
**pos
, ADDR_SUBRANGE
*asr
)
42 delete_range (ADDR_SUBRANGE
**thisasrp
)
44 ADDR_SUBRANGE
*thisasr
;
47 *thisasrp
= thisasr
->next
;
52 /* Add or delete an address range.
53 This code was borrowed from linux's locks.c:posix_lock_file().
54 ??? Todo: Given our simpler needs this could be simplified
55 (split into two fns). */
58 frob_range (ADDR_RANGE
*ar
, address_word start
, address_word end
, int delete_p
)
61 ADDR_SUBRANGE
*new_asr
, *new_asr2
;
62 ADDR_SUBRANGE
*left
= NULL
;
63 ADDR_SUBRANGE
*right
= NULL
;
64 ADDR_SUBRANGE
**before
;
65 ADDR_SUBRANGE init_caller
;
66 ADDR_SUBRANGE
*caller
= &init_caller
;
69 memset (caller
, 0, sizeof (ADDR_SUBRANGE
));
70 new_asr
= ZALLOC (ADDR_SUBRANGE
);
71 new_asr2
= ZALLOC (ADDR_SUBRANGE
);
73 caller
->start
= start
;
77 while ((asr
= *before
) != NULL
)
81 /* Try next range if current range preceeds new one and not
82 adjacent or overlapping. */
83 if (asr
->end
< caller
->start
- 1)
86 /* Break out if new range preceeds current one and not
87 adjacent or overlapping. */
88 if (asr
->start
> caller
->end
+ 1)
91 /* If we come here, the new and current ranges are adjacent or
92 overlapping. Make one range yielding from the lower start address
93 of both ranges to the higher end address. */
94 if (asr
->start
> caller
->start
)
95 asr
->start
= caller
->start
;
97 caller
->start
= asr
->start
;
98 if (asr
->end
< caller
->end
)
99 asr
->end
= caller
->end
;
101 caller
->end
= asr
->end
;
105 delete_range (before
);
111 else /* deleting a range */
113 /* Try next range if current range preceeds new one. */
114 if (asr
->end
< caller
->start
)
117 /* Break out if new range preceeds current one. */
118 if (asr
->start
> caller
->end
)
123 if (asr
->start
< caller
->start
)
126 /* If the next range in the list has a higher end
127 address than the new one, insert the new one here. */
128 if (asr
->end
> caller
->end
)
133 if (asr
->start
>= caller
->start
)
135 /* The new range completely replaces an old
136 one (This may happen several times). */
139 delete_range (before
);
143 /* Replace the old range with the new one. */
144 asr
->start
= caller
->start
;
145 asr
->end
= caller
->end
;
151 /* Go on to next range. */
160 new_asr
->start
= caller
->start
;
161 new_asr
->end
= caller
->end
;
162 insert_range (before
, new_asr
);
169 /* The new range breaks the old one in two pieces,
170 so we have to use the second new range. */
171 new_asr2
->start
= right
->start
;
172 new_asr2
->end
= right
->end
;
174 insert_range (before
, left
);
177 right
->start
= caller
->end
+ 1;
181 left
->end
= caller
->start
- 1;
191 /* Free T and all subtrees. */
194 free_search_tree (ADDR_RANGE_TREE
*t
)
198 free_search_tree (t
->lower
);
199 free_search_tree (t
->higher
);
204 /* Subroutine of build_search_tree to recursively build a balanced tree.
205 ??? It's not an optimum tree though. */
207 static ADDR_RANGE_TREE
*
208 build_tree_1 (ADDR_SUBRANGE
**asrtab
, unsigned int n
)
210 unsigned int mid
= n
/ 2;
215 t
= (ADDR_RANGE_TREE
*) xmalloc (sizeof (ADDR_RANGE_TREE
));
216 t
->start
= asrtab
[mid
]->start
;
217 t
->end
= asrtab
[mid
]->end
;
219 t
->lower
= build_tree_1 (asrtab
, mid
);
223 t
->higher
= build_tree_1 (asrtab
+ mid
+ 1, n
- mid
- 1);
229 /* Build a search tree for address range AR. */
232 build_search_tree (ADDR_RANGE
*ar
)
234 /* ??? Simple version for now. */
235 ADDR_SUBRANGE
*asr
,**asrtab
;
238 for (n
= 0, asr
= ar
->ranges
; asr
!= NULL
; ++n
, asr
= asr
->next
)
240 asrtab
= (ADDR_SUBRANGE
**) xmalloc (n
* sizeof (ADDR_SUBRANGE
*));
241 for (i
= 0, asr
= ar
->ranges
; i
< n
; ++i
, asr
= asr
->next
)
243 ar
->range_tree
= build_tree_1 (asrtab
, n
);
249 sim_addr_range_add (ADDR_RANGE
*ar
, address_word start
, address_word end
)
251 frob_range (ar
, start
, end
, 0);
253 /* Rebuild the search tree. */
254 /* ??? Instead of rebuilding it here it could be done in a module resume
255 handler, say by first checking for a `changed' flag, assuming of course
256 this would never be done while the simulation is running. */
257 free_search_tree (ar
->range_tree
);
258 build_search_tree (ar
);
263 sim_addr_range_delete (ADDR_RANGE
*ar
, address_word start
, address_word end
)
265 frob_range (ar
, start
, end
, 1);
267 /* Rebuild the search tree. */
268 /* ??? Instead of rebuilding it here it could be done in a module resume
269 handler, say by first checking for a `changed' flag, assuming of course
270 this would never be done while the simulation is running. */
271 free_search_tree (ar
->range_tree
);
272 build_search_tree (ar
);
277 sim_addr_range_hit_p (ADDR_RANGE
*ar
, address_word addr
)
279 ADDR_RANGE_TREE
*t
= ar
->range_tree
;
285 else if (addr
> t
->end
)
293 #endif /* _SIM_ARANGE_C_ */