utalloc.c revision 281075
1165000Skientzle/******************************************************************************
2165912Skientzle *
3165000Skientzle * Module Name: utalloc - local memory allocation routines
4165000Skientzle *
5165000Skientzle *****************************************************************************/
6165000Skientzle
7165000Skientzle/*
8165000Skientzle * Copyright (C) 2000 - 2015, Intel Corp.
9165912Skientzle * All rights reserved.
10165000Skientzle *
11165000Skientzle * Redistribution and use in source and binary forms, with or without
12165000Skientzle * modification, are permitted provided that the following conditions
13165000Skientzle * are met:
14165000Skientzle * 1. Redistributions of source code must retain the above copyright
15165000Skientzle *    notice, this list of conditions, and the following disclaimer,
16165000Skientzle *    without modification.
17165000Skientzle * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18165000Skientzle *    substantially similar to the "NO WARRANTY" disclaimer below
19165000Skientzle *    ("Disclaimer") and any redistribution must be conditioned upon
20165000Skientzle *    including a substantially similar Disclaimer requirement for further
21165000Skientzle *    binary redistribution.
22165000Skientzle * 3. Neither the names of the above-listed copyright holders nor the names
23165000Skientzle *    of any contributors may be used to endorse or promote products derived
24165000Skientzle *    from this software without specific prior written permission.
25165000Skientzle *
26165000Skientzle * Alternatively, this software may be distributed under the terms of the
27165000Skientzle * GNU General Public License ("GPL") version 2 as published by the Free
28281988Sjilles * Software Foundation.
29281988Sjilles *
30189431Skientzle * NO WARRANTY
31165000Skientzle * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32232153Smm * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33306379Semaste * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34232153Smm * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35232153Smm * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36232153Smm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37238909Smm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38232153Smm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39232153Smm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40232153Smm * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41232153Smm * POSSIBILITY OF SUCH DAMAGES.
42238909Smm */
43232153Smm
44299529Smm#include <contrib/dev/acpica/include/acpi.h>
45232153Smm#include <contrib/dev/acpica/include/accommon.h>
46232153Smm#include <contrib/dev/acpica/include/acdebug.h>
47232153Smm
48232153Smm#define _COMPONENT          ACPI_UTILITIES
49306379Semaste        ACPI_MODULE_NAME    ("utalloc")
50232153Smm
51232153Smm
52165000Skientzle#if !defined (USE_NATIVE_ALLOCATE_ZEROED)
53165000Skientzle/*******************************************************************************
54191241Skientzle *
55248616Smm * FUNCTION:    AcpiOsAllocateZeroed
56248616Smm *
57232153Smm * PARAMETERS:  Size                - Size of the allocation
58232153Smm *
59232153Smm * RETURN:      Address of the allocated memory on success, NULL on failure.
60232153Smm *
61232153Smm * DESCRIPTION: Subsystem equivalent of calloc. Allocate and zero memory.
62232153Smm *              This is the default implementation. Can be overridden via the
63299529Smm *              USE_NATIVE_ALLOCATE_ZEROED flag.
64232153Smm *
65232153Smm ******************************************************************************/
66232153Smm
67221472Sobrienvoid *
68248616SmmAcpiOsAllocateZeroed (
69232153Smm    ACPI_SIZE               Size)
70232153Smm{
71248616Smm    void                    *Allocation;
72232153Smm
73232153Smm
74232153Smm    ACPI_FUNCTION_ENTRY ();
75232153Smm
76232153Smm
77232153Smm    Allocation = AcpiOsAllocate (Size);
78232153Smm    if (Allocation)
79232153Smm    {
80232153Smm        /* Clear the memory block */
81191241Skientzle
82191241Skientzle        ACPI_MEMSET (Allocation, 0, Size);
83232153Smm    }
84232153Smm
85232153Smm    return (Allocation);
86232153Smm}
87232153Smm
88232153Smm#endif /* !USE_NATIVE_ALLOCATE_ZEROED */
89232153Smm
90232153Smm
91299529Smm/*******************************************************************************
92299529Smm *
93232153Smm * FUNCTION:    AcpiUtCreateCaches
94232153Smm *
95299529Smm * PARAMETERS:  None
96299529Smm *
97232153Smm * RETURN:      Status
98232153Smm *
99232153Smm * DESCRIPTION: Create all local caches
100232153Smm *
101232153Smm ******************************************************************************/
102299529Smm
103232153SmmACPI_STATUS
104232153SmmAcpiUtCreateCaches (
105232153Smm    void)
106232153Smm{
107232153Smm    ACPI_STATUS             Status;
108232153Smm
109232153Smm
110232153Smm    /* Object Caches, for frequently used objects */
111232153Smm
112232153Smm    Status = AcpiOsCreateCache ("Acpi-Namespace", sizeof (ACPI_NAMESPACE_NODE),
113232153Smm                ACPI_MAX_NAMESPACE_CACHE_DEPTH, &AcpiGbl_NamespaceCache);
114232153Smm    if (ACPI_FAILURE (Status))
115232153Smm    {
116232153Smm        return (Status);
117232153Smm    }
118232153Smm
119232153Smm    Status = AcpiOsCreateCache ("Acpi-State", sizeof (ACPI_GENERIC_STATE),
120232153Smm                ACPI_MAX_STATE_CACHE_DEPTH, &AcpiGbl_StateCache);
121232153Smm    if (ACPI_FAILURE (Status))
122232153Smm    {
123232153Smm        return (Status);
124232153Smm    }
125232153Smm
126232153Smm    Status = AcpiOsCreateCache ("Acpi-Parse", sizeof (ACPI_PARSE_OBJ_COMMON),
127232153Smm                ACPI_MAX_PARSE_CACHE_DEPTH, &AcpiGbl_PsNodeCache);
128232153Smm    if (ACPI_FAILURE (Status))
129232153Smm    {
130232153Smm        return (Status);
131232153Smm    }
132232153Smm
133232153Smm    Status = AcpiOsCreateCache ("Acpi-ParseExt", sizeof (ACPI_PARSE_OBJ_NAMED),
134232153Smm                ACPI_MAX_EXTPARSE_CACHE_DEPTH, &AcpiGbl_PsNodeExtCache);
135232153Smm    if (ACPI_FAILURE (Status))
136232153Smm    {
137232153Smm        return (Status);
138232153Smm    }
139232153Smm
140232153Smm    Status = AcpiOsCreateCache ("Acpi-Operand", sizeof (ACPI_OPERAND_OBJECT),
141232153Smm                ACPI_MAX_OBJECT_CACHE_DEPTH, &AcpiGbl_OperandCache);
142232153Smm    if (ACPI_FAILURE (Status))
143232153Smm    {
144232153Smm        return (Status);
145232153Smm    }
146232153Smm
147232153Smm
148232153Smm#ifdef ACPI_DBG_TRACK_ALLOCATIONS
149232153Smm
150232153Smm    /* Memory allocation lists */
151232153Smm
152232153Smm    Status = AcpiUtCreateList ("Acpi-Global", 0,
153232153Smm                &AcpiGbl_GlobalList);
154232153Smm    if (ACPI_FAILURE (Status))
155232153Smm    {
156232153Smm        return (Status);
157232153Smm    }
158232153Smm
159232153Smm    Status = AcpiUtCreateList ("Acpi-Namespace", sizeof (ACPI_NAMESPACE_NODE),
160232153Smm                &AcpiGbl_NsNodeList);
161232153Smm    if (ACPI_FAILURE (Status))
162248616Smm    {
163299529Smm        return (Status);
164232153Smm    }
165232153Smm#endif
166232153Smm
167232153Smm    return (AE_OK);
168299529Smm}
169299529Smm
170232153Smm
171232153Smm/*******************************************************************************
172232153Smm *
173232153Smm * FUNCTION:    AcpiUtDeleteCaches
174232153Smm *
175232153Smm * PARAMETERS:  None
176248616Smm *
177232153Smm * RETURN:      Status
178232153Smm *
179232153Smm * DESCRIPTION: Purge and delete all local caches
180232153Smm *
181232153Smm ******************************************************************************/
182232153Smm
183232153SmmACPI_STATUS
184232153SmmAcpiUtDeleteCaches (
185232153Smm    void)
186232153Smm{
187232153Smm#ifdef ACPI_DBG_TRACK_ALLOCATIONS
188232153Smm    char                    Buffer[7];
189232153Smm
190232153Smm    if (AcpiGbl_DisplayFinalMemStats)
191232153Smm    {
192232153Smm        ACPI_STRCPY (Buffer, "MEMORY");
193232153Smm        (void) AcpiDbDisplayStatistics (Buffer);
194232153Smm    }
195232153Smm#endif
196232153Smm
197232153Smm    (void) AcpiOsDeleteCache (AcpiGbl_NamespaceCache);
198232153Smm    AcpiGbl_NamespaceCache = NULL;
199232153Smm
200232153Smm    (void) AcpiOsDeleteCache (AcpiGbl_StateCache);
201232153Smm    AcpiGbl_StateCache = NULL;
202232153Smm
203232153Smm    (void) AcpiOsDeleteCache (AcpiGbl_OperandCache);
204232153Smm    AcpiGbl_OperandCache = NULL;
205232153Smm
206232153Smm    (void) AcpiOsDeleteCache (AcpiGbl_PsNodeCache);
207232153Smm    AcpiGbl_PsNodeCache = NULL;
208232153Smm
209232153Smm    (void) AcpiOsDeleteCache (AcpiGbl_PsNodeExtCache);
210232153Smm    AcpiGbl_PsNodeExtCache = NULL;
211232153Smm
212232153Smm
213232153Smm#ifdef ACPI_DBG_TRACK_ALLOCATIONS
214232153Smm
215232153Smm    /* Debug only - display leftover memory allocation, if any */
216232153Smm
217232153Smm    AcpiUtDumpAllocations (ACPI_UINT32_MAX, NULL);
218232153Smm
219232153Smm    /* Free memory lists */
220232153Smm
221232153Smm    AcpiOsFree (AcpiGbl_GlobalList);
222232153Smm    AcpiGbl_GlobalList = NULL;
223232153Smm
224232153Smm    AcpiOsFree (AcpiGbl_NsNodeList);
225232153Smm    AcpiGbl_NsNodeList = NULL;
226232153Smm#endif
227232153Smm
228232153Smm    return (AE_OK);
229232153Smm}
230232153Smm
231232153Smm
232232153Smm/*******************************************************************************
233232153Smm *
234299529Smm * FUNCTION:    AcpiUtValidateBuffer
235232153Smm *
236232153Smm * PARAMETERS:  Buffer              - Buffer descriptor to be validated
237165000Skientzle *
238281988Sjilles * RETURN:      Status
239281988Sjilles *
240281988Sjilles * DESCRIPTION: Perform parameter validation checks on an ACPI_BUFFER
241281988Sjilles *
242281988Sjilles ******************************************************************************/
243165000Skientzle
244165000SkientzleACPI_STATUS
245232153SmmAcpiUtValidateBuffer (
246232153Smm    ACPI_BUFFER             *Buffer)
247165000Skientzle{
248224152Smm
249224152Smm    /* Obviously, the structure pointer must be valid */
250224152Smm
251269125Sdim    if (!Buffer)
252269125Sdim    {
253269125Sdim        return (AE_BAD_PARAMETER);
254269125Sdim    }
255269125Sdim
256269125Sdim    /* Special semantics for the length */
257224152Smm
258269125Sdim    if ((Buffer->Length == ACPI_NO_BUFFER)              ||
259269125Sdim        (Buffer->Length == ACPI_ALLOCATE_BUFFER)        ||
260269125Sdim        (Buffer->Length == ACPI_ALLOCATE_LOCAL_BUFFER))
261269125Sdim    {
262224152Smm        return (AE_OK);
263    }
264
265    /* Length is valid, the buffer pointer must be also */
266
267    if (!Buffer->Pointer)
268    {
269        return (AE_BAD_PARAMETER);
270    }
271
272    return (AE_OK);
273}
274
275
276/*******************************************************************************
277 *
278 * FUNCTION:    AcpiUtInitializeBuffer
279 *
280 * PARAMETERS:  Buffer              - Buffer to be validated
281 *              RequiredLength      - Length needed
282 *
283 * RETURN:      Status
284 *
285 * DESCRIPTION: Validate that the buffer is of the required length or
286 *              allocate a new buffer. Returned buffer is always zeroed.
287 *
288 ******************************************************************************/
289
290ACPI_STATUS
291AcpiUtInitializeBuffer (
292    ACPI_BUFFER             *Buffer,
293    ACPI_SIZE               RequiredLength)
294{
295    ACPI_SIZE               InputBufferLength;
296
297
298    /* Parameter validation */
299
300    if (!Buffer || !RequiredLength)
301    {
302        return (AE_BAD_PARAMETER);
303    }
304
305    /*
306     * Buffer->Length is used as both an input and output parameter. Get the
307     * input actual length and set the output required buffer length.
308     */
309    InputBufferLength = Buffer->Length;
310    Buffer->Length = RequiredLength;
311
312    /*
313     * The input buffer length contains the actual buffer length, or the type
314     * of buffer to be allocated by this routine.
315     */
316    switch (InputBufferLength)
317    {
318    case ACPI_NO_BUFFER:
319
320        /* Return the exception (and the required buffer length) */
321
322        return (AE_BUFFER_OVERFLOW);
323
324    case ACPI_ALLOCATE_BUFFER:
325        /*
326         * Allocate a new buffer. We directectly call AcpiOsAllocate here to
327         * purposefully bypass the (optionally enabled) internal allocation
328         * tracking mechanism since we only want to track internal
329         * allocations. Note: The caller should use AcpiOsFree to free this
330         * buffer created via ACPI_ALLOCATE_BUFFER.
331         */
332        Buffer->Pointer = AcpiOsAllocate (RequiredLength);
333        break;
334
335    case ACPI_ALLOCATE_LOCAL_BUFFER:
336
337        /* Allocate a new buffer with local interface to allow tracking */
338
339        Buffer->Pointer = ACPI_ALLOCATE (RequiredLength);
340        break;
341
342    default:
343
344        /* Existing buffer: Validate the size of the buffer */
345
346        if (InputBufferLength < RequiredLength)
347        {
348            return (AE_BUFFER_OVERFLOW);
349        }
350        break;
351    }
352
353    /* Validate allocation from above or input buffer pointer */
354
355    if (!Buffer->Pointer)
356    {
357        return (AE_NO_MEMORY);
358    }
359
360    /* Have a valid buffer, clear it */
361
362    ACPI_MEMSET (Buffer->Pointer, 0, RequiredLength);
363    return (AE_OK);
364}
365