J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001-2002 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | */ |
| 28 | |
| 29 | package sun.nio.ch; |
| 30 | |
| 31 | /** |
| 32 | * Manipulates a native array of structs corresponding to (fd, events) pairs. |
| 33 | * |
| 34 | * typedef struct pollfd { |
| 35 | * SOCKET fd; // 4 bytes |
| 36 | * short events; // 2 bytes |
| 37 | * } pollfd_t; |
| 38 | * |
| 39 | * @author Konstantin Kladko |
| 40 | * @author Mike McCloskey |
| 41 | */ |
| 42 | |
| 43 | class PollArrayWrapper { |
| 44 | |
| 45 | private AllocatedNativeObject pollArray; // The fd array |
| 46 | |
| 47 | long pollArrayAddress; // pollArrayAddress |
| 48 | |
| 49 | private static final short FD_OFFSET = 0; // fd offset in pollfd |
| 50 | private static final short EVENT_OFFSET = 4; // events offset in pollfd |
| 51 | |
| 52 | static short SIZE_POLLFD = 8; // sizeof pollfd struct |
| 53 | |
| 54 | // events masks |
| 55 | static final short POLLIN = AbstractPollArrayWrapper.POLLIN; |
| 56 | static final short POLLOUT = AbstractPollArrayWrapper.POLLOUT; |
| 57 | static final short POLLERR = AbstractPollArrayWrapper.POLLERR; |
| 58 | static final short POLLHUP = AbstractPollArrayWrapper.POLLHUP; |
| 59 | static final short POLLNVAL = AbstractPollArrayWrapper.POLLNVAL; |
| 60 | static final short POLLREMOVE = AbstractPollArrayWrapper.POLLREMOVE; |
| 61 | static final short POLLCONN = 0x0002; |
| 62 | |
| 63 | private int size; // Size of the pollArray |
| 64 | |
| 65 | PollArrayWrapper(int newSize) { |
| 66 | int allocationSize = newSize * SIZE_POLLFD; |
| 67 | pollArray = new AllocatedNativeObject(allocationSize, true); |
| 68 | pollArrayAddress = pollArray.address(); |
| 69 | this.size = newSize; |
| 70 | } |
| 71 | |
| 72 | // Prepare another pollfd struct for use. |
| 73 | void addEntry(int index, SelectionKeyImpl ski) { |
| 74 | putDescriptor(index, ski.channel.getFDVal()); |
| 75 | } |
| 76 | |
| 77 | // Writes the pollfd entry from the source wrapper at the source index |
| 78 | // over the entry in the target wrapper at the target index. |
| 79 | void replaceEntry(PollArrayWrapper source, int sindex, |
| 80 | PollArrayWrapper target, int tindex) { |
| 81 | target.putDescriptor(tindex, source.getDescriptor(sindex)); |
| 82 | target.putEventOps(tindex, source.getEventOps(sindex)); |
| 83 | } |
| 84 | |
| 85 | // Grows the pollfd array to new size |
| 86 | void grow(int newSize) { |
| 87 | PollArrayWrapper temp = new PollArrayWrapper(newSize); |
| 88 | for (int i = 0; i < size; i++) |
| 89 | replaceEntry(this, i, temp, i); |
| 90 | pollArray.free(); |
| 91 | pollArray = temp.pollArray; |
| 92 | this.size = temp.size; |
| 93 | pollArrayAddress = pollArray.address(); |
| 94 | } |
| 95 | |
| 96 | void free() { |
| 97 | pollArray.free(); |
| 98 | } |
| 99 | |
| 100 | // Access methods for fd structures |
| 101 | void putDescriptor(int i, int fd) { |
| 102 | pollArray.putInt(SIZE_POLLFD * i + FD_OFFSET, fd); |
| 103 | } |
| 104 | |
| 105 | void putEventOps(int i, int event) { |
| 106 | pollArray.putShort(SIZE_POLLFD * i + EVENT_OFFSET, (short)event); |
| 107 | } |
| 108 | |
| 109 | int getEventOps(int i) { |
| 110 | return pollArray.getShort(SIZE_POLLFD * i + EVENT_OFFSET); |
| 111 | } |
| 112 | |
| 113 | int getDescriptor(int i) { |
| 114 | return pollArray.getInt(SIZE_POLLFD * i + FD_OFFSET); |
| 115 | } |
| 116 | |
| 117 | // Adds Windows wakeup socket at a given index. |
| 118 | void addWakeupSocket(int fdVal, int index) { |
| 119 | putDescriptor(index, fdVal); |
| 120 | putEventOps(index, POLLIN); |
| 121 | } |
| 122 | } |