utalloc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /******************************************************************************
  2. *
  3. * Module Name: utalloc - local memory allocation routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2017, 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. #include "acdebug.h"
  45. #define _COMPONENT ACPI_UTILITIES
  46. ACPI_MODULE_NAME("utalloc")
  47. #if !defined (USE_NATIVE_ALLOCATE_ZEROED)
  48. /*******************************************************************************
  49. *
  50. * FUNCTION: acpi_os_allocate_zeroed
  51. *
  52. * PARAMETERS: size - Size of the allocation
  53. *
  54. * RETURN: Address of the allocated memory on success, NULL on failure.
  55. *
  56. * DESCRIPTION: Subsystem equivalent of calloc. Allocate and zero memory.
  57. * This is the default implementation. Can be overridden via the
  58. * USE_NATIVE_ALLOCATE_ZEROED flag.
  59. *
  60. ******************************************************************************/
  61. void *acpi_os_allocate_zeroed(acpi_size size)
  62. {
  63. void *allocation;
  64. ACPI_FUNCTION_ENTRY();
  65. allocation = acpi_os_allocate(size);
  66. if (allocation) {
  67. /* Clear the memory block */
  68. memset(allocation, 0, size);
  69. }
  70. return (allocation);
  71. }
  72. #endif /* !USE_NATIVE_ALLOCATE_ZEROED */
  73. /*******************************************************************************
  74. *
  75. * FUNCTION: acpi_ut_create_caches
  76. *
  77. * PARAMETERS: None
  78. *
  79. * RETURN: Status
  80. *
  81. * DESCRIPTION: Create all local caches
  82. *
  83. ******************************************************************************/
  84. acpi_status acpi_ut_create_caches(void)
  85. {
  86. acpi_status status;
  87. /* Object Caches, for frequently used objects */
  88. status =
  89. acpi_os_create_cache("Acpi-Namespace",
  90. sizeof(struct acpi_namespace_node),
  91. ACPI_MAX_NAMESPACE_CACHE_DEPTH,
  92. &acpi_gbl_namespace_cache);
  93. if (ACPI_FAILURE(status)) {
  94. return (status);
  95. }
  96. status =
  97. acpi_os_create_cache("Acpi-State", sizeof(union acpi_generic_state),
  98. ACPI_MAX_STATE_CACHE_DEPTH,
  99. &acpi_gbl_state_cache);
  100. if (ACPI_FAILURE(status)) {
  101. return (status);
  102. }
  103. status =
  104. acpi_os_create_cache("Acpi-Parse",
  105. sizeof(struct acpi_parse_obj_common),
  106. ACPI_MAX_PARSE_CACHE_DEPTH,
  107. &acpi_gbl_ps_node_cache);
  108. if (ACPI_FAILURE(status)) {
  109. return (status);
  110. }
  111. status =
  112. acpi_os_create_cache("Acpi-ParseExt",
  113. sizeof(struct acpi_parse_obj_named),
  114. ACPI_MAX_EXTPARSE_CACHE_DEPTH,
  115. &acpi_gbl_ps_node_ext_cache);
  116. if (ACPI_FAILURE(status)) {
  117. return (status);
  118. }
  119. status =
  120. acpi_os_create_cache("Acpi-Operand",
  121. sizeof(union acpi_operand_object),
  122. ACPI_MAX_OBJECT_CACHE_DEPTH,
  123. &acpi_gbl_operand_cache);
  124. if (ACPI_FAILURE(status)) {
  125. return (status);
  126. }
  127. #ifdef ACPI_ASL_COMPILER
  128. /*
  129. * For use with the ASL-/ASL+ option. This cache keeps track of regular
  130. * 0xA9 0x01 comments.
  131. */
  132. status =
  133. acpi_os_create_cache("Acpi-Comment",
  134. sizeof(struct acpi_comment_node),
  135. ACPI_MAX_COMMENT_CACHE_DEPTH,
  136. &acpi_gbl_reg_comment_cache);
  137. if (ACPI_FAILURE(status)) {
  138. return (status);
  139. }
  140. /*
  141. * This cache keeps track of the starting addresses of where the comments
  142. * lie. This helps prevent duplication of comments.
  143. */
  144. status =
  145. acpi_os_create_cache("Acpi-Comment-Addr",
  146. sizeof(struct acpi_comment_addr_node),
  147. ACPI_MAX_COMMENT_CACHE_DEPTH,
  148. &acpi_gbl_comment_addr_cache);
  149. if (ACPI_FAILURE(status)) {
  150. return (status);
  151. }
  152. /*
  153. * This cache will be used for nodes that represent files.
  154. */
  155. status =
  156. acpi_os_create_cache("Acpi-File", sizeof(struct acpi_file_node),
  157. ACPI_MAX_COMMENT_CACHE_DEPTH,
  158. &acpi_gbl_file_cache);
  159. if (ACPI_FAILURE(status)) {
  160. return (status);
  161. }
  162. #endif
  163. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  164. /* Memory allocation lists */
  165. status = acpi_ut_create_list("Acpi-Global", 0, &acpi_gbl_global_list);
  166. if (ACPI_FAILURE(status)) {
  167. return (status);
  168. }
  169. status =
  170. acpi_ut_create_list("Acpi-Namespace",
  171. sizeof(struct acpi_namespace_node),
  172. &acpi_gbl_ns_node_list);
  173. if (ACPI_FAILURE(status)) {
  174. return (status);
  175. }
  176. #endif
  177. return (AE_OK);
  178. }
  179. /*******************************************************************************
  180. *
  181. * FUNCTION: acpi_ut_delete_caches
  182. *
  183. * PARAMETERS: None
  184. *
  185. * RETURN: Status
  186. *
  187. * DESCRIPTION: Purge and delete all local caches
  188. *
  189. ******************************************************************************/
  190. acpi_status acpi_ut_delete_caches(void)
  191. {
  192. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  193. char buffer[7];
  194. if (acpi_gbl_display_final_mem_stats) {
  195. strcpy(buffer, "MEMORY");
  196. (void)acpi_db_display_statistics(buffer);
  197. }
  198. #endif
  199. (void)acpi_os_delete_cache(acpi_gbl_namespace_cache);
  200. acpi_gbl_namespace_cache = NULL;
  201. (void)acpi_os_delete_cache(acpi_gbl_state_cache);
  202. acpi_gbl_state_cache = NULL;
  203. (void)acpi_os_delete_cache(acpi_gbl_operand_cache);
  204. acpi_gbl_operand_cache = NULL;
  205. (void)acpi_os_delete_cache(acpi_gbl_ps_node_cache);
  206. acpi_gbl_ps_node_cache = NULL;
  207. (void)acpi_os_delete_cache(acpi_gbl_ps_node_ext_cache);
  208. acpi_gbl_ps_node_ext_cache = NULL;
  209. #ifdef ACPI_ASL_COMPILER
  210. (void)acpi_os_delete_cache(acpi_gbl_reg_comment_cache);
  211. acpi_gbl_reg_comment_cache = NULL;
  212. (void)acpi_os_delete_cache(acpi_gbl_comment_addr_cache);
  213. acpi_gbl_comment_addr_cache = NULL;
  214. (void)acpi_os_delete_cache(acpi_gbl_file_cache);
  215. acpi_gbl_file_cache = NULL;
  216. #endif
  217. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  218. /* Debug only - display leftover memory allocation, if any */
  219. acpi_ut_dump_allocations(ACPI_UINT32_MAX, NULL);
  220. /* Free memory lists */
  221. acpi_os_free(acpi_gbl_global_list);
  222. acpi_gbl_global_list = NULL;
  223. acpi_os_free(acpi_gbl_ns_node_list);
  224. acpi_gbl_ns_node_list = NULL;
  225. #endif
  226. return (AE_OK);
  227. }
  228. /*******************************************************************************
  229. *
  230. * FUNCTION: acpi_ut_validate_buffer
  231. *
  232. * PARAMETERS: buffer - Buffer descriptor to be validated
  233. *
  234. * RETURN: Status
  235. *
  236. * DESCRIPTION: Perform parameter validation checks on an struct acpi_buffer
  237. *
  238. ******************************************************************************/
  239. acpi_status acpi_ut_validate_buffer(struct acpi_buffer *buffer)
  240. {
  241. /* Obviously, the structure pointer must be valid */
  242. if (!buffer) {
  243. return (AE_BAD_PARAMETER);
  244. }
  245. /* Special semantics for the length */
  246. if ((buffer->length == ACPI_NO_BUFFER) ||
  247. (buffer->length == ACPI_ALLOCATE_BUFFER) ||
  248. (buffer->length == ACPI_ALLOCATE_LOCAL_BUFFER)) {
  249. return (AE_OK);
  250. }
  251. /* Length is valid, the buffer pointer must be also */
  252. if (!buffer->pointer) {
  253. return (AE_BAD_PARAMETER);
  254. }
  255. return (AE_OK);
  256. }
  257. /*******************************************************************************
  258. *
  259. * FUNCTION: acpi_ut_initialize_buffer
  260. *
  261. * PARAMETERS: buffer - Buffer to be validated
  262. * required_length - Length needed
  263. *
  264. * RETURN: Status
  265. *
  266. * DESCRIPTION: Validate that the buffer is of the required length or
  267. * allocate a new buffer. Returned buffer is always zeroed.
  268. *
  269. ******************************************************************************/
  270. acpi_status
  271. acpi_ut_initialize_buffer(struct acpi_buffer *buffer, acpi_size required_length)
  272. {
  273. acpi_size input_buffer_length;
  274. /* Parameter validation */
  275. if (!buffer || !required_length) {
  276. return (AE_BAD_PARAMETER);
  277. }
  278. /*
  279. * Buffer->Length is used as both an input and output parameter. Get the
  280. * input actual length and set the output required buffer length.
  281. */
  282. input_buffer_length = buffer->length;
  283. buffer->length = required_length;
  284. /*
  285. * The input buffer length contains the actual buffer length, or the type
  286. * of buffer to be allocated by this routine.
  287. */
  288. switch (input_buffer_length) {
  289. case ACPI_NO_BUFFER:
  290. /* Return the exception (and the required buffer length) */
  291. return (AE_BUFFER_OVERFLOW);
  292. case ACPI_ALLOCATE_BUFFER:
  293. /*
  294. * Allocate a new buffer. We directectly call acpi_os_allocate here to
  295. * purposefully bypass the (optionally enabled) internal allocation
  296. * tracking mechanism since we only want to track internal
  297. * allocations. Note: The caller should use acpi_os_free to free this
  298. * buffer created via ACPI_ALLOCATE_BUFFER.
  299. */
  300. buffer->pointer = acpi_os_allocate(required_length);
  301. break;
  302. case ACPI_ALLOCATE_LOCAL_BUFFER:
  303. /* Allocate a new buffer with local interface to allow tracking */
  304. buffer->pointer = ACPI_ALLOCATE(required_length);
  305. break;
  306. default:
  307. /* Existing buffer: Validate the size of the buffer */
  308. if (input_buffer_length < required_length) {
  309. return (AE_BUFFER_OVERFLOW);
  310. }
  311. break;
  312. }
  313. /* Validate allocation from above or input buffer pointer */
  314. if (!buffer->pointer) {
  315. return (AE_NO_MEMORY);
  316. }
  317. /* Have a valid buffer, clear it */
  318. memset(buffer->pointer, 0, required_length);
  319. return (AE_OK);
  320. }