Skip to content

Commit 0cc56b6

Browse files
committed
ports/rp2: HACK: Bring up modbtree with tracked heap.
1 parent f305469 commit 0cc56b6

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

extmod/extmod.cmake

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ if(MICROPY_PY_BTREE)
135135
${MICROPY_LIB_BERKELEY_DIR}/include
136136
)
137137

138+
target_link_options(micropy_extmod_btree PRIVATE
139+
-Wl,--wrap=malloc
140+
-Wl,--wrap=free
141+
-Wl,--wrap=realloc
142+
-Wl,--wrap=calloc
143+
)
144+
138145
if(NOT BERKELEY_DB_CONFIG_FILE)
139146
set(BERKELEY_DB_CONFIG_FILE "${MICROPY_DIR}/extmod/berkeley-db/berkeley_db_config_port.h")
140147
endif()
@@ -152,11 +159,28 @@ if(MICROPY_PY_BTREE)
152159

153160
list(APPEND MICROPY_DEF_CORE
154161
MICROPY_PY_BTREE=1
162+
MICROPY_STREAMS_POSIX_API # Required for mp_stream_posix_ functions if !MICROPY_ENABLE_DYNRUNTIME
155163
BERKELEY_DB_CONFIG_FILE="${BERKELEY_DB_CONFIG_FILE}"
156164
)
157165

158166
list(APPEND MICROPY_SOURCE_EXTMOD
159167
${MICROPY_EXTMOD_DIR}/modbtree.c
168+
${MICROPY_EXTMOD_DIR}/modbtree_malloc.c
169+
170+
${MICROPY_LIB_BERKELEY_DIR}/btree/bt_close.c
171+
${MICROPY_LIB_BERKELEY_DIR}/btree/bt_conv.c
172+
${MICROPY_LIB_BERKELEY_DIR}/btree/bt_debug.c
173+
${MICROPY_LIB_BERKELEY_DIR}/btree/bt_delete.c
174+
${MICROPY_LIB_BERKELEY_DIR}/btree/bt_get.c
175+
${MICROPY_LIB_BERKELEY_DIR}/btree/bt_open.c
176+
${MICROPY_LIB_BERKELEY_DIR}/btree/bt_overflow.c
177+
${MICROPY_LIB_BERKELEY_DIR}/btree/bt_page.c
178+
${MICROPY_LIB_BERKELEY_DIR}/btree/bt_put.c
179+
${MICROPY_LIB_BERKELEY_DIR}/btree/bt_search.c
180+
${MICROPY_LIB_BERKELEY_DIR}/btree/bt_seq.c
181+
${MICROPY_LIB_BERKELEY_DIR}/btree/bt_split.c
182+
${MICROPY_LIB_BERKELEY_DIR}/btree/bt_utils.c
183+
${MICROPY_LIB_BERKELEY_DIR}/mpool/mpool.c
160184
)
161185
endif()
162186

extmod/modbtree_malloc.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "py/runtime.h"
2+
#include <string.h>
3+
4+
void *__wrap_malloc(size_t size) {
5+
// Allocate an extra sizeof(size_t) bytes
6+
size_t *addr = (size_t *)m_tracked_calloc(sizeof(uint8_t), size + sizeof(size_t));
7+
// Tag our memory with its allocated size
8+
*addr = size;
9+
// Skip past the size_t size
10+
addr++;
11+
12+
mp_printf(&mp_plat_print, "malloc %lu %p : %p\n", size, addr, (uint8_t *)addr + size);
13+
14+
return (void *)addr;
15+
}
16+
17+
void __wrap_free(void *p) {
18+
size_t *pp = (size_t *)p; // Convert our void pointer to size_t* so we can read the size marker
19+
pp--; // Skip back to get our real start
20+
size_t size = *pp;
21+
m_tracked_free((void *)pp);
22+
23+
mp_printf(&mp_plat_print, "free %p (size %d)\n", p, size);
24+
}
25+
26+
void *__wrap_realloc(void *p, size_t size) {
27+
void *addr = __wrap_malloc(size);
28+
size_t old_size = *((size_t *)p - 1);
29+
memcpy(addr, p, old_size < size ? old_size : size);
30+
__wrap_free(p);
31+
32+
mp_printf(&mp_plat_print, "realloc %lu -> %lu, %p -> %p : %p\n", old_size, size, p, addr, (uint8_t *)addr + size);
33+
34+
return addr;
35+
}
36+
37+
void *__wrap_calloc(size_t nitems, size_t size) {
38+
return __wrap_malloc(size * nitems);
39+
}

ports/rp2/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ endif()
6969
# Enable extmod components that will be configured by extmod.cmake.
7070
# A board may also have enabled additional components.
7171
set(MICROPY_SSL_MBEDTLS ON)
72+
set(MICROPY_PY_BTREE ON)
7273

7374
# Use the local cyw43_driver instead of the one in pico-sdk
7475
if (MICROPY_PY_NETWORK_CYW43)
@@ -87,6 +88,8 @@ include(${PICO_SDK_PATH}/pico_sdk_init.cmake)
8788
# Define the top-level project
8889
project(${MICROPY_TARGET})
8990

91+
set(SKIP_PICO_MALLOC 1)
92+
9093
pico_sdk_init()
9194

9295
include(${MICROPY_DIR}/py/usermod.cmake)
@@ -508,6 +511,8 @@ target_link_libraries(${MICROPY_TARGET} micropy_lib_mbedtls)
508511

509512
target_link_libraries(${MICROPY_TARGET} usermod)
510513

514+
#target_link_libraries(${MICROPY_TARGET} micropy_extmod_btree)
515+
511516
target_include_directories(${MICROPY_TARGET} PRIVATE
512517
${MICROPY_INC_CORE}
513518
${MICROPY_INC_USERMOD}
@@ -526,6 +531,11 @@ target_link_options(${MICROPY_TARGET} PRIVATE
526531
-Wl,--defsym=__micropy_c_heap_size__=${MICROPY_C_HEAP_SIZE}
527532
-Wl,--wrap=dcd_event_handler
528533
-Wl,--wrap=runtime_init_clocks
534+
535+
-Wl,--wrap=malloc
536+
-Wl,--wrap=free
537+
-Wl,--wrap=realloc
538+
-Wl,--wrap=calloc
529539
)
530540

531541
if(PICO_RP2350)
@@ -558,6 +568,7 @@ set_source_files_properties(
558568
)
559569

560570
target_compile_definitions(${MICROPY_TARGET} PRIVATE
571+
${MICROPY_DEF_CORE} # Required for btree module definitions
561572
${MICROPY_DEF_BOARD}
562573
FFCONF_H=\"${MICROPY_OOFATFS_DIR}/ffconf.h\"
563574
LFS1_NO_MALLOC LFS1_NO_DEBUG LFS1_NO_WARN LFS1_NO_ERROR LFS1_NO_ASSERT

0 commit comments

Comments
 (0)