dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / uts / intel / io / acpica / utilities / utcache.c
blob049dae8e98a0146f105628385a409c122120716b
1 /******************************************************************************
3 * Module Name: utcache - local cache allocation routines
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, Intel Corp.
9 * All rights reserved.
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.
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.
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.
44 #include "acpi.h"
45 #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
61 * RETURN: Status
63 * DESCRIPTION: Create a cache object
65 ******************************************************************************/
67 ACPI_STATUS
68 AcpiOsCreateCache (
69 char *CacheName,
70 UINT16 ObjectSize,
71 UINT16 MaxDepth,
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));
88 if (!Cache)
90 return (AE_NO_MEMORY);
93 /* Populate the cache object and return it */
95 memset (Cache, 0, sizeof (ACPI_MEMORY_LIST));
96 Cache->ListName = CacheName;
97 Cache->ObjectSize = ObjectSize;
98 Cache->MaxDepth = MaxDepth;
100 *ReturnCache = Cache;
101 return (AE_OK);
105 /*******************************************************************************
107 * FUNCTION: AcpiOsPurgeCache
109 * PARAMETERS: Cache - Handle to cache object
111 * RETURN: Status
113 * DESCRIPTION: Free all objects within the requested cache.
115 ******************************************************************************/
117 ACPI_STATUS
118 AcpiOsPurgeCache (
119 ACPI_MEMORY_LIST *Cache)
121 void *Next;
122 ACPI_STATUS Status;
125 ACPI_FUNCTION_ENTRY ();
128 if (!Cache)
130 return (AE_BAD_PARAMETER);
133 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
134 if (ACPI_FAILURE (Status))
136 return (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);
153 return (AE_OK);
157 /*******************************************************************************
159 * FUNCTION: AcpiOsDeleteCache
161 * PARAMETERS: Cache - Handle to cache object
163 * RETURN: Status
165 * DESCRIPTION: Free all objects within the requested cache and delete the
166 * cache object.
168 ******************************************************************************/
170 ACPI_STATUS
171 AcpiOsDeleteCache (
172 ACPI_MEMORY_LIST *Cache)
174 ACPI_STATUS Status;
177 ACPI_FUNCTION_ENTRY ();
180 /* Purge all objects in the cache */
182 Status = AcpiOsPurgeCache (Cache);
183 if (ACPI_FAILURE (Status))
185 return (Status);
188 /* Now we can delete the cache object */
190 AcpiOsFree (Cache);
191 return (AE_OK);
195 /*******************************************************************************
197 * FUNCTION: AcpiOsReleaseObject
199 * PARAMETERS: Cache - Handle to cache object
200 * Object - The object to be released
202 * RETURN: None
204 * DESCRIPTION: Release an object to the specified cache. If cache is full,
205 * the object is deleted.
207 ******************************************************************************/
209 ACPI_STATUS
210 AcpiOsReleaseObject (
211 ACPI_MEMORY_LIST *Cache,
212 void *Object)
214 ACPI_STATUS Status;
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)
229 ACPI_FREE (Object);
230 ACPI_MEM_TRACKING (Cache->TotalFreed++);
233 /* Otherwise put this object back into the cache */
235 else
237 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
238 if (ACPI_FAILURE (Status))
240 return (Status);
243 /* Mark the object as cached */
245 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);
257 return (AE_OK);
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 ******************************************************************************/
274 void *
275 AcpiOsAcquireObject (
276 ACPI_MEMORY_LIST *Cache)
278 ACPI_STATUS Status;
279 void *Object;
282 ACPI_FUNCTION_TRACE (OsAcquireObject);
285 if (!Cache)
287 return_PTR (NULL);
290 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
291 if (ACPI_FAILURE (Status))
293 return_PTR (NULL);
296 ACPI_MEM_TRACKING (Cache->Requests++);
298 /* Check the cache first */
300 if (Cache->ListHead)
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))
316 return_PTR (NULL);
319 /* Clear (zero) the previously used Object */
321 memset (Object, 0, Cache->ObjectSize);
323 else
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;
334 #endif
336 /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
338 Status = AcpiUtReleaseMutex (ACPI_MTX_CACHES);
339 if (ACPI_FAILURE (Status))
341 return_PTR (NULL);
344 Object = ACPI_ALLOCATE_ZEROED (Cache->ObjectSize);
345 if (!Object)
347 return_PTR (NULL);
351 return_PTR (Object);
353 #endif /* ACPI_USE_LOCAL_CACHE */