
program to test a possible memory leak when using NTP
Dependencies: NetServices mbed
Revision 0:3986c9f76b09, committed 2010-12-22
- Comitter:
- hlipka
- Date:
- Wed Dec 22 10:10:39 2010 +0000
- Child:
- 1:07158ff709d6
- Commit message:
- initial version
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AvailableMemory.cpp Wed Dec 22 10:10:39 2010 +0000 @@ -0,0 +1,36 @@ +#include "AvailableMemory.h" +#include <stdlib.h> +#include <stdio.h> + +namespace segundo { +namespace Utilities { + +int AvailableMemory(int resolution, int maximum, bool disableInterrupts) { + + if (resolution < 1) resolution = 1; + if (maximum < 0) maximum = 0; + + int low = 0; + int high = maximum + 1; + + if (disableInterrupts) __disable_irq(); + + while (high - low > resolution) { + int mid = (low + high) / 2; + printf("try malloc %i bytes\n",mid); + void* p = malloc(mid); + if (p == NULL) { + high = mid; + } else { + free(p); + low = mid; + } + } + + if (disableInterrupts) __enable_irq(); + + return low; +} + +} // namespace Utilities +} // namespace segundo \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AvailableMemory.h Wed Dec 22 10:10:39 2010 +0000 @@ -0,0 +1,46 @@ +/** @file + * Return the memory available for a malloc call. + */ +#ifndef SEGUNDO_UTILITIES_AVAILABLEMEMORY_H +#define SEGUNDO_UTILITIES_AVAILABLEMEMORY_H + +/** + * Segundo Equipo + */ +namespace segundo { +/** + * A collection of utilities + */ +namespace Utilities { + +/** Return the memory available for a malloc call. + * This is done by a binary search approach + * calling malloc/free starting with a maximum. + * + * Example: + * @code + * #include <stdio.h> + * #include "AvailableMemory.h" + * + * int main() { + * + * printf("Available memory (bytes to nearest 256) : %d\n", AvailableMemory()); + * printf("Available memory (exact bytes) : %d\n", AvailableMemory(1)); + * + * } + * @endcode + * @param resolution Resolution in number of bytes, + * 1 will return the exact value, + * default will return the available memory to the nearest 256 bytes + * @param maximum Maximum amount of memory to check, default is 32K (0x8000) + * @param disableInterrupts Disable interrupts whilst checking, default is true + * @return Available memory in bytes accurate to within resolution + */ +int AvailableMemory(int resolution = 256, int maximum = 0x8000, bool disableInterrupts = true); + +} // namespace Utilities +} // namespace segundo + +using namespace segundo::Utilities; + +#endif // SEGUNDO_UTILITIES_AVAILABLEMEMORY_H \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NetServices.lib Wed Dec 22 10:10:39 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/segundo/code/NetServices/#4e2468d7d5cb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Dec 22 10:10:39 2010 +0000 @@ -0,0 +1,63 @@ +#include "mbed.h" + +#include "EthernetNetIf.h" +#include "NTPClient.h" + +#include "AvailableMemory.h" + +LocalFileSystem local("local"); + +void updateTime() +{ + time_t ctTime; + time(&ctTime); + printf("Current time is (UTC): %s\n", ctime(&ctTime)); + + NTPClient ntp; + Host server(IpAddr(), 123, "0.de.pool.ntp.org"); + ntp.setTime(server); + + printf("set time ok\n"); + time(&ctTime); + printf("Current time is (UTC): %s\n", ctime(&ctTime)); + +} + +int main() { + printf("calculate free mem 1\n"); + int i=AvailableMemory(1,0x8000,false); + printf("free mem=%i\n",i); + +// void* p1=malloc(8000); +// void* p2=malloc(8000); + + printf("calculate free mem 2\n"); + i=AvailableMemory(1,0x8000,false); + printf("free mem=%i\n",i); + + printf("setup\n"); + EthernetNetIf eth; + EthernetErr ethErr; + printf("Setting up...\n"); + do { + ethErr = eth.setup(); + if (ethErr) printf("waiting for network...\n", ethErr); + } while (ethErr != ETH_OK); + + printf("setup ok\n"); + + printf("calculate free mem 3\n"); + i=AvailableMemory(1,0x8000,false); + printf("free mem=%i\n",i); + printf("calculate free mem 3\n"); + i=AvailableMemory(1,0x8000,false); + printf("free mem=%i\n",i); + + updateTime(); + +// free(p1); +// free(p2); + printf("calculate free mem 4\n"); + i=AvailableMemory(1,0x100,false); + printf("free mem=%i\n",i); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Dec 22 10:10:39 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e