1 /******************************************************************************
3 * Module Name: utcache - local cache allocation routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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.
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
47 #define _COMPONENT ACPI_UTILITIES
48 ACPI_MODULE_NAME ("utcache")
51 #ifdef ACPI_USE_LOCAL_CACHE
52 /*******************************************************************************
54 * FUNCTION: AcpiOsCreateCache
56 * PARAMETERS: CacheName - Ascii name for the cache
57 * ObjectSize - Size of each cached object
58 * MaxDepth - Maximum depth of the cache (in objects)
59 * ReturnCache - Where the new cache object is returned
63 * DESCRIPTION: Create a cache object
65 ******************************************************************************/
72 ACPI_MEMORY_LIST **ReturnCache)
74 ACPI_MEMORY_LIST *Cache;
77 ACPI_FUNCTION_ENTRY ();
80 if (!CacheName || !ReturnCache || (ObjectSize < 16))
82 return (AE_BAD_PARAMETER);
85 /* Create the cache object */
87 Cache = AcpiOsAllocate (sizeof (ACPI_MEMORY_LIST));
90 return (AE_NO_MEMORY);
93 /* Populate the cache object and return it */
95 ACPI_MEMSET (Cache, 0, sizeof (ACPI_MEMORY_LIST));
96 Cache->ListName = CacheName;
97 Cache->ObjectSize = ObjectSize;
98 Cache->MaxDepth = MaxDepth;
100 *ReturnCache = Cache;
105 /*******************************************************************************
107 * FUNCTION: AcpiOsPurgeCache
109 * PARAMETERS: Cache - Handle to cache object
113 * DESCRIPTION: Free all objects within the requested cache.
115 ******************************************************************************/
119 ACPI_MEMORY_LIST *Cache)
125 ACPI_FUNCTION_ENTRY ();
130 return (AE_BAD_PARAMETER);
133 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
134 if (ACPI_FAILURE (Status))
139 /* Walk the list of objects in this cache */
141 while (Cache->ListHead)
143 /* Delete and unlink one cached state object */
145 Next = ACPI_GET_DESCRIPTOR_PTR (Cache->ListHead);
146 ACPI_FREE (Cache->ListHead);
148 Cache->ListHead = Next;
149 Cache->CurrentDepth--;
152 (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
157 /*******************************************************************************
159 * FUNCTION: AcpiOsDeleteCache
161 * PARAMETERS: Cache - Handle to cache object
165 * DESCRIPTION: Free all objects within the requested cache and delete the
168 ******************************************************************************/
172 ACPI_MEMORY_LIST *Cache)
177 ACPI_FUNCTION_ENTRY ();
180 /* Purge all objects in the cache */
182 Status = AcpiOsPurgeCache (Cache);
183 if (ACPI_FAILURE (Status))
188 /* Now we can delete the cache object */
195 /*******************************************************************************
197 * FUNCTION: AcpiOsReleaseObject
199 * PARAMETERS: Cache - Handle to cache object
200 * Object - The object to be released
204 * DESCRIPTION: Release an object to the specified cache. If cache is full,
205 * the object is deleted.
207 ******************************************************************************/
210 AcpiOsReleaseObject (
211 ACPI_MEMORY_LIST *Cache,
217 ACPI_FUNCTION_ENTRY ();
220 if (!Cache || !Object)
222 return (AE_BAD_PARAMETER);
225 /* If cache is full, just free this object */
227 if (Cache->CurrentDepth >= Cache->MaxDepth)
230 ACPI_MEM_TRACKING (Cache->TotalFreed++);
233 /* Otherwise put this object back into the cache */
237 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
238 if (ACPI_FAILURE (Status))
243 /* Mark the object as cached */
245 ACPI_MEMSET (Object, 0xCA, Cache->ObjectSize);
246 ACPI_SET_DESCRIPTOR_TYPE (Object, ACPI_DESC_TYPE_CACHED);
248 /* Put the object at the head of the cache list */
250 ACPI_SET_DESCRIPTOR_PTR (Object, Cache->ListHead);
251 Cache->ListHead = Object;
252 Cache->CurrentDepth++;
254 (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
261 /*******************************************************************************
263 * FUNCTION: AcpiOsAcquireObject
265 * PARAMETERS: Cache - Handle to cache object
267 * RETURN: the acquired object. NULL on error
269 * DESCRIPTION: Get an object from the specified cache. If cache is empty,
270 * the object is allocated.
272 ******************************************************************************/
275 AcpiOsAcquireObject (
276 ACPI_MEMORY_LIST *Cache)
282 ACPI_FUNCTION_NAME (OsAcquireObject);
290 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
291 if (ACPI_FAILURE (Status))
296 ACPI_MEM_TRACKING (Cache->Requests++);
298 /* Check the cache first */
302 /* There is an object available, use it */
304 Object = Cache->ListHead;
305 Cache->ListHead = ACPI_GET_DESCRIPTOR_PTR (Object);
307 Cache->CurrentDepth--;
309 ACPI_MEM_TRACKING (Cache->Hits++);
310 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
311 "Object %p from %s cache\n", Object, Cache->ListName));
313 Status = AcpiUtReleaseMutex (ACPI_MTX_CACHES);
314 if (ACPI_FAILURE (Status))
319 /* Clear (zero) the previously used Object */
321 ACPI_MEMSET (Object, 0, Cache->ObjectSize);
325 /* The cache is empty, create a new object */
327 ACPI_MEM_TRACKING (Cache->TotalAllocated++);
329 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
330 if ((Cache->TotalAllocated - Cache->TotalFreed) > Cache->MaxOccupied)
332 Cache->MaxOccupied = Cache->TotalAllocated - Cache->TotalFreed;
336 /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
338 Status = AcpiUtReleaseMutex (ACPI_MTX_CACHES);
339 if (ACPI_FAILURE (Status))
344 Object = ACPI_ALLOCATE_ZEROED (Cache->ObjectSize);
353 #endif /* ACPI_USE_LOCAL_CACHE */