smartheim / wakaamaNode

lwM2M library based on Wakaama for PlattformIO/cmake with easy object API
https://openhab-nodes.github.io/wakaamaNode/
MIT License
27 stars 19 forks source link

C++ migration: Heap allocations #13

Open davidgraeff opened 6 years ago

davidgraeff commented 6 years ago

The wakaama library allows to override the alloc/free functions. It is not very fine grained control though, as some allocations are just small strings (that basically never change), some are for various sized transactions, some for huge block1 data.

All general heap usage should disappear.

Receiving

It should not be necessary to allocate heap memory at all for receiving.

Sending

Because the client library is restricted to UDP usage, the network MTU defines the maximum packet size. On top of that, for most packets the final size can be precomputed or determined at build time (C++11/14 constexpr). Transactions should not use the general heap directly.

Block1

It can be assumed that only one Block1 data packet is on the wire at any time. The block1 class should use the general heap directly.

Strings

Strings are used for

Most of the time string literals will be used, optimize for this use case. Remove all strdup() calls. The lwm2m server object gets read-only with this change. If an API user wants a modifiable lwm2m server object, he need to roll out his own.

Adding/Removing a server

Instead of passing arguments to an add_server function, that allocates memory to create a new list entry and inserts all those arguments, the user should be requested to create a ServerConnection object that implements a linked list entry interface and is passed to the library.

Necessary allocations and memory management is moved to the users obligations.