mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-29 03:36:07 +08:00
uORB: Remove transfer of memory allocation ownership to CDev
- Allocate and free the node name in uORBDeviceNode. - Add protected build support by de-allocating the name with kmm_free, when running in kernel side. strdup allocates from the kernel heap in NuttX kernel space. - Remove the CDev::unregister_driver_and_memory(), it is no longer used Signed-off-by: Jukka Laitinen <jukkax@ssrc.tii.ae>
This commit is contained in:
committed by
Daniel Agar
parent
25c10eb124
commit
ce6147f570
@@ -99,19 +99,11 @@ int uORB::DeviceMaster::advertise(const struct orb_metadata *meta, bool is_adver
|
|||||||
*instance = group_tries;
|
*instance = group_tries;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* driver wants a permanent copy of the path, so make one here */
|
|
||||||
const char *devpath = strdup(nodepath);
|
|
||||||
|
|
||||||
if (devpath == nullptr) {
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* construct the new node, passing the ownership of path to it */
|
/* construct the new node, passing the ownership of path to it */
|
||||||
uORB::DeviceNode *node = new uORB::DeviceNode(meta, group_tries, devpath);
|
uORB::DeviceNode *node = new uORB::DeviceNode(meta, group_tries, nodepath);
|
||||||
|
|
||||||
/* if we didn't get a device, that's bad, free the path too */
|
/* if we didn't get a device, that's bad */
|
||||||
if (node == nullptr) {
|
if (node == nullptr) {
|
||||||
free((void *)devpath);
|
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,10 @@
|
|||||||
#include "uORBCommunicator.hpp"
|
#include "uORBCommunicator.hpp"
|
||||||
#endif /* ORB_COMMUNICATOR */
|
#endif /* ORB_COMMUNICATOR */
|
||||||
|
|
||||||
|
#if defined(__PX4_NUTTX)
|
||||||
|
#include <nuttx/mm/mm.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
static uORB::SubscriptionInterval *filp_to_subscription(cdev::file_t *filp) { return static_cast<uORB::SubscriptionInterval *>(filp->f_priv); }
|
static uORB::SubscriptionInterval *filp_to_subscription(cdev::file_t *filp) { return static_cast<uORB::SubscriptionInterval *>(filp->f_priv); }
|
||||||
|
|
||||||
// round up to nearest power of two
|
// round up to nearest power of two
|
||||||
@@ -71,7 +75,7 @@ static inline uint8_t round_pow_of_two_8(uint8_t n)
|
|||||||
|
|
||||||
uORB::DeviceNode::DeviceNode(const struct orb_metadata *meta, const uint8_t instance, const char *path,
|
uORB::DeviceNode::DeviceNode(const struct orb_metadata *meta, const uint8_t instance, const char *path,
|
||||||
uint8_t queue_size) :
|
uint8_t queue_size) :
|
||||||
CDev(path),
|
CDev(strdup(path)), // success is checked in CDev::init
|
||||||
_meta(meta),
|
_meta(meta),
|
||||||
_instance(instance),
|
_instance(instance),
|
||||||
_queue_size(round_pow_of_two_8(queue_size))
|
_queue_size(round_pow_of_two_8(queue_size))
|
||||||
@@ -82,7 +86,15 @@ uORB::DeviceNode::~DeviceNode()
|
|||||||
{
|
{
|
||||||
delete[] _data;
|
delete[] _data;
|
||||||
|
|
||||||
CDev::unregister_driver_and_memory();
|
const char *devname = get_devname();
|
||||||
|
|
||||||
|
if (devname) {
|
||||||
|
#if defined(__PX4_NUTTX) && !defined(CONFIG_BUILD_FLAT)
|
||||||
|
kmm_free((void *)devname);
|
||||||
|
#else
|
||||||
|
free((void *)devname);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|||||||
+3
-23
@@ -128,6 +128,9 @@ CDev::init()
|
|||||||
if (ret == PX4_OK) {
|
if (ret == PX4_OK) {
|
||||||
_registered = true;
|
_registered = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ret = -ENODEV;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -373,27 +376,4 @@ CDev::remove_poll_waiter(px4_pollfd_struct_t *fds)
|
|||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CDev::unregister_driver_and_memory()
|
|
||||||
{
|
|
||||||
int retval = PX4_OK;
|
|
||||||
|
|
||||||
if (_registered) {
|
|
||||||
unregister_driver(_devname);
|
|
||||||
_registered = false;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
retval = -ENODEV;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_devname != nullptr) {
|
|
||||||
free((void *)_devname);
|
|
||||||
_devname = nullptr;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
retval = -ENODEV;
|
|
||||||
}
|
|
||||||
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace cdev
|
} // namespace cdev
|
||||||
|
|||||||
@@ -270,17 +270,6 @@ protected:
|
|||||||
|
|
||||||
px4_sem_t _lock; /**< lock to protect access to all class members (also for derived classes) */
|
px4_sem_t _lock; /**< lock to protect access to all class members (also for derived classes) */
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* First, unregisters the driver. Next, free the memory for the devname,
|
|
||||||
* in case it was expected to have ownership. Sets devname to nullptr.
|
|
||||||
*
|
|
||||||
* This is only needed if the ownership of the devname was passed to the CDev, otherwise ~CDev handles it.
|
|
||||||
*
|
|
||||||
* @return PX4_OK on success, -ENODEV if the devname is already nullptr
|
|
||||||
*/
|
|
||||||
int unregister_driver_and_memory();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char *_devname{nullptr}; /**< device node name */
|
const char *_devname{nullptr}; /**< device node name */
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user