Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.os; |
| 18 | |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
| 20 | |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 21 | /** |
| 22 | * Helper class for passing more arguments though a message |
| 23 | * and avoiding allocation of a custom class for wrapping the |
| 24 | * arguments. This class maintains a pool of instances and |
| 25 | * it is responsibility of the client to recycle and instance |
| 26 | * once it is no longer used. |
| 27 | */ |
| 28 | public final class SomeArgs { |
| 29 | |
| 30 | private static final int MAX_POOL_SIZE = 10; |
| 31 | |
| 32 | private static SomeArgs sPool; |
| 33 | private static int sPoolSize; |
| 34 | private static Object sPoolLock = new Object(); |
| 35 | |
| 36 | private SomeArgs mNext; |
| 37 | |
| 38 | private boolean mInPool; |
| 39 | |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 40 | static final int WAIT_NONE = 0; |
| 41 | static final int WAIT_WAITING = 1; |
| 42 | static final int WAIT_FINISHED = 2; |
| 43 | int mWaitState = WAIT_NONE; |
| 44 | |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 45 | @UnsupportedAppUsage |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 46 | public Object arg1; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 47 | @UnsupportedAppUsage |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 48 | public Object arg2; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 49 | @UnsupportedAppUsage |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 50 | public Object arg3; |
| 51 | public Object arg4; |
Dianne Hackborn | c4aad01 | 2013-02-22 15:05:25 -0800 | [diff] [blame] | 52 | public Object arg5; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 53 | public Object arg6; |
Filip Gruszczynski | 2217f61 | 2015-05-26 11:32:08 -0700 | [diff] [blame] | 54 | public Object arg7; |
Jorim Jaggi | a7262a8 | 2015-11-03 15:15:40 +0100 | [diff] [blame] | 55 | public Object arg8; |
Adrian Roos | 5c6b622 | 2017-11-07 17:36:10 +0100 | [diff] [blame] | 56 | public Object arg9; |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 57 | public int argi1; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 58 | @UnsupportedAppUsage |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 59 | public int argi2; |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 60 | @UnsupportedAppUsage |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 61 | public int argi3; |
| 62 | public int argi4; |
| 63 | public int argi5; |
| 64 | public int argi6; |
| 65 | |
| 66 | private SomeArgs() { |
| 67 | /* do nothing - reduce visibility */ |
| 68 | } |
| 69 | |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 70 | @UnsupportedAppUsage |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 71 | public static SomeArgs obtain() { |
| 72 | synchronized (sPoolLock) { |
| 73 | if (sPoolSize > 0) { |
| 74 | SomeArgs args = sPool; |
| 75 | sPool = sPool.mNext; |
| 76 | args.mNext = null; |
| 77 | args.mInPool = false; |
| 78 | sPoolSize--; |
| 79 | return args; |
| 80 | } else { |
| 81 | return new SomeArgs(); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Dianne Hackborn | d0a1590 | 2015-07-15 11:18:09 -0700 | [diff] [blame] | 86 | public void complete() { |
| 87 | synchronized (this) { |
| 88 | if (mWaitState != WAIT_WAITING) { |
| 89 | throw new IllegalStateException("Not waiting"); |
| 90 | } |
| 91 | mWaitState = WAIT_FINISHED; |
| 92 | notifyAll(); |
| 93 | } |
| 94 | } |
| 95 | |
Mathew Inwood | c185f08 | 2018-08-20 14:28:54 +0100 | [diff] [blame] | 96 | @UnsupportedAppUsage |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 97 | public void recycle() { |
| 98 | if (mInPool) { |
| 99 | throw new IllegalStateException("Already recycled."); |
| 100 | } |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 101 | if (mWaitState != WAIT_NONE) { |
| 102 | return; |
| 103 | } |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 104 | synchronized (sPoolLock) { |
| 105 | clear(); |
| 106 | if (sPoolSize < MAX_POOL_SIZE) { |
| 107 | mNext = sPool; |
| 108 | mInPool = true; |
| 109 | sPool = this; |
| 110 | sPoolSize++; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private void clear() { |
| 116 | arg1 = null; |
| 117 | arg2 = null; |
| 118 | arg3 = null; |
| 119 | arg4 = null; |
Dianne Hackborn | c4aad01 | 2013-02-22 15:05:25 -0800 | [diff] [blame] | 120 | arg5 = null; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 121 | arg6 = null; |
Filip Gruszczynski | 2217f61 | 2015-05-26 11:32:08 -0700 | [diff] [blame] | 122 | arg7 = null; |
Bin Chen | 5f0521e | 2019-02-20 17:34:04 +0800 | [diff] [blame] | 123 | arg8 = null; |
| 124 | arg9 = null; |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 125 | argi1 = 0; |
| 126 | argi2 = 0; |
| 127 | argi3 = 0; |
| 128 | argi4 = 0; |
| 129 | argi5 = 0; |
| 130 | argi6 = 0; |
| 131 | } |
| 132 | } |