Otawa  0.10
MemBlockMap.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  * Copyright (c) 2009, IRIT UPS <casse@irit.fr>
4  *
5  * MemBlockMap class interface
6  * This file is part of OTAWA
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Foobar; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #ifndef OTAWA_UTIL_MEMBLOCKMAP_H_
24 #define OTAWA_UTIL_MEMBLOCKMAP_H_
25 
26 #include <otawa/base.h>
27 
28 namespace otawa {
29 
31 
32 // MemBlockMap class
33 template <class T>
34 class MemBlockMap {
35 
36  typedef struct node_t {
37  inline node_t(const T *b, node_t *n): next(n), block(b) { }
38  struct node_t *next;
39  const T *block;
40  } node_t;
41 
42 public:
43  MemBlockMap(ot::size block_size = 32, t::size table_size = 211)
44  : pow(leastGreaterLog2(block_size)), bsize(1 << pow), tsize(table_size) {
45  nodes = new node_t *[tsize];
46  for(ot::size i = 0; i < tsize; i++) nodes[i] = 0;
47  }
48 
49  inline ~MemBlockMap(void) { clear(); delete [] nodes; }
50 
51  void clear(void) {
52  for(ot::size i = 0; i < tsize; i++) {
53  for(node_t *cur = nodes[i], *next; cur; cur = next) {
54  next = cur->next;
55  delete cur;
56  }
57  nodes[i] = 0;
58  }
59  }
60 
61  void add(const T *block) {
62  ASSERT(block->size());
63  ot::size top = (block->topAddress().offset() - 1) >> pow;
64  if(top & (bsize - 1))
65  top++;
66  for(ot::size ptr = block->address().offset() >> pow; ptr <= top; ptr++) {
67  ot::size entry = ptr % tsize;
68  nodes[entry] = new node_t(block, nodes[entry]);
69  }
70  }
71 
72  const T *get(Address address) {
73  ot::size entry = (address.offset() >> pow) % tsize;
74  for(node_t *node = nodes[entry]; node; node = node->next)
75  if(address >= node->block->address() && address < node->block->topAddress())
76  return node->block;
77  return 0;
78  }
79 
80 private:
83 };
84 
85 } // otawa
86 
87 #endif /* OTAWA_UTIL_MEMBLOCKMAP_H_ */
var x(Var *v)
Convert an ilp::Var * to a var.
Definition: expr.h:40
~MemBlockMap(void)
Definition: MemBlockMap.h:49
MemBlockMap(ot::size block_size=32, t::size table_size=211)
Build a memory block map.
Definition: MemBlockMap.h:43
ot::size leastGreaterLog2(ot::size x)
Compute the least greater logarithm of 2 for the given value.
Definition: util_MemBlockMap.cpp:32
struct otawa::MemBlockMap::node_t node_t
ot::size pow
Definition: MemBlockMap.h:81
uint32 size
elm::io::IntFormat address(Address addr)
Build a format to display addresses.
Definition: base.cpp:213
t::uint32 size
Definition: base.h:46
dtd::Element entry(dtd::make("entry", _ENTRY).attr(id))
ot::size tsize
Definition: MemBlockMap.h:81
The representation of an address in OTAWA.
Definition: base.h:54
struct node_t * next
Definition: MemBlockMap.h:38
const T * block
Definition: MemBlockMap.h:39
node_t ** nodes
Definition: MemBlockMap.h:82
void clear(void)
Clear the content of map.
Definition: MemBlockMap.h:51
This class is used to retrieve quickly blocks from their containing address.
Definition: MemBlockMap.h:34
Definition: MemBlockMap.h:36
node_t(const T *b, node_t *n)
Definition: MemBlockMap.h:37
ot::size bsize
Definition: MemBlockMap.h:81
void add(const T *block)
Add a block to the map.
Definition: MemBlockMap.h:61