From 1bdad65849479ef7c64b8455bcf2ddcd7fd37397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Tue, 18 Oct 2016 17:07:40 +0200 Subject: [PATCH] uorb: reduce RAM usage by avoiding string copies The lifetime of the string is guaranteed because we never delete DeviceNode objects and the strings in question are already on the heap. This frees roughly 2.2KB of RAM on Pixracer --- src/modules/uORB/ORBMap.hpp | 39 ++++++++++++++++++-------------- src/modules/uORB/uORBDevices.cpp | 4 ++-- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/modules/uORB/ORBMap.hpp b/src/modules/uORB/ORBMap.hpp index 186c46ce18..cb44936fda 100644 --- a/src/modules/uORB/ORBMap.hpp +++ b/src/modules/uORB/ORBMap.hpp @@ -62,13 +62,19 @@ public: unlinkNext(_top); if (_top->next == nullptr) { - free((void *)_top->node_name); free(_top); _top = nullptr; _end = nullptr; } } } + + /** + * Insert an element with a unique name + * @param node_name name of the node. This will not be copied, so the caller has to ensure + * the pointer is valid until the node is removed from ORBMap + * @param node + */ void insert(const char *node_name, uORB::DeviceNode *node) { Node **p; @@ -90,7 +96,7 @@ public: } _end->next = nullptr; - _end->node_name = strdup(node_name); + _end->node_name = node_name; _end->node = node; } @@ -124,21 +130,6 @@ public: return nullptr; } - void unlinkNext(Node *a) - { - Node *b = a->next; - - if (b != nullptr) { - if (_end == b) { - _end = a; - } - - a->next = b->next; - free((void *)b->node_name); - free(b); - } - } - Node *top() const { return _top; @@ -150,6 +141,20 @@ public: } private: + void unlinkNext(Node *a) + { + Node *b = a->next; + + if (b != nullptr) { + if (_end == b) { + _end = a; + } + + a->next = b->next; + free(b); + } + } + Node *_top; Node *_end; }; diff --git a/src/modules/uORB/uORBDevices.cpp b/src/modules/uORB/uORBDevices.cpp index b03a2980a2..ed44d7a9c9 100644 --- a/src/modules/uORB/uORBDevices.cpp +++ b/src/modules/uORB/uORBDevices.cpp @@ -912,9 +912,9 @@ uORB::DeviceMaster::ioctl(device::file_t *filp, int cmd, unsigned long arg) } else { // add to the node map;. #ifdef __PX4_NUTTX - _node_map.insert(nodepath, node); + _node_map.insert(devpath, node); #else - _node_map[std::string(nodepath)] = node; + _node_map[std::string(devpath)] = node; #endif }