blob: 2de22fbacf4b5b99da87a91e78d2a65e5b83784e [file] [log] [blame]
Lv Zheng6d33b6b2012-10-31 02:25:05 +00001/******************************************************************************
2 *
3 * Module Name: utcache - local cache allocation routines
4 *
5 *****************************************************************************/
6
7/*
Bob Moore25f044e2013-01-25 05:38:56 +00008 * Copyright (C) 2000 - 2013, Intel Corp.
Lv Zheng6d33b6b2012-10-31 02:25:05 +00009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44#include <acpi/acpi.h>
45#include "accommon.h"
46
47#define _COMPONENT ACPI_UTILITIES
48ACPI_MODULE_NAME("utcache")
49
50#ifdef ACPI_USE_LOCAL_CACHE
51/*******************************************************************************
52 *
53 * FUNCTION: acpi_os_create_cache
54 *
55 * PARAMETERS: cache_name - Ascii name for the cache
56 * object_size - Size of each cached object
57 * max_depth - Maximum depth of the cache (in objects)
58 * return_cache - Where the new cache object is returned
59 *
60 * RETURN: Status
61 *
62 * DESCRIPTION: Create a cache object
63 *
64 ******************************************************************************/
65acpi_status
66acpi_os_create_cache(char *cache_name,
67 u16 object_size,
68 u16 max_depth, struct acpi_memory_list ** return_cache)
69{
70 struct acpi_memory_list *cache;
71
72 ACPI_FUNCTION_ENTRY();
73
74 if (!cache_name || !return_cache || (object_size < 16)) {
75 return (AE_BAD_PARAMETER);
76 }
77
78 /* Create the cache object */
79
80 cache = acpi_os_allocate(sizeof(struct acpi_memory_list));
81 if (!cache) {
82 return (AE_NO_MEMORY);
83 }
84
85 /* Populate the cache object and return it */
86
87 ACPI_MEMSET(cache, 0, sizeof(struct acpi_memory_list));
Lv Zheng6d33b6b2012-10-31 02:25:05 +000088 cache->list_name = cache_name;
89 cache->object_size = object_size;
90 cache->max_depth = max_depth;
91
92 *return_cache = cache;
93 return (AE_OK);
94}
95
96/*******************************************************************************
97 *
98 * FUNCTION: acpi_os_purge_cache
99 *
100 * PARAMETERS: cache - Handle to cache object
101 *
102 * RETURN: Status
103 *
104 * DESCRIPTION: Free all objects within the requested cache.
105 *
106 ******************************************************************************/
107
108acpi_status acpi_os_purge_cache(struct acpi_memory_list * cache)
109{
Jung-uk Kim3cf24492013-03-08 09:21:02 +0000110 void *next;
Lv Zheng6d33b6b2012-10-31 02:25:05 +0000111 acpi_status status;
112
113 ACPI_FUNCTION_ENTRY();
114
115 if (!cache) {
116 return (AE_BAD_PARAMETER);
117 }
118
119 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
120 if (ACPI_FAILURE(status)) {
121 return (status);
122 }
123
124 /* Walk the list of objects in this cache */
125
126 while (cache->list_head) {
127
128 /* Delete and unlink one cached state object */
129
Jung-uk Kim3cf24492013-03-08 09:21:02 +0000130 next =
131 ((struct acpi_object_common *)cache->list_head)->
132 next_object;
Lv Zheng6d33b6b2012-10-31 02:25:05 +0000133 ACPI_FREE(cache->list_head);
134
135 cache->list_head = next;
136 cache->current_depth--;
137 }
138
139 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
140 return (AE_OK);
141}
142
143/*******************************************************************************
144 *
145 * FUNCTION: acpi_os_delete_cache
146 *
147 * PARAMETERS: cache - Handle to cache object
148 *
149 * RETURN: Status
150 *
151 * DESCRIPTION: Free all objects within the requested cache and delete the
152 * cache object.
153 *
154 ******************************************************************************/
155
156acpi_status acpi_os_delete_cache(struct acpi_memory_list * cache)
157{
158 acpi_status status;
159
160 ACPI_FUNCTION_ENTRY();
161
162 /* Purge all objects in the cache */
163
164 status = acpi_os_purge_cache(cache);
165 if (ACPI_FAILURE(status)) {
166 return (status);
167 }
168
169 /* Now we can delete the cache object */
170
171 acpi_os_free(cache);
172 return (AE_OK);
173}
174
175/*******************************************************************************
176 *
177 * FUNCTION: acpi_os_release_object
178 *
179 * PARAMETERS: cache - Handle to cache object
180 * object - The object to be released
181 *
182 * RETURN: None
183 *
Bob Moore73a30902012-10-31 02:26:55 +0000184 * DESCRIPTION: Release an object to the specified cache. If cache is full,
Lv Zheng6d33b6b2012-10-31 02:25:05 +0000185 * the object is deleted.
186 *
187 ******************************************************************************/
188
189acpi_status
190acpi_os_release_object(struct acpi_memory_list * cache, void *object)
191{
192 acpi_status status;
193
194 ACPI_FUNCTION_ENTRY();
195
196 if (!cache || !object) {
197 return (AE_BAD_PARAMETER);
198 }
199
200 /* If cache is full, just free this object */
201
202 if (cache->current_depth >= cache->max_depth) {
203 ACPI_FREE(object);
204 ACPI_MEM_TRACKING(cache->total_freed++);
205 }
206
207 /* Otherwise put this object back into the cache */
208
209 else {
210 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
211 if (ACPI_FAILURE(status)) {
212 return (status);
213 }
214
215 /* Mark the object as cached */
216
217 ACPI_MEMSET(object, 0xCA, cache->object_size);
218 ACPI_SET_DESCRIPTOR_TYPE(object, ACPI_DESC_TYPE_CACHED);
219
220 /* Put the object at the head of the cache list */
221
Jung-uk Kim3cf24492013-03-08 09:21:02 +0000222 ((struct acpi_object_common *)object)->next_object =
Lv Zheng6d33b6b2012-10-31 02:25:05 +0000223 cache->list_head;
224 cache->list_head = object;
225 cache->current_depth++;
226
227 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
228 }
229
230 return (AE_OK);
231}
232
233/*******************************************************************************
234 *
235 * FUNCTION: acpi_os_acquire_object
236 *
237 * PARAMETERS: cache - Handle to cache object
238 *
Bob Moore73a30902012-10-31 02:26:55 +0000239 * RETURN: the acquired object. NULL on error
Lv Zheng6d33b6b2012-10-31 02:25:05 +0000240 *
Bob Moore73a30902012-10-31 02:26:55 +0000241 * DESCRIPTION: Get an object from the specified cache. If cache is empty,
Lv Zheng6d33b6b2012-10-31 02:25:05 +0000242 * the object is allocated.
243 *
244 ******************************************************************************/
245
246void *acpi_os_acquire_object(struct acpi_memory_list *cache)
247{
248 acpi_status status;
249 void *object;
250
251 ACPI_FUNCTION_NAME(os_acquire_object);
252
253 if (!cache) {
254 return (NULL);
255 }
256
257 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
258 if (ACPI_FAILURE(status)) {
259 return (NULL);
260 }
261
262 ACPI_MEM_TRACKING(cache->requests++);
263
264 /* Check the cache first */
265
266 if (cache->list_head) {
267
268 /* There is an object available, use it */
269
270 object = cache->list_head;
Jung-uk Kim3cf24492013-03-08 09:21:02 +0000271 cache->list_head =
272 ((struct acpi_object_common *)object)->next_object;
Lv Zheng6d33b6b2012-10-31 02:25:05 +0000273
274 cache->current_depth--;
275
276 ACPI_MEM_TRACKING(cache->hits++);
277 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
278 "Object %p from %s cache\n", object,
279 cache->list_name));
280
281 status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
282 if (ACPI_FAILURE(status)) {
283 return (NULL);
284 }
285
286 /* Clear (zero) the previously used Object */
287
288 ACPI_MEMSET(object, 0, cache->object_size);
289 } else {
290 /* The cache is empty, create a new object */
291
292 ACPI_MEM_TRACKING(cache->total_allocated++);
293
294#ifdef ACPI_DBG_TRACK_ALLOCATIONS
295 if ((cache->total_allocated - cache->total_freed) >
296 cache->max_occupied) {
297 cache->max_occupied =
298 cache->total_allocated - cache->total_freed;
299 }
300#endif
301
302 /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
303
304 status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
305 if (ACPI_FAILURE(status)) {
306 return (NULL);
307 }
308
309 object = ACPI_ALLOCATE_ZEROED(cache->object_size);
310 if (!object) {
311 return (NULL);
312 }
313 }
314
315 return (object);
316}
317#endif /* ACPI_USE_LOCAL_CACHE */