mbed_controller / Mbed 2 deprecated mbed_controller_demo

Dependencies:   CMDB EthernetInterface HTTPD dconfig mbed-rpc mbed-rtos mbed storage_on_flash

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers util.cpp Source File

util.cpp

Go to the documentation of this file.
00001 /**
00002  * @file util.cpp
00003  *
00004  * @brief system wide utility function
00005  *
00006  */
00007 
00008 #include <stdio.h>
00009 #include <stdarg.h>
00010 #include <stdlib.h>
00011 #include <stdint.h>
00012 #include <string.h>
00013 
00014 #if defined(__CC_ARM) && !defined(__MICROLIB)
00015 // Keil MDK with microlib supports __heapstats();
00016 #define SUPPORT_HEAPSTATS
00017 #endif
00018 
00019 #if defined(SUPPORT_HEAPSTATS)
00020 
00021 static void heap_printf(void *dummy, const char *fmt, ...)
00022 {
00023     va_list arg_ptr;
00024 
00025     if (strchr(fmt, '\n') != NULL) {
00026         putchar('\r');
00027     }
00028     va_start(arg_ptr, fmt);
00029     vprintf(fmt, arg_ptr);
00030     va_end(arg_ptr);
00031 }
00032 
00033 void print_memstat(void)
00034 {
00035     __heapstats((__heapprt)heap_printf, NULL);
00036     printf("\r\n");
00037 }
00038 
00039 #else
00040 
00041 /**
00042  * Compute max consecutive memory chunk, by trying to allocate it.
00043  */
00044 static uint32_t comput_free_heap(uint32_t resolution, uint32_t maximum)
00045 {
00046     int low = 0;
00047     int high = maximum + 1;
00048 
00049     while (high - low > resolution) {
00050         int mid = (low + high) / 2;
00051         void* p = malloc(mid);
00052         if (p == NULL) {
00053             high = mid;
00054         } else {
00055             free(p);
00056             low = mid;
00057         }
00058     }
00059 
00060     return low;
00061 }
00062 
00063 void print_memstat(void)
00064 {
00065     printf("heap free %u\r\n", comput_free_heap(512, 192*1024));
00066 }
00067 
00068 #endif
00069 
00070