ReactOS 0.4.15-dev-8390-g075894b
utcache.c
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Module Name: utcache - local cache allocation routines
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2022, Intel Corp.
9 * All rights reserved.
10 *
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.
25 *
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.
29 *
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 MERCHANTABILITY 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.
42 */
43
44#include "acpi.h"
45#include "accommon.h"
46
47#define _COMPONENT ACPI_UTILITIES
48 ACPI_MODULE_NAME ("utcache")
49
50
51#ifdef ACPI_USE_LOCAL_CACHE
52/*******************************************************************************
53 *
54 * FUNCTION: AcpiOsCreateCache
55 *
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
60 *
61 * RETURN: Status
62 *
63 * DESCRIPTION: Create a cache object
64 *
65 ******************************************************************************/
66
69 char *CacheName,
70 UINT16 ObjectSize,
72 ACPI_MEMORY_LIST **ReturnCache)
73{
75
76
78
79
80 if (!CacheName || !ReturnCache || !ObjectSize)
81 {
82 return (AE_BAD_PARAMETER);
83 }
84
85 /* Create the cache object */
86
88 if (!Cache)
89 {
90 return (AE_NO_MEMORY);
91 }
92
93 /* Populate the cache object and return it */
94
95 memset (Cache, 0, sizeof (ACPI_MEMORY_LIST));
96 Cache->ListName = CacheName;
97 Cache->ObjectSize = ObjectSize;
98 Cache->MaxDepth = MaxDepth;
99
100 *ReturnCache = Cache;
101 return (AE_OK);
102}
103
104
105/*******************************************************************************
106 *
107 * FUNCTION: AcpiOsPurgeCache
108 *
109 * PARAMETERS: Cache - Handle to cache object
110 *
111 * RETURN: Status
112 *
113 * DESCRIPTION: Free all objects within the requested cache.
114 *
115 ******************************************************************************/
116
120{
121 void *Next;
123
124
126
127
128 if (!Cache)
129 {
130 return (AE_BAD_PARAMETER);
131 }
132
134 if (ACPI_FAILURE (Status))
135 {
136 return (Status);
137 }
138
139 /* Walk the list of objects in this cache */
140
141 while (Cache->ListHead)
142 {
143 /* Delete and unlink one cached state object */
144
145 Next = ACPI_GET_DESCRIPTOR_PTR (Cache->ListHead);
146 ACPI_FREE (Cache->ListHead);
147
148 Cache->ListHead = Next;
149 Cache->CurrentDepth--;
150 }
151
153 return (AE_OK);
154}
155
156
157/*******************************************************************************
158 *
159 * FUNCTION: AcpiOsDeleteCache
160 *
161 * PARAMETERS: Cache - Handle to cache object
162 *
163 * RETURN: Status
164 *
165 * DESCRIPTION: Free all objects within the requested cache and delete the
166 * cache object.
167 *
168 ******************************************************************************/
169
173{
175
176
178
179
180 /* Purge all objects in the cache */
181
183 if (ACPI_FAILURE (Status))
184 {
185 return (Status);
186 }
187
188 /* Now we can delete the cache object */
189
191 return (AE_OK);
192}
193
194
195/*******************************************************************************
196 *
197 * FUNCTION: AcpiOsReleaseObject
198 *
199 * PARAMETERS: Cache - Handle to cache object
200 * Object - The object to be released
201 *
202 * RETURN: None
203 *
204 * DESCRIPTION: Release an object to the specified cache. If cache is full,
205 * the object is deleted.
206 *
207 ******************************************************************************/
208
212 void *Object)
213{
215
216
218
219
220 if (!Cache || !Object)
221 {
222 return (AE_BAD_PARAMETER);
223 }
224
225 /* If cache is full, just free this object */
226
227 if (Cache->CurrentDepth >= Cache->MaxDepth)
228 {
230 ACPI_MEM_TRACKING (Cache->TotalFreed++);
231 }
232
233 /* Otherwise put this object back into the cache */
234
235 else
236 {
238 if (ACPI_FAILURE (Status))
239 {
240 return (Status);
241 }
242
243 /* Mark the object as cached */
244
245 memset (Object, 0xCA, Cache->ObjectSize);
247
248 /* Put the object at the head of the cache list */
249
251 Cache->ListHead = Object;
252 Cache->CurrentDepth++;
253
255 }
256
257 return (AE_OK);
258}
259
260
261/*******************************************************************************
262 *
263 * FUNCTION: AcpiOsAcquireObject
264 *
265 * PARAMETERS: Cache - Handle to cache object
266 *
267 * RETURN: the acquired object. NULL on error
268 *
269 * DESCRIPTION: Get an object from the specified cache. If cache is empty,
270 * the object is allocated.
271 *
272 ******************************************************************************/
273
274void *
277{
279 void *Object;
280
281
282 ACPI_FUNCTION_TRACE (OsAcquireObject);
283
284
285 if (!Cache)
286 {
288 }
289
291 if (ACPI_FAILURE (Status))
292 {
294 }
295
296 ACPI_MEM_TRACKING (Cache->Requests++);
297
298 /* Check the cache first */
299
300 if (Cache->ListHead)
301 {
302 /* There is an object available, use it */
303
304 Object = Cache->ListHead;
306
307 Cache->CurrentDepth--;
308
309 ACPI_MEM_TRACKING (Cache->Hits++);
311 "%s: Object %p from %s cache\n",
312 ACPI_GET_FUNCTION_NAME, Object, Cache->ListName));
313
315 if (ACPI_FAILURE (Status))
316 {
318 }
319
320 /* Clear (zero) the previously used Object */
321
322 memset (Object, 0, Cache->ObjectSize);
323 }
324 else
325 {
326 /* The cache is empty, create a new object */
327
328 ACPI_MEM_TRACKING (Cache->TotalAllocated++);
329
330#ifdef ACPI_DBG_TRACK_ALLOCATIONS
331 if ((Cache->TotalAllocated - Cache->TotalFreed) > Cache->MaxOccupied)
332 {
333 Cache->MaxOccupied = Cache->TotalAllocated - Cache->TotalFreed;
334 }
335#endif
336
337 /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
338
340 if (ACPI_FAILURE (Status))
341 {
343 }
344
345 Object = ACPI_ALLOCATE_ZEROED (Cache->ObjectSize);
346 if (!Object)
347 {
349 }
350 }
351
353}
354#endif /* ACPI_USE_LOCAL_CACHE */
unsigned short UINT16
#define ACPI_FAILURE(a)
Definition: acexcep.h:95
#define AE_BAD_PARAMETER
Definition: acexcep.h:151
#define AE_NO_MEMORY
Definition: acexcep.h:112
#define AE_OK
Definition: acexcep.h:97
#define ACPI_GET_FUNCTION_NAME
Definition: acgcc.h:67
#define ACPI_MTX_CACHES
Definition: aclocal.h:88
#define ACPI_GET_DESCRIPTOR_PTR(d)
Definition: acmacros.h:412
#define ACPI_SET_DESCRIPTOR_PTR(d, p)
Definition: acmacros.h:413
#define ACPI_SET_DESCRIPTOR_TYPE(d, t)
Definition: acmacros.h:415
#define ACPI_DESC_TYPE_CACHED
Definition: acobject.h:563
#define ACPI_MODULE_NAME(Name)
Definition: acoutput.h:216
#define ACPI_FUNCTION_ENTRY()
Definition: acoutput.h:484
#define return_PTR(s)
Definition: acoutput.h:497
#define ACPI_DB_EXEC
Definition: acoutput.h:165
#define ACPI_FUNCTION_TRACE(a)
Definition: acoutput.h:480
#define ACPI_DEBUG_PRINT_RAW(pl)
Definition: acoutput.h:476
ACPI_STATUS AcpiOsReleaseObject(ACPI_CACHE_T *Cache, void *Object)
ACPI_STATUS AcpiOsDeleteCache(ACPI_CACHE_T *Cache)
void * AcpiOsAllocate(ACPI_SIZE Size)
Definition: osl.c:160
ACPI_STATUS AcpiOsCreateCache(char *CacheName, UINT16 ObjectSize, UINT16 MaxDepth, ACPI_CACHE_T **ReturnCache)
void AcpiOsFree(void *Memory)
Definition: osl.c:167
void * AcpiOsAcquireObject(ACPI_CACHE_T *Cache)
ACPI_STATUS AcpiOsPurgeCache(ACPI_CACHE_T *Cache)
ACPI_PHYSICAL_ADDRESS ACPI_SIZE BOOLEAN Warn UINT32 *TableIdx UINT32 ACPI_TABLE_HEADER *OutTableHeader ACPI_TABLE_HEADER **OutTable ACPI_HANDLE UINT32 MaxDepth
Definition: acpixf.h:640
#define ACPI_FREE(a)
Definition: actypes.h:386
UINT32 ACPI_STATUS
Definition: actypes.h:460
#define ACPI_ALLOCATE_ZEROED(a)
Definition: actypes.h:385
#define ACPI_MEM_TRACKING(a)
Definition: actypes.h:387
ACPI_STATUS AcpiUtAcquireMutex(ACPI_MUTEX_HANDLE MutexId)
Definition: utmutex.c:256
ACPI_STATUS AcpiUtReleaseMutex(ACPI_MUTEX_HANDLE MutexId)
Definition: utmutex.c:348
#define NULL
Definition: types.h:112
Status
Definition: gdiplustypes.h:25
#define memset(x, y, z)
Definition: compat.h:39
static char CacheName[PATH_MAX]
Definition: cache.c:21
Definition: fatfs.h:173
_Must_inspect_result_ _In_ WDFCOLLECTION _In_ WDFOBJECT Object