utalloc.c 9.2 KB

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