Initial Release
This commit is contained in:
14
src/rp2_common/pico_malloc/CMakeLists.txt
Normal file
14
src/rp2_common/pico_malloc/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
||||
if (NOT TARGET pico_malloc)
|
||||
#shims for ROM functions for -lgcc functions (listed below)
|
||||
add_library(pico_malloc INTERFACE)
|
||||
|
||||
target_sources(pico_malloc INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}/pico_malloc.c
|
||||
)
|
||||
|
||||
pico_wrap_function(pico_malloc malloc)
|
||||
pico_wrap_function(pico_malloc calloc)
|
||||
pico_wrap_function(pico_malloc free)
|
||||
|
||||
target_link_libraries(pico_malloc INTERFACE pico_sync)
|
||||
endif()
|
38
src/rp2_common/pico_malloc/include/pico/malloc.h
Normal file
38
src/rp2_common/pico_malloc/include/pico/malloc.h
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef _PICO_MALLOC_H
|
||||
#define _PICO_MALLOC_H
|
||||
|
||||
/** \file malloc.h
|
||||
* \defgroup pico_malloc pico_malloc
|
||||
*
|
||||
* Multi-core safety for malloc, calloc and free
|
||||
*
|
||||
* This library does not provide any additional functions
|
||||
*/
|
||||
|
||||
// PICO_CONFIG: PICO_USE_MALLOC_MUTEX, Whether to protect malloc etc with a mutex, type=bool, default=1 with pico_multicore, 0 otherwise, group=pico_malloc
|
||||
#if PICO_MULTICORE && !defined(PICO_USE_MALLOC_MUTEX)
|
||||
#define PICO_USE_MALLOC_MUTEX 1
|
||||
#endif
|
||||
|
||||
// PICO_CONFIG: PICO_MALLOC_PANIC, Enable/disable panic when an allocation failure occurs, type=bool, default=1, group=pico_malloc
|
||||
#ifndef PICO_MALLOC_PANIC
|
||||
#define PICO_MALLOC_PANIC 1
|
||||
#endif
|
||||
|
||||
// PICO_CONFIG: PICO_DEBUG_MALLOC, Enable/disable debug printf from malloc, type=bool, default=0, group=pico_malloc
|
||||
#ifndef PICO_DEBUG_MALLOC
|
||||
#define PICO_DEBUG_MALLOC 0
|
||||
#endif
|
||||
|
||||
// PICO_CONFIG: PICO_DEBUG_MALLOC_LOW_WATER, Define the lower bound for allocation addresses to be printed by PICO_DEBUG_MALLOC, min=0, default=0, group=pico_malloc
|
||||
#ifndef PICO_DEBUG_MALLOC_LOW_WATER
|
||||
#define PICO_DEBUG_MALLOC_LOW_WATER 0
|
||||
#endif
|
||||
|
||||
#endif
|
72
src/rp2_common/pico_malloc/pico_malloc.c
Normal file
72
src/rp2_common/pico_malloc/pico_malloc.c
Normal file
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "pico.h"
|
||||
|
||||
#if PICO_USE_MALLOC_MUTEX
|
||||
#include "pico/mutex.h"
|
||||
auto_init_mutex(malloc_mutex);
|
||||
#endif
|
||||
|
||||
extern void *__real_malloc(size_t size);
|
||||
extern void *__real_calloc(size_t count, size_t size);
|
||||
extern void __real_free(void *mem);
|
||||
|
||||
extern char __StackLimit; /* Set by linker. */
|
||||
|
||||
static inline void check_alloc(void *mem, uint8_t size) {
|
||||
#if PICO_MALLOC_PANIC
|
||||
if (!mem || (((char *)mem) + size) > &__StackLimit) {
|
||||
panic("Out of memory");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void *__wrap_malloc(size_t size) {
|
||||
#if PICO_USE_MALLOC_MUTEX
|
||||
mutex_enter_blocking(&malloc_mutex);
|
||||
#endif
|
||||
void *rc = __real_malloc(size);
|
||||
#if PICO_USE_MALLOC_MUTEX
|
||||
mutex_exit(&malloc_mutex);
|
||||
#endif
|
||||
#ifdef PICO_DEBUG_MALLOC
|
||||
if (!rc || ((uint8_t *)rc) + size > (uint8_t*)PICO_DEBUG_MALLOC_LOW_WATER) {
|
||||
printf("malloc %d %p->%p\n", (uint) size, rc, ((uint8_t *) rc) + size);
|
||||
}
|
||||
#endif
|
||||
check_alloc(rc, size);
|
||||
return rc;
|
||||
}
|
||||
|
||||
void *__wrap_calloc(size_t count, size_t size) {
|
||||
#if PICO_USE_MALLOC_MUTEX
|
||||
mutex_enter_blocking(&malloc_mutex);
|
||||
#endif
|
||||
void *rc = __real_calloc(count, size);
|
||||
#if PICO_USE_MALLOC_MUTEX
|
||||
mutex_exit(&malloc_mutex);
|
||||
#endif
|
||||
#ifdef PICO_DEBUG_MALLOC
|
||||
if (!rc || ((uint8_t *)rc) + size > (uint8_t*)PICO_DEBUG_MALLOC_LOW_WATER) {
|
||||
printf("calloc %d %p->%p\n", (uint) (count * size), rc, ((uint8_t *) rc) + size);
|
||||
}
|
||||
#endif
|
||||
check_alloc(rc, size);
|
||||
return rc;
|
||||
}
|
||||
|
||||
void __wrap_free(void *mem) {
|
||||
#if PICO_USE_MALLOC_MUTEX
|
||||
mutex_enter_blocking(&malloc_mutex);
|
||||
#endif
|
||||
__real_free(mem);
|
||||
#if PICO_USE_MALLOC_MUTEX
|
||||
mutex_exit(&malloc_mutex);
|
||||
#endif
|
||||
}
|
Reference in New Issue
Block a user