Browse Source

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>
master
Jukka Laitinen 3 years ago committed by Daniel Agar
parent
commit
ce6147f570
  1. 12
      platforms/common/uORB/uORBDeviceMaster.cpp
  2. 16
      platforms/common/uORB/uORBDeviceNode.cpp
  3. 26
      src/lib/cdev/CDev.cpp
  4. 11
      src/lib/cdev/CDev.hpp

12
platforms/common/uORB/uORBDeviceMaster.cpp

@ -99,19 +99,11 @@ int uORB::DeviceMaster::advertise(const struct orb_metadata *meta, bool is_adver @@ -99,19 +99,11 @@ int uORB::DeviceMaster::advertise(const struct orb_metadata *meta, bool is_adver
*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 */
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) {
free((void *)devpath);
return -ENOMEM;
}

16
platforms/common/uORB/uORBDeviceNode.cpp

@ -42,6 +42,10 @@ @@ -42,6 +42,10 @@
#include "uORBCommunicator.hpp"
#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); }
// round up to nearest power of two
@ -71,7 +75,7 @@ static inline uint8_t round_pow_of_two_8(uint8_t n) @@ -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,
uint8_t queue_size) :
CDev(path),
CDev(strdup(path)), // success is checked in CDev::init
_meta(meta),
_instance(instance),
_queue_size(round_pow_of_two_8(queue_size))
@ -82,7 +86,15 @@ uORB::DeviceNode::~DeviceNode() @@ -82,7 +86,15 @@ uORB::DeviceNode::~DeviceNode()
{
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

26
src/lib/cdev/CDev.cpp

@ -128,6 +128,9 @@ CDev::init() @@ -128,6 +128,9 @@ CDev::init()
if (ret == PX4_OK) {
_registered = true;
}
} else {
ret = -ENODEV;
}
return ret;
@ -373,27 +376,4 @@ CDev::remove_poll_waiter(px4_pollfd_struct_t *fds) @@ -373,27 +376,4 @@ CDev::remove_poll_waiter(px4_pollfd_struct_t *fds)
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

11
src/lib/cdev/CDev.hpp

@ -270,17 +270,6 @@ protected: @@ -270,17 +270,6 @@ protected:
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:
const char *_devname{nullptr}; /**< device node name */

Loading…
Cancel
Save