Skip to content

Commit

Permalink
Fix missing aligned_alloc() on some Android devices
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
2 people authored and axboe committed Feb 20, 2025
1 parent d1c1003 commit 5c788d5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ int setup_listening_socket(int port, int ipv6)

return fd;
}

void *aligned_alloc(size_t alignment, size_t size)
{
void *ret;

if (posix_memalign(&ret, alignment, size))
return NULL;

return ret;
}
7 changes: 7 additions & 0 deletions examples/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@

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
2 changes: 2 additions & 0 deletions examples/reg-wait.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <sys/time.h>
#include <liburing.h>

#include "helpers.h"

static unsigned long long mtime_since(const struct timeval *s,
const struct timeval *e)
{
Expand Down
10 changes: 10 additions & 0 deletions test/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,13 @@ unsigned long long utime_since_now(struct timeval *tv)
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;
}
8 changes: 8 additions & 0 deletions test/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern "C" {
#include "../src/setup.h"
#include <arpa/inet.h>
#include <sys/time.h>
#include <stdlib.h>

enum t_setup_ret {
T_SETUP_OK = 0,
Expand All @@ -25,6 +26,13 @@ enum t_test_result {
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.
Expand Down

0 comments on commit 5c788d5

Please sign in to comment.