util: add a steal_fd() helper function

We can't use steal for fds because of the -1 requirement.

Assisted-by: Claude:claude-opus-4-6
Part-of: <https://gitlab.freedesktop.org/libinput/libei/-/merge_requests/388>
This commit is contained in:
Peter Hutterer 2026-05-11 11:37:14 +10:00 committed by Marge Bot
parent 4440f15453
commit c6c8f47b43

View file

@ -96,6 +96,21 @@ _steal(void *ptr)
#define steal(ptr_) \
(typeof(*ptr_))_steal(ptr_)
/**
* Resets the pointer content to -1 and returns
* the original value.
*/
static inline int
steal_fd(int *ptr)
{
if (ptr) {
int original = *ptr;
*ptr = -1;
return original;
}
return -1;
}
/**
* Never-failing calloc with a size limit check.
*/