Return value of static allocation functions

I have a rather esoteric question about the behavior of the various static allocation functions that return an object handle (e.g. xSemaphoreCreateMutexStatic, etc.). My question is: can one presume that the handle value returned by the static create functions is always the address of the static buffer that was passed in?

The code does appear to work this way, however the documentation is silent on this relationship, which leads me to believe the correct answer is no.

I ask because there is a common pattern of implementing newlib’s retargetable locking feature where the global lock variables (e.g. __lock___sfp_recursive_mutex) are declared as StaticSemaphore_t. Unfortunately, the newlib code blindly passes the address of these variables to the lock acquire/release functions, which in the common implementations, gets cast to a SemaphoreHandle_t before being passed to FreeRTOS. In the future, should the handle value not be exactly the address of the static object, this code would obviously break.

I believe it currently works, but is not guarenteed. An alternative if you want to be sure is to make a structure that stores the returned handle and the static creation block, and the lock acquire routine pull the handle out of the structure.

Currently, the handle is a pointer to the memory block. However, we have experimental code that replaces the pointers with indexes for specific ports - so whether your scheme works with future major releases of FreeRTOS may depend on the kernel port.

1 Like

As I suspected. Thanks for the insight. I will structure my code as @richard-damon suggested.

Hey @jaylogue, hope you’re doing well! I wanted to reach out to see if you had any other questions about this topic, or if the answer @richard-damon gave you was what you were looking for?

The information @richard-damon and @rtel provided is exactly what I was looking for. Thanks to all who replied.