Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:03

0001 /******************************************************************************
0002  *
0003  * Module Name: utalloc - local memory allocation routines
0004  *
0005  *****************************************************************************/
0006 
0007 /******************************************************************************
0008  *
0009  * 1. Copyright Notice
0010  *
0011  * Some or all of this work - Copyright (c) 1999 - 2024, Intel Corp.
0012  * All rights reserved.
0013  *
0014  * 2. License
0015  *
0016  * 2.1. This is your license from Intel Corp. under its intellectual property
0017  * rights. You may have additional license terms from the party that provided
0018  * you this software, covering your right to use that party's intellectual
0019  * property rights.
0020  *
0021  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
0022  * copy of the source code appearing in this file ("Covered Code") an
0023  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
0024  * base code distributed originally by Intel ("Original Intel Code") to copy,
0025  * make derivatives, distribute, use and display any portion of the Covered
0026  * Code in any form, with the right to sublicense such rights; and
0027  *
0028  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
0029  * license (with the right to sublicense), under only those claims of Intel
0030  * patents that are infringed by the Original Intel Code, to make, use, sell,
0031  * offer to sell, and import the Covered Code and derivative works thereof
0032  * solely to the minimum extent necessary to exercise the above copyright
0033  * license, and in no event shall the patent license extend to any additions
0034  * to or modifications of the Original Intel Code. No other license or right
0035  * is granted directly or by implication, estoppel or otherwise;
0036  *
0037  * The above copyright and patent license is granted only if the following
0038  * conditions are met:
0039  *
0040  * 3. Conditions
0041  *
0042  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
0043  * Redistribution of source code of any substantial portion of the Covered
0044  * Code or modification with rights to further distribute source must include
0045  * the above Copyright Notice, the above License, this list of Conditions,
0046  * and the following Disclaimer and Export Compliance provision. In addition,
0047  * Licensee must cause all Covered Code to which Licensee contributes to
0048  * contain a file documenting the changes Licensee made to create that Covered
0049  * Code and the date of any change. Licensee must include in that file the
0050  * documentation of any changes made by any predecessor Licensee. Licensee
0051  * must include a prominent statement that the modification is derived,
0052  * directly or indirectly, from Original Intel Code.
0053  *
0054  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
0055  * Redistribution of source code of any substantial portion of the Covered
0056  * Code or modification without rights to further distribute source must
0057  * include the following Disclaimer and Export Compliance provision in the
0058  * documentation and/or other materials provided with distribution. In
0059  * addition, Licensee may not authorize further sublicense of source of any
0060  * portion of the Covered Code, and must include terms to the effect that the
0061  * license from Licensee to its licensee is limited to the intellectual
0062  * property embodied in the software Licensee provides to its licensee, and
0063  * not to intellectual property embodied in modifications its licensee may
0064  * make.
0065  *
0066  * 3.3. Redistribution of Executable. Redistribution in executable form of any
0067  * substantial portion of the Covered Code or modification must reproduce the
0068  * above Copyright Notice, and the following Disclaimer and Export Compliance
0069  * provision in the documentation and/or other materials provided with the
0070  * distribution.
0071  *
0072  * 3.4. Intel retains all right, title, and interest in and to the Original
0073  * Intel Code.
0074  *
0075  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
0076  * Intel shall be used in advertising or otherwise to promote the sale, use or
0077  * other dealings in products derived from or relating to the Covered Code
0078  * without prior written authorization from Intel.
0079  *
0080  * 4. Disclaimer and Export Compliance
0081  *
0082  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
0083  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
0084  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
0085  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
0086  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
0087  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
0088  * PARTICULAR PURPOSE.
0089  *
0090  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
0091  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
0092  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
0093  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
0094  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
0095  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
0096  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
0097  * LIMITED REMEDY.
0098  *
0099  * 4.3. Licensee shall not export, either directly or indirectly, any of this
0100  * software or system incorporating such software without first obtaining any
0101  * required license or other approval from the U. S. Department of Commerce or
0102  * any other agency or department of the United States Government. In the
0103  * event Licensee exports any such software from the United States or
0104  * re-exports any such software from a foreign destination, Licensee shall
0105  * ensure that the distribution and export/re-export of the software is in
0106  * compliance with all laws, regulations, orders, or other restrictions of the
0107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
0108  * any of its subsidiaries will export/re-export any technical data, process,
0109  * software, or service, directly or indirectly, to any country for which the
0110  * United States government or any agency thereof requires an export license,
0111  * other governmental approval, or letter of assurance, without first obtaining
0112  * such license, approval or letter.
0113  *
0114  *****************************************************************************
0115  *
0116  * Alternatively, you may choose to be licensed under the terms of the
0117  * following license:
0118  *
0119  * Redistribution and use in source and binary forms, with or without
0120  * modification, are permitted provided that the following conditions
0121  * are met:
0122  * 1. Redistributions of source code must retain the above copyright
0123  *    notice, this list of conditions, and the following disclaimer,
0124  *    without modification.
0125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
0126  *    substantially similar to the "NO WARRANTY" disclaimer below
0127  *    ("Disclaimer") and any redistribution must be conditioned upon
0128  *    including a substantially similar Disclaimer requirement for further
0129  *    binary redistribution.
0130  * 3. Neither the names of the above-listed copyright holders nor the names
0131  *    of any contributors may be used to endorse or promote products derived
0132  *    from this software without specific prior written permission.
0133  *
0134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0145  *
0146  * Alternatively, you may choose to be licensed under the terms of the
0147  * GNU General Public License ("GPL") version 2 as published by the Free
0148  * Software Foundation.
0149  *
0150  *****************************************************************************/
0151 
0152 #include "acpi.h"
0153 #include "accommon.h"
0154 #include "acdebug.h"
0155 
0156 #define _COMPONENT          ACPI_UTILITIES
0157         ACPI_MODULE_NAME    ("utalloc")
0158 
0159 
0160 #if !defined (USE_NATIVE_ALLOCATE_ZEROED)
0161 /*******************************************************************************
0162  *
0163  * FUNCTION:    AcpiOsAllocateZeroed
0164  *
0165  * PARAMETERS:  Size                - Size of the allocation
0166  *
0167  * RETURN:      Address of the allocated memory on success, NULL on failure.
0168  *
0169  * DESCRIPTION: Subsystem equivalent of calloc. Allocate and zero memory.
0170  *              This is the default implementation. Can be overridden via the
0171  *              USE_NATIVE_ALLOCATE_ZEROED flag.
0172  *
0173  ******************************************************************************/
0174 
0175 void *
0176 AcpiOsAllocateZeroed (
0177     ACPI_SIZE               Size)
0178 {
0179     void                    *Allocation;
0180 
0181 
0182     ACPI_FUNCTION_ENTRY ();
0183 
0184 
0185     Allocation = AcpiOsAllocate (Size);
0186     if (Allocation)
0187     {
0188         /* Clear the memory block */
0189 
0190         memset (Allocation, 0, Size);
0191     }
0192 
0193     return (Allocation);
0194 }
0195 
0196 #endif /* !USE_NATIVE_ALLOCATE_ZEROED */
0197 
0198 
0199 /*******************************************************************************
0200  *
0201  * FUNCTION:    AcpiUtCreateCaches
0202  *
0203  * PARAMETERS:  None
0204  *
0205  * RETURN:      Status
0206  *
0207  * DESCRIPTION: Create all local caches
0208  *
0209  ******************************************************************************/
0210 
0211 ACPI_STATUS
0212 AcpiUtCreateCaches (
0213     void)
0214 {
0215     ACPI_STATUS             Status;
0216 
0217 
0218     /* Object Caches, for frequently used objects */
0219 
0220     Status = AcpiOsCreateCache ("Acpi-Namespace", sizeof (ACPI_NAMESPACE_NODE),
0221         ACPI_MAX_NAMESPACE_CACHE_DEPTH, &AcpiGbl_NamespaceCache);
0222     if (ACPI_FAILURE (Status))
0223     {
0224         return (Status);
0225     }
0226 
0227     Status = AcpiOsCreateCache ("Acpi-State", sizeof (ACPI_GENERIC_STATE),
0228         ACPI_MAX_STATE_CACHE_DEPTH, &AcpiGbl_StateCache);
0229     if (ACPI_FAILURE (Status))
0230     {
0231         return (Status);
0232     }
0233 
0234     Status = AcpiOsCreateCache ("Acpi-Parse", sizeof (ACPI_PARSE_OBJ_COMMON),
0235         ACPI_MAX_PARSE_CACHE_DEPTH, &AcpiGbl_PsNodeCache);
0236     if (ACPI_FAILURE (Status))
0237     {
0238         return (Status);
0239     }
0240 
0241     Status = AcpiOsCreateCache ("Acpi-ParseExt", sizeof (ACPI_PARSE_OBJ_NAMED),
0242         ACPI_MAX_EXTPARSE_CACHE_DEPTH, &AcpiGbl_PsNodeExtCache);
0243     if (ACPI_FAILURE (Status))
0244     {
0245         return (Status);
0246     }
0247 
0248     Status = AcpiOsCreateCache ("Acpi-Operand", sizeof (ACPI_OPERAND_OBJECT),
0249         ACPI_MAX_OBJECT_CACHE_DEPTH, &AcpiGbl_OperandCache);
0250     if (ACPI_FAILURE (Status))
0251     {
0252         return (Status);
0253     }
0254 
0255 #ifdef ACPI_ASL_COMPILER
0256     /*
0257      * For use with the ASL-/ASL+ option. This cache keeps track of regular
0258      * 0xA9 0x01 comments.
0259      */
0260     Status = AcpiOsCreateCache ("Acpi-Comment", sizeof (ACPI_COMMENT_NODE),
0261         ACPI_MAX_COMMENT_CACHE_DEPTH, &AcpiGbl_RegCommentCache);
0262     if (ACPI_FAILURE (Status))
0263     {
0264         return (Status);
0265     }
0266 
0267     /*
0268      * This cache keeps track of the starting addresses of where the comments
0269      * lie. This helps prevent duplication of comments.
0270      */
0271     Status = AcpiOsCreateCache ("Acpi-Comment-Addr", sizeof (ACPI_COMMENT_ADDR_NODE),
0272         ACPI_MAX_COMMENT_CACHE_DEPTH, &AcpiGbl_CommentAddrCache);
0273     if (ACPI_FAILURE (Status))
0274     {
0275         return (Status);
0276     }
0277 
0278     /*
0279      * This cache will be used for nodes that represent files.
0280      */
0281     Status = AcpiOsCreateCache ("Acpi-File", sizeof (ACPI_FILE_NODE),
0282         ACPI_MAX_COMMENT_CACHE_DEPTH, &AcpiGbl_FileCache);
0283     if (ACPI_FAILURE (Status))
0284     {
0285         return (Status);
0286     }
0287 #endif
0288 
0289 
0290 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
0291 
0292     /* Memory allocation lists */
0293 
0294     Status = AcpiUtCreateList ("Acpi-Global", 0,
0295         &AcpiGbl_GlobalList);
0296     if (ACPI_FAILURE (Status))
0297     {
0298         return (Status);
0299     }
0300 
0301     Status = AcpiUtCreateList ("Acpi-Namespace", sizeof (ACPI_NAMESPACE_NODE),
0302         &AcpiGbl_NsNodeList);
0303     if (ACPI_FAILURE (Status))
0304     {
0305         return (Status);
0306     }
0307 #endif
0308 
0309     return (AE_OK);
0310 }
0311 
0312 
0313 /*******************************************************************************
0314  *
0315  * FUNCTION:    AcpiUtDeleteCaches
0316  *
0317  * PARAMETERS:  None
0318  *
0319  * RETURN:      Status
0320  *
0321  * DESCRIPTION: Purge and delete all local caches
0322  *
0323  ******************************************************************************/
0324 
0325 ACPI_STATUS
0326 AcpiUtDeleteCaches (
0327     void)
0328 {
0329 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
0330     char                    Buffer[7];
0331 
0332 
0333     if (AcpiGbl_DisplayFinalMemStats)
0334     {
0335         strcpy (Buffer, "MEMORY");
0336         (void) AcpiDbDisplayStatistics (Buffer);
0337     }
0338 #endif
0339 
0340     (void) AcpiOsDeleteCache (AcpiGbl_NamespaceCache);
0341     AcpiGbl_NamespaceCache = NULL;
0342 
0343     (void) AcpiOsDeleteCache (AcpiGbl_StateCache);
0344     AcpiGbl_StateCache = NULL;
0345 
0346     (void) AcpiOsDeleteCache (AcpiGbl_OperandCache);
0347     AcpiGbl_OperandCache = NULL;
0348 
0349     (void) AcpiOsDeleteCache (AcpiGbl_PsNodeCache);
0350     AcpiGbl_PsNodeCache = NULL;
0351 
0352     (void) AcpiOsDeleteCache (AcpiGbl_PsNodeExtCache);
0353     AcpiGbl_PsNodeExtCache = NULL;
0354 
0355 #ifdef ACPI_ASL_COMPILER
0356     (void) AcpiOsDeleteCache (AcpiGbl_RegCommentCache);
0357     AcpiGbl_RegCommentCache = NULL;
0358 
0359     (void) AcpiOsDeleteCache (AcpiGbl_CommentAddrCache);
0360     AcpiGbl_CommentAddrCache = NULL;
0361 
0362     (void) AcpiOsDeleteCache (AcpiGbl_FileCache);
0363     AcpiGbl_FileCache = NULL;
0364 #endif
0365 
0366 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
0367 
0368     /* Debug only - display leftover memory allocation, if any */
0369 
0370     AcpiUtDumpAllocations (ACPI_UINT32_MAX, NULL);
0371 
0372     /* Free memory lists */
0373 
0374     AcpiOsFree (AcpiGbl_GlobalList);
0375     AcpiGbl_GlobalList = NULL;
0376 
0377     AcpiOsFree (AcpiGbl_NsNodeList);
0378     AcpiGbl_NsNodeList = NULL;
0379 #endif
0380 
0381     return (AE_OK);
0382 }
0383 
0384 
0385 /*******************************************************************************
0386  *
0387  * FUNCTION:    AcpiUtValidateBuffer
0388  *
0389  * PARAMETERS:  Buffer              - Buffer descriptor to be validated
0390  *
0391  * RETURN:      Status
0392  *
0393  * DESCRIPTION: Perform parameter validation checks on an ACPI_BUFFER
0394  *
0395  ******************************************************************************/
0396 
0397 ACPI_STATUS
0398 AcpiUtValidateBuffer (
0399     ACPI_BUFFER             *Buffer)
0400 {
0401 
0402     /* Obviously, the structure pointer must be valid */
0403 
0404     if (!Buffer)
0405     {
0406         return (AE_BAD_PARAMETER);
0407     }
0408 
0409     /* Special semantics for the length */
0410 
0411     if ((Buffer->Length == ACPI_NO_BUFFER)              ||
0412         (Buffer->Length == ACPI_ALLOCATE_BUFFER)        ||
0413         (Buffer->Length == ACPI_ALLOCATE_LOCAL_BUFFER))
0414     {
0415         return (AE_OK);
0416     }
0417 
0418     /* Length is valid, the buffer pointer must be also */
0419 
0420     if (!Buffer->Pointer)
0421     {
0422         return (AE_BAD_PARAMETER);
0423     }
0424 
0425     return (AE_OK);
0426 }
0427 
0428 
0429 /*******************************************************************************
0430  *
0431  * FUNCTION:    AcpiUtInitializeBuffer
0432  *
0433  * PARAMETERS:  Buffer              - Buffer to be validated
0434  *              RequiredLength      - Length needed
0435  *
0436  * RETURN:      Status
0437  *
0438  * DESCRIPTION: Validate that the buffer is of the required length or
0439  *              allocate a new buffer. Returned buffer is always zeroed.
0440  *
0441  ******************************************************************************/
0442 
0443 ACPI_STATUS
0444 AcpiUtInitializeBuffer (
0445     ACPI_BUFFER             *Buffer,
0446     ACPI_SIZE               RequiredLength)
0447 {
0448     ACPI_SIZE               InputBufferLength;
0449 
0450 
0451     /* Parameter validation */
0452 
0453     if (!Buffer || !RequiredLength)
0454     {
0455         return (AE_BAD_PARAMETER);
0456     }
0457 
0458     /*
0459      * Buffer->Length is used as both an input and output parameter. Get the
0460      * input actual length and set the output required buffer length.
0461      */
0462     InputBufferLength = Buffer->Length;
0463     Buffer->Length = RequiredLength;
0464 
0465     /*
0466      * The input buffer length contains the actual buffer length, or the type
0467      * of buffer to be allocated by this routine.
0468      */
0469     switch (InputBufferLength)
0470     {
0471     case ACPI_NO_BUFFER:
0472 
0473         /* Return the exception (and the required buffer length) */
0474 
0475         return (AE_BUFFER_OVERFLOW);
0476 
0477     case ACPI_ALLOCATE_BUFFER:
0478         /*
0479          * Allocate a new buffer. We directectly call AcpiOsAllocate here to
0480          * purposefully bypass the (optionally enabled) internal allocation
0481          * tracking mechanism since we only want to track internal
0482          * allocations. Note: The caller should use AcpiOsFree to free this
0483          * buffer created via ACPI_ALLOCATE_BUFFER.
0484          */
0485         Buffer->Pointer = AcpiOsAllocate (RequiredLength);
0486         break;
0487 
0488     case ACPI_ALLOCATE_LOCAL_BUFFER:
0489 
0490         /* Allocate a new buffer with local interface to allow tracking */
0491 
0492         Buffer->Pointer = ACPI_ALLOCATE (RequiredLength);
0493         break;
0494 
0495     default:
0496 
0497         /* Existing buffer: Validate the size of the buffer */
0498 
0499         if (InputBufferLength < RequiredLength)
0500         {
0501             return (AE_BUFFER_OVERFLOW);
0502         }
0503         break;
0504     }
0505 
0506     /* Validate allocation from above or input buffer pointer */
0507 
0508     if (!Buffer->Pointer)
0509     {
0510         return (AE_NO_MEMORY);
0511     }
0512 
0513     /* Have a valid buffer, clear it */
0514 
0515     memset (Buffer->Pointer, 0, RequiredLength);
0516     return (AE_OK);
0517 }