Skip to content

Commit

Permalink
Add PTR_TO_ERR / ERR_TO_PTR functions
Browse files Browse the repository at this point in the history
Inspired by the Linux kernel source, these inline functions can be used
to return error codes in functions that return pointers.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Apr 7, 2021
1 parent 365108d commit 3b67aa5
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ static inline void *zalloc(size_t size)
return calloc(1, size);
}

static inline __check_ret bool IS_ERR(const void *ptr)
{
return (uintptr_t)ptr >= (uintptr_t)-4095;
}

static inline __check_ret intptr_t PTR_TO_ERR(const void *ptr)
{
return (intptr_t)ptr;
}

static inline __check_ret void * ERR_TO_PTR(intptr_t err)
{
return (void *)err;
}

/*
* If these structures are updated, the qsort functions defined in sort.c
* may need to be updated.
Expand Down

0 comments on commit 3b67aa5

Please sign in to comment.