utcache.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /******************************************************************************
  2. *
  3. * Module Name: utcache - local cache allocation routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, 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 acpi_os_release_object(struct acpi_memory_list *cache, void *object)
  157. {
  158. acpi_status status;
  159. ACPI_FUNCTION_ENTRY();
  160. if (!cache || !object) {
  161. return (AE_BAD_PARAMETER);
  162. }
  163. /* If cache is full, just free this object */
  164. if (cache->current_depth >= cache->max_depth) {
  165. ACPI_FREE(object);
  166. ACPI_MEM_TRACKING(cache->total_freed++);
  167. }
  168. /* Otherwise put this object back into the cache */
  169. else {
  170. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  171. if (ACPI_FAILURE(status)) {
  172. return (status);
  173. }
  174. /* Mark the object as cached */
  175. memset(object, 0xCA, cache->object_size);
  176. ACPI_SET_DESCRIPTOR_TYPE(object, ACPI_DESC_TYPE_CACHED);
  177. /* Put the object at the head of the cache list */
  178. ACPI_SET_DESCRIPTOR_PTR(object, cache->list_head);
  179. cache->list_head = object;
  180. cache->current_depth++;
  181. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  182. }
  183. return (AE_OK);
  184. }
  185. /*******************************************************************************
  186. *
  187. * FUNCTION: acpi_os_acquire_object
  188. *
  189. * PARAMETERS: cache - Handle to cache object
  190. *
  191. * RETURN: the acquired object. NULL on error
  192. *
  193. * DESCRIPTION: Get an object from the specified cache. If cache is empty,
  194. * the object is allocated.
  195. *
  196. ******************************************************************************/
  197. void *acpi_os_acquire_object(struct acpi_memory_list *cache)
  198. {
  199. acpi_status status;
  200. void *object;
  201. ACPI_FUNCTION_TRACE(os_acquire_object);
  202. if (!cache) {
  203. return_PTR(NULL);
  204. }
  205. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  206. if (ACPI_FAILURE(status)) {
  207. return_PTR(NULL);
  208. }
  209. ACPI_MEM_TRACKING(cache->requests++);
  210. /* Check the cache first */
  211. if (cache->list_head) {
  212. /* There is an object available, use it */
  213. object = cache->list_head;
  214. cache->list_head = ACPI_GET_DESCRIPTOR_PTR(object);
  215. cache->current_depth--;
  216. ACPI_MEM_TRACKING(cache->hits++);
  217. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  218. "Object %p from %s cache\n", object,
  219. cache->list_name));
  220. status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
  221. if (ACPI_FAILURE(status)) {
  222. return_PTR(NULL);
  223. }
  224. /* Clear (zero) the previously used Object */
  225. memset(object, 0, cache->object_size);
  226. } else {
  227. /* The cache is empty, create a new object */
  228. ACPI_MEM_TRACKING(cache->total_allocated++);
  229. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  230. if ((cache->total_allocated - cache->total_freed) >
  231. cache->max_occupied) {
  232. cache->max_occupied =
  233. cache->total_allocated - cache->total_freed;
  234. }
  235. #endif
  236. /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
  237. status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
  238. if (ACPI_FAILURE(status)) {
  239. return_PTR(NULL);
  240. }
  241. object = ACPI_ALLOCATE_ZEROED(cache->object_size);
  242. if (!object) {
  243. return_PTR(NULL);
  244. }
  245. }
  246. return_PTR(object);
  247. }
  248. #endif /* ACPI_USE_LOCAL_CACHE */