cdev: Fix deallocating devname in nuttx protected/kernel builds

The CDevs always live in kernel side. In kernel the devname
is allocated in kernel heap. Replace "free" with kmm_free on nuttx to
correct the deallocation; this automatically maps to "free" on flat build.

Define __KERNEL__ flag so that the proper nuttx headers are used for building.

TODO: It is fishy in the first place that the devname is allocated in inherited
classes but deleted in the CDev base class. Especially when the devname is
often passed as a constant string (which cannot even be freed) by many drivers.

The allocation and deallocation of devname should be done within the CDev base class
instead.

Signed-off-by: Jukka Laitinen <jukkax@ssrc.tii.ae>
This commit is contained in:
Jukka Laitinen
2021-02-09 11:02:52 +02:00
parent c2d9797cdb
commit 885fe62fec
2 changed files with 9 additions and 1 deletions
+8
View File
@@ -43,6 +43,10 @@
#include <px4_platform_common/posix.h>
#if defined(__PX4_NUTTX)
#include <nuttx/mm/mm.h>
#endif
namespace cdev
{
@@ -386,7 +390,11 @@ int CDev::unregister_driver_and_memory()
}
if (_devname != nullptr) {
#if defined(__PX4_NUTTX) && !defined(CONFIG_BUILD_FLAT)
kmm_free((void *)_devname);
#else
free((void *)_devname);
#endif
_devname = nullptr;
} else {
+1 -1
View File
@@ -49,7 +49,7 @@ px4_add_library(cdev
CDev.hpp
${SRCS_PLATFORM}
)
target_compile_options(cdev PRIVATE ${MAX_CUSTOM_OPT_LEVEL})
target_compile_options(cdev PRIVATE ${MAX_CUSTOM_OPT_LEVEL} -D__KERNEL__)
if(PX4_TESTING)
add_subdirectory(test)