utalloc.c 9.5 KB

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