blob: 7580f6b3069ec5ecaf7303be5ff88eb5087683c4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
Robert Moore73459f72005-06-24 00:00:00 -04003 * Module Name: utalloc - local memory allocation routines
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *****************************************************************************/
6
7/*
Len Brown75a44ce2008-04-23 23:00:13 -04008 * Copyright (C) 2000 - 2008, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050045#include "accommon.h"
46#include "acdebug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define _COMPONENT ACPI_UTILITIES
Len Brown4be44fc2005-08-05 00:44:28 -040049ACPI_MODULE_NAME("utalloc")
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Robert Moore44f6c012005-04-18 22:49:35 -040051/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *
Robert Moore73459f72005-06-24 00:00:00 -040053 * FUNCTION: acpi_ut_create_caches
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 *
Robert Moore73459f72005-06-24 00:00:00 -040055 * PARAMETERS: None
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 *
Robert Moore73459f72005-06-24 00:00:00 -040057 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 *
Robert Moore73459f72005-06-24 00:00:00 -040059 * DESCRIPTION: Create all local caches
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 *
61 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -040062acpi_status acpi_ut_create_caches(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Len Brown4be44fc2005-08-05 00:44:28 -040064 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Robert Moore73459f72005-06-24 00:00:00 -040066 /* Object Caches, for frequently used objects */
67
Len Brown4be44fc2005-08-05 00:44:28 -040068 status =
Bob Moore616861242006-03-17 16:44:00 -050069 acpi_os_create_cache("Acpi-Namespace",
70 sizeof(struct acpi_namespace_node),
71 ACPI_MAX_NAMESPACE_CACHE_DEPTH,
72 &acpi_gbl_namespace_cache);
73 if (ACPI_FAILURE(status)) {
74 return (status);
75 }
76
77 status =
78 acpi_os_create_cache("Acpi-State", sizeof(union acpi_generic_state),
Len Brown4be44fc2005-08-05 00:44:28 -040079 ACPI_MAX_STATE_CACHE_DEPTH,
80 &acpi_gbl_state_cache);
81 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -040082 return (status);
83 }
84
Len Brown4be44fc2005-08-05 00:44:28 -040085 status =
Bob Moore616861242006-03-17 16:44:00 -050086 acpi_os_create_cache("Acpi-Parse",
Len Brown4be44fc2005-08-05 00:44:28 -040087 sizeof(struct acpi_parse_obj_common),
88 ACPI_MAX_PARSE_CACHE_DEPTH,
89 &acpi_gbl_ps_node_cache);
90 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -040091 return (status);
92 }
93
Len Brown4be44fc2005-08-05 00:44:28 -040094 status =
Bob Mooreb229cf92006-04-21 17:15:00 -040095 acpi_os_create_cache("Acpi-ParseExt",
Len Brown4be44fc2005-08-05 00:44:28 -040096 sizeof(struct acpi_parse_obj_named),
97 ACPI_MAX_EXTPARSE_CACHE_DEPTH,
98 &acpi_gbl_ps_node_ext_cache);
99 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -0400100 return (status);
101 }
102
Len Brown4be44fc2005-08-05 00:44:28 -0400103 status =
Bob Moore616861242006-03-17 16:44:00 -0500104 acpi_os_create_cache("Acpi-Operand",
Len Brown4be44fc2005-08-05 00:44:28 -0400105 sizeof(union acpi_operand_object),
106 ACPI_MAX_OBJECT_CACHE_DEPTH,
107 &acpi_gbl_operand_cache);
108 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -0400109 return (status);
110 }
Bob Moore41195322006-05-26 16:36:00 -0400111#ifdef ACPI_DBG_TRACK_ALLOCATIONS
112
113 /* Memory allocation lists */
114
115 status = acpi_ut_create_list("Acpi-Global", 0, &acpi_gbl_global_list);
116 if (ACPI_FAILURE(status)) {
117 return (status);
118 }
119
120 status =
121 acpi_ut_create_list("Acpi-Namespace",
122 sizeof(struct acpi_namespace_node),
123 &acpi_gbl_ns_node_list);
124 if (ACPI_FAILURE(status)) {
125 return (status);
126 }
127#endif
128
Robert Moore73459f72005-06-24 00:00:00 -0400129 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Robert Moore44f6c012005-04-18 22:49:35 -0400132/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *
Robert Moore73459f72005-06-24 00:00:00 -0400134 * FUNCTION: acpi_ut_delete_caches
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *
Robert Moore73459f72005-06-24 00:00:00 -0400136 * PARAMETERS: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
Robert Moore73459f72005-06-24 00:00:00 -0400138 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 *
Robert Moore73459f72005-06-24 00:00:00 -0400140 * DESCRIPTION: Purge and delete all local caches
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 *
142 ******************************************************************************/
143
Len Brown4be44fc2005-08-05 00:44:28 -0400144acpi_status acpi_ut_delete_caches(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Bob Moored41eb992007-02-02 19:48:23 +0300146#ifdef ACPI_DBG_TRACK_ALLOCATIONS
147 char buffer[7];
148
149 if (acpi_gbl_display_final_mem_stats) {
150 ACPI_STRCPY(buffer, "MEMORY");
Bob Moore1d18c052008-04-10 19:06:40 +0400151 (void)acpi_db_display_statistics(buffer);
Bob Moored41eb992007-02-02 19:48:23 +0300152 }
153#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Bob Moore616861242006-03-17 16:44:00 -0500155 (void)acpi_os_delete_cache(acpi_gbl_namespace_cache);
156 acpi_gbl_namespace_cache = NULL;
157
Len Brown4be44fc2005-08-05 00:44:28 -0400158 (void)acpi_os_delete_cache(acpi_gbl_state_cache);
Robert Moore73459f72005-06-24 00:00:00 -0400159 acpi_gbl_state_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Len Brown4be44fc2005-08-05 00:44:28 -0400161 (void)acpi_os_delete_cache(acpi_gbl_operand_cache);
Robert Moore73459f72005-06-24 00:00:00 -0400162 acpi_gbl_operand_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Len Brown4be44fc2005-08-05 00:44:28 -0400164 (void)acpi_os_delete_cache(acpi_gbl_ps_node_cache);
Robert Moore73459f72005-06-24 00:00:00 -0400165 acpi_gbl_ps_node_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Len Brown4be44fc2005-08-05 00:44:28 -0400167 (void)acpi_os_delete_cache(acpi_gbl_ps_node_ext_cache);
Robert Moore73459f72005-06-24 00:00:00 -0400168 acpi_gbl_ps_node_ext_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Bob Moore41195322006-05-26 16:36:00 -0400170#ifdef ACPI_DBG_TRACK_ALLOCATIONS
171
172 /* Debug only - display leftover memory allocation, if any */
173
174 acpi_ut_dump_allocations(ACPI_UINT32_MAX, NULL);
175
176 /* Free memory lists */
177
Len Brown02438d82006-06-30 03:19:10 -0400178 ACPI_FREE(acpi_gbl_global_list);
Bob Moore41195322006-05-26 16:36:00 -0400179 acpi_gbl_global_list = NULL;
180
Len Brown02438d82006-06-30 03:19:10 -0400181 ACPI_FREE(acpi_gbl_ns_node_list);
Bob Moore41195322006-05-26 16:36:00 -0400182 acpi_gbl_ns_node_list = NULL;
183#endif
184
Robert Moore73459f72005-06-24 00:00:00 -0400185 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/*******************************************************************************
189 *
190 * FUNCTION: acpi_ut_validate_buffer
191 *
192 * PARAMETERS: Buffer - Buffer descriptor to be validated
193 *
194 * RETURN: Status
195 *
196 * DESCRIPTION: Perform parameter validation checks on an struct acpi_buffer
197 *
198 ******************************************************************************/
199
Len Brown4be44fc2005-08-05 00:44:28 -0400200acpi_status acpi_ut_validate_buffer(struct acpi_buffer * buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202
203 /* Obviously, the structure pointer must be valid */
204
205 if (!buffer) {
206 return (AE_BAD_PARAMETER);
207 }
208
209 /* Special semantics for the length */
210
Len Brown4be44fc2005-08-05 00:44:28 -0400211 if ((buffer->length == ACPI_NO_BUFFER) ||
212 (buffer->length == ACPI_ALLOCATE_BUFFER) ||
213 (buffer->length == ACPI_ALLOCATE_LOCAL_BUFFER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return (AE_OK);
215 }
216
217 /* Length is valid, the buffer pointer must be also */
218
219 if (!buffer->pointer) {
220 return (AE_BAD_PARAMETER);
221 }
222
223 return (AE_OK);
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/*******************************************************************************
227 *
228 * FUNCTION: acpi_ut_initialize_buffer
229 *
230 * PARAMETERS: Buffer - Buffer to be validated
231 * required_length - Length needed
232 *
233 * RETURN: Status
234 *
235 * DESCRIPTION: Validate that the buffer is of the required length or
Bob Moore68e125c2008-09-27 11:34:48 +0800236 * allocate a new buffer. Returned buffer is always zeroed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 *
238 ******************************************************************************/
239
240acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400241acpi_ut_initialize_buffer(struct acpi_buffer * buffer,
242 acpi_size required_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Bob Moore68e125c2008-09-27 11:34:48 +0800244 acpi_size input_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Bob Moore3c7db222008-08-04 11:13:01 +0800246 /* Parameter validation */
247
248 if (!buffer || !required_length) {
249 return (AE_BAD_PARAMETER);
Ingo Molnarf88133d2008-07-21 15:57:45 +0200250 }
Bob Moore3c7db222008-08-04 11:13:01 +0800251
Bob Moore68e125c2008-09-27 11:34:48 +0800252 /*
253 * Buffer->Length is used as both an input and output parameter. Get the
254 * input actual length and set the output required buffer length.
255 */
256 input_buffer_length = buffer->length;
257 buffer->length = required_length;
258
259 /*
260 * The input buffer length contains the actual buffer length, or the type
261 * of buffer to be allocated by this routine.
262 */
263 switch (input_buffer_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 case ACPI_NO_BUFFER:
265
Bob Moore68e125c2008-09-27 11:34:48 +0800266 /* Return the exception (and the required buffer length) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Bob Moore68e125c2008-09-27 11:34:48 +0800268 return (AE_BUFFER_OVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 case ACPI_ALLOCATE_BUFFER:
271
272 /* Allocate a new buffer */
273
Len Brown4be44fc2005-08-05 00:44:28 -0400274 buffer->pointer = acpi_os_allocate(required_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 break;
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 case ACPI_ALLOCATE_LOCAL_BUFFER:
278
279 /* Allocate a new buffer with local interface to allow tracking */
280
Bob Moore68e125c2008-09-27 11:34:48 +0800281 buffer->pointer = ACPI_ALLOCATE(required_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 default:
285
286 /* Existing buffer: Validate the size of the buffer */
287
Bob Moore68e125c2008-09-27 11:34:48 +0800288 if (input_buffer_length < required_length) {
289 return (AE_BUFFER_OVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 break;
292 }
293
Bob Moore68e125c2008-09-27 11:34:48 +0800294 /* Validate allocation from above or input buffer pointer */
295
296 if (!buffer->pointer) {
297 return (AE_NO_MEMORY);
298 }
299
300 /* Have a valid buffer, clear it */
301
302 ACPI_MEMSET(buffer->pointer, 0, required_length);
303 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Len Browne21c1ca2006-07-10 01:35:51 -0400306#ifdef NOT_USED_BY_LINUX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/*******************************************************************************
308 *
309 * FUNCTION: acpi_ut_allocate
310 *
311 * PARAMETERS: Size - Size of the allocation
312 * Component - Component type of caller
313 * Module - Source file name of caller
314 * Line - Line number of caller
315 *
316 * RETURN: Address of the allocated memory on success, NULL on failure.
317 *
Bob Moore616861242006-03-17 16:44:00 -0500318 * DESCRIPTION: Subsystem equivalent of malloc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 *
320 ******************************************************************************/
321
Bob Moore4b8ed632008-06-10 13:55:53 +0800322void *acpi_ut_allocate(acpi_size size,
323 u32 component, const char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Len Brown4be44fc2005-08-05 00:44:28 -0400325 void *allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Bob Mooreb229cf92006-04-21 17:15:00 -0400327 ACPI_FUNCTION_TRACE_U32(ut_allocate, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 /* Check for an inadvertent size of zero bytes */
330
331 if (!size) {
Bob Moore616861242006-03-17 16:44:00 -0500332 ACPI_WARNING((module, line,
333 "Attempt to allocate zero bytes, allocating 1 byte"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 size = 1;
335 }
336
Len Brown4be44fc2005-08-05 00:44:28 -0400337 allocation = acpi_os_allocate(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!allocation) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /* Report allocation error */
341
Bob Moore616861242006-03-17 16:44:00 -0500342 ACPI_WARNING((module, line,
343 "Could not allocate size %X", (u32) size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Len Brown4be44fc2005-08-05 00:44:28 -0400345 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
Len Brown4be44fc2005-08-05 00:44:28 -0400348 return_PTR(allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/*******************************************************************************
352 *
Bob Moore616861242006-03-17 16:44:00 -0500353 * FUNCTION: acpi_ut_allocate_zeroed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 *
355 * PARAMETERS: Size - Size of the allocation
356 * Component - Component type of caller
357 * Module - Source file name of caller
358 * Line - Line number of caller
359 *
360 * RETURN: Address of the allocated memory on success, NULL on failure.
361 *
Bob Moore616861242006-03-17 16:44:00 -0500362 * DESCRIPTION: Subsystem equivalent of calloc. Allocate and zero memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 *
364 ******************************************************************************/
365
Bob Moore616861242006-03-17 16:44:00 -0500366void *acpi_ut_allocate_zeroed(acpi_size size,
Bob Moore4b8ed632008-06-10 13:55:53 +0800367 u32 component, const char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Len Brown4be44fc2005-08-05 00:44:28 -0400369 void *allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Len Brown4be44fc2005-08-05 00:44:28 -0400371 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Bob Moore616861242006-03-17 16:44:00 -0500373 allocation = acpi_ut_allocate(size, component, module, line);
374 if (allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Bob Moore616861242006-03-17 16:44:00 -0500376 /* Clear the memory block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Bob Moore616861242006-03-17 16:44:00 -0500378 ACPI_MEMSET(allocation, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
Bob Moore616861242006-03-17 16:44:00 -0500381 return (allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
Len Browne21c1ca2006-07-10 01:35:51 -0400383#endif