blob: 93d48681d2764c4fe5f404aa24320f35a1e9a29d [file] [log] [blame]
Robert Moore73459f72005-06-24 00:00:00 -04001/******************************************************************************
2 *
3 * Module Name: utcache - local cache allocation routines
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2005, R. Byron Moore
9 * 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
Robert Moore73459f72005-06-24 00:00:00 -040044#include <acpi/acpi.h>
45
46#define _COMPONENT ACPI_UTILITIES
Len Brown4be44fc2005-08-05 00:44:28 -040047ACPI_MODULE_NAME("utcache")
Robert Moore73459f72005-06-24 00:00:00 -040048
49#ifdef ACPI_USE_LOCAL_CACHE
50/*******************************************************************************
51 *
52 * FUNCTION: acpi_os_create_cache
53 *
54 * PARAMETERS: cache_name - Ascii name for the cache
55 * object_size - Size of each cached object
56 * max_depth - Maximum depth of the cache (in objects)
57 * return_cache - Where the new cache object is returned
58 *
59 * RETURN: Status
60 *
61 * DESCRIPTION: Create a cache object
62 *
63 ******************************************************************************/
Robert Moore73459f72005-06-24 00:00:00 -040064acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040065acpi_os_create_cache(char *cache_name,
66 u16 object_size,
67 u16 max_depth, struct acpi_memory_list **return_cache)
Robert Moore73459f72005-06-24 00:00:00 -040068{
Len Brown4be44fc2005-08-05 00:44:28 -040069 struct acpi_memory_list *cache;
Robert Moore73459f72005-06-24 00:00:00 -040070
Len Brown4be44fc2005-08-05 00:44:28 -040071 ACPI_FUNCTION_ENTRY();
Robert Mooref9f46012005-07-08 00:00:00 -040072
Robert Moore73459f72005-06-24 00:00:00 -040073 if (!cache_name || !return_cache || (object_size < 16)) {
74 return (AE_BAD_PARAMETER);
75 }
76
77 /* Create the cache object */
78
Len Brown4be44fc2005-08-05 00:44:28 -040079 cache = acpi_os_allocate(sizeof(struct acpi_memory_list));
Robert Moore73459f72005-06-24 00:00:00 -040080 if (!cache) {
81 return (AE_NO_MEMORY);
82 }
83
84 /* Populate the cache object and return it */
85
Len Brown4be44fc2005-08-05 00:44:28 -040086 ACPI_MEMSET(cache, 0, sizeof(struct acpi_memory_list));
Robert Moore73459f72005-06-24 00:00:00 -040087 cache->link_offset = 8;
Len Brown4be44fc2005-08-05 00:44:28 -040088 cache->list_name = cache_name;
Robert Moore73459f72005-06-24 00:00:00 -040089 cache->object_size = object_size;
Len Brown4be44fc2005-08-05 00:44:28 -040090 cache->max_depth = max_depth;
Robert Moore73459f72005-06-24 00:00:00 -040091
92 *return_cache = cache;
93 return (AE_OK);
94}
95
Robert Moore73459f72005-06-24 00:00:00 -040096/*******************************************************************************
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
Len Brown4be44fc2005-08-05 00:44:28 -0400108acpi_status acpi_os_purge_cache(struct acpi_memory_list * cache)
Robert Moore73459f72005-06-24 00:00:00 -0400109{
Len Brown4be44fc2005-08-05 00:44:28 -0400110 char *next;
Robert Moore73459f72005-06-24 00:00:00 -0400111
Len Brown4be44fc2005-08-05 00:44:28 -0400112 ACPI_FUNCTION_ENTRY();
Robert Moore73459f72005-06-24 00:00:00 -0400113
114 if (!cache) {
115 return (AE_BAD_PARAMETER);
116 }
117
118 /* Walk the list of objects in this cache */
119
120 while (cache->list_head) {
121 /* Delete and unlink one cached state object */
122
Len Brown4be44fc2005-08-05 00:44:28 -0400123 next = *(ACPI_CAST_INDIRECT_PTR(char,
124 &(((char *)cache->
125 list_head)[cache->
126 link_offset])));
127 ACPI_MEM_FREE(cache->list_head);
Robert Moore73459f72005-06-24 00:00:00 -0400128
129 cache->list_head = next;
130 cache->current_depth--;
131 }
132
133 return (AE_OK);
134}
135
Robert Moore73459f72005-06-24 00:00:00 -0400136/*******************************************************************************
137 *
138 * FUNCTION: acpi_os_delete_cache
139 *
140 * PARAMETERS: Cache - Handle to cache object
141 *
142 * RETURN: Status
143 *
144 * DESCRIPTION: Free all objects within the requested cache and delete the
145 * cache object.
146 *
147 ******************************************************************************/
148
Len Brown4be44fc2005-08-05 00:44:28 -0400149acpi_status acpi_os_delete_cache(struct acpi_memory_list * cache)
Robert Moore73459f72005-06-24 00:00:00 -0400150{
Len Brown4be44fc2005-08-05 00:44:28 -0400151 acpi_status status;
Robert Moore73459f72005-06-24 00:00:00 -0400152
Len Brown4be44fc2005-08-05 00:44:28 -0400153 ACPI_FUNCTION_ENTRY();
Robert Moore73459f72005-06-24 00:00:00 -0400154
Len Brown4be44fc2005-08-05 00:44:28 -0400155 /* Purge all objects in the cache */
Robert Mooref9f46012005-07-08 00:00:00 -0400156
Len Brown4be44fc2005-08-05 00:44:28 -0400157 status = acpi_os_purge_cache(cache);
158 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -0400159 return (status);
160 }
161
162 /* Now we can delete the cache object */
163
Len Brown4be44fc2005-08-05 00:44:28 -0400164 acpi_os_free(cache);
Robert Moore73459f72005-06-24 00:00:00 -0400165 return (AE_OK);
166}
167
Robert Moore73459f72005-06-24 00:00:00 -0400168/*******************************************************************************
169 *
170 * FUNCTION: acpi_os_release_object
171 *
172 * PARAMETERS: Cache - Handle to cache object
173 * Object - The object to be released
174 *
175 * RETURN: None
176 *
177 * DESCRIPTION: Release an object to the specified cache. If cache is full,
178 * the object is deleted.
179 *
180 ******************************************************************************/
181
182acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400183acpi_os_release_object(struct acpi_memory_list * cache, void *object)
Robert Moore73459f72005-06-24 00:00:00 -0400184{
Len Brown4be44fc2005-08-05 00:44:28 -0400185 acpi_status status;
Robert Moore73459f72005-06-24 00:00:00 -0400186
Len Brown4be44fc2005-08-05 00:44:28 -0400187 ACPI_FUNCTION_ENTRY();
Robert Moore73459f72005-06-24 00:00:00 -0400188
189 if (!cache || !object) {
190 return (AE_BAD_PARAMETER);
191 }
192
193 /* If cache is full, just free this object */
194
195 if (cache->current_depth >= cache->max_depth) {
Len Brown4be44fc2005-08-05 00:44:28 -0400196 ACPI_MEM_FREE(object);
197 ACPI_MEM_TRACKING(cache->total_freed++);
Robert Moore73459f72005-06-24 00:00:00 -0400198 }
199
200 /* Otherwise put this object back into the cache */
201
202 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400203 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
204 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -0400205 return (status);
206 }
207
208 /* Mark the object as cached */
209
Len Brown4be44fc2005-08-05 00:44:28 -0400210 ACPI_MEMSET(object, 0xCA, cache->object_size);
211 ACPI_SET_DESCRIPTOR_TYPE(object, ACPI_DESC_TYPE_CACHED);
Robert Moore73459f72005-06-24 00:00:00 -0400212
213 /* Put the object at the head of the cache list */
214
Len Brown4be44fc2005-08-05 00:44:28 -0400215 *(ACPI_CAST_INDIRECT_PTR(char,
216 &(((char *)object)[cache->
217 link_offset]))) =
218 cache->list_head;
Robert Moore73459f72005-06-24 00:00:00 -0400219 cache->list_head = object;
220 cache->current_depth++;
221
Len Brown4be44fc2005-08-05 00:44:28 -0400222 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
Robert Moore73459f72005-06-24 00:00:00 -0400223 }
224
225 return (AE_OK);
226}
227
Robert Moore73459f72005-06-24 00:00:00 -0400228/*******************************************************************************
229 *
230 * FUNCTION: acpi_os_acquire_object
231 *
232 * PARAMETERS: Cache - Handle to cache object
233 *
234 * RETURN: the acquired object. NULL on error
235 *
236 * DESCRIPTION: Get an object from the specified cache. If cache is empty,
237 * the object is allocated.
238 *
239 ******************************************************************************/
240
Len Brown4be44fc2005-08-05 00:44:28 -0400241void *acpi_os_acquire_object(struct acpi_memory_list *cache)
Robert Moore73459f72005-06-24 00:00:00 -0400242{
Len Brown4be44fc2005-08-05 00:44:28 -0400243 acpi_status status;
244 void *object;
Robert Moore73459f72005-06-24 00:00:00 -0400245
Len Brown4be44fc2005-08-05 00:44:28 -0400246 ACPI_FUNCTION_NAME("os_acquire_object");
Robert Moore73459f72005-06-24 00:00:00 -0400247
248 if (!cache) {
249 return (NULL);
250 }
251
Len Brown4be44fc2005-08-05 00:44:28 -0400252 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
253 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -0400254 return (NULL);
255 }
256
Len Brown4be44fc2005-08-05 00:44:28 -0400257 ACPI_MEM_TRACKING(cache->requests++);
Robert Moore73459f72005-06-24 00:00:00 -0400258
259 /* Check the cache first */
260
261 if (cache->list_head) {
262 /* There is an object available, use it */
263
264 object = cache->list_head;
Len Brown4be44fc2005-08-05 00:44:28 -0400265 cache->list_head = *(ACPI_CAST_INDIRECT_PTR(char,
266 &(((char *)
267 object)[cache->
268 link_offset])));
Robert Moore73459f72005-06-24 00:00:00 -0400269
270 cache->current_depth--;
271
Len Brown4be44fc2005-08-05 00:44:28 -0400272 ACPI_MEM_TRACKING(cache->hits++);
273 ACPI_MEM_TRACKING(ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
274 "Object %p from %s cache\n",
275 object, cache->list_name)));
Robert Moore73459f72005-06-24 00:00:00 -0400276
Len Brown4be44fc2005-08-05 00:44:28 -0400277 status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
278 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -0400279 return (NULL);
280 }
281
282 /* Clear (zero) the previously used Object */
283
Len Brown4be44fc2005-08-05 00:44:28 -0400284 ACPI_MEMSET(object, 0, cache->object_size);
285 } else {
Robert Moore73459f72005-06-24 00:00:00 -0400286 /* The cache is empty, create a new object */
287
Len Brown4be44fc2005-08-05 00:44:28 -0400288 ACPI_MEM_TRACKING(cache->total_allocated++);
Robert Moore73459f72005-06-24 00:00:00 -0400289
290 /* Avoid deadlock with ACPI_MEM_CALLOCATE */
291
Len Brown4be44fc2005-08-05 00:44:28 -0400292 status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
293 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -0400294 return (NULL);
295 }
296
Len Brown4be44fc2005-08-05 00:44:28 -0400297 object = ACPI_MEM_CALLOCATE(cache->object_size);
Robert Moore73459f72005-06-24 00:00:00 -0400298 if (!object) {
299 return (NULL);
300 }
301 }
302
303 return (object);
304}
Len Brown4be44fc2005-08-05 00:44:28 -0400305#endif /* ACPI_USE_LOCAL_CACHE */