Fix Compilation Error on Android and Some Cleanup
cover Fix Compilation Error on Android and Some Cleanup
Hi Jens,
Another day in the thrilling world of cross-platform compatibility...
Alviro discovered that some Android versions are missing aligned_alloc() in <stdlib.h>, leading to a compilation error on Termux 0.118.0:
```
cmd-discard.c:383:11: warning: call to undeclared library function \
'aligned_alloc' with type 'void *(unsigned long, unsigned long)'; ISO \
C99 and later do not support implicit function declarations \
[-Wimplicit-function-declaration]
buffer = aligned_alloc(lba_size, lba_size);
^
```To resolve this without rewriting large portions of liburing tests, introduce a helper function that wraps posix_memalign() and provides our own aligned_alloc().
While we're at it, there's a redundant double negation lurking in liburing.h. Let's clean that up too.
Also, to prevent yet another round of confusion like what happened in PR #1336, document the history of io_uring_get_sqe() in the header file.
Ref: https://github.com/axboe/liburing/pull/1336
---
Ammar Faizi (3):
liburing.h: Remove redundant double negation liburing.h: Explain the history of io_uring_get_sqe() Fix missing aligned_alloc() on some Android devices
examples/helpers.c | 10 ++++++++++ examples/helpers.h | 7 +++++++ examples/reg-wait.c | 2 ++ src/include/liburing.h | 21 ++++++++++++++++++++- test/helpers.c | 10 ++++++++++ test/helpers.h | 8 ++++++++ 6 files changed, 57 insertions(+), 1 deletion(-)
base-commit: 66b071d1470ae787d47d4cb8d9cb3836249baf61
Signature · 1 line
Ammar Faizi
PATCH 1/3 liburing.h: Remove redundant double negation
The enabled variable is already a boolean, so applying the negation operator twice has no effect. Remove it to improves clarity and simplicity.
--- src/include/liburing.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/include/liburing.h +1 -1
@@ -1410,25 +1410,25 @@ IOURINGINLINE bool io_uring_cq_eventfd_enabled(const struct io_uring *ring)return !(*ring->cq.kflags &IORING_CQ_EVENTFD_DISABLED ); }/* * Toggle eventfd notification on or off, if an eventfd is registered with * the ring. */ IOURINGINLINE int io_uring_cq_eventfd_toggle(struct io_uring *ring,bool enabled) {uint32_t flags; -if (!!enabled ==io_uring_cq_eventfd_enabled (ring)) +if (enabled ==io_uring_cq_eventfd_enabled (ring))return 0 ;if (!ring->cq.kflags)return -EOPNOTSUPP ; flags = *ring->cq.kflags;if (enabled) flags &= ~IORING_CQ_EVENTFD_DISABLED ;else flags |=IORING_CQ_EVENTFD_DISABLED ;
Signature · 1 line
Ammar Faizi
PATCH 2/3 liburing.h: Explain the history of `io_uring_get_sqe()`
In reply to Ammar FaiziAdd a comment for io_uring_get_sqe() to provide historical context and prevent misunderstandings, as seen in Pull Request 1336.
--- src/include/liburing.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
src/include/liburing.h +19 -0
@@ -1622,24 +1622,43 @@ IOURINGINLINE int io_uring_buf_ring_available(struct io_uring *ring, unsigned short bgid) {uint16_t head;int ret; ret =io_uring_buf_ring_head (ring, bgid, &head);if (ret)return ret;return (uint16_t ) (br->tail - head); } +/* +* As of liburing-2.2, io_uring_get_sqe() has been converted into a +* "static inline" function. However, this change breaks seamless +* updates of liburing.so, as applications would need to be recompiled. +* To ensure backward compatibility, liburing keeps the original +* io_uring_get_sqe() symbol available in the shared library. +* +* To accomplish this, io_uring_get_sqe() is defined as a non-static +* inline function when LIBURING_INTERNAL is set, which only applies +* during liburing.so builds. +* +* This strategy ensures new users adopt the "static inline" version +* while preserving compatibility for old applications linked against +* the shared library. +* +* Relevant commits: +* 8be8af4afcb4 ("queue: provide io_uring_get_sqe() symbol again") +* 52dcdbba35c8 ("src/queue: protect io_uring_get_sqe() with LIBURING_INTERNAL") +*/ #ifndef LIBURING_INTERNAL IOURINGINLINE struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring) {return _io_uring_get_sqe (ring); }#else struct io_uring_sqe *io_uring_get_sqe (struct io_uring *ring);#endif ssize_t io_uring_mlock_size (unsigned entries,unsigned flags);ssize_t io_uring_mlock_size_params (unsigned entries,struct io_uring_params *p);
Signature · 1 line
Ammar Faizi
PATCH 3/3 Fix missing `aligned_alloc()` on some Android devices
In reply to Ammar FaiziSome Android versions lack aligned_alloc() in <stdlib.h>. Compiling on Termux 0.118.0 yields this error:
```
cmd-discard.c:383:11: warning: call to undeclared library function \
'aligned_alloc' with type 'void *(unsigned long, unsigned long)'; ISO \
C99 and later do not support implicit function declarations \
[-Wimplicit-function-declaration]
buffer = aligned_alloc(lba_size, lba_size);
^
```To avoid making large changes in tests, define a helper function that wraps posix_memalign() as our own aligned_alloc().
Just another day of working around platform quirks.
--- examples/helpers.c | 10 ++++++++++ examples/helpers.h | 7 +++++++ examples/reg-wait.c | 2 ++ test/helpers.c | 10 ++++++++++ test/helpers.h | 8 ++++++++ 5 files changed, 37 insertions(+)
examples/helpers.c +10 -0
@@ -51,12 +51,22 @@ int setup_listening_socket(int port, int ipv6)if (ret <0 ) {perror ("bind()" );return -1 ; }if (listen (fd,1024 ) <0 ) {perror ("listen()" );return -1 ; }return fd; } + +void *aligned_alloc (size_t alignment,size_t size) +{ +void *ret; + +if (posix_memalign (&ret, alignment, size)) +return NULL ; + +return ret; +}
examples/helpers.h +7 -0
@@ -1,7 +1,14 @@/* SPDX-License-Identifier: MIT */ #ifndef LIBURING_EX_HELPERS_H #define LIBURING_EX_HELPERS_H int setup_listening_socket (int port,int ipv6); +/* +* Some Android versions lack aligned_alloc in stdlib.h. +* To avoid making large changes in tests, define a helper +* function that wraps posix_memalign as our own aligned_alloc. +*/ +void *aligned_alloc (size_t alignment,size_t size); +#endif
examples/reg-wait.c +2 -0
@@ -4,24 +4,26 @@* * (C) 2024 Jens Axboe <[email protected]> */ #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <assert.h> #include <sys/time.h> #include <liburing.h> +#include "helpers.h" +static unsigned long long mtime_since (const struct timeval *s,const struct timeval *e) {long long sec, usec; sec = e->tv_sec - s->tv_sec; usec = (e->tv_usec - s->tv_usec);if (sec >0 && usec <0 ) { sec--; usec +=1000000 ; }
test/helpers.c +10 -0
@@ -354,12 +354,22 @@ unsigned long long utime_since(const struct timeval *s, const struct timeval *e) sec *=1000000 ;return sec + usec; }unsigned long long utime_since_now (struct timeval *tv) {struct timeval end;gettimeofday (&end,NULL );return utime_since (tv, &end); } + +void *aligned_alloc (size_t alignment,size_t size) +{ +void *ret; + +if (posix_memalign (&ret, alignment, size)) +return NULL ; + +return ret; +}
test/helpers.h +8 -0
@@ -4,36 +4,44 @@*/ #ifndef LIBURING_HELPERS_H #define LIBURING_HELPERS_H #ifdef __cplusplus extern "C" { #endif #include "liburing.h" #include "../src/setup.h" #include <arpa/inet.h> #include <sys/time.h> +#include <stdlib.h> enum t_setup_ret { T_SETUP_OK = 0, T_SETUP_SKIP, }; enum t_test_result { T_EXIT_PASS = 0, T_EXIT_FAIL = 1, T_EXIT_SKIP = 77, }; +/* + * Some Android versions lack aligned_alloc in stdlib.h. + * To avoid making large changes in tests, define a helper + * function that wraps posix_memalign as our own aligned_alloc. + */ +void *aligned_alloc(size_t alignment, size_t size); + /* * Helper for binding socket to an ephemeral port. * The port number to be bound is returned in @addr->sin_port. */ int t_bind_ephemeral_port(int fd, struct sockaddr_in *addr); /* * Helper for allocating memory in tests. */ void *t_malloc(size_t size);
Signature · 1 line
Ammar Faizi
Quoted 5 lines · Ammar Faizi
Another day in the thrilling world of cross-platform compatibility...
Alviro discovered that some Android versions are missing
aligned_alloc()in<stdlib.h>, leading to a compilation error on Termux 0.118.0:
Quoted 7 lines
```
cmd-discard.c:383:11: warning: call to undeclared library function \
'aligned_alloc' with type 'void *(unsigned long, unsigned long)'; ISO \
C99 and later do not support implicit function declarations \
[-Wimplicit-function-declaration][...]
Applied, thanks!
[1/3] liburing.h: Remove redundant double negation
commit: 1d11475301931478bb35f2573e1741f5d9088132
[2/3] liburing.h: Explain the history of `io_uring_get_sqe()`
commit: d1c100351ffb3483f5d3fc661b2d41d48062bec1
[3/3] Fix missing `aligned_alloc()` on some Android devices
commit: 5c788d514b9ed6d1a3624150de8aa6db403c1c65Best regards,
Signature · 1 line
Jens Axboe