Skip to content

Commit 7b586d4

Browse files
committed
make seperate example for the allocator that uses psram and sram
1 parent 9c5d4df commit 7b586d4

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

examples/all_allocator/CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
# use our own allocator
4+
5+
# the following enables the system malloc/free to be wrapped
6+
set(SKIP_PICO_MALLOC 1)
7+
8+
# the following enables wrapping in sparkfun_pico builds
9+
set(SFE_PICO_ALLOC_WRAP 1)
10+
11+
# the following enables the system malloc/free to be wrapped during compilation
12+
add_definitions(-DSFE_PICO_ALLOC_WRAP)
13+
14+
include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)
15+
16+
project(test_allocator C CXX ASM)
17+
set(CMAKE_C_STANDARD 11)
18+
set(CMAKE_CXX_STANDARD 17)
19+
pico_sdk_init()
20+
21+
add_executable(test_allocator test_allocator.c)
22+
23+
add_subdirectory(sparkfun_pico)
24+
25+
pico_enable_stdio_usb(test_allocator 1)
26+
pico_enable_stdio_uart(test_allocator 1)
27+
28+
# pull in common dependencies and additional spi hardware support
29+
target_link_libraries(test_allocator pico_stdlib sparkfun_pico)
30+
31+
# create map/bin/hex file etc.
32+
pico_add_extra_outputs(test_allocator)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)