2 Copyright (C) 1998-2024 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 /* This must come before any other includes. */
29 #include "libiberty.h"
31 #include "sim-basics.h"
32 #include "sim-arange.h"
37 insert_range (ADDR_SUBRANGE
**pos
, ADDR_SUBRANGE
*asr
)
46 delete_range (ADDR_SUBRANGE
**thisasrp
)
48 ADDR_SUBRANGE
*thisasr
;
51 *thisasrp
= thisasr
->next
;
56 /* Add or delete an address range.
57 This code was borrowed from linux's locks.c:posix_lock_file().
58 ??? Todo: Given our simpler needs this could be simplified
59 (split into two fns). */
62 frob_range (ADDR_RANGE
*ar
, address_word start
, address_word end
, int delete_p
)
65 ADDR_SUBRANGE
*new_asr
, *new_asr2
;
66 ADDR_SUBRANGE
*left
= NULL
;
67 ADDR_SUBRANGE
*right
= NULL
;
68 ADDR_SUBRANGE
**before
;
69 ADDR_SUBRANGE init_caller
;
70 ADDR_SUBRANGE
*caller
= &init_caller
;
73 memset (caller
, 0, sizeof (ADDR_SUBRANGE
));
74 new_asr
= ZALLOC (ADDR_SUBRANGE
);
75 new_asr2
= ZALLOC (ADDR_SUBRANGE
);
77 caller
->start
= start
;
81 while ((asr
= *before
) != NULL
)
85 /* Try next range if current range precedes new one and not
86 adjacent or overlapping. */
87 if (asr
->end
< caller
->start
- 1)
90 /* Break out if new range precedes current one and not
91 adjacent or overlapping. */
92 if (asr
->start
> caller
->end
+ 1)
95 /* If we come here, the new and current ranges are adjacent or
96 overlapping. Make one range yielding from the lower start address
97 of both ranges to the higher end address. */
98 if (asr
->start
> caller
->start
)
99 asr
->start
= caller
->start
;
101 caller
->start
= asr
->start
;
102 if (asr
->end
< caller
->end
)
103 asr
->end
= caller
->end
;
105 caller
->end
= asr
->end
;
109 delete_range (before
);
115 else /* deleting a range */
117 /* Try next range if current range precedes new one. */
118 if (asr
->end
< caller
->start
)
121 /* Break out if new range precedes current one. */
122 if (asr
->start
> caller
->end
)
127 if (asr
->start
< caller
->start
)
130 /* If the next range in the list has a higher end
131 address than the new one, insert the new one here. */
132 if (asr
->end
> caller
->end
)
137 if (asr
->start
>= caller
->start
)
139 /* The new range completely replaces an old
140 one (This may happen several times). */
143 delete_range (before
);
147 /* Replace the old range with the new one. */
148 asr
->start
= caller
->start
;
149 asr
->end
= caller
->end
;
155 /* Go on to next range. */
164 new_asr
->start
= caller
->start
;
165 new_asr
->end
= caller
->end
;
166 insert_range (before
, new_asr
);
173 /* The new range breaks the old one in two pieces,
174 so we have to use the second new range. */
175 new_asr2
->start
= right
->start
;
176 new_asr2
->end
= right
->end
;
178 insert_range (before
, left
);
181 right
->start
= caller
->end
+ 1;
185 left
->end
= caller
->start
- 1;
195 /* Free T and all subtrees. */
198 free_search_tree (ADDR_RANGE_TREE
*t
)
202 free_search_tree (t
->lower
);
203 free_search_tree (t
->higher
);
208 /* Subroutine of build_search_tree to recursively build a balanced tree.
209 ??? It's not an optimum tree though. */
211 static ADDR_RANGE_TREE
*
212 build_tree_1 (ADDR_SUBRANGE
**asrtab
, unsigned int n
)
214 unsigned int mid
= n
/ 2;
219 t
= (ADDR_RANGE_TREE
*) xmalloc (sizeof (ADDR_RANGE_TREE
));
220 t
->start
= asrtab
[mid
]->start
;
221 t
->end
= asrtab
[mid
]->end
;
223 t
->lower
= build_tree_1 (asrtab
, mid
);
227 t
->higher
= build_tree_1 (asrtab
+ mid
+ 1, n
- mid
- 1);
233 /* Build a search tree for address range AR. */
236 build_search_tree (ADDR_RANGE
*ar
)
238 /* ??? Simple version for now. */
239 ADDR_SUBRANGE
*asr
,**asrtab
;
242 for (n
= 0, asr
= ar
->ranges
; asr
!= NULL
; ++n
, asr
= asr
->next
)
244 asrtab
= (ADDR_SUBRANGE
**) xmalloc (n
* sizeof (ADDR_SUBRANGE
*));
245 for (i
= 0, asr
= ar
->ranges
; i
< n
; ++i
, asr
= asr
->next
)
247 ar
->range_tree
= build_tree_1 (asrtab
, n
);
253 sim_addr_range_add (ADDR_RANGE
*ar
, address_word start
, address_word end
)
255 frob_range (ar
, start
, end
, 0);
257 /* Rebuild the search tree. */
258 /* ??? Instead of rebuilding it here it could be done in a module resume
259 handler, say by first checking for a `changed' flag, assuming of course
260 this would never be done while the simulation is running. */
261 free_search_tree (ar
->range_tree
);
262 build_search_tree (ar
);
267 sim_addr_range_delete (ADDR_RANGE
*ar
, address_word start
, address_word end
)
269 frob_range (ar
, start
, end
, 1);
271 /* Rebuild the search tree. */
272 /* ??? Instead of rebuilding it here it could be done in a module resume
273 handler, say by first checking for a `changed' flag, assuming of course
274 this would never be done while the simulation is running. */
275 free_search_tree (ar
->range_tree
);
276 build_search_tree (ar
);
281 sim_addr_range_hit_p (ADDR_RANGE
*ar
, address_word addr
)
283 ADDR_RANGE_TREE
*t
= ar
->range_tree
;
289 else if (addr
> t
->end
)
297 #endif /* _SIM_ARANGE_C_ */