1 /******************************************************************************
2  *
3  * Module Name: utalloc - local memory allocation routines
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2016, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
25  *
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.
29  *
30  * NO WARRANTY
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.
42  */
43 
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acdebug.h"
47 
48 #define _COMPONENT          ACPI_UTILITIES
49         ACPI_MODULE_NAME    ("utalloc")
50 
51 
52 #if !defined (USE_NATIVE_ALLOCATE_ZEROED)
53 /*******************************************************************************
54  *
55  * FUNCTION:    AcpiOsAllocateZeroed
56  *
57  * PARAMETERS:  Size                - Size of the allocation
58  *
59  * RETURN:      Address of the allocated memory on success, NULL on failure.
60  *
61  * DESCRIPTION: Subsystem equivalent of calloc. Allocate and zero memory.
62  *              This is the default implementation. Can be overridden via the
63  *              USE_NATIVE_ALLOCATE_ZEROED flag.
64  *
65  ******************************************************************************/
66 
67 void *
68 AcpiOsAllocateZeroed (
69     ACPI_SIZE               Size)
70 {
71     void                    *Allocation;
72 
73 
74     ACPI_FUNCTION_ENTRY ();
75 
76 
77     Allocation = AcpiOsAllocate (Size);
78     if (Allocation)
79     {
80         /* Clear the memory block */
81 
82         memset (Allocation, 0, Size);
83     }
84 
85     return (Allocation);
86 }
87 
88 #endif /* !USE_NATIVE_ALLOCATE_ZEROED */
89 
90 
91 /*******************************************************************************
92  *
93  * FUNCTION:    AcpiUtCreateCaches
94  *
95  * PARAMETERS:  None
96  *
97  * RETURN:      Status
98  *
99  * DESCRIPTION: Create all local caches
100  *
101  ******************************************************************************/
102 
103 ACPI_STATUS
104 AcpiUtCreateCaches (
105     void)
106 {
107     ACPI_STATUS             Status;
108 
109 
110     /* Object Caches, for frequently used objects */
111 
112     Status = AcpiOsCreateCache ("Acpi-Namespace", sizeof (ACPI_NAMESPACE_NODE),
113         ACPI_MAX_NAMESPACE_CACHE_DEPTH, &AcpiGbl_NamespaceCache);
114     if (ACPI_FAILURE (Status))
115     {
116         return (Status);
117     }
118 
119     Status = AcpiOsCreateCache ("Acpi-State", sizeof (ACPI_GENERIC_STATE),
120         ACPI_MAX_STATE_CACHE_DEPTH, &AcpiGbl_StateCache);
121     if (ACPI_FAILURE (Status))
122     {
123         return (Status);
124     }
125 
126     Status = AcpiOsCreateCache ("Acpi-Parse", sizeof (ACPI_PARSE_OBJ_COMMON),
127         ACPI_MAX_PARSE_CACHE_DEPTH, &AcpiGbl_PsNodeCache);
128     if (ACPI_FAILURE (Status))
129     {
130         return (Status);
131     }
132 
133     Status = AcpiOsCreateCache ("Acpi-ParseExt", sizeof (ACPI_PARSE_OBJ_NAMED),
134         ACPI_MAX_EXTPARSE_CACHE_DEPTH, &AcpiGbl_PsNodeExtCache);
135     if (ACPI_FAILURE (Status))
136     {
137         return (Status);
138     }
139 
140     Status = AcpiOsCreateCache ("Acpi-Operand", sizeof (ACPI_OPERAND_OBJECT),
141         ACPI_MAX_OBJECT_CACHE_DEPTH, &AcpiGbl_OperandCache);
142     if (ACPI_FAILURE (Status))
143     {
144         return (Status);
145     }
146 
147 
148 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
149 
150     /* Memory allocation lists */
151 
152     Status = AcpiUtCreateList ("Acpi-Global", 0,
153         &AcpiGbl_GlobalList);
154     if (ACPI_FAILURE (Status))
155     {
156         return (Status);
157     }
158 
159     Status = AcpiUtCreateList ("Acpi-Namespace", sizeof (ACPI_NAMESPACE_NODE),
160         &AcpiGbl_NsNodeList);
161     if (ACPI_FAILURE (Status))
162     {
163         return (Status);
164     }
165 #endif
166 
167     return (AE_OK);
168 }
169 
170 
171 /*******************************************************************************
172  *
173  * FUNCTION:    AcpiUtDeleteCaches
174  *
175  * PARAMETERS:  None
176  *
177  * RETURN:      Status
178  *
179  * DESCRIPTION: Purge and delete all local caches
180  *
181  ******************************************************************************/
182 
183 ACPI_STATUS
184 AcpiUtDeleteCaches (
185     void)
186 {
187 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
188     char                    Buffer[7];
189 
190 
191     if (AcpiGbl_DisplayFinalMemStats)
192     {
193         strcpy (Buffer, "MEMORY");
194         (void) AcpiDbDisplayStatistics (Buffer);
195     }
196 #endif
197 
198     (void) AcpiOsDeleteCache (AcpiGbl_NamespaceCache);
199     AcpiGbl_NamespaceCache = NULL;
200 
201     (void) AcpiOsDeleteCache (AcpiGbl_StateCache);
202     AcpiGbl_StateCache = NULL;
203 
204     (void) AcpiOsDeleteCache (AcpiGbl_OperandCache);
205     AcpiGbl_OperandCache = NULL;
206 
207     (void) AcpiOsDeleteCache (AcpiGbl_PsNodeCache);
208     AcpiGbl_PsNodeCache = NULL;
209 
210     (void) AcpiOsDeleteCache (AcpiGbl_PsNodeExtCache);
211     AcpiGbl_PsNodeExtCache = NULL;
212 
213 
214 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
215 
216     /* Debug only - display leftover memory allocation, if any */
217 
218     AcpiUtDumpAllocations (ACPI_UINT32_MAX, NULL);
219 
220     /* Free memory lists */
221 
222     AcpiOsFree (AcpiGbl_GlobalList);
223     AcpiGbl_GlobalList = NULL;
224 
225     AcpiOsFree (AcpiGbl_NsNodeList);
226     AcpiGbl_NsNodeList = NULL;
227 #endif
228 
229     return (AE_OK);
230 }
231 
232 
233 /*******************************************************************************
234  *
235  * FUNCTION:    AcpiUtValidateBuffer
236  *
237  * PARAMETERS:  Buffer              - Buffer descriptor to be validated
238  *
239  * RETURN:      Status
240  *
241  * DESCRIPTION: Perform parameter validation checks on an ACPI_BUFFER
242  *
243  ******************************************************************************/
244 
245 ACPI_STATUS
246 AcpiUtValidateBuffer (
247     ACPI_BUFFER             *Buffer)
248 {
249 
250     /* Obviously, the structure pointer must be valid */
251 
252     if (!Buffer)
253     {
254         return (AE_BAD_PARAMETER);
255     }
256 
257     /* Special semantics for the length */
258 
259     if ((Buffer->Length == ACPI_NO_BUFFER)              ||
260         (Buffer->Length == ACPI_ALLOCATE_BUFFER)        ||
261         (Buffer->Length == ACPI_ALLOCATE_LOCAL_BUFFER))
262     {
263         return (AE_OK);
264     }
265 
266     /* Length is valid, the buffer pointer must be also */
267 
268     if (!Buffer->Pointer)
269     {
270         return (AE_BAD_PARAMETER);
271     }
272 
273     return (AE_OK);
274 }
275 
276 
277 /*******************************************************************************
278  *
279  * FUNCTION:    AcpiUtInitializeBuffer
280  *
281  * PARAMETERS:  Buffer              - Buffer to be validated
282  *              RequiredLength      - Length needed
283  *
284  * RETURN:      Status
285  *
286  * DESCRIPTION: Validate that the buffer is of the required length or
287  *              allocate a new buffer. Returned buffer is always zeroed.
288  *
289  ******************************************************************************/
290 
291 ACPI_STATUS
292 AcpiUtInitializeBuffer (
293     ACPI_BUFFER             *Buffer,
294     ACPI_SIZE               RequiredLength)
295 {
296     ACPI_SIZE               InputBufferLength;
297 
298 
299     /* Parameter validation */
300 
301     if (!Buffer || !RequiredLength)
302     {
303         return (AE_BAD_PARAMETER);
304     }
305 
306     /*
307      * Buffer->Length is used as both an input and output parameter. Get the
308      * input actual length and set the output required buffer length.
309      */
310     InputBufferLength = Buffer->Length;
311     Buffer->Length = RequiredLength;
312 
313     /*
314      * The input buffer length contains the actual buffer length, or the type
315      * of buffer to be allocated by this routine.
316      */
317     switch (InputBufferLength)
318     {
319     case ACPI_NO_BUFFER:
320 
321         /* Return the exception (and the required buffer length) */
322 
323         return (AE_BUFFER_OVERFLOW);
324 
325     case ACPI_ALLOCATE_BUFFER:
326         /*
327          * Allocate a new buffer. We directectly call AcpiOsAllocate here to
328          * purposefully bypass the (optionally enabled) internal allocation
329          * tracking mechanism since we only want to track internal
330          * allocations. Note: The caller should use AcpiOsFree to free this
331          * buffer created via ACPI_ALLOCATE_BUFFER.
332          */
333         Buffer->Pointer = AcpiOsAllocate (RequiredLength);
334         break;
335 
336     case ACPI_ALLOCATE_LOCAL_BUFFER:
337 
338         /* Allocate a new buffer with local interface to allow tracking */
339 
340         Buffer->Pointer = ACPI_ALLOCATE (RequiredLength);
341         break;
342 
343     default:
344 
345         /* Existing buffer: Validate the size of the buffer */
346 
347         if (InputBufferLength < RequiredLength)
348         {
349             return (AE_BUFFER_OVERFLOW);
350         }
351         break;
352     }
353 
354     /* Validate allocation from above or input buffer pointer */
355 
356     if (!Buffer->Pointer)
357     {
358         return (AE_NO_MEMORY);
359     }
360 
361     /* Have a valid buffer, clear it */
362 
363     memset (Buffer->Pointer, 0, RequiredLength);
364     return (AE_OK);
365 }
366