utalloc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /******************************************************************************
  2. *
  3. * Module Name: utalloc - local memory 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. #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_DBG_TRACK_ALLOCATIONS
  128. /* Memory allocation lists */
  129. status = acpi_ut_create_list("Acpi-Global", 0, &acpi_gbl_global_list);
  130. if (ACPI_FAILURE(status)) {
  131. return (status);
  132. }
  133. status =
  134. acpi_ut_create_list("Acpi-Namespace",
  135. sizeof(struct acpi_namespace_node),
  136. &acpi_gbl_ns_node_list);
  137. if (ACPI_FAILURE(status)) {
  138. return (status);
  139. }
  140. #endif
  141. return (AE_OK);
  142. }
  143. /*******************************************************************************
  144. *
  145. * FUNCTION: acpi_ut_delete_caches
  146. *
  147. * PARAMETERS: None
  148. *
  149. * RETURN: Status
  150. *
  151. * DESCRIPTION: Purge and delete all local caches
  152. *
  153. ******************************************************************************/
  154. acpi_status acpi_ut_delete_caches(void)
  155. {
  156. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  157. char buffer[7];
  158. if (acpi_gbl_display_final_mem_stats) {
  159. strcpy(buffer, "MEMORY");
  160. (void)acpi_db_display_statistics(buffer);
  161. }
  162. #endif
  163. (void)acpi_os_delete_cache(acpi_gbl_namespace_cache);
  164. acpi_gbl_namespace_cache = NULL;
  165. (void)acpi_os_delete_cache(acpi_gbl_state_cache);
  166. acpi_gbl_state_cache = NULL;
  167. (void)acpi_os_delete_cache(acpi_gbl_operand_cache);
  168. acpi_gbl_operand_cache = NULL;
  169. (void)acpi_os_delete_cache(acpi_gbl_ps_node_cache);
  170. acpi_gbl_ps_node_cache = NULL;
  171. (void)acpi_os_delete_cache(acpi_gbl_ps_node_ext_cache);
  172. acpi_gbl_ps_node_ext_cache = NULL;
  173. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  174. /* Debug only - display leftover memory allocation, if any */
  175. acpi_ut_dump_allocations(ACPI_UINT32_MAX, NULL);
  176. /* Free memory lists */
  177. acpi_os_free(acpi_gbl_global_list);
  178. acpi_gbl_global_list = NULL;
  179. acpi_os_free(acpi_gbl_ns_node_list);
  180. acpi_gbl_ns_node_list = NULL;
  181. #endif
  182. return (AE_OK);
  183. }
  184. /*******************************************************************************
  185. *
  186. * FUNCTION: acpi_ut_validate_buffer
  187. *
  188. * PARAMETERS: buffer - Buffer descriptor to be validated
  189. *
  190. * RETURN: Status
  191. *
  192. * DESCRIPTION: Perform parameter validation checks on an struct acpi_buffer
  193. *
  194. ******************************************************************************/
  195. acpi_status acpi_ut_validate_buffer(struct acpi_buffer * buffer)
  196. {
  197. /* Obviously, the structure pointer must be valid */
  198. if (!buffer) {
  199. return (AE_BAD_PARAMETER);
  200. }
  201. /* Special semantics for the length */
  202. if ((buffer->length == ACPI_NO_BUFFER) ||
  203. (buffer->length == ACPI_ALLOCATE_BUFFER) ||
  204. (buffer->length == ACPI_ALLOCATE_LOCAL_BUFFER)) {
  205. return (AE_OK);
  206. }
  207. /* Length is valid, the buffer pointer must be also */
  208. if (!buffer->pointer) {
  209. return (AE_BAD_PARAMETER);
  210. }
  211. return (AE_OK);
  212. }
  213. /*******************************************************************************
  214. *
  215. * FUNCTION: acpi_ut_initialize_buffer
  216. *
  217. * PARAMETERS: buffer - Buffer to be validated
  218. * required_length - Length needed
  219. *
  220. * RETURN: Status
  221. *
  222. * DESCRIPTION: Validate that the buffer is of the required length or
  223. * allocate a new buffer. Returned buffer is always zeroed.
  224. *
  225. ******************************************************************************/
  226. acpi_status
  227. acpi_ut_initialize_buffer(struct acpi_buffer * buffer,
  228. acpi_size required_length)
  229. {
  230. acpi_size input_buffer_length;
  231. /* Parameter validation */
  232. if (!buffer || !required_length) {
  233. return (AE_BAD_PARAMETER);
  234. }
  235. /*
  236. * Buffer->Length is used as both an input and output parameter. Get the
  237. * input actual length and set the output required buffer length.
  238. */
  239. input_buffer_length = buffer->length;
  240. buffer->length = required_length;
  241. /*
  242. * The input buffer length contains the actual buffer length, or the type
  243. * of buffer to be allocated by this routine.
  244. */
  245. switch (input_buffer_length) {
  246. case ACPI_NO_BUFFER:
  247. /* Return the exception (and the required buffer length) */
  248. return (AE_BUFFER_OVERFLOW);
  249. case ACPI_ALLOCATE_BUFFER:
  250. /*
  251. * Allocate a new buffer. We directectly call acpi_os_allocate here to
  252. * purposefully bypass the (optionally enabled) internal allocation
  253. * tracking mechanism since we only want to track internal
  254. * allocations. Note: The caller should use acpi_os_free to free this
  255. * buffer created via ACPI_ALLOCATE_BUFFER.
  256. */
  257. buffer->pointer = acpi_os_allocate(required_length);
  258. break;
  259. case ACPI_ALLOCATE_LOCAL_BUFFER:
  260. /* Allocate a new buffer with local interface to allow tracking */
  261. buffer->pointer = ACPI_ALLOCATE(required_length);
  262. break;
  263. default:
  264. /* Existing buffer: Validate the size of the buffer */
  265. if (input_buffer_length < required_length) {
  266. return (AE_BUFFER_OVERFLOW);
  267. }
  268. break;
  269. }
  270. /* Validate allocation from above or input buffer pointer */
  271. if (!buffer->pointer) {
  272. return (AE_NO_MEMORY);
  273. }
  274. /* Have a valid buffer, clear it */
  275. memset(buffer->pointer, 0, required_length);
  276. return (AE_OK);
  277. }