utcache.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /******************************************************************************
  2. *
  3. * Module Name: utcache - local cache allocation routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  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. #include "accommon.h"
  44. #define _COMPONENT ACPI_UTILITIES
  45. ACPI_MODULE_NAME("utcache")
  46. #ifdef ACPI_USE_LOCAL_CACHE
  47. /*******************************************************************************
  48. *
  49. * FUNCTION: acpi_os_create_cache
  50. *
  51. * PARAMETERS: cache_name - Ascii name for the cache
  52. * object_size - Size of each cached object
  53. * max_depth - Maximum depth of the cache (in objects)
  54. * return_cache - Where the new cache object is returned
  55. *
  56. * RETURN: Status
  57. *
  58. * DESCRIPTION: Create a cache object
  59. *
  60. ******************************************************************************/
  61. acpi_status
  62. acpi_os_create_cache(char *cache_name,
  63. u16 object_size,
  64. u16 max_depth, struct acpi_memory_list **return_cache)
  65. {
  66. struct acpi_memory_list *cache;
  67. ACPI_FUNCTION_ENTRY();
  68. if (!cache_name || !return_cache || (object_size < 16)) {
  69. return (AE_BAD_PARAMETER);
  70. }
  71. /* Create the cache object */
  72. cache = acpi_os_allocate(sizeof(struct acpi_memory_list));
  73. if (!cache) {
  74. return (AE_NO_MEMORY);
  75. }
  76. /* Populate the cache object and return it */
  77. memset(cache, 0, sizeof(struct acpi_memory_list));
  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. void *next;
  98. acpi_status status;
  99. ACPI_FUNCTION_ENTRY();
  100. if (!cache) {
  101. return (AE_BAD_PARAMETER);
  102. }
  103. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  104. if (ACPI_FAILURE(status)) {
  105. return (status);
  106. }
  107. /* Walk the list of objects in this cache */
  108. while (cache->list_head) {
  109. /* Delete and unlink one cached state object */
  110. next = ACPI_GET_DESCRIPTOR_PTR(cache->list_head);
  111. ACPI_FREE(cache->list_head);
  112. cache->list_head = next;
  113. cache->current_depth--;
  114. }
  115. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  116. return (AE_OK);
  117. }
  118. /*******************************************************************************
  119. *
  120. * FUNCTION: acpi_os_delete_cache
  121. *
  122. * PARAMETERS: cache - Handle to cache object
  123. *
  124. * RETURN: Status
  125. *
  126. * DESCRIPTION: Free all objects within the requested cache and delete the
  127. * cache object.
  128. *
  129. ******************************************************************************/
  130. acpi_status acpi_os_delete_cache(struct acpi_memory_list * cache)
  131. {
  132. acpi_status status;
  133. ACPI_FUNCTION_ENTRY();
  134. /* Purge all objects in the cache */
  135. status = acpi_os_purge_cache(cache);
  136. if (ACPI_FAILURE(status)) {
  137. return (status);
  138. }
  139. /* Now we can delete the cache object */
  140. acpi_os_free(cache);
  141. return (AE_OK);
  142. }
  143. /*******************************************************************************
  144. *
  145. * FUNCTION: acpi_os_release_object
  146. *
  147. * PARAMETERS: cache - Handle to cache object
  148. * object - The object to be released
  149. *
  150. * RETURN: None
  151. *
  152. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  153. * the object is deleted.
  154. *
  155. ******************************************************************************/
  156. acpi_status
  157. acpi_os_release_object(struct acpi_memory_list * cache, void *object)
  158. {
  159. acpi_status status;
  160. ACPI_FUNCTION_ENTRY();
  161. if (!cache || !object) {
  162. return (AE_BAD_PARAMETER);
  163. }
  164. /* If cache is full, just free this object */
  165. if (cache->current_depth >= cache->max_depth) {
  166. ACPI_FREE(object);
  167. ACPI_MEM_TRACKING(cache->total_freed++);
  168. }
  169. /* Otherwise put this object back into the cache */
  170. else {
  171. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  172. if (ACPI_FAILURE(status)) {
  173. return (status);
  174. }
  175. /* Mark the object as cached */
  176. memset(object, 0xCA, cache->object_size);
  177. ACPI_SET_DESCRIPTOR_TYPE(object, ACPI_DESC_TYPE_CACHED);
  178. /* Put the object at the head of the cache list */
  179. ACPI_SET_DESCRIPTOR_PTR(object, 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_PTR(NULL);
  205. }
  206. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  207. if (ACPI_FAILURE(status)) {
  208. return_PTR(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_GET_DESCRIPTOR_PTR(object);
  216. cache->current_depth--;
  217. ACPI_MEM_TRACKING(cache->hits++);
  218. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  219. "Object %p from %s cache\n", object,
  220. cache->list_name));
  221. status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
  222. if (ACPI_FAILURE(status)) {
  223. return_PTR(NULL);
  224. }
  225. /* Clear (zero) the previously used Object */
  226. memset(object, 0, cache->object_size);
  227. } else {
  228. /* The cache is empty, create a new object */
  229. ACPI_MEM_TRACKING(cache->total_allocated++);
  230. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  231. if ((cache->total_allocated - cache->total_freed) >
  232. cache->max_occupied) {
  233. cache->max_occupied =
  234. cache->total_allocated - cache->total_freed;
  235. }
  236. #endif
  237. /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
  238. status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
  239. if (ACPI_FAILURE(status)) {
  240. return_PTR(NULL);
  241. }
  242. object = ACPI_ALLOCATE_ZEROED(cache->object_size);
  243. if (!object) {
  244. return_PTR(NULL);
  245. }
  246. }
  247. return_PTR(object);
  248. }
  249. #endif /* ACPI_USE_LOCAL_CACHE */