program to test a possible memory leak when using NTP
Dependencies: NetServices mbed
AvailableMemory.cpp@2:006625ab89d2, 2011-03-06 (annotated)
- Committer:
- hlipka
- Date:
- Sun Mar 06 21:28:42 2011 +0000
- Revision:
- 2:006625ab89d2
- Parent:
- 0:3986c9f76b09
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hlipka | 0:3986c9f76b09 | 1 | #include "AvailableMemory.h" |
hlipka | 0:3986c9f76b09 | 2 | #include <stdlib.h> |
hlipka | 0:3986c9f76b09 | 3 | #include <stdio.h> |
hlipka | 0:3986c9f76b09 | 4 | |
hlipka | 0:3986c9f76b09 | 5 | namespace segundo { |
hlipka | 0:3986c9f76b09 | 6 | namespace Utilities { |
hlipka | 0:3986c9f76b09 | 7 | |
hlipka | 0:3986c9f76b09 | 8 | int AvailableMemory(int resolution, int maximum, bool disableInterrupts) { |
hlipka | 0:3986c9f76b09 | 9 | |
hlipka | 0:3986c9f76b09 | 10 | if (resolution < 1) resolution = 1; |
hlipka | 0:3986c9f76b09 | 11 | if (maximum < 0) maximum = 0; |
hlipka | 0:3986c9f76b09 | 12 | |
hlipka | 0:3986c9f76b09 | 13 | int low = 0; |
hlipka | 0:3986c9f76b09 | 14 | int high = maximum + 1; |
hlipka | 0:3986c9f76b09 | 15 | |
hlipka | 0:3986c9f76b09 | 16 | if (disableInterrupts) __disable_irq(); |
hlipka | 0:3986c9f76b09 | 17 | |
hlipka | 0:3986c9f76b09 | 18 | while (high - low > resolution) { |
hlipka | 0:3986c9f76b09 | 19 | int mid = (low + high) / 2; |
hlipka | 0:3986c9f76b09 | 20 | printf("try malloc %i bytes\n",mid); |
hlipka | 0:3986c9f76b09 | 21 | void* p = malloc(mid); |
hlipka | 0:3986c9f76b09 | 22 | if (p == NULL) { |
hlipka | 0:3986c9f76b09 | 23 | high = mid; |
hlipka | 0:3986c9f76b09 | 24 | } else { |
hlipka | 0:3986c9f76b09 | 25 | free(p); |
hlipka | 0:3986c9f76b09 | 26 | low = mid; |
hlipka | 0:3986c9f76b09 | 27 | } |
hlipka | 0:3986c9f76b09 | 28 | } |
hlipka | 0:3986c9f76b09 | 29 | |
hlipka | 0:3986c9f76b09 | 30 | if (disableInterrupts) __enable_irq(); |
hlipka | 0:3986c9f76b09 | 31 | |
hlipka | 0:3986c9f76b09 | 32 | return low; |
hlipka | 0:3986c9f76b09 | 33 | } |
hlipka | 0:3986c9f76b09 | 34 | |
hlipka | 0:3986c9f76b09 | 35 | } // namespace Utilities |
hlipka | 0:3986c9f76b09 | 36 | } // namespace segundo |