Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Utility_AvailableMemory_blinky
Fork of AvailableMemory by
AvailableMemory.cpp@0:a98bf0c96bf1, 2010-11-07 (annotated)
- Committer:
- segundo
- Date:
- Sun Nov 07 19:11:52 2010 +0000
- Revision:
- 0:a98bf0c96bf1
- Child:
- 11:0f5d5918761a
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| segundo | 0:a98bf0c96bf1 | 1 | #include "AvailableMemory.h" |
| segundo | 0:a98bf0c96bf1 | 2 | #include <stdlib.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() { |
| segundo | 0:a98bf0c96bf1 | 8 | |
| segundo | 0:a98bf0c96bf1 | 9 | return AvailableMemory(256, 0x8000, true); |
| segundo | 0:a98bf0c96bf1 | 10 | } |
| segundo | 0:a98bf0c96bf1 | 11 | |
| segundo | 0:a98bf0c96bf1 | 12 | int AvailableMemory(int resolution, int maximum, bool disableInterrupts) { |
| segundo | 0:a98bf0c96bf1 | 13 | |
| segundo | 0:a98bf0c96bf1 | 14 | if (resolution < 1) resolution = 1; |
| segundo | 0:a98bf0c96bf1 | 15 | if (maximum < 0) maximum = 0; |
| segundo | 0:a98bf0c96bf1 | 16 | |
| segundo | 0:a98bf0c96bf1 | 17 | int low = 0; |
| segundo | 0:a98bf0c96bf1 | 18 | int high = maximum + 1; |
| segundo | 0:a98bf0c96bf1 | 19 | |
| segundo | 0:a98bf0c96bf1 | 20 | if (disableInterrupts) __disable_irq(); |
| segundo | 0:a98bf0c96bf1 | 21 | |
| segundo | 0:a98bf0c96bf1 | 22 | while (high - low > resolution) { |
| segundo | 0:a98bf0c96bf1 | 23 | int mid = (low + high) / 2; |
| segundo | 0:a98bf0c96bf1 | 24 | void* p = malloc(mid); |
| segundo | 0:a98bf0c96bf1 | 25 | if (p == NULL) { |
| segundo | 0:a98bf0c96bf1 | 26 | high = mid; |
| segundo | 0:a98bf0c96bf1 | 27 | } else { |
| segundo | 0:a98bf0c96bf1 | 28 | free(p); |
| segundo | 0:a98bf0c96bf1 | 29 | low = mid; |
| segundo | 0:a98bf0c96bf1 | 30 | } |
| segundo | 0:a98bf0c96bf1 | 31 | } |
| segundo | 0:a98bf0c96bf1 | 32 | |
| segundo | 0:a98bf0c96bf1 | 33 | if (disableInterrupts) __enable_irq(); |
| segundo | 0:a98bf0c96bf1 | 34 | |
| segundo | 0:a98bf0c96bf1 | 35 | return low; |
| segundo | 0:a98bf0c96bf1 | 36 | } |
| segundo | 0:a98bf0c96bf1 | 37 | |
| segundo | 0:a98bf0c96bf1 | 38 | } // namespace Utilities |
| segundo | 0:a98bf0c96bf1 | 39 | } // namespace segundo |
