Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix realloc method for ChibiOS #13820

Merged
merged 8 commits into from
Mar 23, 2020
7 changes: 6 additions & 1 deletion Tools/ardupilotwaf/boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def configure(self, cfg):

env.DEFINES.update(
ENABLE_SCRIPTING = 1,
ENABLE_HEAP = 1,
LUA_32BITS = 1,
)

Expand Down Expand Up @@ -158,6 +157,10 @@ def configure_env(self, cfg, env):
if cfg.options.bootloader:
# don't let bootloaders try and pull scripting in
cfg.options.disable_scripting = True
else:
env.DEFINES.update(
ENABLE_HEAP = 1,
)

if cfg.options.enable_math_check_indexes:
env.CXXFLAGS += ['-DMATH_CHECK_INDEXES']
Expand Down Expand Up @@ -439,6 +442,7 @@ def configure_env(self, cfg, env):
env.DEFINES.update(
CONFIG_HAL_BOARD = 'HAL_BOARD_CHIBIOS',
HAVE_STD_NULLPTR_T = 0,
USE_LIBC_REALLOC = 0,
)

env.AP_LIBRARIES += [
Expand Down Expand Up @@ -491,6 +495,7 @@ def configure_env(self, cfg, env):
'-specs=nosys.specs',
'-DCHIBIOS_BOARD_NAME="%s"' % self.name,
'-D__USE_CMSIS',
'-Werror=deprecated-declarations'
]
env.CXXFLAGS += env.CFLAGS + [
'-fno-rtti',
Expand Down
2 changes: 1 addition & 1 deletion libraries/AP_Common/AP_ExpandingArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool AP_ExpandingArrayGeneric::expand(uint16_t num_chunks)
// fail if reallocating would leave less than 100 bytes of memory free
return false;
}
chunk_ptr_t *chunk_ptrs_new = (chunk_ptr_t*)realloc(chunk_ptrs, chunk_ptr_size * sizeof(chunk_ptr_t));
chunk_ptr_t *chunk_ptrs_new = (chunk_ptr_t*)hal.util->std_realloc((void*)chunk_ptrs, chunk_ptr_size * sizeof(chunk_ptr_t));
if (chunk_ptrs_new == nullptr) {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions libraries/AP_HAL/AP_HAL_Boards.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,7 @@
#else
#define AP_UAVCAN_SLCAN_ENABLED 0
#endif

#ifndef USE_LIBC_REALLOC
#define USE_LIBC_REALLOC 1
#endif
6 changes: 6 additions & 0 deletions libraries/AP_HAL/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,14 @@ class AP_HAL::Util {
// heap functions, note that a heap once alloc'd cannot be dealloc'd
virtual void *allocate_heap_memory(size_t size) = 0;
virtual void *heap_realloc(void *heap, void *ptr, size_t new_size) = 0;
#if USE_LIBC_REALLOC
virtual void *std_realloc(void *ptr, size_t new_size) { return realloc(ptr, new_size); }
#else
virtual void *std_realloc(void *ptr, size_t new_size) = 0;
#endif // USE_LIBC_REALLOC
#endif // ENABLE_HEAP


/**
how much free memory do we have in bytes. If unknown return 4096
*/
Expand Down
20 changes: 20 additions & 0 deletions libraries/AP_HAL_ChibiOS/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ void *Util::allocate_heap_memory(size_t size)
return heap;
}

/*
realloc implementation thanks to wolfssl, used by AP_Scripting
*/
void *Util::std_realloc(void *addr, size_t size)
{
if (size == 0) {
free(addr);
return nullptr;
}
if (addr == nullptr) {
return malloc(size);
tridge marked this conversation as resolved.
Show resolved Hide resolved
}
void *new_mem = malloc(size);
if (new_mem != nullptr) {
memcpy(new_mem, addr, chHeapGetSize(addr) > size ? size : chHeapGetSize(addr));
free(addr);
}
return new_mem;
}

void *Util::heap_realloc(void *heap, void *ptr, size_t new_size)
{
if (heap == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions libraries/AP_HAL_ChibiOS/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ChibiOS::Util : public AP_HAL::Util {
// heap functions, note that a heap once alloc'd cannot be dealloc'd
virtual void *allocate_heap_memory(size_t size) override;
virtual void *heap_realloc(void *heap, void *ptr, size_t new_size) override;
virtual void *std_realloc(void *ptr, size_t new_size) override;
#endif // ENABLE_HEAP

/*
Expand Down
3 changes: 2 additions & 1 deletion libraries/AP_HAL_ChibiOS/hwdef/common/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ int printf(const char *fmt, ...);
void *malloc(size_t size);
void *calloc(size_t nmemb, size_t size);
void free(void *ptr);

void *realloc(void* ptr, size_t size) __attribute__((deprecated));
extern int (*vprintf_console_hook)(const char *fmt, va_list arg);


#define L_tmpnam 32

#ifdef __cplusplus
Expand Down
46 changes: 23 additions & 23 deletions libraries/AP_Scripting/lua/src/lauxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1005,29 +1005,29 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
}


static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud; (void)osize; /* not used */
if (nsize == 0) {
free(ptr);
return NULL;
}
else
return realloc(ptr, nsize);
}


static int panic (lua_State *L) {
lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
lua_tostring(L, -1));
return 0; /* return to Lua to abort */
}


LUALIB_API lua_State *luaL_newstate (void) {
lua_State *L = lua_newstate(l_alloc, NULL);
if (L) lua_atpanic(L, &panic);
return L;
}
// static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
// (void)ud; (void)osize; /* not used */
// if (nsize == 0) {
// free(ptr);
// return NULL;
// }
// else
// return realloc(ptr, nsize);
// }


// static int panic (lua_State *L) {
// lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
// lua_tostring(L, -1));
// return 0; /* return to Lua to abort */
// }


// LUALIB_API lua_State *luaL_newstate (void) {
// lua_State *L = lua_newstate(l_alloc, NULL);
// if (L) lua_atpanic(L, &panic);
// return L;
// }


LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
Expand Down