Path: blob/main/sys/contrib/dev/acpica/components/utilities/utalloc.c
48406 views
/******************************************************************************1*2* Module Name: utalloc - local memory allocation routines3*4*****************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151#include <contrib/dev/acpica/include/acpi.h>152#include <contrib/dev/acpica/include/accommon.h>153#include <contrib/dev/acpica/include/acdebug.h>154155#define _COMPONENT ACPI_UTILITIES156ACPI_MODULE_NAME ("utalloc")157158159#if !defined (USE_NATIVE_ALLOCATE_ZEROED)160/*******************************************************************************161*162* FUNCTION: AcpiOsAllocateZeroed163*164* PARAMETERS: Size - Size of the allocation165*166* RETURN: Address of the allocated memory on success, NULL on failure.167*168* DESCRIPTION: Subsystem equivalent of calloc. Allocate and zero memory.169* This is the default implementation. Can be overridden via the170* USE_NATIVE_ALLOCATE_ZEROED flag.171*172******************************************************************************/173174void *175AcpiOsAllocateZeroed (176ACPI_SIZE Size)177{178void *Allocation;179180181ACPI_FUNCTION_ENTRY ();182183184Allocation = AcpiOsAllocate (Size);185if (Allocation)186{187/* Clear the memory block */188189memset (Allocation, 0, Size);190}191192return (Allocation);193}194195#endif /* !USE_NATIVE_ALLOCATE_ZEROED */196197198/*******************************************************************************199*200* FUNCTION: AcpiUtCreateCaches201*202* PARAMETERS: None203*204* RETURN: Status205*206* DESCRIPTION: Create all local caches207*208******************************************************************************/209210ACPI_STATUS211AcpiUtCreateCaches (212void)213{214ACPI_STATUS Status;215216217/* Object Caches, for frequently used objects */218219Status = AcpiOsCreateCache ("Acpi-Namespace", sizeof (ACPI_NAMESPACE_NODE),220ACPI_MAX_NAMESPACE_CACHE_DEPTH, &AcpiGbl_NamespaceCache);221if (ACPI_FAILURE (Status))222{223return (Status);224}225226Status = AcpiOsCreateCache ("Acpi-State", sizeof (ACPI_GENERIC_STATE),227ACPI_MAX_STATE_CACHE_DEPTH, &AcpiGbl_StateCache);228if (ACPI_FAILURE (Status))229{230return (Status);231}232233Status = AcpiOsCreateCache ("Acpi-Parse", sizeof (ACPI_PARSE_OBJ_COMMON),234ACPI_MAX_PARSE_CACHE_DEPTH, &AcpiGbl_PsNodeCache);235if (ACPI_FAILURE (Status))236{237return (Status);238}239240Status = AcpiOsCreateCache ("Acpi-ParseExt", sizeof (ACPI_PARSE_OBJ_NAMED),241ACPI_MAX_EXTPARSE_CACHE_DEPTH, &AcpiGbl_PsNodeExtCache);242if (ACPI_FAILURE (Status))243{244return (Status);245}246247Status = AcpiOsCreateCache ("Acpi-Operand", sizeof (ACPI_OPERAND_OBJECT),248ACPI_MAX_OBJECT_CACHE_DEPTH, &AcpiGbl_OperandCache);249if (ACPI_FAILURE (Status))250{251return (Status);252}253254#ifdef ACPI_ASL_COMPILER255/*256* For use with the ASL-/ASL+ option. This cache keeps track of regular257* 0xA9 0x01 comments.258*/259Status = AcpiOsCreateCache ("Acpi-Comment", sizeof (ACPI_COMMENT_NODE),260ACPI_MAX_COMMENT_CACHE_DEPTH, &AcpiGbl_RegCommentCache);261if (ACPI_FAILURE (Status))262{263return (Status);264}265266/*267* This cache keeps track of the starting addresses of where the comments268* lie. This helps prevent duplication of comments.269*/270Status = AcpiOsCreateCache ("Acpi-Comment-Addr", sizeof (ACPI_COMMENT_ADDR_NODE),271ACPI_MAX_COMMENT_CACHE_DEPTH, &AcpiGbl_CommentAddrCache);272if (ACPI_FAILURE (Status))273{274return (Status);275}276277/*278* This cache will be used for nodes that represent files.279*/280Status = AcpiOsCreateCache ("Acpi-File", sizeof (ACPI_FILE_NODE),281ACPI_MAX_COMMENT_CACHE_DEPTH, &AcpiGbl_FileCache);282if (ACPI_FAILURE (Status))283{284return (Status);285}286#endif287288289#ifdef ACPI_DBG_TRACK_ALLOCATIONS290291/* Memory allocation lists */292293Status = AcpiUtCreateList ("Acpi-Global", 0,294&AcpiGbl_GlobalList);295if (ACPI_FAILURE (Status))296{297return (Status);298}299300Status = AcpiUtCreateList ("Acpi-Namespace", sizeof (ACPI_NAMESPACE_NODE),301&AcpiGbl_NsNodeList);302if (ACPI_FAILURE (Status))303{304return (Status);305}306#endif307308return (AE_OK);309}310311312/*******************************************************************************313*314* FUNCTION: AcpiUtDeleteCaches315*316* PARAMETERS: None317*318* RETURN: Status319*320* DESCRIPTION: Purge and delete all local caches321*322******************************************************************************/323324ACPI_STATUS325AcpiUtDeleteCaches (326void)327{328#ifdef ACPI_DBG_TRACK_ALLOCATIONS329char Buffer[7];330331332if (AcpiGbl_DisplayFinalMemStats)333{334strcpy (Buffer, "MEMORY");335(void) AcpiDbDisplayStatistics (Buffer);336}337#endif338339(void) AcpiOsDeleteCache (AcpiGbl_NamespaceCache);340AcpiGbl_NamespaceCache = NULL;341342(void) AcpiOsDeleteCache (AcpiGbl_StateCache);343AcpiGbl_StateCache = NULL;344345(void) AcpiOsDeleteCache (AcpiGbl_OperandCache);346AcpiGbl_OperandCache = NULL;347348(void) AcpiOsDeleteCache (AcpiGbl_PsNodeCache);349AcpiGbl_PsNodeCache = NULL;350351(void) AcpiOsDeleteCache (AcpiGbl_PsNodeExtCache);352AcpiGbl_PsNodeExtCache = NULL;353354#ifdef ACPI_ASL_COMPILER355(void) AcpiOsDeleteCache (AcpiGbl_RegCommentCache);356AcpiGbl_RegCommentCache = NULL;357358(void) AcpiOsDeleteCache (AcpiGbl_CommentAddrCache);359AcpiGbl_CommentAddrCache = NULL;360361(void) AcpiOsDeleteCache (AcpiGbl_FileCache);362AcpiGbl_FileCache = NULL;363#endif364365#ifdef ACPI_DBG_TRACK_ALLOCATIONS366367/* Debug only - display leftover memory allocation, if any */368369AcpiUtDumpAllocations (ACPI_UINT32_MAX, NULL);370371/* Free memory lists */372373AcpiOsFree (AcpiGbl_GlobalList);374AcpiGbl_GlobalList = NULL;375376AcpiOsFree (AcpiGbl_NsNodeList);377AcpiGbl_NsNodeList = NULL;378#endif379380return (AE_OK);381}382383384/*******************************************************************************385*386* FUNCTION: AcpiUtValidateBuffer387*388* PARAMETERS: Buffer - Buffer descriptor to be validated389*390* RETURN: Status391*392* DESCRIPTION: Perform parameter validation checks on an ACPI_BUFFER393*394******************************************************************************/395396ACPI_STATUS397AcpiUtValidateBuffer (398ACPI_BUFFER *Buffer)399{400401/* Obviously, the structure pointer must be valid */402403if (!Buffer)404{405return (AE_BAD_PARAMETER);406}407408/* Special semantics for the length */409410if ((Buffer->Length == ACPI_NO_BUFFER) ||411(Buffer->Length == ACPI_ALLOCATE_BUFFER) ||412(Buffer->Length == ACPI_ALLOCATE_LOCAL_BUFFER))413{414return (AE_OK);415}416417/* Length is valid, the buffer pointer must be also */418419if (!Buffer->Pointer)420{421return (AE_BAD_PARAMETER);422}423424return (AE_OK);425}426427428/*******************************************************************************429*430* FUNCTION: AcpiUtInitializeBuffer431*432* PARAMETERS: Buffer - Buffer to be validated433* RequiredLength - Length needed434*435* RETURN: Status436*437* DESCRIPTION: Validate that the buffer is of the required length or438* allocate a new buffer. Returned buffer is always zeroed.439*440******************************************************************************/441442ACPI_STATUS443AcpiUtInitializeBuffer (444ACPI_BUFFER *Buffer,445ACPI_SIZE RequiredLength)446{447ACPI_SIZE InputBufferLength;448449450/* Parameter validation */451452if (!Buffer || !RequiredLength)453{454return (AE_BAD_PARAMETER);455}456457/*458* Buffer->Length is used as both an input and output parameter. Get the459* input actual length and set the output required buffer length.460*/461InputBufferLength = Buffer->Length;462Buffer->Length = RequiredLength;463464/*465* The input buffer length contains the actual buffer length, or the type466* of buffer to be allocated by this routine.467*/468switch (InputBufferLength)469{470case ACPI_NO_BUFFER:471472/* Return the exception (and the required buffer length) */473474return (AE_BUFFER_OVERFLOW);475476case ACPI_ALLOCATE_BUFFER:477/*478* Allocate a new buffer. We directectly call AcpiOsAllocate here to479* purposefully bypass the (optionally enabled) internal allocation480* tracking mechanism since we only want to track internal481* allocations. Note: The caller should use AcpiOsFree to free this482* buffer created via ACPI_ALLOCATE_BUFFER.483*/484Buffer->Pointer = AcpiOsAllocate (RequiredLength);485break;486487case ACPI_ALLOCATE_LOCAL_BUFFER:488489/* Allocate a new buffer with local interface to allow tracking */490491Buffer->Pointer = ACPI_ALLOCATE (RequiredLength);492break;493494default:495496/* Existing buffer: Validate the size of the buffer */497498if (InputBufferLength < RequiredLength)499{500return (AE_BUFFER_OVERFLOW);501}502break;503}504505/* Validate allocation from above or input buffer pointer */506507if (!Buffer->Pointer)508{509return (AE_NO_MEMORY);510}511512/* Have a valid buffer, clear it */513514memset (Buffer->Pointer, 0, RequiredLength);515return (AE_OK);516}517518519