1 /******************************************************************************
2  *
3  * Module Name: aslcache -- Local cache support for iASL
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2022, 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 MERCHANTABILITY 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 "aslcompiler.h"
45 
46 /*
47  * Local caches. The caches are fully deleted after the compilation/disassembly
48  * of each individual input file. Thus, individual allocations from the cache
49  * memory do not need to be freed or even released back into the cache.
50  *
51  * See aslallocate.c for standard heap allocations.
52  */
53 
54 
55 /*******************************************************************************
56  *
57  * FUNCTION:    UtLocalCacheCalloc
58  *
59  * PARAMETERS:  Length              - Size of buffer requested
60  *
61  * RETURN:      Pointer to the buffer. Aborts compiler on allocation failure
62  *
63  * DESCRIPTION: Allocate a string buffer. Bypass the local
64  *              dynamic memory manager for performance reasons (This has a
65  *              major impact on the speed of the compiler.)
66  *
67  ******************************************************************************/
68 
69 char *
UtLocalCacheCalloc(UINT32 Length)70 UtLocalCacheCalloc (
71     UINT32                  Length)
72 {
73     char                    *Buffer;
74     ASL_CACHE_INFO          *Cache;
75     UINT32                  CacheSize = ASL_STRING_CACHE_SIZE;
76 
77 
78 #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
79     /* Used for objects other than strings, so keep allocations aligned */
80     Length = ACPI_ROUND_UP_TO_NATIVE_WORD (Length);
81 #endif
82 
83     if (Length > CacheSize)
84     {
85         CacheSize = Length;
86 
87         if (AslGbl_StringCacheList)
88         {
89             Cache = UtLocalCalloc (sizeof (Cache->Next) + CacheSize);
90 
91             /* Link new cache buffer just following head of list */
92 
93             Cache->Next = AslGbl_StringCacheList->Next;
94             AslGbl_StringCacheList->Next = Cache;
95 
96             /* Leave cache management pointers alone as they pertain to head */
97 
98             AslGbl_StringCount++;
99             AslGbl_StringSize += Length;
100 
101             return (Cache->Buffer);
102         }
103     }
104 
105     if ((AslGbl_StringCacheNext + Length) >= AslGbl_StringCacheLast)
106     {
107         /* Allocate a new buffer */
108 
109         Cache = UtLocalCalloc (sizeof (Cache->Next) + CacheSize);
110 
111         /* Link new cache buffer to head of list */
112 
113         Cache->Next = AslGbl_StringCacheList;
114         AslGbl_StringCacheList = Cache;
115 
116         /* Setup cache management pointers */
117 
118         AslGbl_StringCacheNext = Cache->Buffer;
119         AslGbl_StringCacheLast = AslGbl_StringCacheNext + CacheSize;
120     }
121 
122     AslGbl_StringCount++;
123     AslGbl_StringSize += Length;
124 
125     Buffer = AslGbl_StringCacheNext;
126     AslGbl_StringCacheNext += Length;
127     return (Buffer);
128 }
129 
130 
131 /*******************************************************************************
132  *
133  * FUNCTION:    UtParseOpCacheCalloc
134  *
135  * PARAMETERS:  None
136  *
137  * RETURN:      New parse op. Aborts on allocation failure
138  *
139  * DESCRIPTION: Allocate a new parse op for the parse tree. Bypass the local
140  *              dynamic memory manager for performance reasons (This has a
141  *              major impact on the speed of the compiler.)
142  *
143  ******************************************************************************/
144 
145 ACPI_PARSE_OBJECT *
UtParseOpCacheCalloc(void)146 UtParseOpCacheCalloc (
147     void)
148 {
149     ASL_CACHE_INFO          *Cache;
150 
151 
152     if (AslGbl_ParseOpCacheNext >= AslGbl_ParseOpCacheLast)
153     {
154         /* Allocate a new buffer */
155 
156         Cache = UtLocalCalloc (sizeof (Cache->Next) +
157             (sizeof (ACPI_PARSE_OBJECT) * ASL_PARSEOP_CACHE_SIZE));
158 
159         /* Link new cache buffer to head of list */
160 
161         Cache->Next = AslGbl_ParseOpCacheList;
162         AslGbl_ParseOpCacheList = Cache;
163 
164         /* Setup cache management pointers */
165 
166         AslGbl_ParseOpCacheNext = ACPI_CAST_PTR (ACPI_PARSE_OBJECT, Cache->Buffer);
167         AslGbl_ParseOpCacheLast = AslGbl_ParseOpCacheNext + ASL_PARSEOP_CACHE_SIZE;
168     }
169 
170     AslGbl_ParseOpCount++;
171     return (AslGbl_ParseOpCacheNext++);
172 }
173 
174 
175 /*******************************************************************************
176  *
177  * FUNCTION:    UtSubtableCacheCalloc - Data Table compiler
178  *
179  * PARAMETERS:  None
180  *
181  * RETURN:      Pointer to the buffer. Aborts on allocation failure
182  *
183  * DESCRIPTION: Allocate a subtable object buffer. Bypass the local
184  *              dynamic memory manager for performance reasons (This has a
185  *              major impact on the speed of the compiler.)
186  *
187  ******************************************************************************/
188 
189 DT_SUBTABLE *
UtSubtableCacheCalloc(void)190 UtSubtableCacheCalloc (
191     void)
192 {
193     ASL_CACHE_INFO          *Cache;
194 
195 
196     if (AslGbl_SubtableCacheNext >= AslGbl_SubtableCacheLast)
197     {
198         /* Allocate a new buffer */
199 
200         Cache = UtLocalCalloc (sizeof (Cache->Next) +
201             (sizeof (DT_SUBTABLE) * ASL_SUBTABLE_CACHE_SIZE));
202 
203         /* Link new cache buffer to head of list */
204 
205         Cache->Next = AslGbl_SubtableCacheList;
206         AslGbl_SubtableCacheList = Cache;
207 
208         /* Setup cache management pointers */
209 
210         AslGbl_SubtableCacheNext = ACPI_CAST_PTR (DT_SUBTABLE, Cache->Buffer);
211         AslGbl_SubtableCacheLast = AslGbl_SubtableCacheNext + ASL_SUBTABLE_CACHE_SIZE;
212     }
213 
214     AslGbl_SubtableCount++;
215     return (AslGbl_SubtableCacheNext++);
216 }
217 
218 
219 /*******************************************************************************
220  *
221  * FUNCTION:    UtFieldCacheCalloc - Data Table compiler
222  *
223  * PARAMETERS:  None
224  *
225  * RETURN:      Pointer to the buffer. Aborts on allocation failure
226  *
227  * DESCRIPTION: Allocate a field object buffer. Bypass the local
228  *              dynamic memory manager for performance reasons (This has a
229  *              major impact on the speed of the compiler.)
230  *
231  ******************************************************************************/
232 
233 DT_FIELD *
UtFieldCacheCalloc(void)234 UtFieldCacheCalloc (
235     void)
236 {
237     ASL_CACHE_INFO          *Cache;
238 
239 
240     if (AslGbl_FieldCacheNext >= AslGbl_FieldCacheLast)
241     {
242         /* Allocate a new buffer */
243 
244         Cache = UtLocalCalloc (sizeof (Cache->Next) +
245             (sizeof (DT_FIELD) * ASL_FIELD_CACHE_SIZE));
246 
247         /* Link new cache buffer to head of list */
248 
249         Cache->Next = AslGbl_FieldCacheList;
250         AslGbl_FieldCacheList = Cache;
251 
252         /* Setup cache management pointers */
253 
254         AslGbl_FieldCacheNext = ACPI_CAST_PTR (DT_FIELD, Cache->Buffer);
255         AslGbl_FieldCacheLast =AslGbl_FieldCacheNext + ASL_FIELD_CACHE_SIZE;
256     }
257 
258     AslGbl_FieldCount++;
259     return (AslGbl_FieldCacheNext++);
260 }
261 
262 
263 /*******************************************************************************
264  *
265  * FUNCTION:    UtDeleteLocalCaches
266  *
267  * PARAMETERS:  None
268  *
269  * RETURN:      None
270  *
271  * DESCRIPTION: Delete all local cache buffer blocks
272  *
273  ******************************************************************************/
274 
275 void
UtDeleteLocalCaches(void)276 UtDeleteLocalCaches (
277     void)
278 {
279     UINT32                  BufferCount;
280     ASL_CACHE_INFO          *Next;
281 
282 
283     /*
284      * Generic cache, arbitrary size allocations
285      */
286     BufferCount = 0;
287     while (AslGbl_StringCacheList)
288     {
289         Next = AslGbl_StringCacheList->Next;
290         ACPI_FREE (AslGbl_StringCacheList);
291         AslGbl_StringCacheList = Next;
292         BufferCount++;
293     }
294 
295     DbgPrint (ASL_DEBUG_OUTPUT,
296         "%u Strings (%u bytes), Buffer size: %u bytes, %u Buffers\n",
297         AslGbl_StringCount, AslGbl_StringSize, ASL_STRING_CACHE_SIZE, BufferCount);
298 
299     /* Reset cache globals */
300 
301     AslGbl_StringSize = 0;
302     AslGbl_StringCount = 0;
303     AslGbl_StringCacheNext = NULL;
304     AslGbl_StringCacheLast = NULL;
305 
306     /*
307      * Parse Op cache
308      */
309     BufferCount = 0;
310     while (AslGbl_ParseOpCacheList)
311     {
312         Next = AslGbl_ParseOpCacheList->Next;
313         ACPI_FREE (AslGbl_ParseOpCacheList);
314         AslGbl_ParseOpCacheList = Next;
315         BufferCount++;
316     }
317 
318     DbgPrint (ASL_DEBUG_OUTPUT,
319         "%u ParseOps, Buffer size: %u ops (%u bytes), %u Buffers\n",
320         AslGbl_ParseOpCount, ASL_PARSEOP_CACHE_SIZE,
321         ((UINT32) sizeof (ACPI_PARSE_OBJECT) * ASL_PARSEOP_CACHE_SIZE), BufferCount);
322 
323     /* Reset cache globals */
324 
325     AslGbl_ParseOpCount = 0;
326     AslGbl_ParseOpCacheNext = NULL;
327     AslGbl_ParseOpCacheLast = NULL;
328     AslGbl_ParseTreeRoot = NULL;
329 
330     /*
331      * Table Compiler - Field cache
332      */
333     BufferCount = 0;
334     while (AslGbl_FieldCacheList)
335     {
336         Next = AslGbl_FieldCacheList->Next;
337         ACPI_FREE (AslGbl_FieldCacheList);
338         AslGbl_FieldCacheList = Next;
339         BufferCount++;
340     }
341 
342     DbgPrint (ASL_DEBUG_OUTPUT,
343         "%u Fields, Buffer size: %u fields (%u bytes), %u Buffers\n",
344         AslGbl_FieldCount, ASL_FIELD_CACHE_SIZE,
345         ((UINT32) sizeof (DT_FIELD) * ASL_FIELD_CACHE_SIZE), BufferCount);
346 
347     /* Reset cache globals */
348 
349     AslGbl_FieldCount = 0;
350     AslGbl_FieldCacheNext = NULL;
351     AslGbl_FieldCacheLast = NULL;
352 
353     /*
354      * Table Compiler - Subtable cache
355      */
356     BufferCount = 0;
357     while (AslGbl_SubtableCacheList)
358     {
359         Next = AslGbl_SubtableCacheList->Next;
360         ACPI_FREE (AslGbl_SubtableCacheList);
361         AslGbl_SubtableCacheList = Next;
362         BufferCount++;
363     }
364 
365     DbgPrint (ASL_DEBUG_OUTPUT,
366         "%u Subtables, Buffer size: %u subtables (%u bytes), %u Buffers\n",
367         AslGbl_SubtableCount, ASL_SUBTABLE_CACHE_SIZE,
368         ((UINT32) sizeof (DT_SUBTABLE) * ASL_SUBTABLE_CACHE_SIZE), BufferCount);
369 
370     /* Reset cache globals */
371 
372     AslGbl_SubtableCount = 0;
373     AslGbl_SubtableCacheNext = NULL;
374     AslGbl_SubtableCacheLast = NULL;
375 }
376