|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | - |
17 | 16 | #ifndef __SESSIONMAP_H__ |
18 | 17 | #define __SESSIONMAP_H__ |
19 | 18 |
|
20 | 19 | #include <utils/KeyedVector.h> |
| 20 | +#include <utils/threads.h> |
21 | 21 |
|
22 | 22 | namespace android { |
23 | 23 |
|
24 | 24 | /** |
25 | | - * A wrapper template class for handling DRM Engine sessions. |
| 25 | + * A thread safe wrapper template class for session handlings for Drm Engines. It wraps a |
| 26 | + * pointer type over KeyedVector. It keeps pointer as data in the vector and free up memory |
| 27 | + * allocated pointer can be of any type of structure/class meant for keeping session data. |
| 28 | + * so session object here means pointer to the session data. |
26 | 29 | */ |
27 | | -template <typename NODE> |
| 30 | +template <typename TValue> |
28 | 31 | class SessionMap { |
29 | 32 |
|
30 | 33 | public: |
31 | | - KeyedVector<int, NODE> map; |
32 | | - |
33 | 34 | SessionMap() {} |
34 | 35 |
|
35 | 36 | virtual ~SessionMap() { |
| 37 | + Mutex::Autolock lock(mLock); |
36 | 38 | destroyMap(); |
37 | 39 | } |
38 | 40 |
|
39 | | -/** |
40 | | - * Adds a new value in the session map table. It expects memory to be allocated already |
41 | | - * for the session object |
42 | | - * |
43 | | - * @param key - key or Session ID |
44 | | - * @param value - session object to add |
45 | | - * |
46 | | - * @return boolean result of adding value. returns false if key is already exist. |
47 | | - */ |
48 | | -bool addValue(int key, NODE value) { |
49 | | - bool result = false; |
50 | | - |
51 | | - if (!isCreated(key)) { |
52 | | - map.add(key, value); |
53 | | - result = true; |
| 41 | + /** |
| 42 | + * Adds a new value in the session map table. It expects memory to be allocated already |
| 43 | + * for the session object |
| 44 | + * |
| 45 | + * @param key - key or Session ID |
| 46 | + * @param value - session object to add |
| 47 | + * |
| 48 | + * @return boolean result of adding value. returns false if key is already exist. |
| 49 | + */ |
| 50 | + bool addValue(int key, TValue value) { |
| 51 | + Mutex::Autolock lock(mLock); |
| 52 | + if (!isCreatedInternal(key)) { |
| 53 | + map.add(key, value); |
| 54 | + return true; |
| 55 | + } |
| 56 | + return false; |
54 | 57 | } |
55 | 58 |
|
56 | | - return result; |
57 | | -} |
58 | | - |
59 | | - |
60 | | -/** |
61 | | - * returns the session object by the key |
62 | | - * |
63 | | - * @param key - key or Session ID |
64 | | - * |
65 | | - * @return session object as per the key |
66 | | - */ |
67 | | -NODE getValue(int key) { |
68 | | - NODE value = NULL; |
69 | | - |
70 | | - if (isCreated(key)) { |
71 | | - value = (NODE) map.valueFor(key); |
| 59 | + /** |
| 60 | + * returns the session object by the key |
| 61 | + * |
| 62 | + * @param key - key or Session ID |
| 63 | + * |
| 64 | + * @return session object as per the key |
| 65 | + */ |
| 66 | + TValue getValue(int key) { |
| 67 | + Mutex::Autolock lock(mLock); |
| 68 | + return getValueInternal(key); |
72 | 69 | } |
73 | 70 |
|
74 | | - return value; |
75 | | -} |
76 | | - |
77 | | -/** |
78 | | - * returns the number of objects in the session map table |
79 | | - * |
80 | | - * @return count of number of session objects. |
81 | | - */ |
82 | | -int getSize() { |
83 | | - return map.size(); |
84 | | -} |
85 | | - |
86 | | -/** |
87 | | - * returns the session object by the index in the session map table |
88 | | - * |
89 | | - * @param index - index of the value required |
90 | | - * |
91 | | - * @return session object as per the index |
92 | | - */ |
93 | | -NODE getValueAt(unsigned int index) { |
94 | | - NODE value = NULL; |
| 71 | + /** |
| 72 | + * returns the number of objects in the session map table |
| 73 | + * |
| 74 | + * @return count of number of session objects. |
| 75 | + */ |
| 76 | + int getSize() { |
| 77 | + Mutex::Autolock lock(mLock); |
| 78 | + return map.size(); |
| 79 | + } |
95 | 80 |
|
96 | | - if (map.size() > index) { |
97 | | - value = map.valueAt(index); |
| 81 | + /** |
| 82 | + * returns the session object by the index in the session map table |
| 83 | + * |
| 84 | + * @param index - index of the value required |
| 85 | + * |
| 86 | + * @return session object as per the index |
| 87 | + */ |
| 88 | + TValue getValueAt(unsigned int index) { |
| 89 | + TValue value = NULL; |
| 90 | + Mutex::Autolock lock(mLock); |
| 91 | + |
| 92 | + if (map.size() > index) { |
| 93 | + value = map.valueAt(index); |
| 94 | + } |
| 95 | + return value; |
98 | 96 | } |
99 | 97 |
|
100 | | - return value; |
101 | | -} |
| 98 | + /** |
| 99 | + * deletes the object from session map. It also frees up memory for the session object. |
| 100 | + * |
| 101 | + * @param key - key of the value to be deleted |
| 102 | + * |
| 103 | + */ |
| 104 | + void removeValue(int key) { |
| 105 | + Mutex::Autolock lock(mLock); |
| 106 | + deleteValue(getValueInternal(key)); |
| 107 | + map.removeItem(key); |
| 108 | + } |
102 | 109 |
|
103 | | -/** |
104 | | - * deletes the object from session map. It also frees up memory for the session object. |
105 | | - * |
106 | | - * @param key - key of the value to be deleted |
107 | | - * |
108 | | - */ |
109 | | -void removeValue(int key) { |
110 | | - deleteValue(getValue(key)); |
111 | | - map.removeItem(key); |
112 | | -} |
| 110 | + /** |
| 111 | + * decides if session is already created. |
| 112 | + * |
| 113 | + * @param key - key of the value for the session |
| 114 | + * |
| 115 | + * @return boolean result of whether session is created |
| 116 | + */ |
| 117 | + bool isCreated(int key) { |
| 118 | + Mutex::Autolock lock(mLock); |
| 119 | + return isCreatedInternal(key); |
| 120 | + } |
113 | 121 |
|
114 | | -/** |
115 | | - * decides if session is already created. |
116 | | - * |
117 | | - * @param key - key of the value for the session |
118 | | - * |
119 | | - * @return boolean result of whether session is created |
120 | | - */ |
121 | | -bool isCreated(int key) { |
122 | | - return (0 <= map.indexOfKey(key)); |
123 | | -} |
| 122 | + SessionMap<TValue> & operator=(const SessionMap<TValue> & objectCopy) { |
| 123 | + Mutex::Autolock lock(mLock); |
124 | 124 |
|
125 | | -/** |
126 | | - * empty the entire session table. It releases all the memory for session objects. |
127 | | - */ |
128 | | -void destroyMap() { |
129 | | - int size = map.size(); |
130 | | - int i = 0; |
| 125 | + destroyMap(); |
| 126 | + map = objectCopy.map; |
| 127 | + return *this; |
| 128 | + } |
131 | 129 |
|
132 | | - for (i = 0; i < size; i++) { |
133 | | - deleteValue(map.valueAt(i)); |
| 130 | +private: |
| 131 | + KeyedVector<int, TValue> map; |
| 132 | + Mutex mLock; |
| 133 | + |
| 134 | + /** |
| 135 | + * free up the memory for the session object. |
| 136 | + * Make sure if any reference to the session object anywhere, otherwise it will be a |
| 137 | + * dangle pointer after this call. |
| 138 | + * |
| 139 | + * @param value - session object to free |
| 140 | + * |
| 141 | + */ |
| 142 | + void deleteValue(TValue value) { |
| 143 | + delete value; |
134 | 144 | } |
135 | 145 |
|
136 | | - map.clear(); |
137 | | -} |
| 146 | + /** |
| 147 | + * free up the memory for the entire map. |
| 148 | + * free up any resources in the sessions before calling this funtion. |
| 149 | + * |
| 150 | + */ |
| 151 | + void destroyMap() { |
| 152 | + int size = map.size(); |
| 153 | + |
| 154 | + for (int i = 0; i < size; i++) { |
| 155 | + deleteValue(map.valueAt(i)); |
| 156 | + } |
| 157 | + map.clear(); |
| 158 | + } |
138 | 159 |
|
139 | | -/** |
140 | | - * free up the memory for the session object. |
141 | | - * Make sure if any reference to the session object anywhere, otherwise it will be a |
142 | | - * dangle pointer after this call. |
143 | | - * |
144 | | - * @param value - session object to free |
145 | | - * |
146 | | - */ |
147 | | -void deleteValue(NODE value) { |
148 | | - delete value; |
149 | | -} |
| 160 | + /** |
| 161 | + * decides if session is already created. |
| 162 | + * |
| 163 | + * @param key - key of the value for the session |
| 164 | + * |
| 165 | + * @return boolean result of whether session is created |
| 166 | + */ |
| 167 | + bool isCreatedInternal(int key) { |
| 168 | + return(0 <= map.indexOfKey(key)); |
| 169 | + } |
150 | 170 |
|
| 171 | + /** |
| 172 | + * returns the session object by the key |
| 173 | + * |
| 174 | + * @param key - key or Session ID |
| 175 | + * |
| 176 | + * @return session object as per the key |
| 177 | + */ |
| 178 | + TValue getValueInternal(int key) { |
| 179 | + TValue value = NULL; |
| 180 | + if (isCreatedInternal(key)) { |
| 181 | + value = (TValue) map.valueFor(key); |
| 182 | + } |
| 183 | + return value; |
| 184 | + } |
151 | 185 | }; |
152 | 186 |
|
153 | 187 | }; |
|
0 commit comments