Files
OrangePi_CM5/drivers/acpi/utilities/utalloc.c
T

372 lines
9.7 KiB
C
Raw Normal View History

2005-04-16 15:20:36 -07:00
/******************************************************************************
*
* Module Name: utalloc - local memory allocation routines
2005-04-16 15:20:36 -07:00
*
*****************************************************************************/
/*
2007-02-02 19:48:24 +03:00
* Copyright (C) 2000 - 2007, R. Byron Moore
2005-04-16 15:20:36 -07:00
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* substantially similar to the "NO WARRANTY" disclaimer below
* ("Disclaimer") and any redistribution must be conditioned upon
* including a substantially similar Disclaimer requirement for further
* binary redistribution.
* 3. Neither the names of the above-listed copyright holders nor the names
* of any contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2 as published by the Free
* Software Foundation.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*/
#include <acpi/acpi.h>
#include <acpi/acdebug.h>
2005-04-16 15:20:36 -07:00
#define _COMPONENT ACPI_UTILITIES
2005-08-05 00:44:28 -04:00
ACPI_MODULE_NAME("utalloc")
2005-04-16 15:20:36 -07:00
2005-04-18 22:49:35 -04:00
/*******************************************************************************
2005-04-16 15:20:36 -07:00
*
* FUNCTION: acpi_ut_create_caches
2005-04-16 15:20:36 -07:00
*
* PARAMETERS: None
2005-04-16 15:20:36 -07:00
*
* RETURN: Status
2005-04-16 15:20:36 -07:00
*
* DESCRIPTION: Create all local caches
2005-04-16 15:20:36 -07:00
*
******************************************************************************/
2005-08-05 00:44:28 -04:00
acpi_status acpi_ut_create_caches(void)
2005-04-16 15:20:36 -07:00
{
2005-08-05 00:44:28 -04:00
acpi_status status;
2005-04-16 15:20:36 -07:00
/* Object Caches, for frequently used objects */
2005-04-16 15:20:36 -07:00
2005-08-05 00:44:28 -04:00
status =
2006-03-17 16:44:00 -05:00
acpi_os_create_cache("Acpi-Namespace",
sizeof(struct acpi_namespace_node),
ACPI_MAX_NAMESPACE_CACHE_DEPTH,
&acpi_gbl_namespace_cache);
if (ACPI_FAILURE(status)) {
return (status);
}
status =
acpi_os_create_cache("Acpi-State", sizeof(union acpi_generic_state),
2005-08-05 00:44:28 -04:00
ACPI_MAX_STATE_CACHE_DEPTH,
&acpi_gbl_state_cache);
if (ACPI_FAILURE(status)) {
return (status);
2005-04-16 15:20:36 -07:00
}
2005-08-05 00:44:28 -04:00
status =
2006-03-17 16:44:00 -05:00
acpi_os_create_cache("Acpi-Parse",
2005-08-05 00:44:28 -04:00
sizeof(struct acpi_parse_obj_common),
ACPI_MAX_PARSE_CACHE_DEPTH,
&acpi_gbl_ps_node_cache);
if (ACPI_FAILURE(status)) {
return (status);
2005-04-16 15:20:36 -07:00
}
2005-08-05 00:44:28 -04:00
status =
2006-04-21 17:15:00 -04:00
acpi_os_create_cache("Acpi-ParseExt",
2005-08-05 00:44:28 -04:00
sizeof(struct acpi_parse_obj_named),
ACPI_MAX_EXTPARSE_CACHE_DEPTH,
&acpi_gbl_ps_node_ext_cache);
if (ACPI_FAILURE(status)) {
return (status);
}
2005-04-16 15:20:36 -07:00
2005-08-05 00:44:28 -04:00
status =
2006-03-17 16:44:00 -05:00
acpi_os_create_cache("Acpi-Operand",
2005-08-05 00:44:28 -04:00
sizeof(union acpi_operand_object),
ACPI_MAX_OBJECT_CACHE_DEPTH,
&acpi_gbl_operand_cache);
if (ACPI_FAILURE(status)) {
return (status);
}
2006-05-26 16:36:00 -04:00
#ifdef ACPI_DBG_TRACK_ALLOCATIONS
/* Memory allocation lists */
status = acpi_ut_create_list("Acpi-Global", 0, &acpi_gbl_global_list);
if (ACPI_FAILURE(status)) {
return (status);
}
status =
acpi_ut_create_list("Acpi-Namespace",
sizeof(struct acpi_namespace_node),
&acpi_gbl_ns_node_list);
if (ACPI_FAILURE(status)) {
return (status);
}
#endif
return (AE_OK);
2005-04-16 15:20:36 -07:00
}
2005-04-18 22:49:35 -04:00
/*******************************************************************************
2005-04-16 15:20:36 -07:00
*
* FUNCTION: acpi_ut_delete_caches
2005-04-16 15:20:36 -07:00
*
* PARAMETERS: None
2005-04-16 15:20:36 -07:00
*
* RETURN: Status
2005-04-16 15:20:36 -07:00
*
* DESCRIPTION: Purge and delete all local caches
2005-04-16 15:20:36 -07:00
*
******************************************************************************/
2005-08-05 00:44:28 -04:00
acpi_status acpi_ut_delete_caches(void)
2005-04-16 15:20:36 -07:00
{
#ifdef ACPI_DBG_TRACK_ALLOCATIONS
char buffer[7];
if (acpi_gbl_display_final_mem_stats) {
ACPI_STRCPY(buffer, "MEMORY");
acpi_db_display_statistics(buffer);
}
#endif
2005-04-16 15:20:36 -07:00
2006-03-17 16:44:00 -05:00
(void)acpi_os_delete_cache(acpi_gbl_namespace_cache);
acpi_gbl_namespace_cache = NULL;
2005-08-05 00:44:28 -04:00
(void)acpi_os_delete_cache(acpi_gbl_state_cache);
acpi_gbl_state_cache = NULL;
2005-04-16 15:20:36 -07:00
2005-08-05 00:44:28 -04:00
(void)acpi_os_delete_cache(acpi_gbl_operand_cache);
acpi_gbl_operand_cache = NULL;
2005-04-16 15:20:36 -07:00
2005-08-05 00:44:28 -04:00
(void)acpi_os_delete_cache(acpi_gbl_ps_node_cache);
acpi_gbl_ps_node_cache = NULL;
2005-04-16 15:20:36 -07:00
2005-08-05 00:44:28 -04:00
(void)acpi_os_delete_cache(acpi_gbl_ps_node_ext_cache);
acpi_gbl_ps_node_ext_cache = NULL;
2005-04-16 15:20:36 -07:00
2006-05-26 16:36:00 -04:00
#ifdef ACPI_DBG_TRACK_ALLOCATIONS
/* Debug only - display leftover memory allocation, if any */
acpi_ut_dump_allocations(ACPI_UINT32_MAX, NULL);
/* Free memory lists */
ACPI_FREE(acpi_gbl_global_list);
2006-05-26 16:36:00 -04:00
acpi_gbl_global_list = NULL;
ACPI_FREE(acpi_gbl_ns_node_list);
2006-05-26 16:36:00 -04:00
acpi_gbl_ns_node_list = NULL;
#endif
return (AE_OK);
2005-04-16 15:20:36 -07:00
}
/*******************************************************************************
*
* FUNCTION: acpi_ut_validate_buffer
*
* PARAMETERS: Buffer - Buffer descriptor to be validated
*
* RETURN: Status
*
* DESCRIPTION: Perform parameter validation checks on an struct acpi_buffer
*
******************************************************************************/
2005-08-05 00:44:28 -04:00
acpi_status acpi_ut_validate_buffer(struct acpi_buffer * buffer)
2005-04-16 15:20:36 -07:00
{
/* Obviously, the structure pointer must be valid */
if (!buffer) {
return (AE_BAD_PARAMETER);
}
/* Special semantics for the length */
2005-08-05 00:44:28 -04:00
if ((buffer->length == ACPI_NO_BUFFER) ||
(buffer->length == ACPI_ALLOCATE_BUFFER) ||
(buffer->length == ACPI_ALLOCATE_LOCAL_BUFFER)) {
2005-04-16 15:20:36 -07:00
return (AE_OK);
}
/* Length is valid, the buffer pointer must be also */
if (!buffer->pointer) {
return (AE_BAD_PARAMETER);
}
return (AE_OK);
}
/*******************************************************************************
*
* FUNCTION: acpi_ut_initialize_buffer
*
* PARAMETERS: Buffer - Buffer to be validated
* required_length - Length needed
*
* RETURN: Status
*
* DESCRIPTION: Validate that the buffer is of the required length or
* allocate a new buffer. Returned buffer is always zeroed.
*
******************************************************************************/
acpi_status
2005-08-05 00:44:28 -04:00
acpi_ut_initialize_buffer(struct acpi_buffer * buffer,
acpi_size required_length)
2005-04-16 15:20:36 -07:00
{
2005-08-05 00:44:28 -04:00
acpi_status status = AE_OK;
2005-04-16 15:20:36 -07:00
switch (buffer->length) {
case ACPI_NO_BUFFER:
/* Set the exception and returned the required length */
status = AE_BUFFER_OVERFLOW;
break;
case ACPI_ALLOCATE_BUFFER:
/* Allocate a new buffer */
2005-08-05 00:44:28 -04:00
buffer->pointer = acpi_os_allocate(required_length);
2005-04-16 15:20:36 -07:00
if (!buffer->pointer) {
return (AE_NO_MEMORY);
}
/* Clear the buffer */
2005-08-05 00:44:28 -04:00
ACPI_MEMSET(buffer->pointer, 0, required_length);
2005-04-16 15:20:36 -07:00
break;
case ACPI_ALLOCATE_LOCAL_BUFFER:
/* Allocate a new buffer with local interface to allow tracking */
2006-10-03 00:00:00 -04:00
buffer->pointer = ACPI_ALLOCATE_ZEROED(required_length);
2005-04-16 15:20:36 -07:00
if (!buffer->pointer) {
return (AE_NO_MEMORY);
}
break;
default:
/* Existing buffer: Validate the size of the buffer */
if (buffer->length < required_length) {
status = AE_BUFFER_OVERFLOW;
break;
}
/* Clear the buffer */
2005-08-05 00:44:28 -04:00
ACPI_MEMSET(buffer->pointer, 0, required_length);
2005-04-16 15:20:36 -07:00
break;
}
buffer->length = required_length;
return (status);
}
2006-07-10 01:35:51 -04:00
#ifdef NOT_USED_BY_LINUX
2005-04-16 15:20:36 -07:00
/*******************************************************************************
*
* FUNCTION: acpi_ut_allocate
*
* PARAMETERS: Size - Size of the allocation
* Component - Component type of caller
* Module - Source file name of caller
* Line - Line number of caller
*
* RETURN: Address of the allocated memory on success, NULL on failure.
*
2006-03-17 16:44:00 -05:00
* DESCRIPTION: Subsystem equivalent of malloc.
2005-04-16 15:20:36 -07:00
*
******************************************************************************/
2005-08-05 00:44:28 -04:00
void *acpi_ut_allocate(acpi_size size, u32 component, char *module, u32 line)
2005-04-16 15:20:36 -07:00
{
2005-08-05 00:44:28 -04:00
void *allocation;
2005-04-16 15:20:36 -07:00
2006-04-21 17:15:00 -04:00
ACPI_FUNCTION_TRACE_U32(ut_allocate, size);
2005-04-16 15:20:36 -07:00
/* Check for an inadvertent size of zero bytes */
if (!size) {
2006-03-17 16:44:00 -05:00
ACPI_WARNING((module, line,
"Attempt to allocate zero bytes, allocating 1 byte"));
2005-04-16 15:20:36 -07:00
size = 1;
}
2005-08-05 00:44:28 -04:00
allocation = acpi_os_allocate(size);
2005-04-16 15:20:36 -07:00
if (!allocation) {
2006-10-02 00:00:00 -04:00
2005-04-16 15:20:36 -07:00
/* Report allocation error */
2006-03-17 16:44:00 -05:00
ACPI_WARNING((module, line,
"Could not allocate size %X", (u32) size));
2005-04-16 15:20:36 -07:00
2005-08-05 00:44:28 -04:00
return_PTR(NULL);
2005-04-16 15:20:36 -07:00
}
2005-08-05 00:44:28 -04:00
return_PTR(allocation);
2005-04-16 15:20:36 -07:00
}
/*******************************************************************************
*
2006-03-17 16:44:00 -05:00
* FUNCTION: acpi_ut_allocate_zeroed
2005-04-16 15:20:36 -07:00
*
* PARAMETERS: Size - Size of the allocation
* Component - Component type of caller
* Module - Source file name of caller
* Line - Line number of caller
*
* RETURN: Address of the allocated memory on success, NULL on failure.
*
2006-03-17 16:44:00 -05:00
* DESCRIPTION: Subsystem equivalent of calloc. Allocate and zero memory.
2005-04-16 15:20:36 -07:00
*
******************************************************************************/
2006-03-17 16:44:00 -05:00
void *acpi_ut_allocate_zeroed(acpi_size size,
u32 component, char *module, u32 line)
2005-04-16 15:20:36 -07:00
{
2005-08-05 00:44:28 -04:00
void *allocation;
2005-04-16 15:20:36 -07:00
2005-08-05 00:44:28 -04:00
ACPI_FUNCTION_ENTRY();
2005-04-16 15:20:36 -07:00
2006-03-17 16:44:00 -05:00
allocation = acpi_ut_allocate(size, component, module, line);
if (allocation) {
2005-04-16 15:20:36 -07:00
2006-03-17 16:44:00 -05:00
/* Clear the memory block */
2005-04-16 15:20:36 -07:00
2006-03-17 16:44:00 -05:00
ACPI_MEMSET(allocation, 0, size);
2005-04-16 15:20:36 -07:00
}
2006-03-17 16:44:00 -05:00
return (allocation);
2005-04-16 15:20:36 -07:00
}
2006-07-10 01:35:51 -04:00
#endif