1 /*
   2  * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  */
  28 
  29 package sun.nio.ch;
  30 
  31 import java.lang.annotation.Native;
  32 
  33 /**
  34  * Manipulates a native array of structs corresponding to (fd, events) pairs.
  35  *
  36  * typedef struct pollfd {
  37  *    SOCKET fd;            // 4 bytes
  38  *    short events;         // 2 bytes
  39  * } pollfd_t;
  40  *
  41  * @author Konstantin Kladko
  42  * @author Mike McCloskey
  43  */
  44 
  45 class PollArrayWrapper {
  46 
  47     private AllocatedNativeObject pollArray; // The fd array
  48 
  49     long pollArrayAddress; // pollArrayAddress
  50 
  51     @Native private static final short FD_OFFSET     = 0; // fd offset in pollfd
  52     @Native private static final short EVENT_OFFSET  = 4; // events offset in pollfd
  53 
  54     static short SIZE_POLLFD = 8; // sizeof pollfd struct
  55 
  56     // events masks
  57     @Native static final short POLLIN     = AbstractPollArrayWrapper.POLLIN;
  58     @Native static final short POLLOUT    = AbstractPollArrayWrapper.POLLOUT;
  59     @Native static final short POLLERR    = AbstractPollArrayWrapper.POLLERR;
  60     @Native static final short POLLHUP    = AbstractPollArrayWrapper.POLLHUP;
  61     @Native static final short POLLNVAL   = AbstractPollArrayWrapper.POLLNVAL;
  62     @Native static final short POLLREMOVE = AbstractPollArrayWrapper.POLLREMOVE;
  63     @Native static final short POLLCONN   = 0x0002;
  64 
  65     private int size; // Size of the pollArray
  66 
  67     PollArrayWrapper(int newSize) {
  68         int allocationSize = newSize * SIZE_POLLFD;
  69         pollArray = new AllocatedNativeObject(allocationSize, true);
  70         pollArrayAddress = pollArray.address();
  71         this.size = newSize;
  72     }
  73 
  74     // Prepare another pollfd struct for use.
  75     void addEntry(int index, SelectionKeyImpl ski) {
  76         putDescriptor(index, ski.channel.getFDVal());
  77     }
  78 
  79     // Writes the pollfd entry from the source wrapper at the source index
  80     // over the entry in the target wrapper at the target index.
  81     void replaceEntry(PollArrayWrapper source, int sindex,
  82                                      PollArrayWrapper target, int tindex) {
  83         target.putDescriptor(tindex, source.getDescriptor(sindex));
  84         target.putEventOps(tindex, source.getEventOps(sindex));
  85     }
  86 
  87     // Grows the pollfd array to new size
  88     void grow(int newSize) {
  89         PollArrayWrapper temp = new PollArrayWrapper(newSize);
  90         for (int i = 0; i < size; i++)
  91             replaceEntry(this, i, temp, i);
  92         pollArray.free();
  93         pollArray = temp.pollArray;
  94         this.size = temp.size;
  95         pollArrayAddress = pollArray.address();
  96     }
  97 
  98     void free() {
  99         pollArray.free();
 100     }
 101 
 102     // Access methods for fd structures
 103     void putDescriptor(int i, int fd) {
 104         pollArray.putInt(SIZE_POLLFD * i + FD_OFFSET, fd);
 105     }
 106 
 107     void putEventOps(int i, int event) {
 108         pollArray.putShort(SIZE_POLLFD * i + EVENT_OFFSET, (short)event);
 109     }
 110 
 111     int getEventOps(int i) {
 112         return pollArray.getShort(SIZE_POLLFD * i + EVENT_OFFSET);
 113     }
 114 
 115     int getDescriptor(int i) {
 116        return pollArray.getInt(SIZE_POLLFD * i + FD_OFFSET);
 117     }
 118 
 119     // Adds Windows wakeup socket at a given index.
 120     void addWakeupSocket(int fdVal, int index) {
 121         putDescriptor(index, fdVal);
 122         putEventOps(index, POLLIN);
 123     }
 124 }