View Javadoc
1   /*
2    * Copyright 2024 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.uring;
17  
18  import io.netty.util.internal.PlatformDependent;
19  
20  /**
21   * <pre>{@code
22   * struct iovec {
23   *     void  *iov_base;    // Starting address
24   *     size_t iov_len;     // Number of bytes to transfer
25   * };
26   * }</pre>
27   */
28  final class Iov {
29  
30      private Iov() { }
31  
32      static void write(long iovAddress, long bufferAddress, int length) {
33          if (Native.SIZEOF_SIZE_T == 4) {
34              PlatformDependent.putInt(iovAddress + Native.IOVEC_OFFSETOF_IOV_BASE, (int) bufferAddress);
35              PlatformDependent.putInt(iovAddress + Native.IOVEC_OFFSETOF_IOV_LEN, length);
36          } else {
37              assert Native.SIZEOF_SIZE_T == 8;
38              PlatformDependent.putLong(iovAddress + Native.IOVEC_OFFSETOF_IOV_BASE, bufferAddress);
39              PlatformDependent.putLong(iovAddress + Native.IOVEC_OFFSETOF_IOV_LEN, length);
40          }
41      }
42  
43      static long readBufferAddress(long iovAddress) {
44          if (Native.SIZEOF_SIZE_T == 4) {
45              return PlatformDependent.getInt(iovAddress + Native.IOVEC_OFFSETOF_IOV_BASE);
46          }
47          assert Native.SIZEOF_SIZE_T == 8;
48          return PlatformDependent.getLong(iovAddress + Native.IOVEC_OFFSETOF_IOV_BASE);
49      }
50  
51      static int readBufferLength(long iovAddress) {
52          if (Native.SIZEOF_SIZE_T == 4) {
53              return PlatformDependent.getInt(iovAddress + Native.IOVEC_OFFSETOF_IOV_LEN);
54          }
55          assert Native.SIZEOF_SIZE_T == 8;
56          return (int) PlatformDependent.getLong(iovAddress + Native.IOVEC_OFFSETOF_IOV_LEN);
57      }
58  
59      static long sumSize(long iovAddress, int length) {
60          long sum = 0;
61          for (int i = 0; i < length; i++) {
62              sum += readBufferLength(iovAddress);
63              iovAddress += 2L * Native.SIZEOF_SIZE_T;
64          }
65          return sum;
66      }
67  }