![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "test_env.h" 00002 00003 static char *initial_stack_p; 00004 static char *initial_heap_p; 00005 00006 static char line[256]; 00007 static unsigned int iterations = 0; 00008 00009 void report_iterations(void) { 00010 unsigned int tot = (0x100 * iterations)*2; 00011 printf("\nAllocated (%d)Kb in (%u) iterations\n", tot/1024, iterations); 00012 #if !defined(TOOLCHAIN_GCC_CR) 00013 // EA: This causes a crash when compiling with GCC_CR??? 00014 printf("%.2f\n", ((float)(tot)/(float)(initial_stack_p - initial_heap_p))*100.); 00015 #endif 00016 #ifdef TOOLCHAIN_ARM 00017 #ifndef __MICROLIB 00018 __heapvalid((__heapprt) fprintf, stdout, 1); 00019 #endif 00020 #endif 00021 } 00022 00023 void stack_test(char *latest_heap_pointer) { 00024 char stack_line[256]; 00025 iterations++; 00026 00027 sprintf(stack_line, "\nstack pointer: %p", &stack_line[255]); 00028 puts(stack_line); 00029 00030 char *heap_pointer = (char*)malloc(0x100); 00031 00032 if (heap_pointer == NULL) { 00033 int diff = (&stack_line[255] - latest_heap_pointer); 00034 if (diff > 0x200) { 00035 sprintf(stack_line, "\n[WARNING] Malloc failed to allocate memory too soon. There are (0x%x) free bytes", diff); 00036 report_iterations(); 00037 puts(stack_line); 00038 } else { 00039 puts("\n[SUCCESS] Stack/Heap collision detected"); 00040 report_iterations(); 00041 } 00042 return; 00043 } else { 00044 heap_pointer += 0x100; 00045 sprintf(line, "heap pointer: %p", heap_pointer); 00046 puts(line); 00047 } 00048 00049 if ((&stack_line[255]) > heap_pointer) { 00050 stack_test(heap_pointer); 00051 } else { 00052 puts("\n[WARNING] The Stack/Heap collision was not detected"); 00053 report_iterations(); 00054 } 00055 } 00056 00057 00058 int main (void) { 00059 char c; 00060 initial_stack_p = &c; 00061 00062 initial_heap_p = (char*)malloc(1); 00063 if (initial_heap_p == NULL) { 00064 printf("Unable to malloc a single byte\n"); 00065 notify_completion(false); 00066 } 00067 00068 printf("Initial stack/heap geometry:\n"); 00069 printf(" stack pointer:V %p\n", initial_stack_p); 00070 printf(" heap pointer :^ %p\n", initial_heap_p); 00071 00072 initial_heap_p++; 00073 stack_test(initial_heap_p); 00074 00075 notify_completion(true); 00076 }
Generated on Sun Jul 17 2022 08:25:26 by
![doxygen](doxygen.png)