Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Module Name: utcache - local cache allocation routines |
| 4 | * |
| 5 | *****************************************************************************/ |
| 6 | |
| 7 | /* |
| 8 | * Copyright (C) 2000 - 2005, R. Byron Moore |
| 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 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. |
| 42 | */ |
| 43 | |
| 44 | |
| 45 | #include <acpi/acpi.h> |
| 46 | |
| 47 | #define _COMPONENT ACPI_UTILITIES |
| 48 | ACPI_MODULE_NAME ("utcache") |
| 49 | |
| 50 | |
| 51 | #ifdef ACPI_USE_LOCAL_CACHE |
| 52 | /******************************************************************************* |
| 53 | * |
| 54 | * FUNCTION: acpi_os_create_cache |
| 55 | * |
| 56 | * PARAMETERS: cache_name - Ascii name for the cache |
| 57 | * object_size - Size of each cached object |
| 58 | * max_depth - Maximum depth of the cache (in objects) |
| 59 | * return_cache - Where the new cache object is returned |
| 60 | * |
| 61 | * RETURN: Status |
| 62 | * |
| 63 | * DESCRIPTION: Create a cache object |
| 64 | * |
| 65 | ******************************************************************************/ |
| 66 | |
| 67 | acpi_status |
| 68 | acpi_os_create_cache ( |
| 69 | char *cache_name, |
| 70 | u16 object_size, |
| 71 | u16 max_depth, |
| 72 | struct acpi_memory_list **return_cache) |
| 73 | { |
| 74 | struct acpi_memory_list *cache; |
| 75 | |
| 76 | |
Robert Moore | f9f4601 | 2005-07-08 00:00:00 -0400 | [diff] [blame] | 77 | ACPI_FUNCTION_ENTRY (); |
| 78 | |
| 79 | |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 80 | if (!cache_name || !return_cache || (object_size < 16)) { |
| 81 | return (AE_BAD_PARAMETER); |
| 82 | } |
| 83 | |
| 84 | /* Create the cache object */ |
| 85 | |
| 86 | cache = acpi_os_allocate (sizeof (struct acpi_memory_list)); |
| 87 | if (!cache) { |
| 88 | return (AE_NO_MEMORY); |
| 89 | } |
| 90 | |
| 91 | /* Populate the cache object and return it */ |
| 92 | |
| 93 | ACPI_MEMSET (cache, 0, sizeof (struct acpi_memory_list)); |
| 94 | cache->link_offset = 8; |
| 95 | cache->list_name = cache_name; |
| 96 | cache->object_size = object_size; |
| 97 | cache->max_depth = max_depth; |
| 98 | |
| 99 | *return_cache = cache; |
| 100 | return (AE_OK); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /******************************************************************************* |
| 105 | * |
| 106 | * FUNCTION: acpi_os_purge_cache |
| 107 | * |
| 108 | * PARAMETERS: Cache - Handle to cache object |
| 109 | * |
| 110 | * RETURN: Status |
| 111 | * |
| 112 | * DESCRIPTION: Free all objects within the requested cache. |
| 113 | * |
| 114 | ******************************************************************************/ |
| 115 | |
| 116 | acpi_status |
| 117 | acpi_os_purge_cache ( |
| 118 | struct acpi_memory_list *cache) |
| 119 | { |
| 120 | char *next; |
| 121 | |
| 122 | |
| 123 | ACPI_FUNCTION_ENTRY (); |
| 124 | |
| 125 | |
| 126 | if (!cache) { |
| 127 | return (AE_BAD_PARAMETER); |
| 128 | } |
| 129 | |
| 130 | /* Walk the list of objects in this cache */ |
| 131 | |
| 132 | while (cache->list_head) { |
| 133 | /* Delete and unlink one cached state object */ |
| 134 | |
| 135 | next = *(ACPI_CAST_INDIRECT_PTR (char, |
| 136 | &(((char *) cache->list_head)[cache->link_offset]))); |
| 137 | ACPI_MEM_FREE (cache->list_head); |
| 138 | |
| 139 | cache->list_head = next; |
| 140 | cache->current_depth--; |
| 141 | } |
| 142 | |
| 143 | return (AE_OK); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /******************************************************************************* |
| 148 | * |
| 149 | * FUNCTION: acpi_os_delete_cache |
| 150 | * |
| 151 | * PARAMETERS: Cache - Handle to cache object |
| 152 | * |
| 153 | * RETURN: Status |
| 154 | * |
| 155 | * DESCRIPTION: Free all objects within the requested cache and delete the |
| 156 | * cache object. |
| 157 | * |
| 158 | ******************************************************************************/ |
| 159 | |
| 160 | acpi_status |
| 161 | acpi_os_delete_cache ( |
| 162 | struct acpi_memory_list *cache) |
| 163 | { |
| 164 | acpi_status status; |
| 165 | |
| 166 | |
Robert Moore | f9f4601 | 2005-07-08 00:00:00 -0400 | [diff] [blame] | 167 | ACPI_FUNCTION_ENTRY (); |
| 168 | |
| 169 | |
| 170 | /* Purge all objects in the cache */ |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 171 | |
| 172 | status = acpi_os_purge_cache (cache); |
| 173 | if (ACPI_FAILURE (status)) { |
| 174 | return (status); |
| 175 | } |
| 176 | |
| 177 | /* Now we can delete the cache object */ |
| 178 | |
| 179 | acpi_os_free (cache); |
| 180 | return (AE_OK); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /******************************************************************************* |
| 185 | * |
| 186 | * FUNCTION: acpi_os_release_object |
| 187 | * |
| 188 | * PARAMETERS: Cache - Handle to cache object |
| 189 | * Object - The object to be released |
| 190 | * |
| 191 | * RETURN: None |
| 192 | * |
| 193 | * DESCRIPTION: Release an object to the specified cache. If cache is full, |
| 194 | * the object is deleted. |
| 195 | * |
| 196 | ******************************************************************************/ |
| 197 | |
| 198 | acpi_status |
| 199 | acpi_os_release_object ( |
| 200 | struct acpi_memory_list *cache, |
| 201 | void *object) |
| 202 | { |
| 203 | acpi_status status; |
| 204 | |
| 205 | |
| 206 | ACPI_FUNCTION_ENTRY (); |
| 207 | |
| 208 | |
| 209 | if (!cache || !object) { |
| 210 | return (AE_BAD_PARAMETER); |
| 211 | } |
| 212 | |
| 213 | /* If cache is full, just free this object */ |
| 214 | |
| 215 | if (cache->current_depth >= cache->max_depth) { |
| 216 | ACPI_MEM_FREE (object); |
| 217 | ACPI_MEM_TRACKING (cache->total_freed++); |
| 218 | } |
| 219 | |
| 220 | /* Otherwise put this object back into the cache */ |
| 221 | |
| 222 | else { |
| 223 | status = acpi_ut_acquire_mutex (ACPI_MTX_CACHES); |
| 224 | if (ACPI_FAILURE (status)) { |
| 225 | return (status); |
| 226 | } |
| 227 | |
| 228 | /* Mark the object as cached */ |
| 229 | |
| 230 | ACPI_MEMSET (object, 0xCA, cache->object_size); |
| 231 | ACPI_SET_DESCRIPTOR_TYPE (object, ACPI_DESC_TYPE_CACHED); |
| 232 | |
| 233 | /* Put the object at the head of the cache list */ |
| 234 | |
| 235 | * (ACPI_CAST_INDIRECT_PTR (char, |
| 236 | &(((char *) object)[cache->link_offset]))) = cache->list_head; |
| 237 | cache->list_head = object; |
| 238 | cache->current_depth++; |
| 239 | |
| 240 | (void) acpi_ut_release_mutex (ACPI_MTX_CACHES); |
| 241 | } |
| 242 | |
| 243 | return (AE_OK); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /******************************************************************************* |
| 248 | * |
| 249 | * FUNCTION: acpi_os_acquire_object |
| 250 | * |
| 251 | * PARAMETERS: Cache - Handle to cache object |
| 252 | * |
| 253 | * RETURN: the acquired object. NULL on error |
| 254 | * |
| 255 | * DESCRIPTION: Get an object from the specified cache. If cache is empty, |
| 256 | * the object is allocated. |
| 257 | * |
| 258 | ******************************************************************************/ |
| 259 | |
| 260 | void * |
| 261 | acpi_os_acquire_object ( |
| 262 | struct acpi_memory_list *cache) |
| 263 | { |
| 264 | acpi_status status; |
| 265 | void *object; |
| 266 | |
| 267 | |
Robert Moore | f9f4601 | 2005-07-08 00:00:00 -0400 | [diff] [blame] | 268 | ACPI_FUNCTION_NAME ("os_acquire_object"); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 269 | |
| 270 | |
| 271 | if (!cache) { |
| 272 | return (NULL); |
| 273 | } |
| 274 | |
| 275 | status = acpi_ut_acquire_mutex (ACPI_MTX_CACHES); |
| 276 | if (ACPI_FAILURE (status)) { |
| 277 | return (NULL); |
| 278 | } |
| 279 | |
| 280 | ACPI_MEM_TRACKING (cache->requests++); |
| 281 | |
| 282 | /* Check the cache first */ |
| 283 | |
| 284 | if (cache->list_head) { |
| 285 | /* There is an object available, use it */ |
| 286 | |
| 287 | object = cache->list_head; |
| 288 | cache->list_head = *(ACPI_CAST_INDIRECT_PTR (char, |
| 289 | &(((char *) object)[cache->link_offset]))); |
| 290 | |
| 291 | cache->current_depth--; |
| 292 | |
| 293 | ACPI_MEM_TRACKING (cache->hits++); |
| 294 | ACPI_MEM_TRACKING (ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, |
Robert Moore | f9f4601 | 2005-07-08 00:00:00 -0400 | [diff] [blame] | 295 | "Object %p from %s cache\n", object, cache->list_name))); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 296 | |
| 297 | status = acpi_ut_release_mutex (ACPI_MTX_CACHES); |
| 298 | if (ACPI_FAILURE (status)) { |
| 299 | return (NULL); |
| 300 | } |
| 301 | |
| 302 | /* Clear (zero) the previously used Object */ |
| 303 | |
| 304 | ACPI_MEMSET (object, 0, cache->object_size); |
| 305 | } |
| 306 | else { |
| 307 | /* The cache is empty, create a new object */ |
| 308 | |
| 309 | ACPI_MEM_TRACKING (cache->total_allocated++); |
| 310 | |
| 311 | /* Avoid deadlock with ACPI_MEM_CALLOCATE */ |
| 312 | |
| 313 | status = acpi_ut_release_mutex (ACPI_MTX_CACHES); |
| 314 | if (ACPI_FAILURE (status)) { |
| 315 | return (NULL); |
| 316 | } |
| 317 | |
| 318 | object = ACPI_MEM_CALLOCATE (cache->object_size); |
| 319 | if (!object) { |
| 320 | return (NULL); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | return (object); |
| 325 | } |
| 326 | #endif /* ACPI_USE_LOCAL_CACHE */ |
| 327 | |
| 328 | |