Add section "Notes on disk format" to the manual.
[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_set.hpp>
29 #include <boost/icl/interval_map.hpp>
30
31 using namespace std;
32
33 /* Maps intervals (uint64_t, uint64_t) to a set of strings, where each
34  * string represents an object that covers that range.
35  */
36 typedef set<string> objects;
37 typedef boost::icl::interval_map<uint64_t, objects> ranges;
38
39 extern "C" void *
40 new_ranges (void)
41 {
42   return new ranges ();
43 }
44
45 extern "C" void
46 free_ranges (void *mapv)
47 {
48   ranges *map = (ranges *) mapv;
49   delete map;
50 }
51
52 extern "C" void
53 insert_range (void *mapv, uint64_t start, uint64_t end, const char *object)
54 {
55   ranges *map = (ranges *) mapv;
56   objects obj_set;
57   obj_set.insert (object);
58   map->add (make_pair (boost::icl::interval<uint64_t>::right_open (start, end),
59                        obj_set));
60 }
61
62 extern "C" void
63 iter_range (void *mapv, void (*f) (uint64_t start, uint64_t end, const char *object, void *opaque), void *opaque)
64 {
65   ranges *map = (ranges *) mapv;
66   ranges::iterator iter = map->begin ();
67   while (iter != map->end ()) {
68     boost::icl::interval<uint64_t>::type range = iter->first;
69     uint64_t start = range.lower ();
70     uint64_t end = range.upper ();
71
72     objects obj_set = iter->second;
73     objects::iterator iter2 = obj_set.begin ();
74     while (iter2 != obj_set.end ()) {
75       f (start, end, iter2->c_str (), opaque);
76       iter2++;
77     }
78     iter++;
79   }
80 }
81
82 extern "C" void
83 find_range (void *mapv, uint64_t start, uint64_t end, void (*f) (uint64_t start, uint64_t end, const char *object, void *opaque), void *opaque)
84 {
85   const ranges *map = (const ranges *) mapv;
86
87   boost::icl::interval<uint64_t>::type window;
88   window = boost::icl::interval<uint64_t>::right_open (start, end);
89
90   const ranges r = *map & window;
91
92   ranges::const_iterator iter = r.begin ();
93   while (iter != r.end ()) {
94     boost::icl::interval<uint64_t>::type range = iter->first;
95     uint64_t start = range.lower ();
96     uint64_t end = range.upper ();
97
98     objects obj_set = iter->second;
99     objects::iterator iter2 = obj_set.begin ();
100     while (iter2 != obj_set.end ()) {
101       f (start, end, iter2->c_str (), opaque);
102       iter2++;
103     }
104     iter++;
105   }
106 }