1 /******************************************************************************
3 * Module Name: utcache - local cache allocation routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2008, 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>
46 #define _COMPONENT ACPI_UTILITIES
47 ACPI_MODULE_NAME("utcache")
48 #ifdef ACPI_USE_LOCAL_CACHE
49 /*******************************************************************************
51 * FUNCTION: acpi_os_create_cache
53 * PARAMETERS: cache_name - Ascii name for the cache
54 * object_size - Size of each cached object
55 * max_depth - Maximum depth of the cache (in objects)
56 * return_cache - Where the new cache object is returned
60 * DESCRIPTION: Create a cache object
62 ******************************************************************************/
64 acpi_os_create_cache(char *cache_name
,
66 u16 max_depth
, struct acpi_memory_list
** return_cache
)
68 struct acpi_memory_list
*cache
;
70 ACPI_FUNCTION_ENTRY();
72 if (!cache_name
|| !return_cache
|| (object_size
< 16)) {
73 return (AE_BAD_PARAMETER
);
76 /* Create the cache object */
78 cache
= acpi_os_allocate(sizeof(struct acpi_memory_list
));
80 return (AE_NO_MEMORY
);
83 /* Populate the cache object and return it */
85 ACPI_MEMSET(cache
, 0, sizeof(struct acpi_memory_list
));
86 cache
->link_offset
= 8;
87 cache
->list_name
= cache_name
;
88 cache
->object_size
= object_size
;
89 cache
->max_depth
= max_depth
;
91 *return_cache
= cache
;
95 /*******************************************************************************
97 * FUNCTION: acpi_os_purge_cache
99 * PARAMETERS: Cache - Handle to cache object
103 * DESCRIPTION: Free all objects within the requested cache.
105 ******************************************************************************/
107 acpi_status
acpi_os_purge_cache(struct acpi_memory_list
* cache
)
111 ACPI_FUNCTION_ENTRY();
114 return (AE_BAD_PARAMETER
);
117 /* Walk the list of objects in this cache */
119 while (cache
->list_head
) {
121 /* Delete and unlink one cached state object */
123 next
= *(ACPI_CAST_INDIRECT_PTR(char,
127 ACPI_FREE(cache
->list_head
);
129 cache
->list_head
= next
;
130 cache
->current_depth
--;
136 /*******************************************************************************
138 * FUNCTION: acpi_os_delete_cache
140 * PARAMETERS: Cache - Handle to cache object
144 * DESCRIPTION: Free all objects within the requested cache and delete the
147 ******************************************************************************/
149 acpi_status
acpi_os_delete_cache(struct acpi_memory_list
* cache
)
153 ACPI_FUNCTION_ENTRY();
155 /* Purge all objects in the cache */
157 status
= acpi_os_purge_cache(cache
);
158 if (ACPI_FAILURE(status
)) {
162 /* Now we can delete the cache object */
168 /*******************************************************************************
170 * FUNCTION: acpi_os_release_object
172 * PARAMETERS: Cache - Handle to cache object
173 * Object - The object to be released
177 * DESCRIPTION: Release an object to the specified cache. If cache is full,
178 * the object is deleted.
180 ******************************************************************************/
183 acpi_os_release_object(struct acpi_memory_list
* cache
, void *object
)
187 ACPI_FUNCTION_ENTRY();
189 if (!cache
|| !object
) {
190 return (AE_BAD_PARAMETER
);
193 /* If cache is full, just free this object */
195 if (cache
->current_depth
>= cache
->max_depth
) {
197 ACPI_MEM_TRACKING(cache
->total_freed
++);
200 /* Otherwise put this object back into the cache */
203 status
= acpi_ut_acquire_mutex(ACPI_MTX_CACHES
);
204 if (ACPI_FAILURE(status
)) {
208 /* Mark the object as cached */
210 ACPI_MEMSET(object
, 0xCA, cache
->object_size
);
211 ACPI_SET_DESCRIPTOR_TYPE(object
, ACPI_DESC_TYPE_CACHED
);
213 /* Put the object at the head of the cache list */
215 *(ACPI_CAST_INDIRECT_PTR(char,
216 &(((char *)object
)[cache
->
219 cache
->list_head
= object
;
220 cache
->current_depth
++;
222 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES
);
228 /*******************************************************************************
230 * FUNCTION: acpi_os_acquire_object
232 * PARAMETERS: Cache - Handle to cache object
234 * RETURN: the acquired object. NULL on error
236 * DESCRIPTION: Get an object from the specified cache. If cache is empty,
237 * the object is allocated.
239 ******************************************************************************/
241 void *acpi_os_acquire_object(struct acpi_memory_list
*cache
)
246 ACPI_FUNCTION_NAME(os_acquire_object
);
252 status
= acpi_ut_acquire_mutex(ACPI_MTX_CACHES
);
253 if (ACPI_FAILURE(status
)) {
257 ACPI_MEM_TRACKING(cache
->requests
++);
259 /* Check the cache first */
261 if (cache
->list_head
) {
263 /* There is an object available, use it */
265 object
= cache
->list_head
;
266 cache
->list_head
= *(ACPI_CAST_INDIRECT_PTR(char,
271 cache
->current_depth
--;
273 ACPI_MEM_TRACKING(cache
->hits
++);
274 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
275 "Object %p from %s cache\n", object
,
278 status
= acpi_ut_release_mutex(ACPI_MTX_CACHES
);
279 if (ACPI_FAILURE(status
)) {
283 /* Clear (zero) the previously used Object */
285 ACPI_MEMSET(object
, 0, cache
->object_size
);
287 /* The cache is empty, create a new object */
289 ACPI_MEM_TRACKING(cache
->total_allocated
++);
291 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
292 if ((cache
->total_allocated
- cache
->total_freed
) >
293 cache
->max_occupied
) {
294 cache
->max_occupied
=
295 cache
->total_allocated
- cache
->total_freed
;
299 /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
301 status
= acpi_ut_release_mutex(ACPI_MTX_CACHES
);
302 if (ACPI_FAILURE(status
)) {
306 object
= ACPI_ALLOCATE_ZEROED(cache
->object_size
);
314 #endif /* ACPI_USE_LOCAL_CACHE */