utalloc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /******************************************************************************
  2. *
  3. * Module Name: utalloc - local memory 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. #include "acdebug.h"
  45. #define _COMPONENT ACPI_UTILITIES
  46. ACPI_MODULE_NAME("utalloc")
  47. /*******************************************************************************
  48. *
  49. * FUNCTION: acpi_ut_create_caches
  50. *
  51. * PARAMETERS: None
  52. *
  53. * RETURN: Status
  54. *
  55. * DESCRIPTION: Create all local caches
  56. *
  57. ******************************************************************************/
  58. acpi_status acpi_ut_create_caches(void)
  59. {
  60. acpi_status status;
  61. /* Object Caches, for frequently used objects */
  62. status =
  63. acpi_os_create_cache("Acpi-Namespace",
  64. sizeof(struct acpi_namespace_node),
  65. ACPI_MAX_NAMESPACE_CACHE_DEPTH,
  66. &acpi_gbl_namespace_cache);
  67. if (ACPI_FAILURE(status)) {
  68. return (status);
  69. }
  70. status =
  71. acpi_os_create_cache("Acpi-State", sizeof(union acpi_generic_state),
  72. ACPI_MAX_STATE_CACHE_DEPTH,
  73. &acpi_gbl_state_cache);
  74. if (ACPI_FAILURE(status)) {
  75. return (status);
  76. }
  77. status =
  78. acpi_os_create_cache("Acpi-Parse",
  79. sizeof(struct acpi_parse_obj_common),
  80. ACPI_MAX_PARSE_CACHE_DEPTH,
  81. &acpi_gbl_ps_node_cache);
  82. if (ACPI_FAILURE(status)) {
  83. return (status);
  84. }
  85. status =
  86. acpi_os_create_cache("Acpi-ParseExt",
  87. sizeof(struct acpi_parse_obj_named),
  88. ACPI_MAX_EXTPARSE_CACHE_DEPTH,
  89. &acpi_gbl_ps_node_ext_cache);
  90. if (ACPI_FAILURE(status)) {
  91. return (status);
  92. }
  93. status =
  94. acpi_os_create_cache("Acpi-Operand",
  95. sizeof(union acpi_operand_object),
  96. ACPI_MAX_OBJECT_CACHE_DEPTH,
  97. &acpi_gbl_operand_cache);
  98. if (ACPI_FAILURE(status)) {
  99. return (status);
  100. }
  101. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  102. /* Memory allocation lists */
  103. status = acpi_ut_create_list("Acpi-Global", 0, &acpi_gbl_global_list);
  104. if (ACPI_FAILURE(status)) {
  105. return (status);
  106. }
  107. status =
  108. acpi_ut_create_list("Acpi-Namespace",
  109. sizeof(struct acpi_namespace_node),
  110. &acpi_gbl_ns_node_list);
  111. if (ACPI_FAILURE(status)) {
  112. return (status);
  113. }
  114. #endif
  115. return (AE_OK);
  116. }
  117. /*******************************************************************************
  118. *
  119. * FUNCTION: acpi_ut_delete_caches
  120. *
  121. * PARAMETERS: None
  122. *
  123. * RETURN: Status
  124. *
  125. * DESCRIPTION: Purge and delete all local caches
  126. *
  127. ******************************************************************************/
  128. acpi_status acpi_ut_delete_caches(void)
  129. {
  130. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  131. char buffer[7];
  132. if (acpi_gbl_display_final_mem_stats) {
  133. ACPI_STRCPY(buffer, "MEMORY");
  134. (void)acpi_db_display_statistics(buffer);
  135. }
  136. #endif
  137. (void)acpi_os_delete_cache(acpi_gbl_namespace_cache);
  138. acpi_gbl_namespace_cache = NULL;
  139. (void)acpi_os_delete_cache(acpi_gbl_state_cache);
  140. acpi_gbl_state_cache = NULL;
  141. (void)acpi_os_delete_cache(acpi_gbl_operand_cache);
  142. acpi_gbl_operand_cache = NULL;
  143. (void)acpi_os_delete_cache(acpi_gbl_ps_node_cache);
  144. acpi_gbl_ps_node_cache = NULL;
  145. (void)acpi_os_delete_cache(acpi_gbl_ps_node_ext_cache);
  146. acpi_gbl_ps_node_ext_cache = NULL;
  147. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  148. /* Debug only - display leftover memory allocation, if any */
  149. acpi_ut_dump_allocations(ACPI_UINT32_MAX, NULL);
  150. /* Free memory lists */
  151. ACPI_FREE(acpi_gbl_global_list);
  152. acpi_gbl_global_list = NULL;
  153. ACPI_FREE(acpi_gbl_ns_node_list);
  154. acpi_gbl_ns_node_list = NULL;
  155. #endif
  156. return (AE_OK);
  157. }
  158. /*******************************************************************************
  159. *
  160. * FUNCTION: acpi_ut_validate_buffer
  161. *
  162. * PARAMETERS: Buffer - Buffer descriptor to be validated
  163. *
  164. * RETURN: Status
  165. *
  166. * DESCRIPTION: Perform parameter validation checks on an struct acpi_buffer
  167. *
  168. ******************************************************************************/
  169. acpi_status acpi_ut_validate_buffer(struct acpi_buffer * buffer)
  170. {
  171. /* Obviously, the structure pointer must be valid */
  172. if (!buffer) {
  173. return (AE_BAD_PARAMETER);
  174. }
  175. /* Special semantics for the length */
  176. if ((buffer->length == ACPI_NO_BUFFER) ||
  177. (buffer->length == ACPI_ALLOCATE_BUFFER) ||
  178. (buffer->length == ACPI_ALLOCATE_LOCAL_BUFFER)) {
  179. return (AE_OK);
  180. }
  181. /* Length is valid, the buffer pointer must be also */
  182. if (!buffer->pointer) {
  183. return (AE_BAD_PARAMETER);
  184. }
  185. return (AE_OK);
  186. }
  187. /*******************************************************************************
  188. *
  189. * FUNCTION: acpi_ut_initialize_buffer
  190. *
  191. * PARAMETERS: Buffer - Buffer to be validated
  192. * required_length - Length needed
  193. *
  194. * RETURN: Status
  195. *
  196. * DESCRIPTION: Validate that the buffer is of the required length or
  197. * allocate a new buffer. Returned buffer is always zeroed.
  198. *
  199. ******************************************************************************/
  200. acpi_status
  201. acpi_ut_initialize_buffer(struct acpi_buffer * buffer,
  202. acpi_size required_length)
  203. {
  204. acpi_size input_buffer_length;
  205. /* Parameter validation */
  206. if (!buffer || !required_length) {
  207. return (AE_BAD_PARAMETER);
  208. }
  209. /*
  210. * Buffer->Length is used as both an input and output parameter. Get the
  211. * input actual length and set the output required buffer length.
  212. */
  213. input_buffer_length = buffer->length;
  214. buffer->length = required_length;
  215. /*
  216. * The input buffer length contains the actual buffer length, or the type
  217. * of buffer to be allocated by this routine.
  218. */
  219. switch (input_buffer_length) {
  220. case ACPI_NO_BUFFER:
  221. /* Return the exception (and the required buffer length) */
  222. return (AE_BUFFER_OVERFLOW);
  223. case ACPI_ALLOCATE_BUFFER:
  224. /* Allocate a new buffer */
  225. buffer->pointer = acpi_os_allocate(required_length);
  226. break;
  227. case ACPI_ALLOCATE_LOCAL_BUFFER:
  228. /* Allocate a new buffer with local interface to allow tracking */
  229. buffer->pointer = ACPI_ALLOCATE(required_length);
  230. break;
  231. default:
  232. /* Existing buffer: Validate the size of the buffer */
  233. if (input_buffer_length < required_length) {
  234. return (AE_BUFFER_OVERFLOW);
  235. }
  236. break;
  237. }
  238. /* Validate allocation from above or input buffer pointer */
  239. if (!buffer->pointer) {
  240. return (AE_NO_MEMORY);
  241. }
  242. /* Have a valid buffer, clear it */
  243. ACPI_MEMSET(buffer->pointer, 0, required_length);
  244. return (AE_OK);
  245. }
  246. #ifdef NOT_USED_BY_LINUX
  247. /*******************************************************************************
  248. *
  249. * FUNCTION: acpi_ut_allocate
  250. *
  251. * PARAMETERS: Size - Size of the allocation
  252. * Component - Component type of caller
  253. * Module - Source file name of caller
  254. * Line - Line number of caller
  255. *
  256. * RETURN: Address of the allocated memory on success, NULL on failure.
  257. *
  258. * DESCRIPTION: Subsystem equivalent of malloc.
  259. *
  260. ******************************************************************************/
  261. void *acpi_ut_allocate(acpi_size size,
  262. u32 component, const char *module, u32 line)
  263. {
  264. void *allocation;
  265. ACPI_FUNCTION_TRACE_U32(ut_allocate, size);
  266. /* Check for an inadvertent size of zero bytes */
  267. if (!size) {
  268. ACPI_WARNING((module, line,
  269. "Attempt to allocate zero bytes, allocating 1 byte"));
  270. size = 1;
  271. }
  272. allocation = acpi_os_allocate(size);
  273. if (!allocation) {
  274. /* Report allocation error */
  275. ACPI_WARNING((module, line,
  276. "Could not allocate size %u", (u32) size));
  277. return_PTR(NULL);
  278. }
  279. return_PTR(allocation);
  280. }
  281. /*******************************************************************************
  282. *
  283. * FUNCTION: acpi_ut_allocate_zeroed
  284. *
  285. * PARAMETERS: Size - Size of the allocation
  286. * Component - Component type of caller
  287. * Module - Source file name of caller
  288. * Line - Line number of caller
  289. *
  290. * RETURN: Address of the allocated memory on success, NULL on failure.
  291. *
  292. * DESCRIPTION: Subsystem equivalent of calloc. Allocate and zero memory.
  293. *
  294. ******************************************************************************/
  295. void *acpi_ut_allocate_zeroed(acpi_size size,
  296. u32 component, const char *module, u32 line)
  297. {
  298. void *allocation;
  299. ACPI_FUNCTION_ENTRY();
  300. allocation = acpi_ut_allocate(size, component, module, line);
  301. if (allocation) {
  302. /* Clear the memory block */
  303. ACPI_MEMSET(allocation, 0, size);
  304. }
  305. return (allocation);
  306. }
  307. #endif