Skip to content

Commit 4a832dc

Browse files
committed
Check for aligned_alloc() and use posix_memalign() instead on platforms, like Android before API 28, that didn't have it
1 parent 7776634 commit 4a832dc

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL Android)
188188
endif()
189189

190190
check_function_exists(_pthread_workqueue_init HAVE__PTHREAD_WORKQUEUE_INIT)
191+
check_function_exists(aligned_alloc HAVE_ALIGNED_ALLOC)
191192
check_function_exists(getprogname HAVE_GETPROGNAME)
192193
check_function_exists(mach_absolute_time HAVE_MACH_ABSOLUTE_TIME)
193194
check_function_exists(mach_approximate_time HAVE_MACH_APPROXIMATE_TIME)

src/io.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -2373,8 +2373,13 @@ _dispatch_operation_perform(dispatch_operation_t op)
23732373
bQueried = true;
23742374
}
23752375
op->buf = _aligned_malloc(op->buf_siz, siInfo.dwPageSize);
2376-
#else
2376+
#elif defined(HAVE_ALIGNED_ALLOC)
23772377
op->buf = aligned_alloc((size_t)PAGE_SIZE, op->buf_siz);
2378+
#else
2379+
err = posix_memalign(&op->buf, (size_t)PAGE_SIZE, op->buf_siz);
2380+
if (err != 0) {
2381+
goto error;
2382+
}
23782383
#endif
23792384
_dispatch_op_debug("buffer allocated", op);
23802385
} else if (op->direction == DOP_DIR_WRITE) {

tests/dispatch_io.c

+4
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,11 @@ test_async_read(char *path, size_t size, int option, dispatch_queue_t queue,
398398
buffer = _aligned_malloc(size, si.dwPageSize);
399399
#else
400400
size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);
401+
#if defined(HAVE_ALIGNED_ALLOC)
401402
buffer = aligned_alloc(pagesize, size);
403+
#else
404+
posix_memalign((void **)&buffer, pagesize, size);
405+
#endif
402406
#endif
403407
ssize_t r = dispatch_test_fd_read(fd, buffer, size);
404408
if (r == -1) {

tests/dispatch_read2.c

+4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ dispatch_read2(dispatch_fd_t fd,
9191
buffer = _aligned_malloc(bufsiz, pagesize);
9292
#else
9393
size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);
94+
#if defined(HAVE_ALIGNED_ALLOC)
9495
buffer = aligned_alloc(pagesize, bufsiz);
96+
#else
97+
posix_memalign((void **)&buffer, pagesize, bufsiz);
98+
#endif
9599
#endif
96100
ssize_t actual = dispatch_test_fd_read(fd, buffer, bufsiz);
97101
if (actual == -1) {

0 commit comments

Comments
 (0)