diff --git a/libraries/AP_HAL/utility/replace.cpp b/libraries/AP_HAL/utility/replace.cpp new file mode 100644 index 0000000000..df5a9e40ca --- /dev/null +++ b/libraries/AP_HAL/utility/replace.cpp @@ -0,0 +1,22 @@ +/* + replacement functions for systems that are missing required library functions + */ + +#include "replace.h" + +#ifndef HAVE_MEMRCHR +/* + replacement for memrchr(). Note that we make the buffer non-const to + avoid issues with converting const to non-const + */ +void *replace_memrchr(void *s, int c, size_t n) +{ + uint8_t *b = (uint8_t *)s; + for (int32_t i=n-1; i>=0; i--) { + if (b[i] == (uint8_t)c) { + return (void *)&b[i]; + } + } + return nullptr; +} +#endif diff --git a/libraries/AP_HAL/utility/replace.h b/libraries/AP_HAL/utility/replace.h new file mode 100644 index 0000000000..e1b6d3c50e --- /dev/null +++ b/libraries/AP_HAL/utility/replace.h @@ -0,0 +1,12 @@ +/* + replacement functions for systems that are missing required library functions + */ + +#pragma once + +#include + +#ifndef HAVE_MEMRCHR +void *replace_memrchr(void *s, int c, size_t n); +#define memrchr(s,c,n) replace_memrchr(s,c,n) +#endif