|
| 1 | +#include <ti/screen.h> |
| 2 | +#include <ti/getcsc.h> |
| 3 | +#include <ti/sprintf.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | + |
| 7 | +extern int malloc_calls; |
| 8 | + |
| 9 | +void atexit_func(void) { |
| 10 | + /* |
| 11 | + * The custom free function is supposed to print a string if it is called, |
| 12 | + * however, it is unspecified if atexit_func or free will be called first. |
| 13 | + * This means that we cannot print anything in this function since we |
| 14 | + * cannot guarantee the order. However, this function must be non-empty so |
| 15 | + * Clang does not optimize away the call to atexit. Now some of this could |
| 16 | + * be fixed with -ffreestanding, but that may also hide the assumptions |
| 17 | + * that Clang makes. |
| 18 | + */ |
| 19 | + if (*(const char*)-1 != 0) { |
| 20 | + puts("0xFFFFFF should be zero"); |
| 21 | + while (!os_GetCSC()); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +int main(void) { |
| 26 | + char buf[50]; |
| 27 | + os_ClrHome(); |
| 28 | + |
| 29 | + // CRT0 may use malloc/free so we cannot assume this is zero |
| 30 | + int malloc_calls_before = malloc_calls; |
| 31 | + |
| 32 | + // calloc should call the custom malloc |
| 33 | + int *ptr = calloc(1, sizeof(int[3])); |
| 34 | + |
| 35 | + if (ptr == NULL) { |
| 36 | + puts("calloc failed"); |
| 37 | + while (!os_GetCSC()); |
| 38 | + return 0; |
| 39 | + } |
| 40 | + |
| 41 | + for (int n = 0; n < 3; ++n) { |
| 42 | + boot_sprintf(buf, "ptr(%d) == %d", n, ptr[n]); |
| 43 | + puts(buf); |
| 44 | + } |
| 45 | + |
| 46 | + while (!os_GetCSC()); |
| 47 | + |
| 48 | + if (atexit(atexit_func) != 0) { |
| 49 | + puts("atexit(atexit_func) failed"); |
| 50 | + while (!os_GetCSC()); |
| 51 | + return 0; |
| 52 | + } |
| 53 | + |
| 54 | + // we expect malloc to be called twice (for calloc and atexit) |
| 55 | + int malloc_call_count = malloc_calls - malloc_calls_before; |
| 56 | + boot_sprintf(buf, "malloc calls: %d", malloc_call_count); |
| 57 | + puts(buf); |
| 58 | + |
| 59 | + while (!os_GetCSC()); |
| 60 | + |
| 61 | + exit(EXIT_SUCCESS); |
| 62 | +} |
0 commit comments