Skip to content

Commit 73817e0

Browse files
committed
Prefer aligned_alloc over posix_memalign
aligned_alloc is more cleaner and portable.
1 parent ee39300 commit 73817e0

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

src/allocator.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -542,31 +542,33 @@ _dispatch_alloc_maybe_madvise_page(dispatch_continuation_t c)
542542
}
543543
// They are all unallocated, so we could madvise the page. Try to
544544
// take ownership of them all.
545-
int last_locked = 0;
546-
do {
547-
if (!os_atomic_cmpxchg(&page_bitmaps[last_locked], BITMAP_C(0),
545+
for (i = 0; i < BITMAPS_PER_PAGE; i++) {
546+
if (!os_atomic_cmpxchg(&page_bitmaps[i], BITMAP_C(0),
548547
BITMAP_ALL_ONES, relaxed)) {
549548
// We didn't get one; since there is a cont allocated in
550549
// the page, we can't madvise. Give up and unlock all.
551-
goto unlock;
550+
break;
552551
}
553-
} while (++last_locked < (signed)BITMAPS_PER_PAGE);
552+
}
553+
554+
if (i >= BITMAPS_PER_PAGE) {
554555
#if DISPATCH_DEBUG
555-
//fprintf(stderr, "%s: madvised page %p for cont %p (next = %p), "
556-
// "[%u+1]=%u bitmaps at %p\n", __func__, page, c, c->do_next,
557-
// last_locked-1, BITMAPS_PER_PAGE, &page_bitmaps[0]);
558-
// Scribble to expose use-after-free bugs
559-
// madvise (syscall) flushes these stores
560-
memset(page, DISPATCH_ALLOCATOR_SCRIBBLE, DISPATCH_ALLOCATOR_PAGE_SIZE);
556+
// fprintf(stderr, "%s: madvised page %p for cont %p (next = %p), "
557+
// "[%u+1]=%u bitmaps at %p\n", __func__, page, c, c->do_next,
558+
// last_locked-1, BITMAPS_PER_PAGE, &page_bitmaps[0]);
559+
// Scribble to expose use-after-free bugs
560+
// madvise (syscall) flushes these stores
561+
memset(page, DISPATCH_ALLOCATOR_SCRIBBLE, DISPATCH_ALLOCATOR_PAGE_SIZE);
561562
#endif
562-
(void)dispatch_assume_zero(madvise(page, DISPATCH_ALLOCATOR_PAGE_SIZE,
563-
MADV_FREE));
563+
// madvise the page
564+
(void)dispatch_assume_zero(madvise(page, DISPATCH_ALLOCATOR_PAGE_SIZE,
565+
MADV_FREE));
566+
}
564567

565-
unlock:
566-
while (last_locked > 1) {
567-
page_bitmaps[--last_locked] = BITMAP_C(0);
568+
while (i > 1) {
569+
page_bitmaps[--i] = BITMAP_C(0);
568570
}
569-
if (last_locked) {
571+
if (i) {
570572
os_atomic_store(&page_bitmaps[0], BITMAP_C(0), relaxed);
571573
}
572574
return;

src/io.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,10 +2374,7 @@ _dispatch_operation_perform(dispatch_operation_t op)
23742374
}
23752375
op->buf = _aligned_malloc(op->buf_siz, siInfo.dwPageSize);
23762376
#else
2377-
err = posix_memalign(&op->buf, (size_t)PAGE_SIZE, op->buf_siz);
2378-
if (err != 0) {
2379-
goto error;
2380-
}
2377+
op->buf = aligned_alloc((size_t)PAGE_SIZE, op->buf_siz)
23812378
#endif
23822379
_dispatch_op_debug("buffer allocated", op);
23832380
} else if (op->direction == DOP_DIR_WRITE) {

tests/dispatch_io.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ 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-
posix_memalign((void **)&buffer, pagesize, size);
401+
buffer = aligned_alloc(pagesize, size);
402402
#endif
403403
ssize_t r = dispatch_test_fd_read(fd, buffer, size);
404404
if (r == -1) {

tests/dispatch_read2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ dispatch_read2(dispatch_fd_t fd,
9191
buffer = _aligned_malloc(bufsiz, pagesize);
9292
#else
9393
size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);
94-
posix_memalign((void **)&buffer, pagesize, bufsiz);
94+
buffer = aligned_alloc(pagesize, bufsiz);
9595
#endif
9696
ssize_t actual = dispatch_test_fd_read(fd, buffer, bufsiz);
9797
if (actual == -1) {

0 commit comments

Comments
 (0)