blob: 6149f09d182696b567b189e5d17cb2b9519aaf63 [file] [log] [blame]
Jerry Zhangecee4342017-07-18 14:07:57 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <asyncio/AsyncIO.h>
18#include <sys/syscall.h>
19#include <unistd.h>
Jerry Zhangc3d4e722018-02-12 16:15:50 -080020#include <cstdint>
21#include <cstring>
Jerry Zhangecee4342017-07-18 14:07:57 -070022
23int io_setup(unsigned nr, aio_context_t* ctxp) {
Jerry Zhangecee4342017-07-18 14:07:57 -070024 return syscall(__NR_io_setup, nr, ctxp);
25}
26
27int io_destroy(aio_context_t ctx) {
28 return syscall(__NR_io_destroy, ctx);
29}
30
31int io_submit(aio_context_t ctx, long nr, iocb** iocbpp) {
32 return syscall(__NR_io_submit, ctx, nr, iocbpp);
33}
34
35int io_getevents(aio_context_t ctx, long min_nr, long max_nr, io_event* events, timespec* timeout) {
36 return syscall(__NR_io_getevents, ctx, min_nr, max_nr, events, timeout);
37}
38
39int io_cancel(aio_context_t ctx, iocb* iocbp, io_event* result) {
40 return syscall(__NR_io_cancel, ctx, iocbp, result);
41}
42
43void io_prep(iocb* iocb, int fd, const void* buf, uint64_t count, int64_t offset, bool read) {
44 memset(iocb, 0, sizeof(*iocb));
45 iocb->aio_fildes = fd;
46 iocb->aio_lio_opcode = read ? IOCB_CMD_PREAD : IOCB_CMD_PWRITE;
47 iocb->aio_reqprio = 0;
48 iocb->aio_buf = reinterpret_cast<uint64_t>(buf);
49 iocb->aio_nbytes = count;
50 iocb->aio_offset = offset;
51}
Jerry Zhangc3d4e722018-02-12 16:15:50 -080052
53void io_prep_pread(struct iocb* iocb, int fd, void* buf, size_t count, long long offset) {
54 io_prep(iocb, fd, buf, count, offset, true);
55}
56
57void io_prep_pwrite(struct iocb* iocb, int fd, void* buf, size_t count, long long offset) {
58 io_prep(iocb, fd, buf, count, offset, false);
59}