1/**
2 * @file symbol_container.cpp
3 * Internal container for symbols
4 *
5 * @remark Copyright 2002 OProfile authors
6 * @remark Read the file COPYING
7 *
8 * @author Philippe Elie
9 * @author John Levon
10 */
11
12#include <string>
13#include <algorithm>
14#include <set>
15#include <vector>
16
17#include "symbol_container.h"
18
19using namespace std;
20
21symbol_container::size_type symbol_container::size() const
22{
23	return symbols.size();
24}
25
26
27symbol_entry const * symbol_container::insert(symbol_entry const & symb)
28{
29	pair<symbols_t::iterator, bool> p = symbols.insert(symb);
30	if (!p.second) {
31		// safe: count is not used by sorting criteria
32		symbol_entry * symbol = const_cast<symbol_entry*>(&*p.first);
33		symbol->sample.counts += symb.sample.counts;
34	}
35
36	return &*p.first;
37}
38
39
40symbol_collection const
41symbol_container::find(debug_name_id filename, size_t linenr) const
42{
43	build_by_loc();
44
45	symbol_entry symbol;
46	symbol.sample.file_loc.filename = filename;
47	symbol.sample.file_loc.linenr = linenr;
48
49	symbol_collection result;
50
51	typedef symbols_by_loc_t::const_iterator it;
52	pair<it, it> p_it = symbols_by_loc.equal_range(&symbol);
53	for ( ; p_it.first != p_it.second; ++p_it.first)
54		result.push_back(*p_it.first);
55
56	return result;
57}
58
59
60symbol_collection const
61symbol_container::find(debug_name_id filename) const
62{
63	build_by_loc();
64
65	symbol_entry symbol;
66	symbol.sample.file_loc.filename = filename;
67	symbol.sample.file_loc.linenr = 0;
68
69	typedef symbols_by_loc_t::const_iterator it;
70	it first = symbols_by_loc.lower_bound(&symbol);
71	symbol.sample.file_loc.linenr = (unsigned int)size_t(-1);
72	it last  = symbols_by_loc.upper_bound(&symbol);
73
74	symbol_collection result;
75	for ( ; first != last ; ++first)
76		result.push_back(*first);
77
78	return result;
79}
80
81
82void symbol_container::build_by_loc() const
83{
84	if (!symbols_by_loc.empty())
85		return;
86
87	symbols_t::const_iterator cit = symbols.begin();
88	symbols_t::const_iterator end = symbols.end();
89	for (; cit != end; ++cit)
90		symbols_by_loc.insert(&*cit);
91}
92
93
94symbol_entry const * symbol_container::find_by_vma(string const & image_name,
95						   bfd_vma vma) const
96{
97	// FIXME: this is too inefficient probably
98	symbols_t::const_iterator it;
99	for (it = symbols.begin(); it != symbols.end(); ++it) {
100		if (it->sample.vma == vma &&
101		    image_names.name(it->image_name) == image_name)
102			return &*it;
103	}
104
105	return 0;
106}
107
108
109symbol_container::symbols_t::iterator symbol_container::begin()
110{
111	return symbols.begin();
112}
113
114
115symbol_container::symbols_t::iterator symbol_container::end()
116{
117	return symbols.end();
118}
119
120symbol_entry const * symbol_container::find(symbol_entry const & symbol) const
121{
122	symbols_t::const_iterator it = symbols.find(symbol);
123	return it == symbols.end() ? 0 : &*it;
124}
125