1 /******************************************************************************
3 * Module Name: utalloc - local memory allocation routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, Intel Corp.
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.
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acdebug.h>
50 #define _COMPONENT ACPI_UTILITIES
51 ACPI_MODULE_NAME ("utalloc")
54 /*******************************************************************************
56 * FUNCTION: AcpiUtCreateCaches
62 * DESCRIPTION: Create all local caches
64 ******************************************************************************/
73 /* Object Caches, for frequently used objects */
75 Status = AcpiOsCreateCache ("Acpi-Namespace", sizeof (ACPI_NAMESPACE_NODE),
76 ACPI_MAX_NAMESPACE_CACHE_DEPTH, &AcpiGbl_NamespaceCache);
77 if (ACPI_FAILURE (Status))
82 Status = AcpiOsCreateCache ("Acpi-State", sizeof (ACPI_GENERIC_STATE),
83 ACPI_MAX_STATE_CACHE_DEPTH, &AcpiGbl_StateCache);
84 if (ACPI_FAILURE (Status))
89 Status = AcpiOsCreateCache ("Acpi-Parse", sizeof (ACPI_PARSE_OBJ_COMMON),
90 ACPI_MAX_PARSE_CACHE_DEPTH, &AcpiGbl_PsNodeCache);
91 if (ACPI_FAILURE (Status))
96 Status = AcpiOsCreateCache ("Acpi-ParseExt", sizeof (ACPI_PARSE_OBJ_NAMED),
97 ACPI_MAX_EXTPARSE_CACHE_DEPTH, &AcpiGbl_PsNodeExtCache);
98 if (ACPI_FAILURE (Status))
103 Status = AcpiOsCreateCache ("Acpi-Operand", sizeof (ACPI_OPERAND_OBJECT),
104 ACPI_MAX_OBJECT_CACHE_DEPTH, &AcpiGbl_OperandCache);
105 if (ACPI_FAILURE (Status))
111 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
113 /* Memory allocation lists */
115 Status = AcpiUtCreateList ("Acpi-Global", 0,
116 &AcpiGbl_GlobalList);
117 if (ACPI_FAILURE (Status))
122 Status = AcpiUtCreateList ("Acpi-Namespace", sizeof (ACPI_NAMESPACE_NODE),
123 &AcpiGbl_NsNodeList);
124 if (ACPI_FAILURE (Status))
134 /*******************************************************************************
136 * FUNCTION: AcpiUtDeleteCaches
142 * DESCRIPTION: Purge and delete all local caches
144 ******************************************************************************/
150 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
153 if (AcpiGbl_DisplayFinalMemStats)
155 ACPI_STRCPY (Buffer, "MEMORY");
156 (void) AcpiDbDisplayStatistics (Buffer);
160 (void) AcpiOsDeleteCache (AcpiGbl_NamespaceCache);
161 AcpiGbl_NamespaceCache = NULL;
163 (void) AcpiOsDeleteCache (AcpiGbl_StateCache);
164 AcpiGbl_StateCache = NULL;
166 (void) AcpiOsDeleteCache (AcpiGbl_OperandCache);
167 AcpiGbl_OperandCache = NULL;
169 (void) AcpiOsDeleteCache (AcpiGbl_PsNodeCache);
170 AcpiGbl_PsNodeCache = NULL;
172 (void) AcpiOsDeleteCache (AcpiGbl_PsNodeExtCache);
173 AcpiGbl_PsNodeExtCache = NULL;
176 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
178 /* Debug only - display leftover memory allocation, if any */
180 AcpiUtDumpAllocations (ACPI_UINT32_MAX, NULL);
182 /* Free memory lists */
184 AcpiOsFree (AcpiGbl_GlobalList);
185 AcpiGbl_GlobalList = NULL;
187 AcpiOsFree (AcpiGbl_NsNodeList);
188 AcpiGbl_NsNodeList = NULL;
195 /*******************************************************************************
197 * FUNCTION: AcpiUtValidateBuffer
199 * PARAMETERS: Buffer - Buffer descriptor to be validated
203 * DESCRIPTION: Perform parameter validation checks on an ACPI_BUFFER
205 ******************************************************************************/
208 AcpiUtValidateBuffer (
212 /* Obviously, the structure pointer must be valid */
216 return (AE_BAD_PARAMETER);
219 /* Special semantics for the length */
221 if ((Buffer->Length == ACPI_NO_BUFFER) ||
222 (Buffer->Length == ACPI_ALLOCATE_BUFFER) ||
223 (Buffer->Length == ACPI_ALLOCATE_LOCAL_BUFFER))
228 /* Length is valid, the buffer pointer must be also */
230 if (!Buffer->Pointer)
232 return (AE_BAD_PARAMETER);
239 /*******************************************************************************
241 * FUNCTION: AcpiUtInitializeBuffer
243 * PARAMETERS: Buffer - Buffer to be validated
244 * RequiredLength - Length needed
248 * DESCRIPTION: Validate that the buffer is of the required length or
249 * allocate a new buffer. Returned buffer is always zeroed.
251 ******************************************************************************/
254 AcpiUtInitializeBuffer (
256 ACPI_SIZE RequiredLength)
258 ACPI_SIZE InputBufferLength;
261 /* Parameter validation */
263 if (!Buffer || !RequiredLength)
265 return (AE_BAD_PARAMETER);
269 * Buffer->Length is used as both an input and output parameter. Get the
270 * input actual length and set the output required buffer length.
272 InputBufferLength = Buffer->Length;
273 Buffer->Length = RequiredLength;
276 * The input buffer length contains the actual buffer length, or the type
277 * of buffer to be allocated by this routine.
279 switch (InputBufferLength)
283 /* Return the exception (and the required buffer length) */
285 return (AE_BUFFER_OVERFLOW);
287 case ACPI_ALLOCATE_BUFFER:
289 /* Allocate a new buffer */
291 Buffer->Pointer = AcpiOsAllocate (RequiredLength);
294 case ACPI_ALLOCATE_LOCAL_BUFFER:
296 /* Allocate a new buffer with local interface to allow tracking */
298 Buffer->Pointer = ACPI_ALLOCATE (RequiredLength);
303 /* Existing buffer: Validate the size of the buffer */
305 if (InputBufferLength < RequiredLength)
307 return (AE_BUFFER_OVERFLOW);
312 /* Validate allocation from above or input buffer pointer */
314 if (!Buffer->Pointer)
316 return (AE_NO_MEMORY);
319 /* Have a valid buffer, clear it */
321 ACPI_MEMSET (Buffer->Pointer, 0, RequiredLength);
326 /*******************************************************************************
328 * FUNCTION: AcpiUtAllocate
330 * PARAMETERS: Size - Size of the allocation
331 * Component - Component type of caller
332 * Module - Source file name of caller
333 * Line - Line number of caller
335 * RETURN: Address of the allocated memory on success, NULL on failure.
337 * DESCRIPTION: Subsystem equivalent of malloc.
339 ******************************************************************************/
351 ACPI_FUNCTION_TRACE_U32 (UtAllocate, Size);
354 /* Check for an inadvertent size of zero bytes */
358 ACPI_WARNING ((Module, Line,
359 "Attempt to allocate zero bytes, allocating 1 byte"));
363 Allocation = AcpiOsAllocate (Size);
366 /* Report allocation error */
368 ACPI_WARNING ((Module, Line,
369 "Could not allocate size %u", (UINT32) Size));
374 return_PTR (Allocation);
378 /*******************************************************************************
380 * FUNCTION: AcpiUtAllocateZeroed
382 * PARAMETERS: Size - Size of the allocation
383 * Component - Component type of caller
384 * Module - Source file name of caller
385 * Line - Line number of caller
387 * RETURN: Address of the allocated memory on success, NULL on failure.
389 * DESCRIPTION: Subsystem equivalent of calloc. Allocate and zero memory.
391 ******************************************************************************/
394 AcpiUtAllocateZeroed (
403 ACPI_FUNCTION_ENTRY ();
406 Allocation = AcpiUtAllocate (Size, Component, Module, Line);
409 /* Clear the memory block */
411 ACPI_MEMSET (Allocation, 0, Size);