logi loader fork from jpiat
Fork of mbed-src by
common/CallChain.cpp@15:4892fe388435, 2013-08-07 (annotated)
- Committer:
- bogdanm
- Date:
- Wed Aug 07 16:43:59 2013 +0300
- Revision:
- 15:4892fe388435
- Child:
- 36:ab3ee77451e7
Added LPC4088 target and interrupt chaining code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 15:4892fe388435 | 1 | #include "CallChain.h" |
bogdanm | 15:4892fe388435 | 2 | #include "cmsis.h" |
bogdanm | 15:4892fe388435 | 3 | |
bogdanm | 15:4892fe388435 | 4 | namespace mbed { |
bogdanm | 15:4892fe388435 | 5 | |
bogdanm | 15:4892fe388435 | 6 | CallChain::CallChain(int size) : _size(size), _elements(0) { |
bogdanm | 15:4892fe388435 | 7 | _chain = new pFunctionPointer_t[size](); |
bogdanm | 15:4892fe388435 | 8 | } |
bogdanm | 15:4892fe388435 | 9 | |
bogdanm | 15:4892fe388435 | 10 | CallChain::~CallChain() { |
bogdanm | 15:4892fe388435 | 11 | clear(); |
bogdanm | 15:4892fe388435 | 12 | delete _chain; |
bogdanm | 15:4892fe388435 | 13 | } |
bogdanm | 15:4892fe388435 | 14 | |
bogdanm | 15:4892fe388435 | 15 | pFunctionPointer_t CallChain::add(void (*function)(void)) { |
bogdanm | 15:4892fe388435 | 16 | return common_add(new FunctionPointer(function)); |
bogdanm | 15:4892fe388435 | 17 | } |
bogdanm | 15:4892fe388435 | 18 | |
bogdanm | 15:4892fe388435 | 19 | pFunctionPointer_t CallChain::add_front(void (*function)(void)) { |
bogdanm | 15:4892fe388435 | 20 | return common_add_front(new FunctionPointer(function)); |
bogdanm | 15:4892fe388435 | 21 | } |
bogdanm | 15:4892fe388435 | 22 | |
bogdanm | 15:4892fe388435 | 23 | int CallChain::size() const { |
bogdanm | 15:4892fe388435 | 24 | return _elements; |
bogdanm | 15:4892fe388435 | 25 | } |
bogdanm | 15:4892fe388435 | 26 | |
bogdanm | 15:4892fe388435 | 27 | pFunctionPointer_t CallChain::get(int i) const { |
bogdanm | 15:4892fe388435 | 28 | if (i < 0 || i >= _elements) |
bogdanm | 15:4892fe388435 | 29 | return NULL; |
bogdanm | 15:4892fe388435 | 30 | return _chain[i]; |
bogdanm | 15:4892fe388435 | 31 | } |
bogdanm | 15:4892fe388435 | 32 | |
bogdanm | 15:4892fe388435 | 33 | int CallChain::find(pFunctionPointer_t f) const { |
bogdanm | 15:4892fe388435 | 34 | for (int i = 0; i < _elements; i++) |
bogdanm | 15:4892fe388435 | 35 | if (f == _chain[i]) |
bogdanm | 15:4892fe388435 | 36 | return i; |
bogdanm | 15:4892fe388435 | 37 | return -1; |
bogdanm | 15:4892fe388435 | 38 | } |
bogdanm | 15:4892fe388435 | 39 | |
bogdanm | 15:4892fe388435 | 40 | void CallChain::clear() { |
bogdanm | 15:4892fe388435 | 41 | __disable_irq(); |
bogdanm | 15:4892fe388435 | 42 | for(int i = 0; i < _elements; i ++) { |
bogdanm | 15:4892fe388435 | 43 | delete _chain[i]; |
bogdanm | 15:4892fe388435 | 44 | _chain[i] = NULL; |
bogdanm | 15:4892fe388435 | 45 | } |
bogdanm | 15:4892fe388435 | 46 | _elements = 0; |
bogdanm | 15:4892fe388435 | 47 | __enable_irq(); |
bogdanm | 15:4892fe388435 | 48 | } |
bogdanm | 15:4892fe388435 | 49 | |
bogdanm | 15:4892fe388435 | 50 | bool CallChain::remove(pFunctionPointer_t f) { |
bogdanm | 15:4892fe388435 | 51 | int i; |
bogdanm | 15:4892fe388435 | 52 | |
bogdanm | 15:4892fe388435 | 53 | if ((i = find(f)) == -1) |
bogdanm | 15:4892fe388435 | 54 | return false; |
bogdanm | 15:4892fe388435 | 55 | __disable_irq(); |
bogdanm | 15:4892fe388435 | 56 | if (i != _elements - 1) |
bogdanm | 15:4892fe388435 | 57 | memmove(_chain + i, _chain + i + 1, (_elements - i - 1) * sizeof(pFunctionPointer_t)); |
bogdanm | 15:4892fe388435 | 58 | delete f; |
bogdanm | 15:4892fe388435 | 59 | _elements --; |
bogdanm | 15:4892fe388435 | 60 | __enable_irq(); |
bogdanm | 15:4892fe388435 | 61 | return true; |
bogdanm | 15:4892fe388435 | 62 | } |
bogdanm | 15:4892fe388435 | 63 | |
bogdanm | 15:4892fe388435 | 64 | void CallChain::call() { |
bogdanm | 15:4892fe388435 | 65 | for(int i = 0; i < _elements; i++) |
bogdanm | 15:4892fe388435 | 66 | _chain[i]->call(); |
bogdanm | 15:4892fe388435 | 67 | } |
bogdanm | 15:4892fe388435 | 68 | |
bogdanm | 15:4892fe388435 | 69 | void CallChain::_check_size() { |
bogdanm | 15:4892fe388435 | 70 | if (_elements < _size) |
bogdanm | 15:4892fe388435 | 71 | return; |
bogdanm | 15:4892fe388435 | 72 | __disable_irq(); |
bogdanm | 15:4892fe388435 | 73 | _size = (_size < 4) ? 4 : _size + 4; |
bogdanm | 15:4892fe388435 | 74 | pFunctionPointer_t* new_chain = new pFunctionPointer_t[_size](); |
bogdanm | 15:4892fe388435 | 75 | memcpy(new_chain, _chain, _elements * sizeof(pFunctionPointer_t)); |
bogdanm | 15:4892fe388435 | 76 | delete _chain; |
bogdanm | 15:4892fe388435 | 77 | _chain = new_chain; |
bogdanm | 15:4892fe388435 | 78 | __enable_irq(); |
bogdanm | 15:4892fe388435 | 79 | } |
bogdanm | 15:4892fe388435 | 80 | |
bogdanm | 15:4892fe388435 | 81 | pFunctionPointer_t CallChain::common_add(pFunctionPointer_t pf) { |
bogdanm | 15:4892fe388435 | 82 | _check_size(); |
bogdanm | 15:4892fe388435 | 83 | _chain[_elements] = pf; |
bogdanm | 15:4892fe388435 | 84 | _elements ++; |
bogdanm | 15:4892fe388435 | 85 | return pf; |
bogdanm | 15:4892fe388435 | 86 | } |
bogdanm | 15:4892fe388435 | 87 | |
bogdanm | 15:4892fe388435 | 88 | pFunctionPointer_t CallChain::common_add_front(pFunctionPointer_t pf) { |
bogdanm | 15:4892fe388435 | 89 | _check_size(); |
bogdanm | 15:4892fe388435 | 90 | __disable_irq(); |
bogdanm | 15:4892fe388435 | 91 | memmove(_chain + 1, _chain, _elements * sizeof(pFunctionPointer_t)); |
bogdanm | 15:4892fe388435 | 92 | _chain[0] = pf; |
bogdanm | 15:4892fe388435 | 93 | _elements ++; |
bogdanm | 15:4892fe388435 | 94 | __enable_irq(); |
bogdanm | 15:4892fe388435 | 95 | return pf; |
bogdanm | 15:4892fe388435 | 96 | } |
bogdanm | 15:4892fe388435 | 97 | |
bogdanm | 15:4892fe388435 | 98 | } // namespace mbed |