1 /******************************************************************************
3 * Module Name: utcache - local cache allocation routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, Intel Corp.
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.
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
49 #define _COMPONENT ACPI_UTILITIES
50 ACPI_MODULE_NAME ("utcache")
53 #ifdef ACPI_USE_LOCAL_CACHE
54 /*******************************************************************************
56 * FUNCTION: AcpiOsCreateCache
58 * PARAMETERS: CacheName - Ascii name for the cache
59 * ObjectSize - Size of each cached object
60 * MaxDepth - Maximum depth of the cache (in objects)
61 * ReturnCache - Where the new cache object is returned
65 * DESCRIPTION: Create a cache object
67 ******************************************************************************/
74 ACPI_MEMORY_LIST **ReturnCache)
76 ACPI_MEMORY_LIST *Cache;
79 ACPI_FUNCTION_ENTRY ();
82 if (!CacheName || !ReturnCache || (ObjectSize < 16))
84 return (AE_BAD_PARAMETER);
87 /* Create the cache object */
89 Cache = AcpiOsAllocate (sizeof (ACPI_MEMORY_LIST));
92 return (AE_NO_MEMORY);
95 /* Populate the cache object and return it */
97 ACPI_MEMSET (Cache, 0, sizeof (ACPI_MEMORY_LIST));
98 Cache->LinkOffset = 8;
99 Cache->ListName = CacheName;
100 Cache->ObjectSize = ObjectSize;
101 Cache->MaxDepth = MaxDepth;
103 *ReturnCache = Cache;
108 /*******************************************************************************
110 * FUNCTION: AcpiOsPurgeCache
112 * PARAMETERS: Cache - Handle to cache object
116 * DESCRIPTION: Free all objects within the requested cache.
118 ******************************************************************************/
122 ACPI_MEMORY_LIST *Cache)
128 ACPI_FUNCTION_ENTRY ();
133 return (AE_BAD_PARAMETER);
136 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
137 if (ACPI_FAILURE (Status))
142 /* Walk the list of objects in this cache */
144 while (Cache->ListHead)
146 /* Delete and unlink one cached state object */
148 Next = *(ACPI_CAST_INDIRECT_PTR (char,
149 &(((char *) Cache->ListHead)[Cache->LinkOffset])));
150 ACPI_FREE (Cache->ListHead);
152 Cache->ListHead = Next;
153 Cache->CurrentDepth--;
156 (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
161 /*******************************************************************************
163 * FUNCTION: AcpiOsDeleteCache
165 * PARAMETERS: Cache - Handle to cache object
169 * DESCRIPTION: Free all objects within the requested cache and delete the
172 ******************************************************************************/
176 ACPI_MEMORY_LIST *Cache)
181 ACPI_FUNCTION_ENTRY ();
184 /* Purge all objects in the cache */
186 Status = AcpiOsPurgeCache (Cache);
187 if (ACPI_FAILURE (Status))
192 /* Now we can delete the cache object */
199 /*******************************************************************************
201 * FUNCTION: AcpiOsReleaseObject
203 * PARAMETERS: Cache - Handle to cache object
204 * Object - The object to be released
208 * DESCRIPTION: Release an object to the specified cache. If cache is full,
209 * the object is deleted.
211 ******************************************************************************/
214 AcpiOsReleaseObject (
215 ACPI_MEMORY_LIST *Cache,
221 ACPI_FUNCTION_ENTRY ();
224 if (!Cache || !Object)
226 return (AE_BAD_PARAMETER);
229 /* If cache is full, just free this object */
231 if (Cache->CurrentDepth >= Cache->MaxDepth)
234 ACPI_MEM_TRACKING (Cache->TotalFreed++);
237 /* Otherwise put this object back into the cache */
241 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
242 if (ACPI_FAILURE (Status))
247 /* Mark the object as cached */
249 ACPI_MEMSET (Object, 0xCA, Cache->ObjectSize);
250 ACPI_SET_DESCRIPTOR_TYPE (Object, ACPI_DESC_TYPE_CACHED);
252 /* Put the object at the head of the cache list */
254 * (ACPI_CAST_INDIRECT_PTR (char,
255 &(((char *) Object)[Cache->LinkOffset]))) = Cache->ListHead;
256 Cache->ListHead = Object;
257 Cache->CurrentDepth++;
259 (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
266 /*******************************************************************************
268 * FUNCTION: AcpiOsAcquireObject
270 * PARAMETERS: Cache - Handle to cache object
272 * RETURN: the acquired object. NULL on error
274 * DESCRIPTION: Get an object from the specified cache. If cache is empty,
275 * the object is allocated.
277 ******************************************************************************/
280 AcpiOsAcquireObject (
281 ACPI_MEMORY_LIST *Cache)
287 ACPI_FUNCTION_NAME (OsAcquireObject);
295 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
296 if (ACPI_FAILURE (Status))
301 ACPI_MEM_TRACKING (Cache->Requests++);
303 /* Check the cache first */
307 /* There is an object available, use it */
309 Object = Cache->ListHead;
310 Cache->ListHead = *(ACPI_CAST_INDIRECT_PTR (char,
311 &(((char *) Object)[Cache->LinkOffset])));
313 Cache->CurrentDepth--;
315 ACPI_MEM_TRACKING (Cache->Hits++);
316 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
317 "Object %p from %s cache\n", Object, Cache->ListName));
319 Status = AcpiUtReleaseMutex (ACPI_MTX_CACHES);
320 if (ACPI_FAILURE (Status))
325 /* Clear (zero) the previously used Object */
327 ACPI_MEMSET (Object, 0, Cache->ObjectSize);
331 /* The cache is empty, create a new object */
333 ACPI_MEM_TRACKING (Cache->TotalAllocated++);
335 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
336 if ((Cache->TotalAllocated - Cache->TotalFreed) > Cache->MaxOccupied)
338 Cache->MaxOccupied = Cache->TotalAllocated - Cache->TotalFreed;
342 /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
344 Status = AcpiUtReleaseMutex (ACPI_MTX_CACHES);
345 if (ACPI_FAILURE (Status))
350 Object = ACPI_ALLOCATE_ZEROED (Cache->ObjectSize);
359 #endif /* ACPI_USE_LOCAL_CACHE */