Provide a view onto this structure with the given alignment. @param byteBoundary Align memory to this number of bytes; should be a power of two. @throws IndexOutOfBoundsException if the requested alignment can not be met. @throws IllegalArgumentException if the requested alignment is not a positive
(int byteBoundary)
| 160 | * a positive power of two. |
| 161 | */ |
| 162 | public Memory align(int byteBoundary) { |
| 163 | if (byteBoundary <= 0) { |
| 164 | throw new IllegalArgumentException("Byte boundary must be positive: " + byteBoundary); |
| 165 | } |
| 166 | for (int i=0;i < 32;i++) { |
| 167 | if (byteBoundary == (1<<i)) { |
| 168 | long mask = ~((long)byteBoundary - 1); |
| 169 | |
| 170 | if ((peer & mask) != peer) { |
| 171 | long newPeer = (peer + byteBoundary - 1) & mask; |
| 172 | long newSize = peer + size - newPeer; |
| 173 | if (newSize <= 0) { |
| 174 | throw new IllegalArgumentException("Insufficient memory to align to the requested boundary"); |
| 175 | } |
| 176 | return (Memory)share(newPeer - peer, newSize); |
| 177 | } |
| 178 | return this; |
| 179 | } |
| 180 | } |
| 181 | throw new IllegalArgumentException("Byte boundary must be a power of two"); |
| 182 | } |
| 183 | |
| 184 | /** Free the native memory and set peer to zero */ |
| 185 | @Override |