1 /* addrmap.c --- implementation of address map data structure.
3 Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GDB.
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/>. */
24 #include "splay-tree.h"
25 #include "gdb_obstack.h"
27 #include "gdb_assert.h"
31 /* The "abstract class". */
33 /* Functions implementing the addrmap functions for a particular
37 void (*set_empty
) (struct addrmap
*this,
38 CORE_ADDR start
, CORE_ADDR end_inclusive
,
40 void *(*find
) (struct addrmap
*this, CORE_ADDR addr
);
41 struct addrmap
*(*create_fixed
) (struct addrmap
*this,
42 struct obstack
*obstack
);
43 void (*relocate
) (struct addrmap
*this, CORE_ADDR offset
);
44 int (*foreach
) (struct addrmap
*this, addrmap_foreach_fn fn
, void *data
);
50 const struct addrmap_funcs
*funcs
;
55 addrmap_set_empty (struct addrmap
*map
,
56 CORE_ADDR start
, CORE_ADDR end_inclusive
,
59 map
->funcs
->set_empty (map
, start
, end_inclusive
, obj
);
64 addrmap_find (struct addrmap
*map
, CORE_ADDR addr
)
66 return map
->funcs
->find (map
, addr
);
71 addrmap_create_fixed (struct addrmap
*original
, struct obstack
*obstack
)
73 return original
->funcs
->create_fixed (original
, obstack
);
77 /* Relocate all the addresses in MAP by OFFSET. (This can be applied
78 to either mutable or immutable maps.) */
80 addrmap_relocate (struct addrmap
*map
, CORE_ADDR offset
)
82 map
->funcs
->relocate (map
, offset
);
87 addrmap_foreach (struct addrmap
*map
, addrmap_foreach_fn fn
, void *data
)
89 return map
->funcs
->foreach (map
, fn
, data
);
92 /* Fixed address maps. */
94 /* A transition: a point in an address map where the value changes.
95 The map maps ADDR to VALUE, but if ADDR > 0, it maps ADDR-1 to
97 struct addrmap_transition
106 struct addrmap addrmap
;
108 /* The number of transitions in TRANSITIONS. */
109 size_t num_transitions
;
111 /* An array of transitions, sorted by address. For every point in
112 the map where either ADDR == 0 or ADDR is mapped to one value and
113 ADDR - 1 is mapped to something different, we have an entry here
114 containing ADDR and VALUE. (Note that this means we always have
115 an entry for address 0). */
116 struct addrmap_transition transitions
[1];
121 addrmap_fixed_set_empty (struct addrmap
*this,
122 CORE_ADDR start
, CORE_ADDR end_inclusive
,
125 internal_error (__FILE__
, __LINE__
,
126 "addrmap_fixed_set_empty: "
127 "fixed addrmaps can't be changed\n");
132 addrmap_fixed_find (struct addrmap
*this, CORE_ADDR addr
)
134 struct addrmap_fixed
*map
= (struct addrmap_fixed
*) this;
135 struct addrmap_transition
*bottom
= &map
->transitions
[0];
136 struct addrmap_transition
*top
= &map
->transitions
[map
->num_transitions
- 1];
140 /* This needs to round towards top, or else when top = bottom +
141 1 (i.e., two entries are under consideration), then mid ==
142 bottom, and then we may not narrow the range when (mid->addr
144 struct addrmap_transition
*mid
= top
- (top
- bottom
) / 2;
146 if (mid
->addr
== addr
)
151 else if (mid
->addr
< addr
)
152 /* We don't eliminate mid itself here, since each transition
153 covers all subsequent addresses until the next. This is why
154 we must round up in computing the midpoint. */
160 return bottom
->value
;
164 static struct addrmap
*
165 addrmap_fixed_create_fixed (struct addrmap
*this, struct obstack
*obstack
)
167 internal_error (__FILE__
, __LINE__
,
168 _("addrmap_create_fixed is not implemented yet "
169 "for fixed addrmaps"));
174 addrmap_fixed_relocate (struct addrmap
*this, CORE_ADDR offset
)
176 struct addrmap_fixed
*map
= (struct addrmap_fixed
*) this;
179 for (i
= 0; i
< map
->num_transitions
; i
++)
180 map
->transitions
[i
].addr
+= offset
;
185 addrmap_fixed_foreach (struct addrmap
*this, addrmap_foreach_fn fn
,
188 struct addrmap_fixed
*map
= (struct addrmap_fixed
*) this;
191 for (i
= 0; i
< map
->num_transitions
; i
++)
193 int res
= fn (data
, map
->transitions
[i
].addr
, map
->transitions
[i
].value
);
203 static const struct addrmap_funcs addrmap_fixed_funcs
=
205 addrmap_fixed_set_empty
,
207 addrmap_fixed_create_fixed
,
208 addrmap_fixed_relocate
,
209 addrmap_fixed_foreach
214 /* Mutable address maps. */
216 struct addrmap_mutable
218 struct addrmap addrmap
;
220 /* The obstack to use for allocations for this map. */
221 struct obstack
*obstack
;
223 /* A splay tree, with a node for each transition; there is a
224 transition at address T if T-1 and T map to different objects.
226 Any addresses below the first node map to NULL. (Unlike
227 fixed maps, we have no entry at (CORE_ADDR) 0; it doesn't
230 The last region is assumed to end at CORE_ADDR_MAX.
232 Since we can't know whether CORE_ADDR is larger or smaller than
233 splay_tree_key (unsigned long) --- I think both are possible,
234 given all combinations of 32- and 64-bit hosts and targets ---
235 our keys are pointers to CORE_ADDR values. Since the splay tree
236 library doesn't pass any closure pointer to the key free
237 function, we can't keep a freelist for keys. Since mutable
238 addrmaps are only used temporarily right now, we just leak keys
239 from deleted nodes; they'll be freed when the obstack is freed. */
242 /* A freelist for splay tree nodes, allocated on obstack, and
243 chained together by their 'right' pointers. */
244 splay_tree_node free_nodes
;
248 /* Allocate a copy of CORE_ADDR in MAP's obstack. */
249 static splay_tree_key
250 allocate_key (struct addrmap_mutable
*map
, CORE_ADDR addr
)
252 CORE_ADDR
*key
= obstack_alloc (map
->obstack
, sizeof (*key
));
255 return (splay_tree_key
) key
;
259 /* Type-correct wrappers for splay tree access. */
260 static splay_tree_node
261 addrmap_splay_tree_lookup (struct addrmap_mutable
*map
, CORE_ADDR addr
)
263 return splay_tree_lookup (map
->tree
, (splay_tree_key
) &addr
);
267 static splay_tree_node
268 addrmap_splay_tree_predecessor (struct addrmap_mutable
*map
, CORE_ADDR addr
)
270 return splay_tree_predecessor (map
->tree
, (splay_tree_key
) &addr
);
274 static splay_tree_node
275 addrmap_splay_tree_successor (struct addrmap_mutable
*map
, CORE_ADDR addr
)
277 return splay_tree_successor (map
->tree
, (splay_tree_key
) &addr
);
282 addrmap_splay_tree_remove (struct addrmap_mutable
*map
, CORE_ADDR addr
)
284 splay_tree_remove (map
->tree
, (splay_tree_key
) &addr
);
289 addrmap_node_key (splay_tree_node node
)
291 return * (CORE_ADDR
*) node
->key
;
296 addrmap_node_value (splay_tree_node node
)
298 return (void *) node
->value
;
303 addrmap_node_set_value (splay_tree_node node
, void *value
)
305 node
->value
= (splay_tree_value
) value
;
310 addrmap_splay_tree_insert (struct addrmap_mutable
*map
, CORE_ADDR key
, void *value
)
312 splay_tree_insert (map
->tree
,
313 allocate_key (map
, key
),
314 (splay_tree_value
) value
);
318 /* Without changing the mapping of any address, ensure that there is a
319 tree node at ADDR, even if it would represent a "transition" from
320 one value to the same value. */
322 force_transition (struct addrmap_mutable
*this, CORE_ADDR addr
)
325 = addrmap_splay_tree_lookup (this, addr
);
329 n
= addrmap_splay_tree_predecessor (this, addr
);
330 addrmap_splay_tree_insert (this, addr
,
331 n
? addrmap_node_value (n
) : NULL
);
337 addrmap_mutable_set_empty (struct addrmap
*this,
338 CORE_ADDR start
, CORE_ADDR end_inclusive
,
341 struct addrmap_mutable
*map
= (struct addrmap_mutable
*) this;
342 splay_tree_node n
, next
;
345 /* If we're being asked to set all empty portions of the given
346 address range to empty, then probably the caller is confused.
347 (If that turns out to be useful in some cases, then we can change
348 this to simply return, since overriding NULL with NULL is a
352 /* We take a two-pass approach, for simplicity.
353 - Establish transitions where we think we might need them.
354 - First pass: change all NULL regions to OBJ.
355 - Second pass: remove any unnecessary transitions. */
357 /* Establish transitions at the start and end. */
358 force_transition (map
, start
);
359 if (end_inclusive
< CORE_ADDR_MAX
)
360 force_transition (map
, end_inclusive
+ 1);
362 /* Walk the area, changing all NULL regions to OBJ. */
363 for (n
= addrmap_splay_tree_lookup (map
, start
), gdb_assert (n
);
364 n
&& addrmap_node_key (n
) <= end_inclusive
;
365 n
= addrmap_splay_tree_successor (map
, addrmap_node_key (n
)))
367 if (! addrmap_node_value (n
))
368 addrmap_node_set_value (n
, obj
);
371 /* Walk the area again, removing transitions from any value to
372 itself. Be sure to visit both the transitions we forced
374 n
= addrmap_splay_tree_predecessor (map
, start
);
375 prior_value
= n
? addrmap_node_value (n
) : NULL
;
376 for (n
= addrmap_splay_tree_lookup (map
, start
), gdb_assert (n
);
377 n
&& (end_inclusive
== CORE_ADDR_MAX
378 || addrmap_node_key (n
) <= end_inclusive
+ 1);
381 next
= addrmap_splay_tree_successor (map
, addrmap_node_key (n
));
382 if (addrmap_node_value (n
) == prior_value
)
383 addrmap_splay_tree_remove (map
, addrmap_node_key (n
));
385 prior_value
= addrmap_node_value (n
);
391 addrmap_mutable_find (struct addrmap
*this, CORE_ADDR addr
)
393 /* Not needed yet. */
394 internal_error (__FILE__
, __LINE__
,
395 _("addrmap_find is not implemented yet "
396 "for mutable addrmaps"));
400 /* A function to pass to splay_tree_foreach to count the number of nodes
403 splay_foreach_count (splay_tree_node n
, void *closure
)
405 size_t *count
= (size_t *) closure
;
412 /* A function to pass to splay_tree_foreach to copy entries into a
413 fixed address map. */
415 splay_foreach_copy (splay_tree_node n
, void *closure
)
417 struct addrmap_fixed
*fixed
= (struct addrmap_fixed
*) closure
;
418 struct addrmap_transition
*t
= &fixed
->transitions
[fixed
->num_transitions
];
420 t
->addr
= addrmap_node_key (n
);
421 t
->value
= addrmap_node_value (n
);
422 fixed
->num_transitions
++;
428 static struct addrmap
*
429 addrmap_mutable_create_fixed (struct addrmap
*this, struct obstack
*obstack
)
431 struct addrmap_mutable
*mutable = (struct addrmap_mutable
*) this;
432 struct addrmap_fixed
*fixed
;
433 size_t num_transitions
;
435 /* Count the number of transitions in the tree. */
437 splay_tree_foreach (mutable->tree
, splay_foreach_count
, &num_transitions
);
439 /* Include an extra entry for the transition at zero (which fixed
440 maps have, but mutable maps do not.) */
443 fixed
= obstack_alloc (obstack
,
446 * sizeof (fixed
->transitions
[0]))));
447 fixed
->addrmap
.funcs
= &addrmap_fixed_funcs
;
448 fixed
->num_transitions
= 1;
449 fixed
->transitions
[0].addr
= 0;
450 fixed
->transitions
[0].value
= NULL
;
452 /* Copy all entries from the splay tree to the array, in order
453 of increasing address. */
454 splay_tree_foreach (mutable->tree
, splay_foreach_copy
, fixed
);
456 /* We should have filled the array. */
457 gdb_assert (fixed
->num_transitions
== num_transitions
);
459 return (struct addrmap
*) fixed
;
464 addrmap_mutable_relocate (struct addrmap
*this, CORE_ADDR offset
)
466 /* Not needed yet. */
467 internal_error (__FILE__
, __LINE__
,
468 _("addrmap_relocate is not implemented yet "
469 "for mutable addrmaps"));
473 /* Struct to map addrmap's foreach function to splay_tree's version. */
474 struct mutable_foreach_data
476 addrmap_foreach_fn fn
;
481 /* This is a splay_tree_foreach_fn. */
484 addrmap_mutable_foreach_worker (splay_tree_node node
, void *data
)
486 struct mutable_foreach_data
*foreach_data
= data
;
488 return foreach_data
->fn (foreach_data
->data
,
489 addrmap_node_key (node
),
490 addrmap_node_value (node
));
495 addrmap_mutable_foreach (struct addrmap
*this, addrmap_foreach_fn fn
,
498 struct addrmap_mutable
*mutable = (struct addrmap_mutable
*) this;
499 struct mutable_foreach_data foreach_data
;
501 foreach_data
.fn
= fn
;
502 foreach_data
.data
= data
;
503 return splay_tree_foreach (mutable->tree
, addrmap_mutable_foreach_worker
,
508 static const struct addrmap_funcs addrmap_mutable_funcs
=
510 addrmap_mutable_set_empty
,
511 addrmap_mutable_find
,
512 addrmap_mutable_create_fixed
,
513 addrmap_mutable_relocate
,
514 addrmap_mutable_foreach
519 splay_obstack_alloc (int size
, void *closure
)
521 struct addrmap_mutable
*map
= closure
;
524 /* We should only be asked to allocate nodes and larger things.
525 (If, at some point in the future, this is no longer true, we can
526 just round up the size to sizeof (*n).) */
527 gdb_assert (size
>= sizeof (*n
));
532 map
->free_nodes
= n
->right
;
536 return obstack_alloc (map
->obstack
, size
);
541 splay_obstack_free (void *obj
, void *closure
)
543 struct addrmap_mutable
*map
= closure
;
544 splay_tree_node n
= obj
;
546 /* We've asserted in the allocation function that we only allocate
547 nodes or larger things, so it should be safe to put whatever
548 we get passed back on the free list. */
549 n
->right
= map
->free_nodes
;
554 /* Compare keys as CORE_ADDR * values. */
556 splay_compare_CORE_ADDR_ptr (splay_tree_key ak
, splay_tree_key bk
)
558 CORE_ADDR a
= * (CORE_ADDR
*) ak
;
559 CORE_ADDR b
= * (CORE_ADDR
*) bk
;
561 /* We can't just return a-b here, because of over/underflow. */
572 addrmap_create_mutable (struct obstack
*obstack
)
574 struct addrmap_mutable
*map
= obstack_alloc (obstack
, sizeof (*map
));
576 map
->addrmap
.funcs
= &addrmap_mutable_funcs
;
577 map
->obstack
= obstack
;
579 /* splay_tree_new_with_allocator uses the provided allocation
580 function to allocate the main splay_tree structure itself, so our
581 free list has to be initialized before we create the tree. */
582 map
->free_nodes
= NULL
;
584 map
->tree
= splay_tree_new_with_allocator (splay_compare_CORE_ADDR_ptr
,
585 NULL
, /* no delete key */
586 NULL
, /* no delete value */
591 return (struct addrmap
*) map
;
596 /* Initialization. */
598 /* Provide a prototype to silence -Wmissing-prototypes. */
599 extern initialize_file_ftype _initialize_addrmap
;
602 _initialize_addrmap (void)
604 /* Make sure splay trees can actually hold the values we want to
606 gdb_assert (sizeof (splay_tree_key
) >= sizeof (CORE_ADDR
*));
607 gdb_assert (sizeof (splay_tree_value
) >= sizeof (void *));