utcache.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /******************************************************************************
  2. *
  3. * Module Name: utcache - local cache allocation routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2012, 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. ACPI_MEMSET(cache, 0, sizeof(struct acpi_memory_list));
  78. cache->link_offset = 8;
  79. cache->list_name = cache_name;
  80. cache->object_size = object_size;
  81. cache->max_depth = max_depth;
  82. *return_cache = cache;
  83. return (AE_OK);
  84. }
  85. /*******************************************************************************
  86. *
  87. * FUNCTION: acpi_os_purge_cache
  88. *
  89. * PARAMETERS: cache - Handle to cache object
  90. *
  91. * RETURN: Status
  92. *
  93. * DESCRIPTION: Free all objects within the requested cache.
  94. *
  95. ******************************************************************************/
  96. acpi_status acpi_os_purge_cache(struct acpi_memory_list * cache)
  97. {
  98. char *next;
  99. acpi_status status;
  100. ACPI_FUNCTION_ENTRY();
  101. if (!cache) {
  102. return (AE_BAD_PARAMETER);
  103. }
  104. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  105. if (ACPI_FAILURE(status)) {
  106. return (status);
  107. }
  108. /* Walk the list of objects in this cache */
  109. while (cache->list_head) {
  110. /* Delete and unlink one cached state object */
  111. next = *(ACPI_CAST_INDIRECT_PTR(char,
  112. &(((char *)cache->
  113. list_head)[cache->
  114. link_offset])));
  115. ACPI_FREE(cache->list_head);
  116. cache->list_head = next;
  117. cache->current_depth--;
  118. }
  119. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  120. return (AE_OK);
  121. }
  122. /*******************************************************************************
  123. *
  124. * FUNCTION: acpi_os_delete_cache
  125. *
  126. * PARAMETERS: cache - Handle to cache object
  127. *
  128. * RETURN: Status
  129. *
  130. * DESCRIPTION: Free all objects within the requested cache and delete the
  131. * cache object.
  132. *
  133. ******************************************************************************/
  134. acpi_status acpi_os_delete_cache(struct acpi_memory_list * cache)
  135. {
  136. acpi_status status;
  137. ACPI_FUNCTION_ENTRY();
  138. /* Purge all objects in the cache */
  139. status = acpi_os_purge_cache(cache);
  140. if (ACPI_FAILURE(status)) {
  141. return (status);
  142. }
  143. /* Now we can delete the cache object */
  144. acpi_os_free(cache);
  145. return (AE_OK);
  146. }
  147. /*******************************************************************************
  148. *
  149. * FUNCTION: acpi_os_release_object
  150. *
  151. * PARAMETERS: cache - Handle to cache object
  152. * object - The object to be released
  153. *
  154. * RETURN: None
  155. *
  156. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  157. * the object is deleted.
  158. *
  159. ******************************************************************************/
  160. acpi_status
  161. acpi_os_release_object(struct acpi_memory_list * cache, void *object)
  162. {
  163. acpi_status status;
  164. ACPI_FUNCTION_ENTRY();
  165. if (!cache || !object) {
  166. return (AE_BAD_PARAMETER);
  167. }
  168. /* If cache is full, just free this object */
  169. if (cache->current_depth >= cache->max_depth) {
  170. ACPI_FREE(object);
  171. ACPI_MEM_TRACKING(cache->total_freed++);
  172. }
  173. /* Otherwise put this object back into the cache */
  174. else {
  175. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  176. if (ACPI_FAILURE(status)) {
  177. return (status);
  178. }
  179. /* Mark the object as cached */
  180. ACPI_MEMSET(object, 0xCA, cache->object_size);
  181. ACPI_SET_DESCRIPTOR_TYPE(object, ACPI_DESC_TYPE_CACHED);
  182. /* Put the object at the head of the cache list */
  183. *(ACPI_CAST_INDIRECT_PTR(char,
  184. &(((char *)object)[cache->
  185. link_offset]))) =
  186. cache->list_head;
  187. cache->list_head = object;
  188. cache->current_depth++;
  189. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  190. }
  191. return (AE_OK);
  192. }
  193. /*******************************************************************************
  194. *
  195. * FUNCTION: acpi_os_acquire_object
  196. *
  197. * PARAMETERS: cache - Handle to cache object
  198. *
  199. * RETURN: the acquired object. NULL on error
  200. *
  201. * DESCRIPTION: Get an object from the specified cache. If cache is empty,
  202. * the object is allocated.
  203. *
  204. ******************************************************************************/
  205. void *acpi_os_acquire_object(struct acpi_memory_list *cache)
  206. {
  207. acpi_status status;
  208. void *object;
  209. ACPI_FUNCTION_NAME(os_acquire_object);
  210. if (!cache) {
  211. return (NULL);
  212. }
  213. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  214. if (ACPI_FAILURE(status)) {
  215. return (NULL);
  216. }
  217. ACPI_MEM_TRACKING(cache->requests++);
  218. /* Check the cache first */
  219. if (cache->list_head) {
  220. /* There is an object available, use it */
  221. object = cache->list_head;
  222. cache->list_head = *(ACPI_CAST_INDIRECT_PTR(char,
  223. &(((char *)
  224. object)[cache->
  225. link_offset])));
  226. cache->current_depth--;
  227. ACPI_MEM_TRACKING(cache->hits++);
  228. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  229. "Object %p from %s cache\n", object,
  230. cache->list_name));
  231. status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
  232. if (ACPI_FAILURE(status)) {
  233. return (NULL);
  234. }
  235. /* Clear (zero) the previously used Object */
  236. ACPI_MEMSET(object, 0, cache->object_size);
  237. } else {
  238. /* The cache is empty, create a new object */
  239. ACPI_MEM_TRACKING(cache->total_allocated++);
  240. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  241. if ((cache->total_allocated - cache->total_freed) >
  242. cache->max_occupied) {
  243. cache->max_occupied =
  244. cache->total_allocated - cache->total_freed;
  245. }
  246. #endif
  247. /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
  248. status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
  249. if (ACPI_FAILURE(status)) {
  250. return (NULL);
  251. }
  252. object = ACPI_ALLOCATE_ZEROED(cache->object_size);
  253. if (!object) {
  254. return (NULL);
  255. }
  256. }
  257. return (object);
  258. }
  259. #endif /* ACPI_USE_LOCAL_CACHE */