Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Committer:
Michael J. Spencer
Date:
Fri Feb 28 18:52:52 2014 -0800
Revision:
2:1df0b61d3b5a
Child:
3:f151d08d335c
Update to latest Smoothie.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Michael J. Spencer 2:1df0b61d3b5a 1 #ifndef _MEMORYPOOL_H
Michael J. Spencer 2:1df0b61d3b5a 2 #define _MEMORYPOOL_H
Michael J. Spencer 2:1df0b61d3b5a 3
Michael J. Spencer 2:1df0b61d3b5a 4 #include <cstdint>
Michael J. Spencer 2:1df0b61d3b5a 5 // #include <cstdio>
Michael J. Spencer 2:1df0b61d3b5a 6 #include <cstdlib>
Michael J. Spencer 2:1df0b61d3b5a 7
Michael J. Spencer 2:1df0b61d3b5a 8 #ifdef MEMDEBUG
Michael J. Spencer 2:1df0b61d3b5a 9 #define MDEBUG(...) printf(__VA_ARGS__)
Michael J. Spencer 2:1df0b61d3b5a 10 #else
Michael J. Spencer 2:1df0b61d3b5a 11 #define MDEBUG(...) do {} while (0)
Michael J. Spencer 2:1df0b61d3b5a 12 #endif
Michael J. Spencer 2:1df0b61d3b5a 13
Michael J. Spencer 2:1df0b61d3b5a 14 #include "StreamOutput.h"
Michael J. Spencer 2:1df0b61d3b5a 15
Michael J. Spencer 2:1df0b61d3b5a 16 /*
Michael J. Spencer 2:1df0b61d3b5a 17 * with MUCH thanks to http://www.parashift.com/c++-faq-lite/memory-pools.html
Michael J. Spencer 2:1df0b61d3b5a 18 *
Michael J. Spencer 2:1df0b61d3b5a 19 * test framework at https://gist.github.com/triffid/5563987
Michael J. Spencer 2:1df0b61d3b5a 20 */
Michael J. Spencer 2:1df0b61d3b5a 21
Michael J. Spencer 2:1df0b61d3b5a 22 class MemoryPool
Michael J. Spencer 2:1df0b61d3b5a 23 {
Michael J. Spencer 2:1df0b61d3b5a 24 public:
Michael J. Spencer 2:1df0b61d3b5a 25 MemoryPool(void* base, uint16_t size);
Michael J. Spencer 2:1df0b61d3b5a 26 ~MemoryPool();
Michael J. Spencer 2:1df0b61d3b5a 27
Michael J. Spencer 2:1df0b61d3b5a 28 void* alloc(size_t);
Michael J. Spencer 2:1df0b61d3b5a 29 void dealloc(void* p);
Michael J. Spencer 2:1df0b61d3b5a 30
Michael J. Spencer 2:1df0b61d3b5a 31 void debug(StreamOutput*);
Michael J. Spencer 2:1df0b61d3b5a 32
Michael J. Spencer 2:1df0b61d3b5a 33 bool has(void*);
Michael J. Spencer 2:1df0b61d3b5a 34
Michael J. Spencer 2:1df0b61d3b5a 35 uint32_t free(void);
Michael J. Spencer 2:1df0b61d3b5a 36
Michael J. Spencer 2:1df0b61d3b5a 37 MemoryPool* next;
Michael J. Spencer 2:1df0b61d3b5a 38
Michael J. Spencer 2:1df0b61d3b5a 39 static MemoryPool* first;
Michael J. Spencer 2:1df0b61d3b5a 40
Michael J. Spencer 2:1df0b61d3b5a 41 private:
Michael J. Spencer 2:1df0b61d3b5a 42 void* base;
Michael J. Spencer 2:1df0b61d3b5a 43 uint16_t size;
Michael J. Spencer 2:1df0b61d3b5a 44 };
Michael J. Spencer 2:1df0b61d3b5a 45
Michael J. Spencer 2:1df0b61d3b5a 46 // this overloads "placement new"
Michael J. Spencer 2:1df0b61d3b5a 47 inline void* operator new(size_t nbytes, MemoryPool& pool)
Michael J. Spencer 2:1df0b61d3b5a 48 {
Michael J. Spencer 2:1df0b61d3b5a 49 return pool.alloc(nbytes);
Michael J. Spencer 2:1df0b61d3b5a 50 }
Michael J. Spencer 2:1df0b61d3b5a 51
Michael J. Spencer 2:1df0b61d3b5a 52 // this allows placement new to free memory if the constructor fails
Michael J. Spencer 2:1df0b61d3b5a 53 inline void operator delete(void* p, MemoryPool& pool)
Michael J. Spencer 2:1df0b61d3b5a 54 {
Michael J. Spencer 2:1df0b61d3b5a 55 pool.dealloc(p);
Michael J. Spencer 2:1df0b61d3b5a 56 }
Michael J. Spencer 2:1df0b61d3b5a 57
Michael J. Spencer 2:1df0b61d3b5a 58 // this catches all usages of delete blah. The object's destructor is called before we get here
Michael J. Spencer 2:1df0b61d3b5a 59 // it first checks if the deleted object is part of a pool, and uses free otherwise.
Michael J. Spencer 2:1df0b61d3b5a 60 inline void operator delete(void* p)
Michael J. Spencer 2:1df0b61d3b5a 61 {
Michael J. Spencer 2:1df0b61d3b5a 62 MemoryPool* m = MemoryPool::first;
Michael J. Spencer 2:1df0b61d3b5a 63 while (m)
Michael J. Spencer 2:1df0b61d3b5a 64 {
Michael J. Spencer 2:1df0b61d3b5a 65 if (m->has(p))
Michael J. Spencer 2:1df0b61d3b5a 66 {
Michael J. Spencer 2:1df0b61d3b5a 67 MDEBUG("Pool %p has %p, using dealloc()\n", m, p);
Michael J. Spencer 2:1df0b61d3b5a 68 m->dealloc(p);
Michael J. Spencer 2:1df0b61d3b5a 69 return;
Michael J. Spencer 2:1df0b61d3b5a 70 }
Michael J. Spencer 2:1df0b61d3b5a 71 m = m->next;
Michael J. Spencer 2:1df0b61d3b5a 72 }
Michael J. Spencer 2:1df0b61d3b5a 73
Michael J. Spencer 2:1df0b61d3b5a 74 MDEBUG("no pool has %p, using free()\n", p);
Michael J. Spencer 2:1df0b61d3b5a 75 free(p);
Michael J. Spencer 2:1df0b61d3b5a 76 }
Michael J. Spencer 2:1df0b61d3b5a 77
Michael J. Spencer 2:1df0b61d3b5a 78 #endif /* _MEMORYPOOL_H */