Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/ch/PollArrayWrapper.java
32288 views
/*1* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*/2728package sun.nio.ch;2930import java.lang.annotation.Native;3132/**33* Manipulates a native array of structs corresponding to (fd, events) pairs.34*35* typedef struct pollfd {36* SOCKET fd; // 4 bytes37* short events; // 2 bytes38* } pollfd_t;39*40* @author Konstantin Kladko41* @author Mike McCloskey42*/4344class PollArrayWrapper {4546private AllocatedNativeObject pollArray; // The fd array4748long pollArrayAddress; // pollArrayAddress4950@Native private static final short FD_OFFSET = 0; // fd offset in pollfd51@Native private static final short EVENT_OFFSET = 4; // events offset in pollfd5253static short SIZE_POLLFD = 8; // sizeof pollfd struct5455private int size; // Size of the pollArray5657PollArrayWrapper(int newSize) {58int allocationSize = newSize * SIZE_POLLFD;59pollArray = new AllocatedNativeObject(allocationSize, true);60pollArrayAddress = pollArray.address();61this.size = newSize;62}6364// Prepare another pollfd struct for use.65void addEntry(int index, SelectionKeyImpl ski) {66putDescriptor(index, ski.channel.getFDVal());67}6869// Writes the pollfd entry from the source wrapper at the source index70// over the entry in the target wrapper at the target index.71void replaceEntry(PollArrayWrapper source, int sindex,72PollArrayWrapper target, int tindex) {73target.putDescriptor(tindex, source.getDescriptor(sindex));74target.putEventOps(tindex, source.getEventOps(sindex));75}7677// Grows the pollfd array to new size78void grow(int newSize) {79PollArrayWrapper temp = new PollArrayWrapper(newSize);80for (int i = 0; i < size; i++)81replaceEntry(this, i, temp, i);82pollArray.free();83pollArray = temp.pollArray;84this.size = temp.size;85pollArrayAddress = pollArray.address();86}8788void free() {89pollArray.free();90}9192// Access methods for fd structures93void putDescriptor(int i, int fd) {94pollArray.putInt(SIZE_POLLFD * i + FD_OFFSET, fd);95}9697void putEventOps(int i, int event) {98pollArray.putShort(SIZE_POLLFD * i + EVENT_OFFSET, (short)event);99}100101int getEventOps(int i) {102return pollArray.getShort(SIZE_POLLFD * i + EVENT_OFFSET);103}104105int getDescriptor(int i) {106return pollArray.getInt(SIZE_POLLFD * i + FD_OFFSET);107}108109// Adds Windows wakeup socket at a given index.110void addWakeupSocket(int fdVal, int index) {111putDescriptor(index, fdVal);112putEventOps(index, Net.POLLIN);113}114}115116117