1 /******************************************************************************
3 * Module Name: utcache - local cache allocation routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2005, R. Byron Moore
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
45 #include <acpi/acpi.h>
47 #define _COMPONENT ACPI_UTILITIES
48 ACPI_MODULE_NAME ("utcache")
51 #ifdef ACPI_USE_LOCAL_CACHE
52 /*******************************************************************************
54 * FUNCTION: acpi_os_create_cache
56 * PARAMETERS: cache_name - Ascii name for the cache
57 * object_size - Size of each cached object
58 * max_depth - Maximum depth of the cache (in objects)
59 * return_cache - Where the new cache object is returned
63 * DESCRIPTION: Create a cache object
65 ******************************************************************************/
68 acpi_os_create_cache (
72 struct acpi_memory_list **return_cache)
74 struct acpi_memory_list *cache;
77 if (!cache_name || !return_cache || (object_size < 16)) {
78 return (AE_BAD_PARAMETER);
81 /* Create the cache object */
83 cache = acpi_os_allocate (sizeof (struct acpi_memory_list));
85 return (AE_NO_MEMORY);
88 /* Populate the cache object and return it */
90 ACPI_MEMSET (cache, 0, sizeof (struct acpi_memory_list));
91 cache->link_offset = 8;
92 cache->list_name = cache_name;
93 cache->object_size = object_size;
94 cache->max_depth = max_depth;
96 *return_cache = cache;
101 /*******************************************************************************
103 * FUNCTION: acpi_os_purge_cache
105 * PARAMETERS: Cache - Handle to cache object
109 * DESCRIPTION: Free all objects within the requested cache.
111 ******************************************************************************/
114 acpi_os_purge_cache (
115 struct acpi_memory_list *cache)
120 ACPI_FUNCTION_ENTRY ();
124 return (AE_BAD_PARAMETER);
127 /* Walk the list of objects in this cache */
129 while (cache->list_head) {
130 /* Delete and unlink one cached state object */
132 next = *(ACPI_CAST_INDIRECT_PTR (char,
133 &(((char *) cache->list_head)[cache->link_offset])));
134 ACPI_MEM_FREE (cache->list_head);
136 cache->list_head = next;
137 cache->current_depth--;
144 /*******************************************************************************
146 * FUNCTION: acpi_os_delete_cache
148 * PARAMETERS: Cache - Handle to cache object
152 * DESCRIPTION: Free all objects within the requested cache and delete the
155 ******************************************************************************/
158 acpi_os_delete_cache (
159 struct acpi_memory_list *cache)
164 /* Purge all objects in the cache */
166 status = acpi_os_purge_cache (cache);
167 if (ACPI_FAILURE (status)) {
171 /* Now we can delete the cache object */
173 acpi_os_free (cache);
178 /*******************************************************************************
180 * FUNCTION: acpi_os_release_object
182 * PARAMETERS: Cache - Handle to cache object
183 * Object - The object to be released
187 * DESCRIPTION: Release an object to the specified cache. If cache is full,
188 * the object is deleted.
190 ******************************************************************************/
193 acpi_os_release_object (
194 struct acpi_memory_list *cache,
200 ACPI_FUNCTION_ENTRY ();
203 if (!cache || !object) {
204 return (AE_BAD_PARAMETER);
207 /* If cache is full, just free this object */
209 if (cache->current_depth >= cache->max_depth) {
210 ACPI_MEM_FREE (object);
211 ACPI_MEM_TRACKING (cache->total_freed++);
214 /* Otherwise put this object back into the cache */
217 status = acpi_ut_acquire_mutex (ACPI_MTX_CACHES);
218 if (ACPI_FAILURE (status)) {
222 /* Mark the object as cached */
224 ACPI_MEMSET (object, 0xCA, cache->object_size);
225 ACPI_SET_DESCRIPTOR_TYPE (object, ACPI_DESC_TYPE_CACHED);
227 /* Put the object at the head of the cache list */
229 * (ACPI_CAST_INDIRECT_PTR (char,
230 &(((char *) object)[cache->link_offset]))) = cache->list_head;
231 cache->list_head = object;
232 cache->current_depth++;
234 (void) acpi_ut_release_mutex (ACPI_MTX_CACHES);
241 /*******************************************************************************
243 * FUNCTION: acpi_os_acquire_object
245 * PARAMETERS: Cache - Handle to cache object
247 * RETURN: the acquired object. NULL on error
249 * DESCRIPTION: Get an object from the specified cache. If cache is empty,
250 * the object is allocated.
252 ******************************************************************************/
255 acpi_os_acquire_object (
256 struct acpi_memory_list *cache)
262 ACPI_FUNCTION_NAME ("ut_acquire_from_cache");
269 status = acpi_ut_acquire_mutex (ACPI_MTX_CACHES);
270 if (ACPI_FAILURE (status)) {
274 ACPI_MEM_TRACKING (cache->requests++);
276 /* Check the cache first */
278 if (cache->list_head) {
279 /* There is an object available, use it */
281 object = cache->list_head;
282 cache->list_head = *(ACPI_CAST_INDIRECT_PTR (char,
283 &(((char *) object)[cache->link_offset])));
285 cache->current_depth--;
287 ACPI_MEM_TRACKING (cache->hits++);
288 ACPI_MEM_TRACKING (ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
289 "Object %p from %s\n", object, cache->list_name)));
291 status = acpi_ut_release_mutex (ACPI_MTX_CACHES);
292 if (ACPI_FAILURE (status)) {
296 /* Clear (zero) the previously used Object */
298 ACPI_MEMSET (object, 0, cache->object_size);
301 /* The cache is empty, create a new object */
303 ACPI_MEM_TRACKING (cache->total_allocated++);
305 /* Avoid deadlock with ACPI_MEM_CALLOCATE */
307 status = acpi_ut_release_mutex (ACPI_MTX_CACHES);
308 if (ACPI_FAILURE (status)) {
312 object = ACPI_MEM_CALLOCATE (cache->object_size);
320 #endif /* ACPI_USE_LOCAL_CACHE */