mm.h revision 12855
1
2// *******************************************************************
3// User-defined memory manager, which maintains a pool of transactions
4// *******************************************************************
5
6#include "tlm.h"
7
8class mm: public tlm::tlm_mm_interface
9{
10  typedef tlm::tlm_generic_payload gp_t;
11
12public:
13  mm() : free_list(0), empties(0) {}
14
15  virtual ~mm()
16  {
17    gp_t* ptr;
18
19    while (free_list)
20    {
21      ptr = free_list->trans;
22
23      // Delete generic payload and all extensions
24      sc_assert(ptr);
25      delete ptr;
26
27      free_list = free_list->next;
28    }
29
30    while (empties)
31    {
32      access* x = empties;
33      empties = empties->next;
34
35      // Delete free list access struct
36      delete x;
37    }
38  }
39
40  gp_t* allocate();
41  void  free(gp_t* trans);
42
43private:
44  struct access
45  {
46    gp_t* trans;
47    access* next;
48    access* prev;
49  };
50
51  access* free_list;
52  access* empties;
53};
54
55mm::gp_t* mm::allocate()
56{
57  gp_t* ptr;
58  if (free_list)
59  {
60    ptr = free_list->trans;
61    empties = free_list;
62    free_list = free_list->next;
63  }
64  else
65  {
66    ptr = new gp_t(this);
67  }
68  return ptr;
69}
70
71void mm::free(gp_t* trans)
72{
73  trans->reset(); // Delete auto extensions
74  if (!empties)
75  {
76    empties = new access;
77    empties->next = free_list;
78    empties->prev = 0;
79    if (free_list)
80      free_list->prev = empties;
81  }
82  free_list = empties;
83  free_list->trans = trans;
84  empties = free_list->prev;
85}
86