Fixed compatibility of output.
[AROS.git] / arch / all-pc / acpica / source / components / utilities / utcache.c
blob54c73e2a425a431e789a2ce7c7963a405a100805
1 /******************************************************************************
3 * Module Name: utcache - local cache allocation routines
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, 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 #define __UTCACHE_C__
46 #include "acpi.h"
47 #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
63 * RETURN: Status
65 * DESCRIPTION: Create a cache object
67 ******************************************************************************/
69 ACPI_STATUS
70 AcpiOsCreateCache (
71 char *CacheName,
72 UINT16 ObjectSize,
73 UINT16 MaxDepth,
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));
90 if (!Cache)
92 return (AE_NO_MEMORY);
95 /* Populate the cache object and return it */
97 ACPI_MEMSET (Cache, 0, sizeof (ACPI_MEMORY_LIST));
98 Cache->ListName = CacheName;
99 Cache->ObjectSize = ObjectSize;
100 Cache->MaxDepth = MaxDepth;
102 *ReturnCache = Cache;
103 return (AE_OK);
107 /*******************************************************************************
109 * FUNCTION: AcpiOsPurgeCache
111 * PARAMETERS: Cache - Handle to cache object
113 * RETURN: Status
115 * DESCRIPTION: Free all objects within the requested cache.
117 ******************************************************************************/
119 ACPI_STATUS
120 AcpiOsPurgeCache (
121 ACPI_MEMORY_LIST *Cache)
123 void *Next;
124 ACPI_STATUS Status;
127 ACPI_FUNCTION_ENTRY ();
130 if (!Cache)
132 return (AE_BAD_PARAMETER);
135 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
136 if (ACPI_FAILURE (Status))
138 return (Status);
141 /* Walk the list of objects in this cache */
143 while (Cache->ListHead)
145 /* Delete and unlink one cached state object */
147 Next = ACPI_GET_DESCRIPTOR_PTR (Cache->ListHead);
148 ACPI_FREE (Cache->ListHead);
150 Cache->ListHead = Next;
151 Cache->CurrentDepth--;
154 (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
155 return (AE_OK);
159 /*******************************************************************************
161 * FUNCTION: AcpiOsDeleteCache
163 * PARAMETERS: Cache - Handle to cache object
165 * RETURN: Status
167 * DESCRIPTION: Free all objects within the requested cache and delete the
168 * cache object.
170 ******************************************************************************/
172 ACPI_STATUS
173 AcpiOsDeleteCache (
174 ACPI_MEMORY_LIST *Cache)
176 ACPI_STATUS Status;
179 ACPI_FUNCTION_ENTRY ();
182 /* Purge all objects in the cache */
184 Status = AcpiOsPurgeCache (Cache);
185 if (ACPI_FAILURE (Status))
187 return (Status);
190 /* Now we can delete the cache object */
192 AcpiOsFree (Cache);
193 return (AE_OK);
197 /*******************************************************************************
199 * FUNCTION: AcpiOsReleaseObject
201 * PARAMETERS: Cache - Handle to cache object
202 * Object - The object to be released
204 * RETURN: None
206 * DESCRIPTION: Release an object to the specified cache. If cache is full,
207 * the object is deleted.
209 ******************************************************************************/
211 ACPI_STATUS
212 AcpiOsReleaseObject (
213 ACPI_MEMORY_LIST *Cache,
214 void *Object)
216 ACPI_STATUS Status;
219 ACPI_FUNCTION_ENTRY ();
222 if (!Cache || !Object)
224 return (AE_BAD_PARAMETER);
227 /* If cache is full, just free this object */
229 if (Cache->CurrentDepth >= Cache->MaxDepth)
231 ACPI_FREE (Object);
232 ACPI_MEM_TRACKING (Cache->TotalFreed++);
235 /* Otherwise put this object back into the cache */
237 else
239 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
240 if (ACPI_FAILURE (Status))
242 return (Status);
245 /* Mark the object as cached */
247 ACPI_MEMSET (Object, 0xCA, Cache->ObjectSize);
248 ACPI_SET_DESCRIPTOR_TYPE (Object, ACPI_DESC_TYPE_CACHED);
250 /* Put the object at the head of the cache list */
252 ACPI_SET_DESCRIPTOR_PTR (Object, Cache->ListHead);
253 Cache->ListHead = Object;
254 Cache->CurrentDepth++;
256 (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
259 return (AE_OK);
263 /*******************************************************************************
265 * FUNCTION: AcpiOsAcquireObject
267 * PARAMETERS: Cache - Handle to cache object
269 * RETURN: the acquired object. NULL on error
271 * DESCRIPTION: Get an object from the specified cache. If cache is empty,
272 * the object is allocated.
274 ******************************************************************************/
276 void *
277 AcpiOsAcquireObject (
278 ACPI_MEMORY_LIST *Cache)
280 ACPI_STATUS Status;
281 void *Object;
284 ACPI_FUNCTION_NAME (OsAcquireObject);
287 if (!Cache)
289 return (NULL);
292 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
293 if (ACPI_FAILURE (Status))
295 return (NULL);
298 ACPI_MEM_TRACKING (Cache->Requests++);
300 /* Check the cache first */
302 if (Cache->ListHead)
304 /* There is an object available, use it */
306 Object = Cache->ListHead;
307 Cache->ListHead = ACPI_GET_DESCRIPTOR_PTR (Object);
309 Cache->CurrentDepth--;
311 ACPI_MEM_TRACKING (Cache->Hits++);
312 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
313 "Object %p from %s cache\n", Object, Cache->ListName));
315 Status = AcpiUtReleaseMutex (ACPI_MTX_CACHES);
316 if (ACPI_FAILURE (Status))
318 return (NULL);
321 /* Clear (zero) the previously used Object */
323 ACPI_MEMSET (Object, 0, Cache->ObjectSize);
325 else
327 /* The cache is empty, create a new object */
329 ACPI_MEM_TRACKING (Cache->TotalAllocated++);
331 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
332 if ((Cache->TotalAllocated - Cache->TotalFreed) > Cache->MaxOccupied)
334 Cache->MaxOccupied = Cache->TotalAllocated - Cache->TotalFreed;
336 #endif
338 /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
340 Status = AcpiUtReleaseMutex (ACPI_MTX_CACHES);
341 if (ACPI_FAILURE (Status))
343 return (NULL);
346 Object = ACPI_ALLOCATE_ZEROED (Cache->ObjectSize);
347 if (!Object)
349 return (NULL);
353 return (Object);
355 #endif /* ACPI_USE_LOCAL_CACHE */