2 * Copyright (C) 2016 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import android.os.Parcel;
20 import android.os.ParcelFileDescriptor;
21 import android.os.Parcelable;
22 import android.os.Process;
24 import java.io.Closeable;
25 import java.io.IOException;
26 import java.util.UUID;
29 * This class is an array of integers that is backed by shared memory.
30 * It is useful for efficiently sharing state between processes. The
31 * write and read operations are guaranteed to not result in read/
32 * write memory tear, i.e. they are atomic. However, multiple read/
33 * write operations are <strong>not</strong> synchronized between
36 * The data structure is designed to have one owner process that can
37 * read/write. There may be multiple client processes that can only read or
38 * read/write depending how the data structure was configured when
39 * instantiated. The owner process is the process that created the array.
40 * The shared memory is pinned (not reclaimed by the system) until the
41 * owning process dies or the data structure is closed. This class
42 * is <strong>not</strong> thread safe. You should not interact with
43 * an instance of this class once it is closed.
48 public final class MemoryIntArray implements Parcelable, Closeable {
49 private static final int MAX_SIZE = 1024;
51 private final int mOwnerPid;
52 private final boolean mClientWritable;
53 private final long mMemoryAddr;
54 private ParcelFileDescriptor mFd;
57 * Creates a new instance.
59 * @param size The size of the array in terms of integer slots. Cannot be
60 * more than {@link #getMaxSize()}.
61 * @param clientWritable Whether other processes can write to the array.
62 * @throws IOException If an error occurs while accessing the shared memory.
64 public MemoryIntArray(int size, boolean clientWritable) throws IOException {
65 if (size > MAX_SIZE) {
66 throw new IllegalArgumentException("Max size is " + MAX_SIZE);
68 mOwnerPid = Process.myPid();
69 mClientWritable = clientWritable;
70 final String name = UUID.randomUUID().toString();
71 mFd = ParcelFileDescriptor.fromFd(nativeCreate(name, size));
72 mMemoryAddr = nativeOpen(mFd.getFd(), true, clientWritable);
75 private MemoryIntArray(Parcel parcel) throws IOException {
76 mOwnerPid = parcel.readInt();
77 mClientWritable = (parcel.readInt() == 1);
78 mFd = parcel.readParcelable(null);
80 throw new IOException("No backing file descriptor");
82 final long memoryAddress = parcel.readLong();
84 mMemoryAddr = memoryAddress;
86 mMemoryAddr = nativeOpen(mFd.getFd(), false, mClientWritable);
91 * @return Gets whether this array is mutable.
93 public boolean isWritable() {
95 return isOwner() || mClientWritable;
99 * Gets the value at a given index.
101 * @param index The index.
102 * @return The value at this index.
103 * @throws IOException If an error occurs while accessing the shared memory.
105 public int get(int index) throws IOException {
107 enforceValidIndex(index);
108 return nativeGet(mFd.getFd(), mMemoryAddr, index, isOwner());
112 * Sets the value at a given index. This method can be called only if
113 * {@link #isWritable()} returns true which means your process is the
116 * @param index The index.
117 * @param value The value to set.
118 * @throws IOException If an error occurs while accessing the shared memory.
120 public void set(int index, int value) throws IOException {
123 enforceValidIndex(index);
124 nativeSet(mFd.getFd(), mMemoryAddr, index, value, isOwner());
128 * Gets the array size.
130 * @throws IOException If an error occurs while accessing the shared memory.
132 public int size() throws IOException {
134 return nativeSize(mFd.getFd());
138 * Closes the array releasing resources.
140 * @throws IOException If an error occurs while accessing the shared memory.
143 public void close() throws IOException {
145 nativeClose(mFd.getFd(), mMemoryAddr, isOwner());
151 * @return Whether this array is closed and shouldn't be used.
153 public boolean isClosed() {
158 protected void finalize() throws Throwable {
164 public int describeContents() {
165 return CONTENTS_FILE_DESCRIPTOR;
169 public void writeToParcel(Parcel parcel, int flags) {
170 parcel.writeInt(mOwnerPid);
171 parcel.writeInt(mClientWritable ? 1 : 0);
172 parcel.writeParcelable(mFd, 0);
173 parcel.writeLong(mMemoryAddr);
177 public boolean equals(Object obj) {
184 if (getClass() != obj.getClass()) {
187 MemoryIntArray other = (MemoryIntArray) obj;
189 if (other.mFd != null) {
192 } else if (mFd.getFd() != other.mFd.getFd()) {
199 public int hashCode() {
200 return mFd != null ? mFd.hashCode() : 1;
203 private boolean isOwner() {
204 return mOwnerPid == Process.myPid();
207 private void enforceNotClosed() {
209 throw new IllegalStateException("cannot interact with a closed instance");
213 private void enforceValidIndex(int index) throws IOException {
214 final int size = size();
215 if (index < 0 || index > size - 1) {
216 throw new IndexOutOfBoundsException(
217 index + " not between 0 and " + (size - 1));
221 private void enforceWritable() {
223 throw new UnsupportedOperationException("array is not writable");
227 private native int nativeCreate(String name, int size);
228 private native long nativeOpen(int fd, boolean owner, boolean writable);
229 private native void nativeClose(int fd, long memoryAddr, boolean owner);
230 private native int nativeGet(int fd, long memoryAddr, int index, boolean owner);
231 private native void nativeSet(int fd, long memoryAddr, int index, int value, boolean owner);
232 private native int nativeSize(int fd);
233 private native static int nativeGetMemoryPageSize();
236 * @return The max array size.
238 public static int getMaxSize() {
242 public static final Parcelable.Creator<MemoryIntArray> CREATOR =
243 new Parcelable.Creator<MemoryIntArray>() {
245 public MemoryIntArray createFromParcel(Parcel parcel) {
247 return new MemoryIntArray(parcel);
248 } catch (IOException ioe) {
249 throw new RuntimeException(ioe);
254 public MemoryIntArray[] newArray(int size) {
255 return new MemoryIntArray[size];