1 /******************************************************************************
3 * Module Name: utalloc - local memory allocation routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2005, R. Byron Moore
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
46 #define _COMPONENT ACPI_UTILITIES
47 ACPI_MODULE_NAME("utalloc")
49 /* Local prototypes */
50 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
51 static struct acpi_debug_mem_block *acpi_ut_find_allocation(void *allocation);
54 acpi_ut_track_allocation(struct acpi_debug_mem_block *address,
56 u8 alloc_type, u32 component, char *module, u32 line);
59 acpi_ut_remove_allocation(struct acpi_debug_mem_block *address,
60 u32 component, char *module, u32 line);
61 #endif /* ACPI_DBG_TRACK_ALLOCATIONS */
63 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
65 acpi_ut_create_list(char *list_name,
66 u16 object_size, struct acpi_memory_list **return_cache);
69 /*******************************************************************************
71 * FUNCTION: acpi_ut_create_caches
77 * DESCRIPTION: Create all local caches
79 ******************************************************************************/
81 acpi_status acpi_ut_create_caches(void)
85 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
87 /* Memory allocation lists */
89 status = acpi_ut_create_list("Acpi-Global", 0, &acpi_gbl_global_list);
90 if (ACPI_FAILURE(status)) {
95 acpi_ut_create_list("Acpi-Namespace",
96 sizeof(struct acpi_namespace_node),
97 &acpi_gbl_ns_node_list);
98 if (ACPI_FAILURE(status)) {
103 /* Object Caches, for frequently used objects */
106 acpi_os_create_cache("acpi_state", sizeof(union acpi_generic_state),
107 ACPI_MAX_STATE_CACHE_DEPTH,
108 &acpi_gbl_state_cache);
109 if (ACPI_FAILURE(status)) {
114 acpi_os_create_cache("acpi_parse",
115 sizeof(struct acpi_parse_obj_common),
116 ACPI_MAX_PARSE_CACHE_DEPTH,
117 &acpi_gbl_ps_node_cache);
118 if (ACPI_FAILURE(status)) {
123 acpi_os_create_cache("acpi_parse_ext",
124 sizeof(struct acpi_parse_obj_named),
125 ACPI_MAX_EXTPARSE_CACHE_DEPTH,
126 &acpi_gbl_ps_node_ext_cache);
127 if (ACPI_FAILURE(status)) {
132 acpi_os_create_cache("acpi_operand",
133 sizeof(union acpi_operand_object),
134 ACPI_MAX_OBJECT_CACHE_DEPTH,
135 &acpi_gbl_operand_cache);
136 if (ACPI_FAILURE(status)) {
143 /*******************************************************************************
145 * FUNCTION: acpi_ut_delete_caches
151 * DESCRIPTION: Purge and delete all local caches
153 ******************************************************************************/
155 acpi_status acpi_ut_delete_caches(void)
158 (void)acpi_os_delete_cache(acpi_gbl_state_cache);
159 acpi_gbl_state_cache = NULL;
161 (void)acpi_os_delete_cache(acpi_gbl_operand_cache);
162 acpi_gbl_operand_cache = NULL;
164 (void)acpi_os_delete_cache(acpi_gbl_ps_node_cache);
165 acpi_gbl_ps_node_cache = NULL;
167 (void)acpi_os_delete_cache(acpi_gbl_ps_node_ext_cache);
168 acpi_gbl_ps_node_ext_cache = NULL;
173 /*******************************************************************************
175 * FUNCTION: acpi_ut_validate_buffer
177 * PARAMETERS: Buffer - Buffer descriptor to be validated
181 * DESCRIPTION: Perform parameter validation checks on an struct acpi_buffer
183 ******************************************************************************/
185 acpi_status acpi_ut_validate_buffer(struct acpi_buffer * buffer)
188 /* Obviously, the structure pointer must be valid */
191 return (AE_BAD_PARAMETER);
194 /* Special semantics for the length */
196 if ((buffer->length == ACPI_NO_BUFFER) ||
197 (buffer->length == ACPI_ALLOCATE_BUFFER) ||
198 (buffer->length == ACPI_ALLOCATE_LOCAL_BUFFER)) {
202 /* Length is valid, the buffer pointer must be also */
204 if (!buffer->pointer) {
205 return (AE_BAD_PARAMETER);
211 /*******************************************************************************
213 * FUNCTION: acpi_ut_initialize_buffer
215 * PARAMETERS: Buffer - Buffer to be validated
216 * required_length - Length needed
220 * DESCRIPTION: Validate that the buffer is of the required length or
221 * allocate a new buffer. Returned buffer is always zeroed.
223 ******************************************************************************/
226 acpi_ut_initialize_buffer(struct acpi_buffer * buffer,
227 acpi_size required_length)
229 acpi_status status = AE_OK;
231 switch (buffer->length) {
234 /* Set the exception and returned the required length */
236 status = AE_BUFFER_OVERFLOW;
239 case ACPI_ALLOCATE_BUFFER:
241 /* Allocate a new buffer */
243 buffer->pointer = acpi_os_allocate(required_length);
244 if (!buffer->pointer) {
245 return (AE_NO_MEMORY);
248 /* Clear the buffer */
250 ACPI_MEMSET(buffer->pointer, 0, required_length);
253 case ACPI_ALLOCATE_LOCAL_BUFFER:
255 /* Allocate a new buffer with local interface to allow tracking */
257 buffer->pointer = ACPI_MEM_CALLOCATE(required_length);
258 if (!buffer->pointer) {
259 return (AE_NO_MEMORY);
265 /* Existing buffer: Validate the size of the buffer */
267 if (buffer->length < required_length) {
268 status = AE_BUFFER_OVERFLOW;
272 /* Clear the buffer */
274 ACPI_MEMSET(buffer->pointer, 0, required_length);
278 buffer->length = required_length;
282 /*******************************************************************************
284 * FUNCTION: acpi_ut_allocate
286 * PARAMETERS: Size - Size of the allocation
287 * Component - Component type of caller
288 * Module - Source file name of caller
289 * Line - Line number of caller
291 * RETURN: Address of the allocated memory on success, NULL on failure.
293 * DESCRIPTION: The subsystem's equivalent of malloc.
295 ******************************************************************************/
297 void *acpi_ut_allocate(acpi_size size, u32 component, char *module, u32 line)
301 ACPI_FUNCTION_TRACE_U32("ut_allocate", size);
303 /* Check for an inadvertent size of zero bytes */
306 _ACPI_REPORT_ERROR(module, line, component,
307 ("ut_allocate: Attempt to allocate zero bytes\n"));
311 allocation = acpi_os_allocate(size);
313 /* Report allocation error */
315 _ACPI_REPORT_ERROR(module, line, component,
316 ("ut_allocate: Could not allocate size %X\n",
322 return_PTR(allocation);
325 /*******************************************************************************
327 * FUNCTION: acpi_ut_callocate
329 * PARAMETERS: Size - Size of the allocation
330 * Component - Component type of caller
331 * Module - Source file name of caller
332 * Line - Line number of caller
334 * RETURN: Address of the allocated memory on success, NULL on failure.
336 * DESCRIPTION: Subsystem equivalent of calloc.
338 ******************************************************************************/
340 void *acpi_ut_callocate(acpi_size size, u32 component, char *module, u32 line)
344 ACPI_FUNCTION_TRACE_U32("ut_callocate", size);
346 /* Check for an inadvertent size of zero bytes */
349 _ACPI_REPORT_ERROR(module, line, component,
350 ("ut_callocate: Attempt to allocate zero bytes\n"));
354 allocation = acpi_os_allocate(size);
356 /* Report allocation error */
358 _ACPI_REPORT_ERROR(module, line, component,
359 ("ut_callocate: Could not allocate size %X\n",
364 /* Clear the memory block */
366 ACPI_MEMSET(allocation, 0, size);
367 return_PTR(allocation);
370 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
372 * These procedures are used for tracking memory leaks in the subsystem, and
373 * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
375 * Each memory allocation is tracked via a doubly linked list. Each
376 * element contains the caller's component, module name, function name, and
377 * line number. acpi_ut_allocate and acpi_ut_callocate call
378 * acpi_ut_track_allocation to add an element to the list; deletion
379 * occurs in the body of acpi_ut_free.
382 /*******************************************************************************
384 * FUNCTION: acpi_ut_create_list
386 * PARAMETERS: cache_name - Ascii name for the cache
387 * object_size - Size of each cached object
388 * return_cache - Where the new cache object is returned
392 * DESCRIPTION: Create a local memory list for tracking purposed
394 ******************************************************************************/
397 acpi_ut_create_list(char *list_name,
398 u16 object_size, struct acpi_memory_list **return_cache)
400 struct acpi_memory_list *cache;
402 cache = acpi_os_allocate(sizeof(struct acpi_memory_list));
404 return (AE_NO_MEMORY);
407 ACPI_MEMSET(cache, 0, sizeof(struct acpi_memory_list));
409 cache->list_name = list_name;
410 cache->object_size = object_size;
412 *return_cache = cache;
416 /*******************************************************************************
418 * FUNCTION: acpi_ut_allocate_and_track
420 * PARAMETERS: Size - Size of the allocation
421 * Component - Component type of caller
422 * Module - Source file name of caller
423 * Line - Line number of caller
425 * RETURN: Address of the allocated memory on success, NULL on failure.
427 * DESCRIPTION: The subsystem's equivalent of malloc.
429 ******************************************************************************/
431 void *acpi_ut_allocate_and_track(acpi_size size,
432 u32 component, char *module, u32 line)
434 struct acpi_debug_mem_block *allocation;
438 acpi_ut_allocate(size + sizeof(struct acpi_debug_mem_header),
439 component, module, line);
444 status = acpi_ut_track_allocation(allocation, size,
445 ACPI_MEM_MALLOC, component, module,
447 if (ACPI_FAILURE(status)) {
448 acpi_os_free(allocation);
452 acpi_gbl_global_list->total_allocated++;
453 acpi_gbl_global_list->current_total_size += (u32) size;
455 return ((void *)&allocation->user_space);
458 /*******************************************************************************
460 * FUNCTION: acpi_ut_callocate_and_track
462 * PARAMETERS: Size - Size of the allocation
463 * Component - Component type of caller
464 * Module - Source file name of caller
465 * Line - Line number of caller
467 * RETURN: Address of the allocated memory on success, NULL on failure.
469 * DESCRIPTION: Subsystem equivalent of calloc.
471 ******************************************************************************/
473 void *acpi_ut_callocate_and_track(acpi_size size,
474 u32 component, char *module, u32 line)
476 struct acpi_debug_mem_block *allocation;
480 acpi_ut_callocate(size + sizeof(struct acpi_debug_mem_header),
481 component, module, line);
483 /* Report allocation error */
485 _ACPI_REPORT_ERROR(module, line, component,
486 ("ut_callocate: Could not allocate size %X\n",
491 status = acpi_ut_track_allocation(allocation, size,
492 ACPI_MEM_CALLOC, component, module,
494 if (ACPI_FAILURE(status)) {
495 acpi_os_free(allocation);
499 acpi_gbl_global_list->total_allocated++;
500 acpi_gbl_global_list->current_total_size += (u32) size;
502 return ((void *)&allocation->user_space);
505 /*******************************************************************************
507 * FUNCTION: acpi_ut_free_and_track
509 * PARAMETERS: Allocation - Address of the memory to deallocate
510 * Component - Component type of caller
511 * Module - Source file name of caller
512 * Line - Line number of caller
516 * DESCRIPTION: Frees the memory at Allocation
518 ******************************************************************************/
521 acpi_ut_free_and_track(void *allocation, u32 component, char *module, u32 line)
523 struct acpi_debug_mem_block *debug_block;
526 ACPI_FUNCTION_TRACE_PTR("ut_free", allocation);
528 if (NULL == allocation) {
529 _ACPI_REPORT_ERROR(module, line, component,
530 ("acpi_ut_free: Attempt to delete a NULL address\n"));
535 debug_block = ACPI_CAST_PTR(struct acpi_debug_mem_block,
536 (((char *)allocation) -
537 sizeof(struct acpi_debug_mem_header)));
539 acpi_gbl_global_list->total_freed++;
540 acpi_gbl_global_list->current_total_size -= debug_block->size;
542 status = acpi_ut_remove_allocation(debug_block,
543 component, module, line);
544 if (ACPI_FAILURE(status)) {
545 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Could not free memory, %s\n",
546 acpi_format_exception(status)));
549 acpi_os_free(debug_block);
551 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "%p freed\n", allocation));
556 /*******************************************************************************
558 * FUNCTION: acpi_ut_find_allocation
560 * PARAMETERS: Allocation - Address of allocated memory
562 * RETURN: A list element if found; NULL otherwise.
564 * DESCRIPTION: Searches for an element in the global allocation tracking list.
566 ******************************************************************************/
568 static struct acpi_debug_mem_block *acpi_ut_find_allocation(void *allocation)
570 struct acpi_debug_mem_block *element;
572 ACPI_FUNCTION_ENTRY();
574 element = acpi_gbl_global_list->list_head;
576 /* Search for the address. */
579 if (element == allocation) {
583 element = element->next;
589 /*******************************************************************************
591 * FUNCTION: acpi_ut_track_allocation
593 * PARAMETERS: Allocation - Address of allocated memory
594 * Size - Size of the allocation
595 * alloc_type - MEM_MALLOC or MEM_CALLOC
596 * Component - Component type of caller
597 * Module - Source file name of caller
598 * Line - Line number of caller
602 * DESCRIPTION: Inserts an element into the global allocation tracking list.
604 ******************************************************************************/
607 acpi_ut_track_allocation(struct acpi_debug_mem_block *allocation,
609 u8 alloc_type, u32 component, char *module, u32 line)
611 struct acpi_memory_list *mem_list;
612 struct acpi_debug_mem_block *element;
613 acpi_status status = AE_OK;
615 ACPI_FUNCTION_TRACE_PTR("ut_track_allocation", allocation);
617 mem_list = acpi_gbl_global_list;
618 status = acpi_ut_acquire_mutex(ACPI_MTX_MEMORY);
619 if (ACPI_FAILURE(status)) {
620 return_ACPI_STATUS(status);
624 * Search list for this address to make sure it is not already on the list.
625 * This will catch several kinds of problems.
627 element = acpi_ut_find_allocation(allocation);
629 ACPI_REPORT_ERROR(("ut_track_allocation: Allocation already present in list! (%p)\n", allocation));
631 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Element %p Address %p\n",
632 element, allocation));
634 goto unlock_and_exit;
637 /* Fill in the instance data. */
639 allocation->size = (u32) size;
640 allocation->alloc_type = alloc_type;
641 allocation->component = component;
642 allocation->line = line;
644 ACPI_STRNCPY(allocation->module, module, ACPI_MAX_MODULE_NAME);
645 allocation->module[ACPI_MAX_MODULE_NAME - 1] = 0;
647 /* Insert at list head */
649 if (mem_list->list_head) {
650 ((struct acpi_debug_mem_block *)(mem_list->list_head))->
651 previous = allocation;
654 allocation->next = mem_list->list_head;
655 allocation->previous = NULL;
657 mem_list->list_head = allocation;
660 status = acpi_ut_release_mutex(ACPI_MTX_MEMORY);
661 return_ACPI_STATUS(status);
664 /*******************************************************************************
666 * FUNCTION: acpi_ut_remove_allocation
668 * PARAMETERS: Allocation - Address of allocated memory
669 * Component - Component type of caller
670 * Module - Source file name of caller
671 * Line - Line number of caller
675 * DESCRIPTION: Deletes an element from the global allocation tracking list.
677 ******************************************************************************/
680 acpi_ut_remove_allocation(struct acpi_debug_mem_block *allocation,
681 u32 component, char *module, u32 line)
683 struct acpi_memory_list *mem_list;
686 ACPI_FUNCTION_TRACE("ut_remove_allocation");
688 mem_list = acpi_gbl_global_list;
689 if (NULL == mem_list->list_head) {
690 /* No allocations! */
692 _ACPI_REPORT_ERROR(module, line, component,
693 ("ut_remove_allocation: Empty allocation list, nothing to free!\n"));
695 return_ACPI_STATUS(AE_OK);
698 status = acpi_ut_acquire_mutex(ACPI_MTX_MEMORY);
699 if (ACPI_FAILURE(status)) {
700 return_ACPI_STATUS(status);
705 if (allocation->previous) {
706 (allocation->previous)->next = allocation->next;
708 mem_list->list_head = allocation->next;
711 if (allocation->next) {
712 (allocation->next)->previous = allocation->previous;
715 /* Mark the segment as deleted */
717 ACPI_MEMSET(&allocation->user_space, 0xEA, allocation->size);
719 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "Freeing size 0%X\n",
722 status = acpi_ut_release_mutex(ACPI_MTX_MEMORY);
723 return_ACPI_STATUS(status);
726 /*******************************************************************************
728 * FUNCTION: acpi_ut_dump_allocation_info
734 * DESCRIPTION: Print some info about the outstanding allocations.
736 ******************************************************************************/
738 #ifdef ACPI_FUTURE_USAGE
739 void acpi_ut_dump_allocation_info(void)
742 struct acpi_memory_list *mem_list;
745 ACPI_FUNCTION_TRACE("ut_dump_allocation_info");
748 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
749 ("%30s: %4d (%3d Kb)\n", "Current allocations",
750 mem_list->current_count,
751 ROUND_UP_TO_1K (mem_list->current_size)));
753 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
754 ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
755 mem_list->max_concurrent_count,
756 ROUND_UP_TO_1K (mem_list->max_concurrent_size)));
758 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
759 ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
760 running_object_count,
761 ROUND_UP_TO_1K (running_object_size)));
763 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
764 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
766 ROUND_UP_TO_1K (running_alloc_size)));
768 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
769 ("%30s: %4d (%3d Kb)\n", "Current Nodes",
770 acpi_gbl_current_node_count,
771 ROUND_UP_TO_1K (acpi_gbl_current_node_size)));
773 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
774 ("%30s: %4d (%3d Kb)\n", "Max Nodes",
775 acpi_gbl_max_concurrent_node_count,
776 ROUND_UP_TO_1K ((acpi_gbl_max_concurrent_node_count *
777 sizeof (struct acpi_namespace_node)))));
781 #endif /* ACPI_FUTURE_USAGE */
783 /*******************************************************************************
785 * FUNCTION: acpi_ut_dump_allocations
787 * PARAMETERS: Component - Component(s) to dump info for.
788 * Module - Module to dump info for. NULL means all.
792 * DESCRIPTION: Print a list of all outstanding allocations.
794 ******************************************************************************/
796 void acpi_ut_dump_allocations(u32 component, char *module)
798 struct acpi_debug_mem_block *element;
799 union acpi_descriptor *descriptor;
800 u32 num_outstanding = 0;
802 ACPI_FUNCTION_TRACE("ut_dump_allocations");
805 * Walk the allocation list.
807 if (ACPI_FAILURE(acpi_ut_acquire_mutex(ACPI_MTX_MEMORY))) {
811 element = acpi_gbl_global_list->list_head;
813 if ((element->component & component) &&
815 || (0 == ACPI_STRCMP(module, element->module)))) {
816 /* Ignore allocated objects that are in a cache */
819 ACPI_CAST_PTR(union acpi_descriptor,
820 &element->user_space);
821 if (descriptor->descriptor_id != ACPI_DESC_TYPE_CACHED) {
822 acpi_os_printf("%p Len %04X %9.9s-%d [%s] ",
823 descriptor, element->size,
824 element->module, element->line,
825 acpi_ut_get_descriptor_name
828 /* Most of the elements will be Operand objects. */
830 switch (ACPI_GET_DESCRIPTOR_TYPE(descriptor)) {
831 case ACPI_DESC_TYPE_OPERAND:
832 acpi_os_printf("%12.12s R%hd",
833 acpi_ut_get_type_name
837 common.reference_count);
840 case ACPI_DESC_TYPE_PARSER:
841 acpi_os_printf("aml_opcode %04hX",
846 case ACPI_DESC_TYPE_NAMED:
847 acpi_os_printf("%4.4s",
848 acpi_ut_get_node_name
849 (&descriptor->node));
856 acpi_os_printf("\n");
860 element = element->next;
863 (void)acpi_ut_release_mutex(ACPI_MTX_MEMORY);
867 if (!num_outstanding) {
868 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
869 "No outstanding allocations.\n"));
871 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
872 "%d(%X) Outstanding allocations\n",
873 num_outstanding, num_outstanding));
879 #endif /* #ifdef ACPI_DBG_TRACK_ALLOCATIONS */