utcache.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /******************************************************************************
  2. *
  3. * Module Name: utcache - local cache allocation routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, R. Byron Moore
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #define _COMPONENT ACPI_UTILITIES
  44. ACPI_MODULE_NAME("utcache")
  45. #ifdef ACPI_USE_LOCAL_CACHE
  46. /*******************************************************************************
  47. *
  48. * FUNCTION: acpi_os_create_cache
  49. *
  50. * PARAMETERS: cache_name - Ascii name for the cache
  51. * object_size - Size of each cached object
  52. * max_depth - Maximum depth of the cache (in objects)
  53. * return_cache - Where the new cache object is returned
  54. *
  55. * RETURN: Status
  56. *
  57. * DESCRIPTION: Create a cache object
  58. *
  59. ******************************************************************************/
  60. acpi_status
  61. acpi_os_create_cache(char *cache_name,
  62. u16 object_size,
  63. u16 max_depth, struct acpi_memory_list **return_cache)
  64. {
  65. struct acpi_memory_list *cache;
  66. ACPI_FUNCTION_ENTRY();
  67. if (!cache_name || !return_cache || (object_size < 16)) {
  68. return (AE_BAD_PARAMETER);
  69. }
  70. /* Create the cache object */
  71. cache = acpi_os_allocate(sizeof(struct acpi_memory_list));
  72. if (!cache) {
  73. return (AE_NO_MEMORY);
  74. }
  75. /* Populate the cache object and return it */
  76. ACPI_MEMSET(cache, 0, sizeof(struct acpi_memory_list));
  77. cache->link_offset = 8;
  78. cache->list_name = cache_name;
  79. cache->object_size = object_size;
  80. cache->max_depth = max_depth;
  81. *return_cache = cache;
  82. return (AE_OK);
  83. }
  84. /*******************************************************************************
  85. *
  86. * FUNCTION: acpi_os_purge_cache
  87. *
  88. * PARAMETERS: Cache - Handle to cache object
  89. *
  90. * RETURN: Status
  91. *
  92. * DESCRIPTION: Free all objects within the requested cache.
  93. *
  94. ******************************************************************************/
  95. acpi_status acpi_os_purge_cache(struct acpi_memory_list * cache)
  96. {
  97. char *next;
  98. ACPI_FUNCTION_ENTRY();
  99. if (!cache) {
  100. return (AE_BAD_PARAMETER);
  101. }
  102. /* Walk the list of objects in this cache */
  103. while (cache->list_head) {
  104. /* Delete and unlink one cached state object */
  105. next = *(ACPI_CAST_INDIRECT_PTR(char,
  106. &(((char *)cache->
  107. list_head)[cache->
  108. link_offset])));
  109. ACPI_MEM_FREE(cache->list_head);
  110. cache->list_head = next;
  111. cache->current_depth--;
  112. }
  113. return (AE_OK);
  114. }
  115. /*******************************************************************************
  116. *
  117. * FUNCTION: acpi_os_delete_cache
  118. *
  119. * PARAMETERS: Cache - Handle to cache object
  120. *
  121. * RETURN: Status
  122. *
  123. * DESCRIPTION: Free all objects within the requested cache and delete the
  124. * cache object.
  125. *
  126. ******************************************************************************/
  127. acpi_status acpi_os_delete_cache(struct acpi_memory_list * cache)
  128. {
  129. acpi_status status;
  130. ACPI_FUNCTION_ENTRY();
  131. /* Purge all objects in the cache */
  132. status = acpi_os_purge_cache(cache);
  133. if (ACPI_FAILURE(status)) {
  134. return (status);
  135. }
  136. /* Now we can delete the cache object */
  137. acpi_os_free(cache);
  138. return (AE_OK);
  139. }
  140. /*******************************************************************************
  141. *
  142. * FUNCTION: acpi_os_release_object
  143. *
  144. * PARAMETERS: Cache - Handle to cache object
  145. * Object - The object to be released
  146. *
  147. * RETURN: None
  148. *
  149. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  150. * the object is deleted.
  151. *
  152. ******************************************************************************/
  153. acpi_status
  154. acpi_os_release_object(struct acpi_memory_list * cache, void *object)
  155. {
  156. acpi_status status;
  157. ACPI_FUNCTION_ENTRY();
  158. if (!cache || !object) {
  159. return (AE_BAD_PARAMETER);
  160. }
  161. /* If cache is full, just free this object */
  162. if (cache->current_depth >= cache->max_depth) {
  163. ACPI_MEM_FREE(object);
  164. ACPI_MEM_TRACKING(cache->total_freed++);
  165. }
  166. /* Otherwise put this object back into the cache */
  167. else {
  168. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  169. if (ACPI_FAILURE(status)) {
  170. return (status);
  171. }
  172. /* Mark the object as cached */
  173. ACPI_MEMSET(object, 0xCA, cache->object_size);
  174. ACPI_SET_DESCRIPTOR_TYPE(object, ACPI_DESC_TYPE_CACHED);
  175. /* Put the object at the head of the cache list */
  176. *(ACPI_CAST_INDIRECT_PTR(char,
  177. &(((char *)object)[cache->
  178. link_offset]))) =
  179. cache->list_head;
  180. cache->list_head = object;
  181. cache->current_depth++;
  182. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  183. }
  184. return (AE_OK);
  185. }
  186. /*******************************************************************************
  187. *
  188. * FUNCTION: acpi_os_acquire_object
  189. *
  190. * PARAMETERS: Cache - Handle to cache object
  191. *
  192. * RETURN: the acquired object. NULL on error
  193. *
  194. * DESCRIPTION: Get an object from the specified cache. If cache is empty,
  195. * the object is allocated.
  196. *
  197. ******************************************************************************/
  198. void *acpi_os_acquire_object(struct acpi_memory_list *cache)
  199. {
  200. acpi_status status;
  201. void *object;
  202. ACPI_FUNCTION_NAME("os_acquire_object");
  203. if (!cache) {
  204. return (NULL);
  205. }
  206. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  207. if (ACPI_FAILURE(status)) {
  208. return (NULL);
  209. }
  210. ACPI_MEM_TRACKING(cache->requests++);
  211. /* Check the cache first */
  212. if (cache->list_head) {
  213. /* There is an object available, use it */
  214. object = cache->list_head;
  215. cache->list_head = *(ACPI_CAST_INDIRECT_PTR(char,
  216. &(((char *)
  217. object)[cache->
  218. link_offset])));
  219. cache->current_depth--;
  220. ACPI_MEM_TRACKING(cache->hits++);
  221. ACPI_MEM_TRACKING(ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  222. "Object %p from %s cache\n",
  223. object, cache->list_name)));
  224. status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
  225. if (ACPI_FAILURE(status)) {
  226. return (NULL);
  227. }
  228. /* Clear (zero) the previously used Object */
  229. ACPI_MEMSET(object, 0, cache->object_size);
  230. } else {
  231. /* The cache is empty, create a new object */
  232. ACPI_MEM_TRACKING(cache->total_allocated++);
  233. /* Avoid deadlock with ACPI_MEM_CALLOCATE */
  234. status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
  235. if (ACPI_FAILURE(status)) {
  236. return (NULL);
  237. }
  238. object = ACPI_MEM_CALLOCATE(cache->object_size);
  239. if (!object) {
  240. return (NULL);
  241. }
  242. }
  243. return (object);
  244. }
  245. #endif /* ACPI_USE_LOCAL_CACHE */