2 Copyright (C) 1998 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 2, or (at your option)
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 along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Tell sim-arange.h it's us. */
24 #include "sim-basics.h"
25 #include "sim-assert.h"
27 #define DEFINE_INLINE_P (! defined (SIM_ARANGE_C_INCLUDED))
28 #define DEFINE_NON_INLINE_P defined (SIM_ARANGE_C_INCLUDED)
30 #if DEFINE_NON_INLINE_P
35 insert_range (ADDR_SUBRANGE
**pos
, ADDR_SUBRANGE
*asr
)
44 delete_range (ADDR_SUBRANGE
**thisasrp
)
46 ADDR_SUBRANGE
*thisasr
;
49 *thisasrp
= thisasr
->next
;
54 /* Add or delete an address range.
55 This code was borrowed from linux's locks.c:posix_lock_file().
56 ??? Todo: Given our simpler needs this could be simplified
57 (split into two fns). */
60 frob_range (ADDR_RANGE
*ar
, address_word start
, address_word end
, int delete_p
)
63 ADDR_SUBRANGE
*new_asr
, *new_asr2
;
64 ADDR_SUBRANGE
*left
= NULL
;
65 ADDR_SUBRANGE
*right
= NULL
;
66 ADDR_SUBRANGE
**before
;
67 ADDR_SUBRANGE init_caller
;
68 ADDR_SUBRANGE
*caller
= &init_caller
;
71 memset (caller
, 0, sizeof (ADDR_SUBRANGE
));
72 new_asr
= ZALLOC (ADDR_SUBRANGE
);
73 new_asr2
= ZALLOC (ADDR_SUBRANGE
);
75 caller
->start
= start
;
79 while ((asr
= *before
) != NULL
)
83 /* Try next range if current range preceeds new one and not
84 adjacent or overlapping. */
85 if (asr
->end
< caller
->start
- 1)
88 /* Break out if new range preceeds current one and not
89 adjacent or overlapping. */
90 if (asr
->start
> caller
->end
+ 1)
93 /* If we come here, the new and current ranges are adjacent or
94 overlapping. Make one range yielding from the lower start address
95 of both ranges to the higher end address. */
96 if (asr
->start
> caller
->start
)
97 asr
->start
= caller
->start
;
99 caller
->start
= asr
->start
;
100 if (asr
->end
< caller
->end
)
101 asr
->end
= caller
->end
;
103 caller
->end
= asr
->end
;
107 delete_range (before
);
113 else /* deleting a range */
115 /* Try next range if current range preceeds new one. */
116 if (asr
->end
< caller
->start
)
119 /* Break out if new range preceeds current one. */
120 if (asr
->start
> caller
->end
)
125 if (asr
->start
< caller
->start
)
128 /* If the next range in the list has a higher end
129 address than the new one, insert the new one here. */
130 if (asr
->end
> caller
->end
)
135 if (asr
->start
>= caller
->start
)
137 /* The new range completely replaces an old
138 one (This may happen several times). */
141 delete_range (before
);
145 /* Replace the old range with the new one. */
146 asr
->start
= caller
->start
;
147 asr
->end
= caller
->end
;
153 /* Go on to next range. */
162 new_asr
->start
= caller
->start
;
163 new_asr
->end
= caller
->end
;
164 insert_range (before
, new_asr
);
171 /* The new range breaks the old one in two pieces,
172 so we have to use the second new range. */
173 new_asr2
->start
= right
->start
;
174 new_asr2
->end
= right
->end
;
176 insert_range (before
, left
);
179 right
->start
= caller
->end
+ 1;
183 left
->end
= caller
->start
- 1;
193 /* Free T and all subtrees. */
196 free_search_tree (ADDR_RANGE_TREE
*t
)
200 free_search_tree (t
->lower
);
201 free_search_tree (t
->higher
);
206 /* Subroutine of build_search_tree to recursively build a balanced tree.
207 ??? It's not an optimum tree though. */
209 static ADDR_RANGE_TREE
*
210 build_tree_1 (ADDR_SUBRANGE
**asrtab
, unsigned int n
)
212 unsigned int mid
= n
/ 2;
217 t
= (ADDR_RANGE_TREE
*) xmalloc (sizeof (ADDR_RANGE_TREE
));
218 t
->start
= asrtab
[mid
]->start
;
219 t
->end
= asrtab
[mid
]->end
;
221 t
->lower
= build_tree_1 (asrtab
, mid
);
225 t
->higher
= build_tree_1 (asrtab
+ mid
+ 1, n
- mid
- 1);
231 /* Build a search tree for address range AR. */
234 build_search_tree (ADDR_RANGE
*ar
)
236 /* ??? Simple version for now. */
237 ADDR_SUBRANGE
*asr
,**asrtab
;
240 for (n
= 0, asr
= ar
->ranges
; asr
!= NULL
; ++n
, asr
= asr
->next
)
242 asrtab
= (ADDR_SUBRANGE
**) xmalloc (n
* sizeof (ADDR_SUBRANGE
*));
243 for (i
= 0, asr
= ar
->ranges
; i
< n
; ++i
, asr
= asr
->next
)
245 ar
->range_tree
= build_tree_1 (asrtab
, n
);
250 sim_addr_range_add (ADDR_RANGE
*ar
, address_word start
, address_word end
)
252 frob_range (ar
, start
, end
, 0);
254 /* Rebuild the search tree. */
255 free_search_tree (ar
->range_tree
);
256 build_search_tree (ar
);
260 sim_addr_range_delete (ADDR_RANGE
*ar
, address_word start
, address_word end
)
262 frob_range (ar
, start
, end
, 1);
264 /* Rebuild the search tree. */
265 free_search_tree (ar
->range_tree
);
266 build_search_tree (ar
);
269 #endif /* DEFINE_NON_INLINE_P */
273 SIM_ARANGE_INLINE
int
274 sim_addr_range_hit_p (ADDR_RANGE
*ar
, address_word addr
)
276 ADDR_RANGE_TREE
*t
= ar
->range_tree
;
282 else if (addr
> t
->end
)
290 #endif /* DEFINE_INLINE_P */