|
| 1 | +/** |
| 2 | +
|
| 3 | + */ |
| 4 | + |
| 5 | +#include "pico/stdlib.h" |
| 6 | +#include "sparkfun_pico/sfe_pico_alloc.h" |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +// main() |
| 12 | + |
| 13 | +static void memory_stats() |
| 14 | +{ |
| 15 | + size_t mem_size = sfe_mem_size(); |
| 16 | + size_t mem_used = sfe_mem_used(); |
| 17 | + printf("\tMemory pool - Total: 0x%X (%u) Used: 0x%X (%u) - %3.2f%%\n", mem_size, mem_size, mem_used, mem_used, |
| 18 | + (float)mem_used / (float)mem_size * 100.0); |
| 19 | + |
| 20 | + size_t max_block = sfe_mem_max_free_size(); |
| 21 | + printf("\tMax free block size: 0x%X (%u) \n", max_block, max_block); |
| 22 | +} |
| 23 | +int main() |
| 24 | +{ |
| 25 | + stdio_init_all(); |
| 26 | + |
| 27 | + // wait a little bit - for startup |
| 28 | + sleep_ms(2000); |
| 29 | + printf("\n-----------------------------------------------------------\n"); |
| 30 | + printf("SparkFun - Allocator test - starting\n"); |
| 31 | + sleep_ms(2000); |
| 32 | + |
| 33 | + sfe_pico_alloc_init(); |
| 34 | + printf("Startup\n"); |
| 35 | + |
| 36 | + memory_stats(); |
| 37 | + |
| 38 | + // Allocate a Meg |
| 39 | + uint8_t *big_block = (uint8_t *)sfe_mem_malloc(1024 * 1024); |
| 40 | + if (!big_block) |
| 41 | + { |
| 42 | + printf("Big block allocation failed\n"); |
| 43 | + return 1; |
| 44 | + } |
| 45 | + printf("\nAllocated a Meg using sfe_alloc\n"); |
| 46 | + memory_stats(); |
| 47 | + sfe_mem_free(big_block); |
| 48 | + |
| 49 | + printf("\nFreed a Meg using sfe_free\n"); |
| 50 | + memory_stats(); |
| 51 | + |
| 52 | +// Wrapping the built in malloc/free |
| 53 | +#if defined(SFE_PICO_ALLOC_WRAP) |
| 54 | + // Now with built ins -- did we override ? |
| 55 | + // Allocate a Meg |
| 56 | + big_block = (uint8_t *)malloc(1024 * 1024); |
| 57 | + if (!big_block) |
| 58 | + { |
| 59 | + printf("Big block built in allocation failed\n"); |
| 60 | + return 1; |
| 61 | + } |
| 62 | + printf("\nAllocated a Meg using built in alloc\n"); |
| 63 | + memory_stats(); |
| 64 | + free(big_block); |
| 65 | + |
| 66 | + printf("\nFreed a Meg using built in free\n"); |
| 67 | + memory_stats(); |
| 68 | +#endif |
| 69 | + printf("DONE\n"); |
| 70 | + printf("-----------------------------------------------------------\n"); |
| 71 | + while (1) |
| 72 | + { |
| 73 | + sleep_ms(1000); |
| 74 | + } |
| 75 | +} |
0 commit comments