1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: utcache - local cache allocation routines
6 * Copyright (C) 2000 - 2020, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
13 #define _COMPONENT ACPI_UTILITIES
14 ACPI_MODULE_NAME("utcache")
16 #ifdef ACPI_USE_LOCAL_CACHE
17 /*******************************************************************************
19 * FUNCTION: acpi_os_create_cache
21 * PARAMETERS: cache_name - Ascii name for the cache
22 * object_size - Size of each cached object
23 * max_depth - Maximum depth of the cache (in objects)
24 * return_cache - Where the new cache object is returned
28 * DESCRIPTION: Create a cache object
30 ******************************************************************************/
32 acpi_os_create_cache(char *cache_name,
34 u16 max_depth, struct acpi_memory_list **return_cache)
36 struct acpi_memory_list *cache;
38 ACPI_FUNCTION_ENTRY();
40 if (!cache_name || !return_cache || !object_size) {
41 return (AE_BAD_PARAMETER);
44 /* Create the cache object */
46 cache = acpi_os_allocate(sizeof(struct acpi_memory_list));
48 return (AE_NO_MEMORY);
51 /* Populate the cache object and return it */
53 memset(cache, 0, sizeof(struct acpi_memory_list));
54 cache->list_name = cache_name;
55 cache->object_size = object_size;
56 cache->max_depth = max_depth;
58 *return_cache = cache;
62 /*******************************************************************************
64 * FUNCTION: acpi_os_purge_cache
66 * PARAMETERS: cache - Handle to cache object
70 * DESCRIPTION: Free all objects within the requested cache.
72 ******************************************************************************/
74 acpi_status acpi_os_purge_cache(struct acpi_memory_list *cache)
79 ACPI_FUNCTION_ENTRY();
82 return (AE_BAD_PARAMETER);
85 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
86 if (ACPI_FAILURE(status)) {
90 /* Walk the list of objects in this cache */
92 while (cache->list_head) {
94 /* Delete and unlink one cached state object */
96 next = ACPI_GET_DESCRIPTOR_PTR(cache->list_head);
97 ACPI_FREE(cache->list_head);
99 cache->list_head = next;
100 cache->current_depth--;
103 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
107 /*******************************************************************************
109 * FUNCTION: acpi_os_delete_cache
111 * PARAMETERS: cache - Handle to cache object
115 * DESCRIPTION: Free all objects within the requested cache and delete the
118 ******************************************************************************/
120 acpi_status acpi_os_delete_cache(struct acpi_memory_list *cache)
124 ACPI_FUNCTION_ENTRY();
126 /* Purge all objects in the cache */
128 status = acpi_os_purge_cache(cache);
129 if (ACPI_FAILURE(status)) {
133 /* Now we can delete the cache object */
139 /*******************************************************************************
141 * FUNCTION: acpi_os_release_object
143 * PARAMETERS: cache - Handle to cache object
144 * object - The object to be released
148 * DESCRIPTION: Release an object to the specified cache. If cache is full,
149 * the object is deleted.
151 ******************************************************************************/
153 acpi_status acpi_os_release_object(struct acpi_memory_list *cache, void *object)
157 ACPI_FUNCTION_ENTRY();
159 if (!cache || !object) {
160 return (AE_BAD_PARAMETER);
163 /* If cache is full, just free this object */
165 if (cache->current_depth >= cache->max_depth) {
167 ACPI_MEM_TRACKING(cache->total_freed++);
170 /* Otherwise put this object back into the cache */
173 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
174 if (ACPI_FAILURE(status)) {
178 /* Mark the object as cached */
180 memset(object, 0xCA, cache->object_size);
181 ACPI_SET_DESCRIPTOR_TYPE(object, ACPI_DESC_TYPE_CACHED);
183 /* Put the object at the head of the cache list */
185 ACPI_SET_DESCRIPTOR_PTR(object, cache->list_head);
186 cache->list_head = object;
187 cache->current_depth++;
189 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
195 /*******************************************************************************
197 * FUNCTION: acpi_os_acquire_object
199 * PARAMETERS: cache - Handle to cache object
201 * RETURN: the acquired object. NULL on error
203 * DESCRIPTION: Get an object from the specified cache. If cache is empty,
204 * the object is allocated.
206 ******************************************************************************/
208 void *acpi_os_acquire_object(struct acpi_memory_list *cache)
213 ACPI_FUNCTION_TRACE(os_acquire_object);
219 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
220 if (ACPI_FAILURE(status)) {
224 ACPI_MEM_TRACKING(cache->requests++);
226 /* Check the cache first */
228 if (cache->list_head) {
230 /* There is an object available, use it */
232 object = cache->list_head;
233 cache->list_head = ACPI_GET_DESCRIPTOR_PTR(object);
235 cache->current_depth--;
237 ACPI_MEM_TRACKING(cache->hits++);
238 ACPI_DEBUG_PRINT_RAW((ACPI_DB_EXEC,
239 "%s: Object %p from %s cache\n",
240 ACPI_GET_FUNCTION_NAME, object,
243 status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
244 if (ACPI_FAILURE(status)) {
248 /* Clear (zero) the previously used Object */
250 memset(object, 0, cache->object_size);
252 /* The cache is empty, create a new object */
254 ACPI_MEM_TRACKING(cache->total_allocated++);
256 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
257 if ((cache->total_allocated - cache->total_freed) >
258 cache->max_occupied) {
259 cache->max_occupied =
260 cache->total_allocated - cache->total_freed;
264 /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
266 status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
267 if (ACPI_FAILURE(status)) {
271 object = ACPI_ALLOCATE_ZEROED(cache->object_size);
279 #endif /* ACPI_USE_LOCAL_CACHE */