File: addressmap.hh

package info (click to toggle)
qdmr 0.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,420 kB
  • sloc: cpp: 95,929; xml: 10,749; python: 1,108; makefile: 78; sh: 9
file content (68 lines) | stat: -rw-r--r-- 2,196 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef ADDRESSMAP_HH
#define ADDRESSMAP_HH

#include <cinttypes>
#include <vector>

/** This class represents a memory map.
 * That is, it maintains a vector of memory regions (address and length) that can be searched
 * efficiently. This should speedup the generation of codeplugs consisting of many small memory
 * sections.
 *
 * @ingroup util */
class AddressMap
{
public:
  /** Empty constructor. */
  AddressMap();
  /** Copy constructor. */
  AddressMap(const AddressMap &other);

  /** Copy assignment. */
  AddressMap &operator=(const AddressMap &other);

  /** Clears the address map. */
  void clear();
  /** Adds an item to the address map. */
  bool add(uint32_t addr, uint32_t len, int idx=-1);
  /** Removes an item from the address map associated with the given index. */
  bool rem(uint32_t idx);
  /** Returns @c true if the given address is contained in any of the memory regions. */
  bool contains(uint32_t addr) const;
  /** Finds the index of the memory region containing the given address. If no such region is found,
   * -1 is returned. */
  int find(uint32_t addr) const;

protected:
  /** Memory map item.
   * That is, a collection of address, length and associated index. */
  struct AddrMapItem {
    uint32_t address;   ///< The start address of the item.
    uint32_t length;    ///< The size/length of the memory item.
    uint32_t index;     ///< The associated (element) index.

    /** Constructor. */
    inline AddrMapItem(uint32_t addr, uint32_t len, uint32_t idx)
      : address(addr), length(len), index(idx) {
      // pass...
    }
    /** Comparison operator. */
    inline bool operator<(const AddrMapItem &other) const {
      return address < other.address;
    }
    /** Comparison operator. */
    inline bool operator<(uint32_t addr) const {
      return address < addr;
    }
    /** Returns @c true if the given address is contained within this memory region. */
    inline bool contains(uint32_t addr) const {
      return (address <= addr) && ((address+length) > addr);
    }
  };

protected:
  /** Holds the vector of memory items, the order of these items is maintained. */
  std::vector<AddrMapItem> _items;
};

#endif // ADDRESSMAP_HH