jonblack / arduino-fsm

Arduino library for implementing a finite state machine.
MIT License
200 stars 97 forks source link

C memory management functions #17

Open OxMarco opened 7 years ago

OxMarco commented 7 years ago

Usually in embedded uC it is not recommended to use low-level memory management functions. Can you think of possible workarounds, including checking if there's enough free memory to run dynamic allocation?

jonblack commented 7 years ago

You're right that allocating dynamic memory in embedded systems is generally not a good idea. As far as I'm aware you're relatively safe as long as you don't free up memory that's been allocated (causing fragmentation).

That said it's possible to adapt the library to define a constant number of transitions.

Have you noticed anything funky?

OxMarco commented 7 years ago

Up to now no, I would like to use this FSM within FreeRTOS applied to robotics so I am afraid there might be stability issues when dealing with dynamic memory allocations. Just to be sure, I would add a method to statically initiate the fsm object without using malloc and similar.

jonblack commented 7 years ago

Unless your dynamically changing your state machine (not sure what the use case would be for that) you should be building your state machine in setup. After that no memory allocation is done, so you should be fine.