Home | History | Annotate | Download | only in libpp
      1 /**
      2  * @file symbol_container.h
      3  * Internal container for symbols
      4  *
      5  * @remark Copyright 2002, 2003 OProfile authors
      6  * @remark Read the file COPYING
      7  *
      8  * @author Philippe Elie
      9  * @author John Levon
     10  */
     11 
     12 #ifndef SYMBOL_CONTAINER_H
     13 #define SYMBOL_CONTAINER_H
     14 
     15 #include <string>
     16 #include <set>
     17 
     18 #include "symbol.h"
     19 #include "symbol_functors.h"
     20 
     21 /**
     22  * An arbitrary container of symbols. Supports lookup
     23  * by name, by VMA, and by file location.
     24  *
     25  * Lookup by name or by VMA is O(n). Lookup by file location
     26  * is O(log(n)).
     27  */
     28 class symbol_container {
     29 public:
     30 	/// container type
     31 	typedef std::set<symbol_entry, less_symbol> symbols_t;
     32 
     33 	typedef symbols_t::size_type size_type;
     34 
     35 	/// return the number of symbols stored
     36 	size_type size() const;
     37 
     38 	/**
     39 	 * Insert a new symbol. If the symbol already exists in the container,
     40 	 * then the sample counts are accumulated.
     41 	 * Returns the newly created symbol or the existing one. This pointer
     42 	 * remains valid during the whole life time of a symbol_container
     43 	 * object and is warranted unique according to less_symbol comparator.
     44 	 * Can only be done before any file-location based lookups, since the
     45 	 * two lookup methods are not synchronised.
     46 	 */
     47 	symbol_entry const * insert(symbol_entry const &);
     48 
     49 	/// find the symbols at the given filename and line number, if any
     50 	symbol_collection const find(debug_name_id filename, size_t linenr) const;
     51 
     52 	/// find the symbols defined in the given filename, if any
     53 	symbol_collection const find(debug_name_id filename) const;
     54 
     55 	/// find the symbol with the given image_name vma if any
     56 	symbol_entry const * find_by_vma(std::string const & image_name,
     57 					 bfd_vma vma) const;
     58 
     59 	/// Search a symbol. Return NULL if not found.
     60 	symbol_entry const * find(symbol_entry const & symbol) const;
     61 
     62 	/// return start of symbols
     63 	symbols_t::iterator begin();
     64 
     65 	/// return end of symbols
     66 	symbols_t::iterator end();
     67 
     68 private:
     69 	/// build the symbol by file-location cache
     70 	void build_by_loc() const;
     71 
     72 	/**
     73 	 * The main container of symbols. Multiple symbols with the same
     74 	 * name are allowed.
     75 	 */
     76 	symbols_t symbols;
     77 
     78 	/**
     79 	 * Differently-named symbol at same file location are allowed e.g.
     80 	 * template instantiation.
     81 	 */
     82 	typedef std::multiset<symbol_entry const *, less_by_file_loc>
     83 		symbols_by_loc_t;
     84 
     85 	// must be declared after the set to ensure a correct life-time.
     86 
     87 	/**
     88 	 * Symbols sorted by location order. Lazily built on request,
     89 	 * so mutable.
     90 	 */
     91 	mutable symbols_by_loc_t symbols_by_loc;
     92 };
     93 
     94 #endif /* SYMBOL_CONTAINER_H */
     95