Skip to content

Commit a736ea7

Browse files
Merge pull request swiftlang#405 from adierking/valloc
Replace valloc() usages in portable code
2 parents bd2367c + 883c15f commit a736ea7

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/io.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2320,7 +2320,10 @@ _dispatch_operation_perform(dispatch_operation_t op)
23202320
}
23212321
op->buf = _aligned_malloc(op->buf_siz, siInfo.dwPageSize);
23222322
#else
2323-
op->buf = valloc(op->buf_siz);
2323+
err = posix_memalign(&op->buf, (size_t)PAGE_SIZE, op->buf_siz);
2324+
if (err != 0) {
2325+
goto error;
2326+
}
23242327
#endif
23252328
_dispatch_op_debug("buffer allocated", op);
23262329
} else if (op->direction == DOP_DIR_WRITE) {

tests/dispatch_io.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,9 @@ test_async_read(char *path, size_t size, int option, dispatch_queue_t queue,
372372
case DISPATCH_ASYNC_READ_ON_CONCURRENT_QUEUE:
373373
case DISPATCH_ASYNC_READ_ON_SERIAL_QUEUE:
374374
dispatch_async(queue, ^{
375-
char* buffer = valloc(size);
375+
char* buffer = NULL;
376+
size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);
377+
posix_memalign((void **)&buffer, pagesize, size);
376378
ssize_t r = read(fd, buffer, size);
377379
if (r == -1) {
378380
test_errno("async read error", errno, 0);

tests/dispatch_read2.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ dispatch_read2(dispatch_fd_t fd,
8181
__block int err = 0;
8282
dispatch_source_set_event_handler(reader, ^{
8383
const ssize_t bufsiz = 1024*512; // 512KB buffer
84-
char *buffer = valloc(bufsiz);
84+
char *buffer = NULL;
85+
size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);
86+
posix_memalign((void **)&buffer, pagesize, bufsiz);
8587
ssize_t actual = read(fd, buffer, bufsiz);
8688
if (actual == -1) {
8789
err = errno;

0 commit comments

Comments
 (0)