utalloc.c 9.9 KB

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