Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
cmemory.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef CMEMORY_H
6 #define CMEMORY_H 1
7 
8 #include <set>
9 #include <string>
10 #include <sstream>
11 #include <inttypes.h>
12 #include <pthread.h>
13 #include <stdlib.h>
14 
15 #include "croflexception.h"
16 #include "logging.h"
17 
18 namespace rofl
19 {
20 
21 /* error classes */
22 class eMemBase : public RoflException {};
23 class eMemAllocFailed : public eMemBase {};
24 class eMemOutOfRange : public eMemBase {};
25 class eMemNotFound : public eMemBase {};
26 class eMemInval : public eMemBase {};
27 
28 
29 
44 class cmemory {
45 private:
46 
47  static std::set<cmemory*> cmemory_list;
48  static pthread_mutex_t memlock;
49  static int memlockcnt;
50 
51  std::pair<uint8_t*, size_t> data; //< memory area including head- and tail-space
52 
53 #define CMEMORY_DEFAULT_SIZE 0
54 
55 
56 public:
57 
58 
59 
65  cmemory(
66  size_t len = CMEMORY_DEFAULT_SIZE);
67 
68 
69 
76  cmemory(
77  uint8_t *data,
78  size_t datalen);
79 
80 
81 
87  cmemory(
88  cmemory const& m);
89 
90 
91 
96  virtual
97  ~cmemory();
98 
99 
100 public:
101 
113  cmemory&
114  operator= (
115  cmemory const& m);
116 
117 
118 
126  uint8_t&
127  operator[] (
128  size_t index) const;
129 
130 
131 
137  bool
138  operator== (
139  cmemory const& m) const;
140 
141 
142 
148  bool
149  operator!= (
150  cmemory const& m) const;
151 
152 
153 
159  bool
160  operator< (
161  cmemory const& m) const;
162 
163 
169  bool
170  operator> (
171  cmemory const& m) const;
172 
173 
174 
181  cmemory
182  operator& (
183  cmemory const& m) const;
184 
185 
186 
192  cmemory&
193  operator+= (
194  cmemory const& m);
195 
196 
197 
203  cmemory
204  operator+ (
205  cmemory const& m);
206 
207 
208 
213  uint8_t*
214  somem() const;
215 
216 
217 
222  size_t
223  memlen() const;
224 
225 
226 
233  virtual void
234  assign(
235  uint8_t *buf,
236  size_t buflen);
237 
238 
239 
243  virtual size_t
244  length() const { return data.second; };
245 
246 
247 
258  virtual uint8_t*
259  resize(
260  size_t len);
261 
262 
263 
271  uint8_t*
272  insert(
273  uint8_t *ptr,
274  size_t len);
275 
276 
284  uint8_t*
285  insert(
286  unsigned int offset,
287  size_t len);
288 
289 
290 
298  void
299  remove(
300  uint8_t *ptr,
301  size_t len);
302 
303 
304 
312  void
313  remove(
314  unsigned int offset,
315  size_t len);
316 
317 
318 
326  unsigned int
328  uint8_t value,
329  unsigned int start);
330 
331 
332 
337  void
338  clear();
339 
340 
341 
345  virtual void
346  pack(
347  uint8_t *buf,
348  size_t buflen);
349 
350 
351 
355  virtual void
356  unpack(
357  uint8_t *buf,
358  size_t buflen);
359 
360 
364  virtual std::string
365  toString() const;
366 
367 
371  virtual bool
372  empty() const { return (0 == memlen()); };
373 
378 private: // methods
379 
380 
384  void
385  mallocate(
386  size_t len) throw (eMemAllocFailed);
387 
388 
392  void mfree();
393 
394 public:
395 
396  friend std::ostream&
397  operator<< (std::ostream& os, cmemory const& mem) {
398  os << indent(0) << "<cmemory: ";
399  os << "data:" << (void*)mem.data.first << " ";
400  os << "datalen:" << (int)mem.data.second << " ";
401  os << ">" << std::endl;
402 
403  unsigned int const width = 32;
404 
405  if (mem.data.second > 0) {
406  for (unsigned int i=0; i < mem.data.second; i++) {
407  if (0 == (i % width)) {
408  os << indent(2)
409  << std::setfill('0')
410  << std::setw(4)
411  << std::dec << (i/width) << ": " << std::hex
412  << std::setw(0)
413  << std::setfill(' ');
414  }
415 
416  os << std::setfill('0')
417  << std::setw(2)
418  << std::hex << (int)(*(mem.somem() + i)) << std::dec
419  << std::setw(0)
420  << std::setfill(' ')
421  << " ";
422 
423  if (0 == ((i+1) % 8))
424  os << " ";
425  if (0 == ((i+1) % width))
426  os << std::endl;
427  }
428  os << std::endl;
429  }
430  return os;
431  };
432 };
433 
434 }; // end of namespace
435 
436 #endif
Definition: cmemory.h:22
virtual std::string toString() const
map onto std::string instance
Definition: cmemory.cc:314
virtual void pack(uint8_t *buf, size_t buflen)
Copies content of this cmemory instance to specified buffer.
Definition: cmemory.cc:292
size_t memlen() const
Returns length of allocated memory area.
Definition: cmemory.cc:109
cmemory & operator+=(cmemory const &m)
Append operator with assignment to this cmemory instance.
Definition: cmemory.cc:193
bool operator!=(cmemory const &m) const
Comparison operator (unequal).
Definition: cmemory.cc:232
uint8_t * somem() const
Returns pointer to start of allocated memory area.
Definition: cmemory.cc:101
Definition: cmemory.h:25
bool operator==(cmemory const &m) const
Comparison operator.
Definition: cmemory.cc:219
bool operator<(cmemory const &m) const
Less than operator.
Definition: cmemory.cc:129
cmemory operator+(cmemory const &m)
Append operator with assignment to temporary cmemory instance.
Definition: cmemory.cc:207
Definition: cmemory.h:24
virtual ~cmemory()
Destructor. Calls C-function free() for allocated memory area.
Definition: cmemory.cc:69
virtual uint8_t * resize(size_t len)
Resizes allocated memory area by calling C-function realloc().
Definition: cmemory.cc:253
cmemory operator&(cmemory const &m) const
AND operator.
Definition: cmemory.cc:175
uint8_t & operator[](size_t index) const
Index operator.
Definition: cmemory.cc:117
C++ abstraction for malloc'ed memory areas.
Definition: cmemory.h:44
bool operator>(cmemory const &m) const
Less than operator.
Definition: cmemory.cc:152
cmemory & operator=(cmemory const &m)
Assignment operator.
Definition: cmemory.cc:85
virtual void assign(uint8_t *buf, size_t buflen)
Overwrites memory area with plain buffer specified. Resizes internal memory as necessary.
Definition: cmemory.cc:241
cmemory(size_t len=CMEMORY_DEFAULT_SIZE)
Constructor. Allocates a new memory area with specified size (default: 1024 bytes).
Definition: cmemory.cc:13
Definition: logging.h:76
uint8_t * insert(uint8_t *ptr, size_t len)
Inserts len bytes at pointer ptr into allocated memory area.
Definition: cmemory.cc:354
Definition: cmemory.h:23
virtual bool empty() const
Returns boolean value empty/non empty.
Definition: cmemory.h:372
unsigned int find_first_of(uint8_t value, unsigned int start)
Returns index of first byte with value "value" found in allocated memory area starting at offset "sta...
Definition: cmemory.cc:437
Definition: croflexception.h:27
virtual void unpack(uint8_t *buf, size_t buflen)
Copies content of specified buffer into this cmemory instance .
Definition: cmemory.cc:304
void clear()
Clears the allocated memory area by setting all bytes to 0.
Definition: cmemory.cc:281
Definition: cmemory.h:26