Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Zhang | c3d4e72 | 2018-02-12 16:15:50 -0800 | [diff] [blame] | 20 | #include <cstdint> |
| 21 | #include <cstring> |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 22 | |
| 23 | int io_setup(unsigned nr, aio_context_t* ctxp) { |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 24 | return syscall(__NR_io_setup, nr, ctxp); |
| 25 | } |
| 26 | |
| 27 | int io_destroy(aio_context_t ctx) { |
| 28 | return syscall(__NR_io_destroy, ctx); |
| 29 | } |
| 30 | |
| 31 | int io_submit(aio_context_t ctx, long nr, iocb** iocbpp) { |
| 32 | return syscall(__NR_io_submit, ctx, nr, iocbpp); |
| 33 | } |
| 34 | |
| 35 | int 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 | |
| 39 | int io_cancel(aio_context_t ctx, iocb* iocbp, io_event* result) { |
| 40 | return syscall(__NR_io_cancel, ctx, iocbp, result); |
| 41 | } |
| 42 | |
| 43 | void 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 Zhang | c3d4e72 | 2018-02-12 16:15:50 -0800 | [diff] [blame] | 52 | |
| 53 | void 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 | |
| 57 | void 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 | } |