The term "reference" is used to mean "opaque data that refers to some conceptual object". It is intentionally vague to support "information hiding". Behind the scenes, different implementations can use almost anything that fits into a pointer as a "reference". Often, they are indexes into arrays or actual pointers to memory objects. When passing those references through an API to outside clients, the implementation becomes exposed to crash bugs when clients pass those references back into the API damaged or stale ("stale" meaning something that has been deleted).
Safe References are designed to help protect against damaged or stale references being used by clients.
Client calls an API's "Create" function:
le_ref_CreateRef()
Followed by:
Client calls another API function, passing in the Safe Reference:
le_ref_Lookup()
Finishing with:
Client calls API's "Delete" function, passing in the Safe Reference:
le_ref_DeleteRef()
At this point, if the Client calls an API function and passes that same (now invalid) Safe Reference (or if the client accidentally passes in some garbage value, like a pointer or zero), the API function will try to translate that into an object pointer. But it'll be told that it's an invalid Safe Reference. The API function can then handle it gracefully, rather than just acting as if it were a valid reference and clobbering the object's deallocated memory or some other object that's reusing the old object's memory.
A Reference Map object can be used to create Safe References and keep track of the mappings from Safe References to pointers. At start-up, a Reference Map is created by calling le_ref_CreateMap()
. It takes a single argument, the maximum number of mappings expected to track of at any time.
This API's functions are reentrant, but not thread safe. If there's the slightest possibility the same Reference Map will be accessed by two threads at the same time, use a mutex or some other thread synchronization mechanism to protect the Reference Map from concurrent access.
Here's an API Definition sample:
Here's an API Implementation sample:
Copyright (C) Sierra Wireless Inc. Use of this work is subject to license.