xref: /netbsd-src/sys/external/bsd/acpica/dist/compiler/aslallocate.c (revision 046a29855e04359424fd074e8313af6b6be8cfb6)
1 /******************************************************************************
2  *
3  * Module Name: aslallocate -- Local memory allocation
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2023, 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 heap allocation wrappers. See aslcache.c for allocation from local
48  * cache allocations
49  */
50 
51 
52 /*******************************************************************************
53  *
54  * FUNCTION:    UtLocalCalloc
55  *
56  * PARAMETERS:  Size                - Bytes to be allocated
57  *
58  * RETURN:      Pointer to the allocated memory. If this function returns
59  *              (the compiler is not aborted), the pointer is guaranteed to
60  *              be valid.
61  *
62  * DESCRIPTION: Allocate zero-initialized memory. The point of this function
63  *              is to abort the compile on an allocation failure, on the
64  *              assumption that nothing more can be accomplished.
65  *
66  * NOTE:        For allocation from the local caches, see aslcache.c
67  *
68  ******************************************************************************/
69 
70 void *
UtLocalCalloc(UINT32 Size)71 UtLocalCalloc (
72     UINT32                  Size)
73 {
74     void                    *Allocated;
75 
76 
77     Allocated = ACPI_ALLOCATE_ZEROED (Size);
78     if (!Allocated)
79     {
80         AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
81             AslGbl_CurrentLineNumber, AslGbl_LogicalLineNumber,
82             AslGbl_InputByteCount, AslGbl_CurrentColumn,
83             AslGbl_Files[ASL_FILE_INPUT].Filename, NULL);
84 
85         CmCleanupAndExit ();
86         exit (1);
87     }
88 
89     AslGbl_TotalAllocations++;
90     AslGbl_TotalAllocated += Size;
91     return (Allocated);
92 }
93 
94 
95 /******************************************************************************
96  *
97  * FUNCTION:    UtExpandLineBuffers
98  *
99  * PARAMETERS:  None. Updates global line buffer pointers.
100  *
101  * RETURN:      None. Reallocates the global line buffers
102  *
103  * DESCRIPTION: Called if the current line buffer becomes filled. Reallocates
104  *              all global line buffers and updates AslGbl_LineBufferSize. NOTE:
105  *              Also used for the initial allocation of the buffers, when
106  *              all of the buffer pointers are NULL. Initial allocations are
107  *              of size ASL_DEFAULT_LINE_BUFFER_SIZE
108  *
109  *****************************************************************************/
110 
111 void
UtExpandLineBuffers(void)112 UtExpandLineBuffers (
113     void)
114 {
115     UINT32                  NewSize;
116 
117 
118     /* Attempt to double the size of all line buffers */
119 
120     NewSize = AslGbl_LineBufferSize * 2;
121     if (AslGbl_CurrentLineBuffer)
122     {
123         DbgPrint (ASL_DEBUG_OUTPUT,
124             "Increasing line buffer size from %u to %u\n",
125             AslGbl_LineBufferSize, NewSize);
126     }
127 
128     UtReallocLineBuffers (&AslGbl_CurrentLineBuffer, AslGbl_LineBufferSize, NewSize);
129     UtReallocLineBuffers (&AslGbl_MainTokenBuffer, AslGbl_LineBufferSize, NewSize);
130     UtReallocLineBuffers (&AslGbl_MacroTokenBuffer, AslGbl_LineBufferSize, NewSize);
131     UtReallocLineBuffers (&AslGbl_ExpressionTokenBuffer, AslGbl_LineBufferSize, NewSize);
132 
133     AslGbl_LineBufPtr = AslGbl_CurrentLineBuffer;
134     AslGbl_LineBufferSize = NewSize;
135 }
136 
137 
138 /******************************************************************************
139  *
140  * FUNCTION:    UtReallocLineBuffers
141  *
142  * PARAMETERS:  Buffer              - Buffer to realloc
143  *              OldSize             - Old size of Buffer
144  *              NewSize             - New size of Buffer
145  *
146  * RETURN:      none
147  *
148  * DESCRIPTION: Reallocate and initialize Buffer
149  *
150  *****************************************************************************/
151 
152 void
UtReallocLineBuffers(char ** Buffer,UINT32 OldSize,UINT32 NewSize)153 UtReallocLineBuffers (
154     char                    **Buffer,
155     UINT32                  OldSize,
156     UINT32                  NewSize)
157 {
158 
159     *Buffer = realloc (*Buffer, NewSize);
160     if (*Buffer)
161     {
162         memset (*Buffer + OldSize, 0, NewSize - OldSize);
163         return;
164     }
165 
166     printf ("Could not increase line buffer size from %u to %u\n",
167         OldSize, NewSize);
168 
169     AslError (ASL_ERROR, ASL_MSG_BUFFER_ALLOCATION, NULL, NULL);
170     AslAbort ();
171 }
172 
173 
174 /******************************************************************************
175  *
176  * FUNCTION:    UtFreeLineBuffers
177  *
178  * PARAMETERS:  None
179  *
180  * RETURN:      None
181  *
182  * DESCRIPTION: Free all line buffers
183  *
184  *****************************************************************************/
185 
186 void
UtFreeLineBuffers(void)187 UtFreeLineBuffers (
188     void)
189 {
190     free (AslGbl_CurrentLineBuffer);
191 
192     free (AslGbl_MainTokenBuffer);
193 
194     free (AslGbl_MacroTokenBuffer);
195 
196     free (AslGbl_MacroTokenReplaceBuffer);
197 
198     free (AslGbl_ExpressionTokenBuffer);
199 }
200