Zero  0.1.0
mem_mgmt.h
Go to the documentation of this file.
1 #ifndef __MEM_MGMT_H
2 #define __MEM_MGMT_H
3 
4 #include "w_defines.h"
5 #include "sm_base.h"
6 
7 #undef MM_TEST
8 
31 public:
32  struct slot_t {
33  char* address;
34 
35  size_t length;
36 
37  slot_t(char* a, size_t l)
38  : address(a),
39  length(l) {}
40  };
41 
42 private:
43  class list_header_t {
44  private:
45  uint16_t tag;
46 
47  void update_footer() {
48  char* p = (char*)this + block_size() - 2;
49  memcpy(p, &tag, 2);
50  }
51 
52  public:
53  /*
54  * next and prev could be given also in 2 or 4 bytes is we use
55  * offsets relative to the memory manager buffer (_buf)
56  */
58 
60 
61  uint16_t block_size() {
62  return tag & 0x7FFF;
63  }
64 
65  void init(size_t block_size) {
66  // max length is 32K (15 bits)
67  uint16_t len = block_size;
68  // set new length value but maintain ms bit of tag
69  tag = (len & 0x7FFF) | (tag & 0x8000);
70  update_footer();
71  prev = nullptr;
72  next = nullptr;
73  }
74 
75  void set_free() {
76  tag &= 0x7FFF;
77  update_footer();
78  }
79 
80  void set_occupied() {
81  tag |= 0x8000;
82  update_footer();
83  }
84 
85  bool is_free() {
86  return !(tag & 0x8000);
87  }
88 
89  size_t slot_length() {
90  return block_size() - sizeof(list_header_t) - 2;
91  }
92 
94  uint16_t left_tag;
95  memcpy(&left_tag, (char*)this - 2, 2);
96  return (list_header_t*)((char*)this - (left_tag & 0x7FFF));
97  }
98 
100  return (list_header_t*)
101  ((char*)this + block_size());
102  }
103 
104  uint16_t footer() {
105  return *((uint16_t*)((char*)this + block_size() - 2));
106  }
107 
108  uint16_t header() {
109  return *((uint16_t*)this);
110  }
111 
112  static size_t get_best_fit(size_t length, size_t incr) {
113  size_t needed = length + sizeof(list_header_t) + 2;
114  size_t index = needed / incr;
115  if (needed % incr != 0) {
116  index++; // take ceil
117  }
118  return index * incr;
119  }
120  };
121 
122  const size_t _incr;
123 
124  const size_t _max;
125 
126  size_t _bufsize;
127 
129 
130  char* _buf;
131 
133 
135 
136  size_t _alloc;
137 
138  size_t _used;
139 
140  void add_to_list(size_t block_size, char* address);
141 
142  char* remove_from_list(size_t block_size);
143 
145 
146  bool is_list_empty(size_t block_size);
147 
148 #ifdef MM_TEST
149  void verify_lists();
150  void verify_block(void*);
151  void verify_neighbor(list_header_t*, list_header_t*, bool);
152  void verify_blocks();
153 #endif
154 
155 public:
157  size_t bufsize = 8192 * 10240,
158  size_t incr = 32,
159  size_t max = 16384);
160 
162 
163  rc_t allocate(size_t length, slot_t& slot);
164 
165  rc_t free(slot_t slot);
166 
167  rc_t defrag();
168 };
169 
170 #endif // __MEM_MGMT_H
Definition: mem_mgmt.h:32
uint16_t footer()
Definition: mem_mgmt.h:104
static size_t get_best_fit(size_t length, size_t incr)
Definition: mem_mgmt.h:112
void set_occupied()
Definition: mem_mgmt.h:80
uint16_t header()
Definition: mem_mgmt.h:108
size_t _used
Definition: mem_mgmt.h:138
size_t _alloc
Definition: mem_mgmt.h:136
size_t _bufsize
Definition: mem_mgmt.h:126
const size_t _max
Definition: mem_mgmt.h:124
list_header_t ** _lists
Definition: mem_mgmt.h:128
size_t _last_non_empty
Definition: mem_mgmt.h:134
fixed_lists_mem_t(size_t bufsize=8192 *10240, size_t incr=32, size_t max=16384)
Definition: mem_mgmt.cpp:114
list_header_t * prev
Definition: mem_mgmt.h:59
void update_footer()
Definition: mem_mgmt.h:47
size_t _first_non_empty
Definition: mem_mgmt.h:132
slot_t(char *a, size_t l)
Definition: mem_mgmt.h:37
rc_t allocate(size_t length, slot_t &slot)
Definition: mem_mgmt.cpp:151
uint16_t tag
Definition: mem_mgmt.h:45
~fixed_lists_mem_t()
Definition: mem_mgmt.cpp:135
list_header_t * next
Definition: mem_mgmt.h:57
const T max(const T x, const T y)
Definition: w_minmax.h:45
const size_t _incr
Definition: mem_mgmt.h:122
void set_free()
Definition: mem_mgmt.h:75
Definition: mem_mgmt.h:43
Return code for most functions and methods.
Definition: w_rc.h:87
void add_to_list(size_t block_size, char *address)
Definition: mem_mgmt.cpp:22
size_t slot_length()
Definition: mem_mgmt.h:89
char * address
Definition: mem_mgmt.h:33
char * remove_from_list(size_t block_size)
Definition: mem_mgmt.cpp:86
char * _buf
Definition: mem_mgmt.h:130
list_header_t * get_left_neighbor()
Definition: mem_mgmt.h:93
uint16_t block_size()
Definition: mem_mgmt.h:61
rc_t defrag()
Definition: mem_mgmt.cpp:258
list_header_t * get_right_neighbor()
Definition: mem_mgmt.h:99
rc_t free(slot_t slot)
Definition: mem_mgmt.cpp:211
bool is_free()
Definition: mem_mgmt.h:85
size_t length
Definition: mem_mgmt.h:35
Definition: mem_mgmt.h:30
void init(size_t block_size)
Definition: mem_mgmt.h:65
bool is_list_empty(size_t block_size)
Definition: mem_mgmt.cpp:106