-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_allocator.c
100 lines (84 loc) · 2.97 KB
/
test_allocator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* @file test_allocator.c
*
* @brief This file contains a example on how to use a memory allocator with PSRAM
*/
/*
The MIT License (MIT)
Copyright (c) 2024 SparkFun Electronics
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions: The
above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "pico/stdlib.h"
#include "sparkfun_pico/sfe_pico.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// main()
static void memory_stats()
{
size_t mem_size = sfe_mem_size();
size_t mem_used = sfe_mem_used();
printf("\tMemory pool - Total: 0x%X (%u) Used: 0x%X (%u) - %3.2f%%\n", mem_size, mem_size, mem_used, mem_used,
(float)mem_used / (float)mem_size * 100.0);
size_t max_block = sfe_mem_max_free_size();
printf("\tMax free block size: 0x%X (%u) \n", max_block, max_block);
}
int main()
{
stdio_init_all();
// wait a little bit - for startup
sleep_ms(2000);
printf("\n-----------------------------------------------------------\n");
printf("SparkFun - Allocator test - starting\n");
sleep_ms(2000);
sfe_pico_alloc_init();
printf("Startup\n");
memory_stats();
// Allocate a Meg
uint8_t *big_block = (uint8_t *)sfe_mem_malloc(1024 * 1024);
if (!big_block)
{
printf("Big block allocation failed\n");
return 1;
}
printf("\nAllocated a Meg using sfe_alloc\n");
memory_stats();
sfe_mem_free(big_block);
printf("\nFreed a Meg using sfe_free\n");
memory_stats();
// Wrapping the built in malloc/free
#if defined(SFE_PICO_ALLOC_WRAP)
// Now with built ins -- did we override ?
// Allocate a Meg
big_block = (uint8_t *)malloc(1024 * 1024);
if (!big_block)
{
printf("Big block built in allocation failed\n");
return 1;
}
printf("\nAllocated a Meg using built in alloc\n");
memory_stats();
free(big_block);
printf("\nFreed a Meg using built in free\n");
memory_stats();
#endif
printf("DONE\n");
printf("-----------------------------------------------------------\n");
while (1)
{
sleep_ms(1000);
}
}