forked from http://developer.mbed.org/users/segundo/code/AvailableMemory/
Dependents: Utility_AvailableMemory_blinky
Fork of AvailableMemory by
AvailableMemory.cpp@16:ea1fe2828f6f, 2014-11-27 (annotated)
- Committer:
- grassel
- Date:
- Thu Nov 27 17:20:57 2014 +0000
- Revision:
- 16:ea1fe2828f6f
- Parent:
- 11:0f5d5918761a
initial check-in
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
grassel | 16:ea1fe2828f6f | 1 | #include "mbed.h" |
segundo | 0:a98bf0c96bf1 | 2 | #include "AvailableMemory.h" |
segundo | 0:a98bf0c96bf1 | 3 | |
segundo | 0:a98bf0c96bf1 | 4 | namespace segundo { |
segundo | 0:a98bf0c96bf1 | 5 | namespace Utilities { |
segundo | 0:a98bf0c96bf1 | 6 | |
segundo | 0:a98bf0c96bf1 | 7 | int AvailableMemory(int resolution, int maximum, bool disableInterrupts) { |
segundo | 0:a98bf0c96bf1 | 8 | |
segundo | 0:a98bf0c96bf1 | 9 | if (resolution < 1) resolution = 1; |
segundo | 0:a98bf0c96bf1 | 10 | if (maximum < 0) maximum = 0; |
segundo | 0:a98bf0c96bf1 | 11 | |
segundo | 0:a98bf0c96bf1 | 12 | int low = 0; |
segundo | 0:a98bf0c96bf1 | 13 | int high = maximum + 1; |
segundo | 0:a98bf0c96bf1 | 14 | |
segundo | 0:a98bf0c96bf1 | 15 | if (disableInterrupts) __disable_irq(); |
segundo | 0:a98bf0c96bf1 | 16 | |
segundo | 0:a98bf0c96bf1 | 17 | while (high - low > resolution) { |
segundo | 0:a98bf0c96bf1 | 18 | int mid = (low + high) / 2; |
segundo | 0:a98bf0c96bf1 | 19 | void* p = malloc(mid); |
segundo | 0:a98bf0c96bf1 | 20 | if (p == NULL) { |
segundo | 0:a98bf0c96bf1 | 21 | high = mid; |
segundo | 0:a98bf0c96bf1 | 22 | } else { |
segundo | 0:a98bf0c96bf1 | 23 | free(p); |
segundo | 0:a98bf0c96bf1 | 24 | low = mid; |
segundo | 0:a98bf0c96bf1 | 25 | } |
segundo | 0:a98bf0c96bf1 | 26 | } |
segundo | 0:a98bf0c96bf1 | 27 | |
segundo | 0:a98bf0c96bf1 | 28 | if (disableInterrupts) __enable_irq(); |
segundo | 0:a98bf0c96bf1 | 29 | |
segundo | 0:a98bf0c96bf1 | 30 | return low; |
segundo | 0:a98bf0c96bf1 | 31 | } |
segundo | 0:a98bf0c96bf1 | 32 | |
segundo | 0:a98bf0c96bf1 | 33 | } // namespace Utilities |
segundo | 0:a98bf0c96bf1 | 34 | } // namespace segundo |