virt-bmap: Examine whole devices.
[virt-bmap.git] / ranges.cpp
1 /* virt-bmap examiner plugin
2  * Copyright (C) 2014 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <inttypes.h>
25 #include <assert.h>
26
27 #include <boost/icl/interval.hpp>
28 #include <boost/icl/interval_map.hpp>
29
30 using namespace std;
31
32 /* Maps intervals (uint64_t, uint64_t) to a set of strings, where each
33  * string represents an object that covers that range.
34  */
35 typedef set<string> objects;
36 typedef boost::icl::interval_map<uint64_t, objects> ranges;
37
38 extern "C" void *
39 new_ranges (void)
40 {
41   return new ranges ();
42 }
43
44 extern "C" void
45 free_ranges (void *mapv)
46 {
47   ranges *map = (ranges *) mapv;
48   delete map;
49 }
50
51 extern "C" void
52 insert_range (void *mapv, uint64_t start, uint64_t end, const char *object)
53 {
54   ranges *map = (ranges *) mapv;
55   objects obj_set;
56   obj_set.insert (object);
57   map->add (make_pair (boost::icl::interval<uint64_t>::right_open (start, end),
58                        obj_set));
59 }
60
61 extern "C" void
62 iter_range (void *mapv, void (*f) (uint64_t start, uint64_t end, const char *object, void *opaque), void *opaque)
63 {
64   ranges *map = (ranges *) mapv;
65   ranges::iterator iter = map->begin ();
66   while (iter != map->end ()) {
67     boost::icl::interval<uint64_t>::type range = iter->first;
68     uint64_t start = range.lower ();
69     uint64_t end = range.upper ();
70
71     objects obj_set = iter->second;
72     objects::iterator iter2 = obj_set.begin ();
73     while (iter2 != obj_set.end ()) {
74       f (start, end, iter2->c_str (), opaque);
75       iter2++;
76     }
77     iter++;
78   }
79 }