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>
20 #include <cstdint>
21 #include <cstring>
22 
io_setup(unsigned nr,aio_context_t * ctxp)23 int io_setup(unsigned nr, aio_context_t* ctxp) {
24     return syscall(__NR_io_setup, nr, ctxp);
25 }
26 
io_destroy(aio_context_t ctx)27 int io_destroy(aio_context_t ctx) {
28     return syscall(__NR_io_destroy, ctx);
29 }
30 
io_submit(aio_context_t ctx,long nr,iocb ** iocbpp)31 int io_submit(aio_context_t ctx, long nr, iocb** iocbpp) {
32     return syscall(__NR_io_submit, ctx, nr, iocbpp);
33 }
34 
io_getevents(aio_context_t ctx,long min_nr,long max_nr,io_event * events,timespec * timeout)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 
io_cancel(aio_context_t ctx,iocb * iocbp,io_event * result)39 int io_cancel(aio_context_t ctx, iocb* iocbp, io_event* result) {
40     return syscall(__NR_io_cancel, ctx, iocbp, result);
41 }
42 
io_prep(iocb * iocb,int fd,const void * buf,uint64_t count,int64_t offset,bool read)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 }
52 
io_prep_pread(struct iocb * iocb,int fd,void * buf,size_t count,long long offset)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 
io_prep_pwrite(struct iocb * iocb,int fd,void * buf,size_t count,long long offset)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 }
60