1758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov/* 2758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * Copyright (C) 2012 The Android Open Source Project 3758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * 4758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * Licensed under the Apache License, Version 2.0 (the "License"); 5758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * you may not use this file except in compliance with the License. 6758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * You may obtain a copy of the License at 7758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * 8758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * http://www.apache.org/licenses/LICENSE-2.0 9758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * 10758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * Unless required by applicable law or agreed to in writing, software 11758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * distributed under the License is distributed on an "AS IS" BASIS, 12758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * See the License for the specific language governing permissions and 14758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * limitations under the License. 15758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov */ 16758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov 17758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganovpackage com.android.internal.os; 18758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov 19758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov/** 20758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * Helper class for passing more arguments though a message 21758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * and avoiding allocation of a custom class for wrapping the 22758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * arguments. This class maintains a pool of instances and 23758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * it is responsibility of the client to recycle and instance 24758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * once it is no longer used. 25758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov */ 26758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganovpublic final class SomeArgs { 27758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov 28758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov private static final int MAX_POOL_SIZE = 10; 29758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov 30758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov private static SomeArgs sPool; 31758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov private static int sPoolSize; 32758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov private static Object sPoolLock = new Object(); 33758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov 34758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov private SomeArgs mNext; 35758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov 36758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov private boolean mInPool; 37758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov 3891097de49b0f683b00e26a75dbc0ac6082344137Dianne Hackborn static final int WAIT_NONE = 0; 3991097de49b0f683b00e26a75dbc0ac6082344137Dianne Hackborn static final int WAIT_WAITING = 1; 4091097de49b0f683b00e26a75dbc0ac6082344137Dianne Hackborn static final int WAIT_FINISHED = 2; 4191097de49b0f683b00e26a75dbc0ac6082344137Dianne Hackborn int mWaitState = WAIT_NONE; 4291097de49b0f683b00e26a75dbc0ac6082344137Dianne Hackborn 43758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov public Object arg1; 44758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov public Object arg2; 45758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov public Object arg3; 46758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov public Object arg4; 47c4aad01cbbb69c916ef323693e1fd0560b0eccbaDianne Hackborn public Object arg5; 48a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav public Object arg6; 49758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov public int argi1; 50758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov public int argi2; 51758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov public int argi3; 52758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov public int argi4; 53758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov public int argi5; 54758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov public int argi6; 55758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov 56758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov private SomeArgs() { 57758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov /* do nothing - reduce visibility */ 58758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov } 59758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov 60758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov public static SomeArgs obtain() { 61758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov synchronized (sPoolLock) { 62758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov if (sPoolSize > 0) { 63758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov SomeArgs args = sPool; 64758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov sPool = sPool.mNext; 65758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov args.mNext = null; 66758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov args.mInPool = false; 67758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov sPoolSize--; 68758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov return args; 69758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov } else { 70758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov return new SomeArgs(); 71758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov } 72758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov } 73758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov } 74758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov 75758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov public void recycle() { 76758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov if (mInPool) { 77758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov throw new IllegalStateException("Already recycled."); 78758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov } 7991097de49b0f683b00e26a75dbc0ac6082344137Dianne Hackborn if (mWaitState != WAIT_NONE) { 8091097de49b0f683b00e26a75dbc0ac6082344137Dianne Hackborn return; 8191097de49b0f683b00e26a75dbc0ac6082344137Dianne Hackborn } 82758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov synchronized (sPoolLock) { 83758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov clear(); 84758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov if (sPoolSize < MAX_POOL_SIZE) { 85758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov mNext = sPool; 86758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov mInPool = true; 87758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov sPool = this; 88758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov sPoolSize++; 89758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov } 90758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov } 91758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov } 92758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov 93758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov private void clear() { 94758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov arg1 = null; 95758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov arg2 = null; 96758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov arg3 = null; 97758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov arg4 = null; 98c4aad01cbbb69c916ef323693e1fd0560b0eccbaDianne Hackborn arg5 = null; 99a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav arg6 = null; 100758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov argi1 = 0; 101758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov argi2 = 0; 102758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov argi3 = 0; 103758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov argi4 = 0; 104758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov argi5 = 0; 105758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov argi6 = 0; 106758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov } 107758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov} 108