1 /******************************************************************************
3 * Module Name: utcache - local cache allocation routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, 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 <acpi/acpi.h>
47 #define _COMPONENT ACPI_UTILITIES
48 ACPI_MODULE_NAME("utcache")
50 #ifdef ACPI_USE_LOCAL_CACHE
51 /*******************************************************************************
53 * FUNCTION: acpi_os_create_cache
55 * PARAMETERS: cache_name - Ascii name for the cache
56 * object_size - Size of each cached object
57 * max_depth - Maximum depth of the cache (in objects)
58 * return_cache - Where the new cache object is returned
62 * DESCRIPTION: Create a cache object
64 ******************************************************************************/
66 acpi_os_create_cache(char *cache_name
,
68 u16 max_depth
, struct acpi_memory_list
**return_cache
)
70 struct acpi_memory_list
*cache
;
72 ACPI_FUNCTION_ENTRY();
74 if (!cache_name
|| !return_cache
|| (object_size
< 16)) {
75 return (AE_BAD_PARAMETER
);
78 /* Create the cache object */
80 cache
= acpi_os_allocate(sizeof(struct acpi_memory_list
));
82 return (AE_NO_MEMORY
);
85 /* Populate the cache object and return it */
87 memset(cache
, 0, sizeof(struct acpi_memory_list
));
88 cache
->list_name
= cache_name
;
89 cache
->object_size
= object_size
;
90 cache
->max_depth
= max_depth
;
92 *return_cache
= cache
;
96 /*******************************************************************************
98 * FUNCTION: acpi_os_purge_cache
100 * PARAMETERS: cache - Handle to cache object
104 * DESCRIPTION: Free all objects within the requested cache.
106 ******************************************************************************/
108 acpi_status
acpi_os_purge_cache(struct acpi_memory_list
*cache
)
113 ACPI_FUNCTION_ENTRY();
116 return (AE_BAD_PARAMETER
);
119 status
= acpi_ut_acquire_mutex(ACPI_MTX_CACHES
);
120 if (ACPI_FAILURE(status
)) {
124 /* Walk the list of objects in this cache */
126 while (cache
->list_head
) {
128 /* Delete and unlink one cached state object */
130 next
= ACPI_GET_DESCRIPTOR_PTR(cache
->list_head
);
131 ACPI_FREE(cache
->list_head
);
133 cache
->list_head
= next
;
134 cache
->current_depth
--;
137 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES
);
141 /*******************************************************************************
143 * FUNCTION: acpi_os_delete_cache
145 * PARAMETERS: cache - Handle to cache object
149 * DESCRIPTION: Free all objects within the requested cache and delete the
152 ******************************************************************************/
154 acpi_status
acpi_os_delete_cache(struct acpi_memory_list
*cache
)
158 ACPI_FUNCTION_ENTRY();
160 /* Purge all objects in the cache */
162 status
= acpi_os_purge_cache(cache
);
163 if (ACPI_FAILURE(status
)) {
167 /* Now we can delete the cache object */
173 /*******************************************************************************
175 * FUNCTION: acpi_os_release_object
177 * PARAMETERS: cache - Handle to cache object
178 * object - The object to be released
182 * DESCRIPTION: Release an object to the specified cache. If cache is full,
183 * the object is deleted.
185 ******************************************************************************/
187 acpi_status
acpi_os_release_object(struct acpi_memory_list
*cache
, void *object
)
191 ACPI_FUNCTION_ENTRY();
193 if (!cache
|| !object
) {
194 return (AE_BAD_PARAMETER
);
197 /* If cache is full, just free this object */
199 if (cache
->current_depth
>= cache
->max_depth
) {
201 ACPI_MEM_TRACKING(cache
->total_freed
++);
204 /* Otherwise put this object back into the cache */
207 status
= acpi_ut_acquire_mutex(ACPI_MTX_CACHES
);
208 if (ACPI_FAILURE(status
)) {
212 /* Mark the object as cached */
214 memset(object
, 0xCA, cache
->object_size
);
215 ACPI_SET_DESCRIPTOR_TYPE(object
, ACPI_DESC_TYPE_CACHED
);
217 /* Put the object at the head of the cache list */
219 ACPI_SET_DESCRIPTOR_PTR(object
, cache
->list_head
);
220 cache
->list_head
= object
;
221 cache
->current_depth
++;
223 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES
);
229 /*******************************************************************************
231 * FUNCTION: acpi_os_acquire_object
233 * PARAMETERS: cache - Handle to cache object
235 * RETURN: the acquired object. NULL on error
237 * DESCRIPTION: Get an object from the specified cache. If cache is empty,
238 * the object is allocated.
240 ******************************************************************************/
242 void *acpi_os_acquire_object(struct acpi_memory_list
*cache
)
247 ACPI_FUNCTION_TRACE(os_acquire_object
);
253 status
= acpi_ut_acquire_mutex(ACPI_MTX_CACHES
);
254 if (ACPI_FAILURE(status
)) {
258 ACPI_MEM_TRACKING(cache
->requests
++);
260 /* Check the cache first */
262 if (cache
->list_head
) {
264 /* There is an object available, use it */
266 object
= cache
->list_head
;
267 cache
->list_head
= ACPI_GET_DESCRIPTOR_PTR(object
);
269 cache
->current_depth
--;
271 ACPI_MEM_TRACKING(cache
->hits
++);
272 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
273 "Object %p from %s cache\n", object
,
276 status
= acpi_ut_release_mutex(ACPI_MTX_CACHES
);
277 if (ACPI_FAILURE(status
)) {
281 /* Clear (zero) the previously used Object */
283 memset(object
, 0, cache
->object_size
);
285 /* The cache is empty, create a new object */
287 ACPI_MEM_TRACKING(cache
->total_allocated
++);
289 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
290 if ((cache
->total_allocated
- cache
->total_freed
) >
291 cache
->max_occupied
) {
292 cache
->max_occupied
=
293 cache
->total_allocated
- cache
->total_freed
;
297 /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
299 status
= acpi_ut_release_mutex(ACPI_MTX_CACHES
);
300 if (ACPI_FAILURE(status
)) {
304 object
= ACPI_ALLOCATE_ZEROED(cache
->object_size
);
312 #endif /* ACPI_USE_LOCAL_CACHE */