1 /******************************************************************************
3 * Module Name: utalloc - local memory allocation routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2006, 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
);
63 acpi_ut_create_list(char *list_name
,
64 u16 object_size
, struct acpi_memory_list
**return_cache
);
67 /*******************************************************************************
69 * FUNCTION: acpi_ut_create_caches
75 * DESCRIPTION: Create all local caches
77 ******************************************************************************/
79 acpi_status
acpi_ut_create_caches(void)
83 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
85 /* Memory allocation lists */
87 status
= acpi_ut_create_list("Acpi-Global", 0, &acpi_gbl_global_list
);
88 if (ACPI_FAILURE(status
)) {
93 acpi_ut_create_list("Acpi-Namespace",
94 sizeof(struct acpi_namespace_node
),
95 &acpi_gbl_ns_node_list
);
96 if (ACPI_FAILURE(status
)) {
101 /* Object Caches, for frequently used objects */
104 acpi_os_create_cache("acpi_state", sizeof(union acpi_generic_state
),
105 ACPI_MAX_STATE_CACHE_DEPTH
,
106 &acpi_gbl_state_cache
);
107 if (ACPI_FAILURE(status
)) {
112 acpi_os_create_cache("acpi_parse",
113 sizeof(struct acpi_parse_obj_common
),
114 ACPI_MAX_PARSE_CACHE_DEPTH
,
115 &acpi_gbl_ps_node_cache
);
116 if (ACPI_FAILURE(status
)) {
121 acpi_os_create_cache("acpi_parse_ext",
122 sizeof(struct acpi_parse_obj_named
),
123 ACPI_MAX_EXTPARSE_CACHE_DEPTH
,
124 &acpi_gbl_ps_node_ext_cache
);
125 if (ACPI_FAILURE(status
)) {
130 acpi_os_create_cache("acpi_operand",
131 sizeof(union acpi_operand_object
),
132 ACPI_MAX_OBJECT_CACHE_DEPTH
,
133 &acpi_gbl_operand_cache
);
134 if (ACPI_FAILURE(status
)) {
141 /*******************************************************************************
143 * FUNCTION: acpi_ut_delete_caches
149 * DESCRIPTION: Purge and delete all local caches
151 ******************************************************************************/
153 acpi_status
acpi_ut_delete_caches(void)
156 (void)acpi_os_delete_cache(acpi_gbl_state_cache
);
157 acpi_gbl_state_cache
= NULL
;
159 (void)acpi_os_delete_cache(acpi_gbl_operand_cache
);
160 acpi_gbl_operand_cache
= NULL
;
162 (void)acpi_os_delete_cache(acpi_gbl_ps_node_cache
);
163 acpi_gbl_ps_node_cache
= NULL
;
165 (void)acpi_os_delete_cache(acpi_gbl_ps_node_ext_cache
);
166 acpi_gbl_ps_node_ext_cache
= NULL
;
171 /*******************************************************************************
173 * FUNCTION: acpi_ut_validate_buffer
175 * PARAMETERS: Buffer - Buffer descriptor to be validated
179 * DESCRIPTION: Perform parameter validation checks on an struct acpi_buffer
181 ******************************************************************************/
183 acpi_status
acpi_ut_validate_buffer(struct acpi_buffer
* buffer
)
186 /* Obviously, the structure pointer must be valid */
189 return (AE_BAD_PARAMETER
);
192 /* Special semantics for the length */
194 if ((buffer
->length
== ACPI_NO_BUFFER
) ||
195 (buffer
->length
== ACPI_ALLOCATE_BUFFER
) ||
196 (buffer
->length
== ACPI_ALLOCATE_LOCAL_BUFFER
)) {
200 /* Length is valid, the buffer pointer must be also */
202 if (!buffer
->pointer
) {
203 return (AE_BAD_PARAMETER
);
209 /*******************************************************************************
211 * FUNCTION: acpi_ut_initialize_buffer
213 * PARAMETERS: Buffer - Buffer to be validated
214 * required_length - Length needed
218 * DESCRIPTION: Validate that the buffer is of the required length or
219 * allocate a new buffer. Returned buffer is always zeroed.
221 ******************************************************************************/
224 acpi_ut_initialize_buffer(struct acpi_buffer
* buffer
,
225 acpi_size required_length
)
227 acpi_status status
= AE_OK
;
229 switch (buffer
->length
) {
232 /* Set the exception and returned the required length */
234 status
= AE_BUFFER_OVERFLOW
;
237 case ACPI_ALLOCATE_BUFFER
:
239 /* Allocate a new buffer */
241 buffer
->pointer
= acpi_os_allocate(required_length
);
242 if (!buffer
->pointer
) {
243 return (AE_NO_MEMORY
);
246 /* Clear the buffer */
248 ACPI_MEMSET(buffer
->pointer
, 0, required_length
);
251 case ACPI_ALLOCATE_LOCAL_BUFFER
:
253 /* Allocate a new buffer with local interface to allow tracking */
255 buffer
->pointer
= ACPI_MEM_CALLOCATE(required_length
);
256 if (!buffer
->pointer
) {
257 return (AE_NO_MEMORY
);
263 /* Existing buffer: Validate the size of the buffer */
265 if (buffer
->length
< required_length
) {
266 status
= AE_BUFFER_OVERFLOW
;
270 /* Clear the buffer */
272 ACPI_MEMSET(buffer
->pointer
, 0, required_length
);
276 buffer
->length
= required_length
;
280 /*******************************************************************************
282 * FUNCTION: acpi_ut_allocate
284 * PARAMETERS: Size - Size of the allocation
285 * Component - Component type of caller
286 * Module - Source file name of caller
287 * Line - Line number of caller
289 * RETURN: Address of the allocated memory on success, NULL on failure.
291 * DESCRIPTION: The subsystem's equivalent of malloc.
293 ******************************************************************************/
295 void *acpi_ut_allocate(acpi_size size
, u32 component
, char *module
, u32 line
)
299 ACPI_FUNCTION_TRACE_U32("ut_allocate", size
);
301 /* Check for an inadvertent size of zero bytes */
304 ACPI_ERROR((module
, line
,
305 "ut_allocate: Attempt to allocate zero bytes, allocating 1 byte"));
309 allocation
= acpi_os_allocate(size
);
311 /* Report allocation error */
313 ACPI_ERROR((module
, line
,
314 "ut_allocate: Could not allocate size %X",
320 return_PTR(allocation
);
323 /*******************************************************************************
325 * FUNCTION: acpi_ut_callocate
327 * PARAMETERS: Size - Size of the allocation
328 * Component - Component type of caller
329 * Module - Source file name of caller
330 * Line - Line number of caller
332 * RETURN: Address of the allocated memory on success, NULL on failure.
334 * DESCRIPTION: Subsystem equivalent of calloc.
336 ******************************************************************************/
338 void *acpi_ut_callocate(acpi_size size
, u32 component
, char *module
, u32 line
)
342 ACPI_FUNCTION_TRACE_U32("ut_callocate", size
);
344 /* Check for an inadvertent size of zero bytes */
347 ACPI_ERROR((module
, line
,
348 "Attempt to allocate zero bytes, allocating 1 byte"));
352 allocation
= acpi_os_allocate(size
);
354 /* Report allocation error */
356 ACPI_ERROR((module
, line
,
357 "Could not allocate size %X", (u32
) size
));
361 /* Clear the memory block */
363 ACPI_MEMSET(allocation
, 0, size
);
364 return_PTR(allocation
);
367 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
369 * These procedures are used for tracking memory leaks in the subsystem, and
370 * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
372 * Each memory allocation is tracked via a doubly linked list. Each
373 * element contains the caller's component, module name, function name, and
374 * line number. acpi_ut_allocate and acpi_ut_callocate call
375 * acpi_ut_track_allocation to add an element to the list; deletion
376 * occurs in the body of acpi_ut_free.
379 /*******************************************************************************
381 * FUNCTION: acpi_ut_create_list
383 * PARAMETERS: cache_name - Ascii name for the cache
384 * object_size - Size of each cached object
385 * return_cache - Where the new cache object is returned
389 * DESCRIPTION: Create a local memory list for tracking purposed
391 ******************************************************************************/
394 acpi_ut_create_list(char *list_name
,
395 u16 object_size
, struct acpi_memory_list
**return_cache
)
397 struct acpi_memory_list
*cache
;
399 cache
= acpi_os_allocate(sizeof(struct acpi_memory_list
));
401 return (AE_NO_MEMORY
);
404 ACPI_MEMSET(cache
, 0, sizeof(struct acpi_memory_list
));
406 cache
->list_name
= list_name
;
407 cache
->object_size
= object_size
;
409 *return_cache
= cache
;
413 /*******************************************************************************
415 * FUNCTION: acpi_ut_allocate_and_track
417 * PARAMETERS: Size - Size of the allocation
418 * Component - Component type of caller
419 * Module - Source file name of caller
420 * Line - Line number of caller
422 * RETURN: Address of the allocated memory on success, NULL on failure.
424 * DESCRIPTION: The subsystem's equivalent of malloc.
426 ******************************************************************************/
428 void *acpi_ut_allocate_and_track(acpi_size size
,
429 u32 component
, char *module
, u32 line
)
431 struct acpi_debug_mem_block
*allocation
;
435 acpi_ut_allocate(size
+ sizeof(struct acpi_debug_mem_header
),
436 component
, module
, line
);
441 status
= acpi_ut_track_allocation(allocation
, size
,
442 ACPI_MEM_MALLOC
, component
, module
,
444 if (ACPI_FAILURE(status
)) {
445 acpi_os_free(allocation
);
449 acpi_gbl_global_list
->total_allocated
++;
450 acpi_gbl_global_list
->current_total_size
+= (u32
) size
;
452 return ((void *)&allocation
->user_space
);
455 /*******************************************************************************
457 * FUNCTION: acpi_ut_callocate_and_track
459 * PARAMETERS: Size - Size of the allocation
460 * Component - Component type of caller
461 * Module - Source file name of caller
462 * Line - Line number of caller
464 * RETURN: Address of the allocated memory on success, NULL on failure.
466 * DESCRIPTION: Subsystem equivalent of calloc.
468 ******************************************************************************/
470 void *acpi_ut_callocate_and_track(acpi_size size
,
471 u32 component
, char *module
, u32 line
)
473 struct acpi_debug_mem_block
*allocation
;
477 acpi_ut_callocate(size
+ sizeof(struct acpi_debug_mem_header
),
478 component
, module
, line
);
480 /* Report allocation error */
482 ACPI_ERROR((module
, line
,
483 "Could not allocate size %X", (u32
) size
));
487 status
= acpi_ut_track_allocation(allocation
, size
,
488 ACPI_MEM_CALLOC
, component
, module
,
490 if (ACPI_FAILURE(status
)) {
491 acpi_os_free(allocation
);
495 acpi_gbl_global_list
->total_allocated
++;
496 acpi_gbl_global_list
->current_total_size
+= (u32
) size
;
498 return ((void *)&allocation
->user_space
);
501 /*******************************************************************************
503 * FUNCTION: acpi_ut_free_and_track
505 * PARAMETERS: Allocation - Address of the memory to deallocate
506 * Component - Component type of caller
507 * Module - Source file name of caller
508 * Line - Line number of caller
512 * DESCRIPTION: Frees the memory at Allocation
514 ******************************************************************************/
517 acpi_ut_free_and_track(void *allocation
, u32 component
, char *module
, u32 line
)
519 struct acpi_debug_mem_block
*debug_block
;
522 ACPI_FUNCTION_TRACE_PTR("ut_free", allocation
);
524 if (NULL
== allocation
) {
525 ACPI_ERROR((module
, line
, "Attempt to delete a NULL address"));
530 debug_block
= ACPI_CAST_PTR(struct acpi_debug_mem_block
,
531 (((char *)allocation
) -
532 sizeof(struct acpi_debug_mem_header
)));
534 acpi_gbl_global_list
->total_freed
++;
535 acpi_gbl_global_list
->current_total_size
-= debug_block
->size
;
537 status
= acpi_ut_remove_allocation(debug_block
,
538 component
, module
, line
);
539 if (ACPI_FAILURE(status
)) {
540 ACPI_EXCEPTION((AE_INFO
, status
, "Could not free memory"));
543 acpi_os_free(debug_block
);
544 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
, "%p freed\n", allocation
));
548 /*******************************************************************************
550 * FUNCTION: acpi_ut_find_allocation
552 * PARAMETERS: Allocation - Address of allocated memory
554 * RETURN: A list element if found; NULL otherwise.
556 * DESCRIPTION: Searches for an element in the global allocation tracking list.
558 ******************************************************************************/
560 static struct acpi_debug_mem_block
*acpi_ut_find_allocation(void *allocation
)
562 struct acpi_debug_mem_block
*element
;
564 ACPI_FUNCTION_ENTRY();
566 element
= acpi_gbl_global_list
->list_head
;
568 /* Search for the address. */
571 if (element
== allocation
) {
575 element
= element
->next
;
581 /*******************************************************************************
583 * FUNCTION: acpi_ut_track_allocation
585 * PARAMETERS: Allocation - Address of allocated memory
586 * Size - Size of the allocation
587 * alloc_type - MEM_MALLOC or MEM_CALLOC
588 * Component - Component type of caller
589 * Module - Source file name of caller
590 * Line - Line number of caller
594 * DESCRIPTION: Inserts an element into the global allocation tracking list.
596 ******************************************************************************/
599 acpi_ut_track_allocation(struct acpi_debug_mem_block
*allocation
,
601 u8 alloc_type
, u32 component
, char *module
, u32 line
)
603 struct acpi_memory_list
*mem_list
;
604 struct acpi_debug_mem_block
*element
;
605 acpi_status status
= AE_OK
;
607 ACPI_FUNCTION_TRACE_PTR("ut_track_allocation", allocation
);
609 mem_list
= acpi_gbl_global_list
;
610 status
= acpi_ut_acquire_mutex(ACPI_MTX_MEMORY
);
611 if (ACPI_FAILURE(status
)) {
612 return_ACPI_STATUS(status
);
616 * Search list for this address to make sure it is not already on the list.
617 * This will catch several kinds of problems.
619 element
= acpi_ut_find_allocation(allocation
);
622 "ut_track_allocation: Allocation already present in list! (%p)",
625 ACPI_ERROR((AE_INFO
, "Element %p Address %p",
626 element
, allocation
));
628 goto unlock_and_exit
;
631 /* Fill in the instance data. */
633 allocation
->size
= (u32
) size
;
634 allocation
->alloc_type
= alloc_type
;
635 allocation
->component
= component
;
636 allocation
->line
= line
;
638 ACPI_STRNCPY(allocation
->module
, module
, ACPI_MAX_MODULE_NAME
);
639 allocation
->module
[ACPI_MAX_MODULE_NAME
- 1] = 0;
641 /* Insert at list head */
643 if (mem_list
->list_head
) {
644 ((struct acpi_debug_mem_block
*)(mem_list
->list_head
))->
645 previous
= allocation
;
648 allocation
->next
= mem_list
->list_head
;
649 allocation
->previous
= NULL
;
651 mem_list
->list_head
= allocation
;
654 status
= acpi_ut_release_mutex(ACPI_MTX_MEMORY
);
655 return_ACPI_STATUS(status
);
658 /*******************************************************************************
660 * FUNCTION: acpi_ut_remove_allocation
662 * PARAMETERS: Allocation - Address of allocated memory
663 * Component - Component type of caller
664 * Module - Source file name of caller
665 * Line - Line number of caller
669 * DESCRIPTION: Deletes an element from the global allocation tracking list.
671 ******************************************************************************/
674 acpi_ut_remove_allocation(struct acpi_debug_mem_block
*allocation
,
675 u32 component
, char *module
, u32 line
)
677 struct acpi_memory_list
*mem_list
;
680 ACPI_FUNCTION_TRACE("ut_remove_allocation");
682 mem_list
= acpi_gbl_global_list
;
683 if (NULL
== mem_list
->list_head
) {
684 /* No allocations! */
686 ACPI_ERROR((module
, line
,
687 "Empty allocation list, nothing to free!"));
689 return_ACPI_STATUS(AE_OK
);
692 status
= acpi_ut_acquire_mutex(ACPI_MTX_MEMORY
);
693 if (ACPI_FAILURE(status
)) {
694 return_ACPI_STATUS(status
);
699 if (allocation
->previous
) {
700 (allocation
->previous
)->next
= allocation
->next
;
702 mem_list
->list_head
= allocation
->next
;
705 if (allocation
->next
) {
706 (allocation
->next
)->previous
= allocation
->previous
;
709 /* Mark the segment as deleted */
711 ACPI_MEMSET(&allocation
->user_space
, 0xEA, allocation
->size
);
713 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
, "Freeing size 0%X\n",
716 status
= acpi_ut_release_mutex(ACPI_MTX_MEMORY
);
717 return_ACPI_STATUS(status
);
720 /*******************************************************************************
722 * FUNCTION: acpi_ut_dump_allocation_info
728 * DESCRIPTION: Print some info about the outstanding allocations.
730 ******************************************************************************/
732 #ifdef ACPI_FUTURE_USAGE
733 void acpi_ut_dump_allocation_info(void)
736 struct acpi_memory_list *mem_list;
739 ACPI_FUNCTION_TRACE("ut_dump_allocation_info");
742 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
743 ("%30s: %4d (%3d Kb)\n", "Current allocations",
744 mem_list->current_count,
745 ROUND_UP_TO_1K (mem_list->current_size)));
747 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
748 ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
749 mem_list->max_concurrent_count,
750 ROUND_UP_TO_1K (mem_list->max_concurrent_size)));
752 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
753 ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
754 running_object_count,
755 ROUND_UP_TO_1K (running_object_size)));
757 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
758 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
760 ROUND_UP_TO_1K (running_alloc_size)));
762 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
763 ("%30s: %4d (%3d Kb)\n", "Current Nodes",
764 acpi_gbl_current_node_count,
765 ROUND_UP_TO_1K (acpi_gbl_current_node_size)));
767 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
768 ("%30s: %4d (%3d Kb)\n", "Max Nodes",
769 acpi_gbl_max_concurrent_node_count,
770 ROUND_UP_TO_1K ((acpi_gbl_max_concurrent_node_count *
771 sizeof (struct acpi_namespace_node)))));
775 #endif /* ACPI_FUTURE_USAGE */
777 /*******************************************************************************
779 * FUNCTION: acpi_ut_dump_allocations
781 * PARAMETERS: Component - Component(s) to dump info for.
782 * Module - Module to dump info for. NULL means all.
786 * DESCRIPTION: Print a list of all outstanding allocations.
788 ******************************************************************************/
790 void acpi_ut_dump_allocations(u32 component
, char *module
)
792 struct acpi_debug_mem_block
*element
;
793 union acpi_descriptor
*descriptor
;
794 u32 num_outstanding
= 0;
796 ACPI_FUNCTION_TRACE("ut_dump_allocations");
799 * Walk the allocation list.
801 if (ACPI_FAILURE(acpi_ut_acquire_mutex(ACPI_MTX_MEMORY
))) {
805 element
= acpi_gbl_global_list
->list_head
;
807 if ((element
->component
& component
) &&
809 || (0 == ACPI_STRCMP(module
, element
->module
)))) {
810 /* Ignore allocated objects that are in a cache */
813 ACPI_CAST_PTR(union acpi_descriptor
,
814 &element
->user_space
);
815 if (descriptor
->descriptor_id
!= ACPI_DESC_TYPE_CACHED
) {
816 acpi_os_printf("%p Len %04X %9.9s-%d [%s] ",
817 descriptor
, element
->size
,
818 element
->module
, element
->line
,
819 acpi_ut_get_descriptor_name
822 /* Most of the elements will be Operand objects. */
824 switch (ACPI_GET_DESCRIPTOR_TYPE(descriptor
)) {
825 case ACPI_DESC_TYPE_OPERAND
:
826 acpi_os_printf("%12.12s R%hd",
827 acpi_ut_get_type_name
831 common
.reference_count
);
834 case ACPI_DESC_TYPE_PARSER
:
835 acpi_os_printf("aml_opcode %04hX",
840 case ACPI_DESC_TYPE_NAMED
:
841 acpi_os_printf("%4.4s",
842 acpi_ut_get_node_name
843 (&descriptor
->node
));
850 acpi_os_printf("\n");
854 element
= element
->next
;
857 (void)acpi_ut_release_mutex(ACPI_MTX_MEMORY
);
861 if (!num_outstanding
) {
862 ACPI_INFO((AE_INFO
, "No outstanding allocations"));
865 "%d(%X) Outstanding allocations",
866 num_outstanding
, num_outstanding
));
872 #endif /* #ifdef ACPI_DBG_TRACK_ALLOCATIONS */