Skip to content

Commit 5c788d5

Browse files
ammarfaizi2alviroiskandar
authored andcommitted
Fix missing aligned_alloc() on some Android devices
Some 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. Co-authored-by: Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org> Signed-off-by: Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org> Signed-off-by: Ammar Faizi <ammarfaizi2@gnuweeb.org> Link: https://lore.kernel.org/r/20250220143422.3597245-4-ammarfaizi2@gnuweeb.org Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent d1c1003 commit 5c788d5

5 files changed

Lines changed: 37 additions & 0 deletions

File tree

examples/helpers.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,13 @@ int setup_listening_socket(int port, int ipv6)
6060

6161
return fd;
6262
}
63+
64+
void *aligned_alloc(size_t alignment, size_t size)
65+
{
66+
void *ret;
67+
68+
if (posix_memalign(&ret, alignment, size))
69+
return NULL;
70+
71+
return ret;
72+
}

examples/helpers.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@
44

55
int setup_listening_socket(int port, int ipv6);
66

7+
/*
8+
* Some Android versions lack aligned_alloc in stdlib.h.
9+
* To avoid making large changes in tests, define a helper
10+
* function that wraps posix_memalign as our own aligned_alloc.
11+
*/
12+
void *aligned_alloc(size_t alignment, size_t size);
13+
714
#endif

examples/reg-wait.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <sys/time.h>
1414
#include <liburing.h>
1515

16+
#include "helpers.h"
17+
1618
static unsigned long long mtime_since(const struct timeval *s,
1719
const struct timeval *e)
1820
{

test/helpers.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,13 @@ unsigned long long utime_since_now(struct timeval *tv)
363363
gettimeofday(&end, NULL);
364364
return utime_since(tv, &end);
365365
}
366+
367+
void *aligned_alloc(size_t alignment, size_t size)
368+
{
369+
void *ret;
370+
371+
if (posix_memalign(&ret, alignment, size))
372+
return NULL;
373+
374+
return ret;
375+
}

test/helpers.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern "C" {
1313
#include "../src/setup.h"
1414
#include <arpa/inet.h>
1515
#include <sys/time.h>
16+
#include <stdlib.h>
1617

1718
enum t_setup_ret {
1819
T_SETUP_OK = 0,
@@ -25,6 +26,13 @@ enum t_test_result {
2526
T_EXIT_SKIP = 77,
2627
};
2728

29+
/*
30+
* Some Android versions lack aligned_alloc in stdlib.h.
31+
* To avoid making large changes in tests, define a helper
32+
* function that wraps posix_memalign as our own aligned_alloc.
33+
*/
34+
void *aligned_alloc(size_t alignment, size_t size);
35+
2836
/*
2937
* Helper for binding socket to an ephemeral port.
3038
* The port number to be bound is returned in @addr->sin_port.

0 commit comments

Comments
 (0)