blob: 03b0044974c22b3846472167e1e11768fc362aac [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/*
Bob Moore4a90c7e2006-01-13 16:22:00 -05008 * Copyright (C) 2000 - 2006, R. Byron Moore
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>
45
46#define _COMPONENT ACPI_UTILITIES
Len Brown4be44fc2005-08-05 00:44:28 -040047ACPI_MODULE_NAME("utalloc")
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Robert Moore44f6c012005-04-18 22:49:35 -040049/* Local prototypes */
Bob Mooredefba1d2005-12-16 17:05:00 -050050#ifdef ACPI_DBG_TRACK_ALLOCATIONS
Len Brown4be44fc2005-08-05 00:44:28 -040051static struct acpi_debug_mem_block *acpi_ut_find_allocation(void *allocation);
Robert Moore44f6c012005-04-18 22:49:35 -040052
53static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040054acpi_ut_track_allocation(struct acpi_debug_mem_block *address,
55 acpi_size size,
56 u8 alloc_type, u32 component, char *module, u32 line);
Robert Moore44f6c012005-04-18 22:49:35 -040057
58static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040059acpi_ut_remove_allocation(struct acpi_debug_mem_block *address,
60 u32 component, char *module, u32 line);
Robert Moore44f6c012005-04-18 22:49:35 -040061
Robert Moore73459f72005-06-24 00:00:00 -040062static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040063acpi_ut_create_list(char *list_name,
64 u16 object_size, struct acpi_memory_list **return_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Robert Moore44f6c012005-04-18 22:49:35 -040067/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 *
Robert Moore73459f72005-06-24 00:00:00 -040069 * FUNCTION: acpi_ut_create_caches
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 *
Robert Moore73459f72005-06-24 00:00:00 -040071 * PARAMETERS: None
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
Robert Moore73459f72005-06-24 00:00:00 -040073 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
Robert Moore73459f72005-06-24 00:00:00 -040075 * DESCRIPTION: Create all local caches
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 ******************************************************************************/
78
Len Brown4be44fc2005-08-05 00:44:28 -040079acpi_status acpi_ut_create_caches(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Len Brown4be44fc2005-08-05 00:44:28 -040081 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#ifdef ACPI_DBG_TRACK_ALLOCATIONS
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Robert Moore73459f72005-06-24 00:00:00 -040085 /* Memory allocation lists */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Len Brown4be44fc2005-08-05 00:44:28 -040087 status = acpi_ut_create_list("Acpi-Global", 0, &acpi_gbl_global_list);
88 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -040089 return (status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91
Len Brown4be44fc2005-08-05 00:44:28 -040092 status =
93 acpi_ut_create_list("Acpi-Namespace",
94 sizeof(struct acpi_namespace_node),
95 &acpi_gbl_ns_node_list);
96 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -040097 return (status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#endif
100
Robert Moore73459f72005-06-24 00:00:00 -0400101 /* Object Caches, for frequently used objects */
102
Len Brown4be44fc2005-08-05 00:44:28 -0400103 status =
104 acpi_os_create_cache("acpi_state", sizeof(union acpi_generic_state),
105 ACPI_MAX_STATE_CACHE_DEPTH,
106 &acpi_gbl_state_cache);
107 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -0400108 return (status);
109 }
110
Len Brown4be44fc2005-08-05 00:44:28 -0400111 status =
112 acpi_os_create_cache("acpi_parse",
113 sizeof(struct acpi_parse_obj_common),
114 ACPI_MAX_PARSE_CACHE_DEPTH,
115 &acpi_gbl_ps_node_cache);
116 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -0400117 return (status);
118 }
119
Len Brown4be44fc2005-08-05 00:44:28 -0400120 status =
121 acpi_os_create_cache("acpi_parse_ext",
122 sizeof(struct acpi_parse_obj_named),
123 ACPI_MAX_EXTPARSE_CACHE_DEPTH,
124 &acpi_gbl_ps_node_ext_cache);
125 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -0400126 return (status);
127 }
128
Len Brown4be44fc2005-08-05 00:44:28 -0400129 status =
130 acpi_os_create_cache("acpi_operand",
131 sizeof(union acpi_operand_object),
132 ACPI_MAX_OBJECT_CACHE_DEPTH,
133 &acpi_gbl_operand_cache);
134 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -0400135 return (status);
136 }
137
138 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Robert Moore44f6c012005-04-18 22:49:35 -0400141/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 *
Robert Moore73459f72005-06-24 00:00:00 -0400143 * FUNCTION: acpi_ut_delete_caches
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 *
Robert Moore73459f72005-06-24 00:00:00 -0400145 * PARAMETERS: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 *
Robert Moore73459f72005-06-24 00:00:00 -0400147 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *
Robert Moore73459f72005-06-24 00:00:00 -0400149 * DESCRIPTION: Purge and delete all local caches
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
151 ******************************************************************************/
152
Len Brown4be44fc2005-08-05 00:44:28 -0400153acpi_status acpi_ut_delete_caches(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Len Brown4be44fc2005-08-05 00:44:28 -0400156 (void)acpi_os_delete_cache(acpi_gbl_state_cache);
Robert Moore73459f72005-06-24 00:00:00 -0400157 acpi_gbl_state_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Len Brown4be44fc2005-08-05 00:44:28 -0400159 (void)acpi_os_delete_cache(acpi_gbl_operand_cache);
Robert Moore73459f72005-06-24 00:00:00 -0400160 acpi_gbl_operand_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Len Brown4be44fc2005-08-05 00:44:28 -0400162 (void)acpi_os_delete_cache(acpi_gbl_ps_node_cache);
Robert Moore73459f72005-06-24 00:00:00 -0400163 acpi_gbl_ps_node_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Len Brown4be44fc2005-08-05 00:44:28 -0400165 (void)acpi_os_delete_cache(acpi_gbl_ps_node_ext_cache);
Robert Moore73459f72005-06-24 00:00:00 -0400166 acpi_gbl_ps_node_ext_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Robert Moore73459f72005-06-24 00:00:00 -0400168 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/*******************************************************************************
172 *
173 * FUNCTION: acpi_ut_validate_buffer
174 *
175 * PARAMETERS: Buffer - Buffer descriptor to be validated
176 *
177 * RETURN: Status
178 *
179 * DESCRIPTION: Perform parameter validation checks on an struct acpi_buffer
180 *
181 ******************************************************************************/
182
Len Brown4be44fc2005-08-05 00:44:28 -0400183acpi_status acpi_ut_validate_buffer(struct acpi_buffer * buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185
186 /* Obviously, the structure pointer must be valid */
187
188 if (!buffer) {
189 return (AE_BAD_PARAMETER);
190 }
191
192 /* Special semantics for the length */
193
Len Brown4be44fc2005-08-05 00:44:28 -0400194 if ((buffer->length == ACPI_NO_BUFFER) ||
195 (buffer->length == ACPI_ALLOCATE_BUFFER) ||
196 (buffer->length == ACPI_ALLOCATE_LOCAL_BUFFER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return (AE_OK);
198 }
199
200 /* Length is valid, the buffer pointer must be also */
201
202 if (!buffer->pointer) {
203 return (AE_BAD_PARAMETER);
204 }
205
206 return (AE_OK);
207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*******************************************************************************
210 *
211 * FUNCTION: acpi_ut_initialize_buffer
212 *
213 * PARAMETERS: Buffer - Buffer to be validated
214 * required_length - Length needed
215 *
216 * RETURN: Status
217 *
218 * DESCRIPTION: Validate that the buffer is of the required length or
219 * allocate a new buffer. Returned buffer is always zeroed.
220 *
221 ******************************************************************************/
222
223acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400224acpi_ut_initialize_buffer(struct acpi_buffer * buffer,
225 acpi_size required_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Len Brown4be44fc2005-08-05 00:44:28 -0400227 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 switch (buffer->length) {
230 case ACPI_NO_BUFFER:
231
232 /* Set the exception and returned the required length */
233
234 status = AE_BUFFER_OVERFLOW;
235 break;
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 case ACPI_ALLOCATE_BUFFER:
238
239 /* Allocate a new buffer */
240
Len Brown4be44fc2005-08-05 00:44:28 -0400241 buffer->pointer = acpi_os_allocate(required_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (!buffer->pointer) {
243 return (AE_NO_MEMORY);
244 }
245
246 /* Clear the buffer */
247
Len Brown4be44fc2005-08-05 00:44:28 -0400248 ACPI_MEMSET(buffer->pointer, 0, required_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 break;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 case ACPI_ALLOCATE_LOCAL_BUFFER:
252
253 /* Allocate a new buffer with local interface to allow tracking */
254
Len Brown4be44fc2005-08-05 00:44:28 -0400255 buffer->pointer = ACPI_MEM_CALLOCATE(required_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (!buffer->pointer) {
257 return (AE_NO_MEMORY);
258 }
259 break;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 default:
262
263 /* Existing buffer: Validate the size of the buffer */
264
265 if (buffer->length < required_length) {
266 status = AE_BUFFER_OVERFLOW;
267 break;
268 }
269
270 /* Clear the buffer */
271
Len Brown4be44fc2005-08-05 00:44:28 -0400272 ACPI_MEMSET(buffer->pointer, 0, required_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 break;
274 }
275
276 buffer->length = required_length;
277 return (status);
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/*******************************************************************************
281 *
282 * FUNCTION: acpi_ut_allocate
283 *
284 * PARAMETERS: Size - Size of the allocation
285 * Component - Component type of caller
286 * Module - Source file name of caller
287 * Line - Line number of caller
288 *
289 * RETURN: Address of the allocated memory on success, NULL on failure.
290 *
291 * DESCRIPTION: The subsystem's equivalent of malloc.
292 *
293 ******************************************************************************/
294
Len Brown4be44fc2005-08-05 00:44:28 -0400295void *acpi_ut_allocate(acpi_size size, u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Len Brown4be44fc2005-08-05 00:44:28 -0400297 void *allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Len Brown4be44fc2005-08-05 00:44:28 -0400299 ACPI_FUNCTION_TRACE_U32("ut_allocate", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* Check for an inadvertent size of zero bytes */
302
303 if (!size) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500304 ACPI_ERROR((module, line,
305 "ut_allocate: Attempt to allocate zero bytes, allocating 1 byte"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 size = 1;
307 }
308
Len Brown4be44fc2005-08-05 00:44:28 -0400309 allocation = acpi_os_allocate(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!allocation) {
311 /* Report allocation error */
312
Bob Mooreb8e4d892006-01-27 16:43:00 -0500313 ACPI_ERROR((module, line,
314 "ut_allocate: Could not allocate size %X",
315 (u32) size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Len Brown4be44fc2005-08-05 00:44:28 -0400317 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
Len Brown4be44fc2005-08-05 00:44:28 -0400320 return_PTR(allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323/*******************************************************************************
324 *
325 * FUNCTION: acpi_ut_callocate
326 *
327 * PARAMETERS: Size - Size of the allocation
328 * Component - Component type of caller
329 * Module - Source file name of caller
330 * Line - Line number of caller
331 *
332 * RETURN: Address of the allocated memory on success, NULL on failure.
333 *
334 * DESCRIPTION: Subsystem equivalent of calloc.
335 *
336 ******************************************************************************/
337
Len Brown4be44fc2005-08-05 00:44:28 -0400338void *acpi_ut_callocate(acpi_size size, u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Len Brown4be44fc2005-08-05 00:44:28 -0400340 void *allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Len Brown4be44fc2005-08-05 00:44:28 -0400342 ACPI_FUNCTION_TRACE_U32("ut_callocate", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* Check for an inadvertent size of zero bytes */
345
346 if (!size) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500347 ACPI_ERROR((module, line,
348 "Attempt to allocate zero bytes, allocating 1 byte"));
Bob Moore50eca3e2005-09-30 19:03:00 -0400349 size = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
Len Brown4be44fc2005-08-05 00:44:28 -0400352 allocation = acpi_os_allocate(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!allocation) {
354 /* Report allocation error */
355
Bob Mooreb8e4d892006-01-27 16:43:00 -0500356 ACPI_ERROR((module, line,
357 "Could not allocate size %X", (u32) size));
Len Brown4be44fc2005-08-05 00:44:28 -0400358 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360
361 /* Clear the memory block */
362
Len Brown4be44fc2005-08-05 00:44:28 -0400363 ACPI_MEMSET(allocation, 0, size);
364 return_PTR(allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#ifdef ACPI_DBG_TRACK_ALLOCATIONS
368/*
369 * These procedures are used for tracking memory leaks in the subsystem, and
370 * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
371 *
372 * Each memory allocation is tracked via a doubly linked list. Each
373 * element contains the caller's component, module name, function name, and
374 * line number. acpi_ut_allocate and acpi_ut_callocate call
375 * acpi_ut_track_allocation to add an element to the list; deletion
376 * occurs in the body of acpi_ut_free.
377 */
378
Robert Moore73459f72005-06-24 00:00:00 -0400379/*******************************************************************************
380 *
381 * FUNCTION: acpi_ut_create_list
382 *
383 * PARAMETERS: cache_name - Ascii name for the cache
384 * object_size - Size of each cached object
385 * return_cache - Where the new cache object is returned
386 *
387 * RETURN: Status
388 *
389 * DESCRIPTION: Create a local memory list for tracking purposed
390 *
391 ******************************************************************************/
392
393static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400394acpi_ut_create_list(char *list_name,
395 u16 object_size, struct acpi_memory_list **return_cache)
Robert Moore73459f72005-06-24 00:00:00 -0400396{
Len Brown4be44fc2005-08-05 00:44:28 -0400397 struct acpi_memory_list *cache;
Robert Moore73459f72005-06-24 00:00:00 -0400398
Len Brown4be44fc2005-08-05 00:44:28 -0400399 cache = acpi_os_allocate(sizeof(struct acpi_memory_list));
Robert Moore73459f72005-06-24 00:00:00 -0400400 if (!cache) {
401 return (AE_NO_MEMORY);
402 }
403
Len Brown4be44fc2005-08-05 00:44:28 -0400404 ACPI_MEMSET(cache, 0, sizeof(struct acpi_memory_list));
Robert Moore73459f72005-06-24 00:00:00 -0400405
Len Brown4be44fc2005-08-05 00:44:28 -0400406 cache->list_name = list_name;
Robert Moore73459f72005-06-24 00:00:00 -0400407 cache->object_size = object_size;
408
409 *return_cache = cache;
410 return (AE_OK);
411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/*******************************************************************************
414 *
415 * FUNCTION: acpi_ut_allocate_and_track
416 *
417 * PARAMETERS: Size - Size of the allocation
418 * Component - Component type of caller
419 * Module - Source file name of caller
420 * Line - Line number of caller
421 *
422 * RETURN: Address of the allocated memory on success, NULL on failure.
423 *
424 * DESCRIPTION: The subsystem's equivalent of malloc.
425 *
426 ******************************************************************************/
427
Len Brown4be44fc2005-08-05 00:44:28 -0400428void *acpi_ut_allocate_and_track(acpi_size size,
429 u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Len Brown4be44fc2005-08-05 00:44:28 -0400431 struct acpi_debug_mem_block *allocation;
432 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Len Brown4be44fc2005-08-05 00:44:28 -0400434 allocation =
435 acpi_ut_allocate(size + sizeof(struct acpi_debug_mem_header),
436 component, module, line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!allocation) {
438 return (NULL);
439 }
440
Len Brown4be44fc2005-08-05 00:44:28 -0400441 status = acpi_ut_track_allocation(allocation, size,
442 ACPI_MEM_MALLOC, component, module,
443 line);
444 if (ACPI_FAILURE(status)) {
445 acpi_os_free(allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return (NULL);
447 }
448
Robert Moore73459f72005-06-24 00:00:00 -0400449 acpi_gbl_global_list->total_allocated++;
450 acpi_gbl_global_list->current_total_size += (u32) size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Len Brown4be44fc2005-08-05 00:44:28 -0400452 return ((void *)&allocation->user_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/*******************************************************************************
456 *
457 * FUNCTION: acpi_ut_callocate_and_track
458 *
459 * PARAMETERS: Size - Size of the allocation
460 * Component - Component type of caller
461 * Module - Source file name of caller
462 * Line - Line number of caller
463 *
464 * RETURN: Address of the allocated memory on success, NULL on failure.
465 *
466 * DESCRIPTION: Subsystem equivalent of calloc.
467 *
468 ******************************************************************************/
469
Len Brown4be44fc2005-08-05 00:44:28 -0400470void *acpi_ut_callocate_and_track(acpi_size size,
471 u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Len Brown4be44fc2005-08-05 00:44:28 -0400473 struct acpi_debug_mem_block *allocation;
474 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Len Brown4be44fc2005-08-05 00:44:28 -0400476 allocation =
477 acpi_ut_callocate(size + sizeof(struct acpi_debug_mem_header),
478 component, module, line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (!allocation) {
480 /* Report allocation error */
481
Bob Mooreb8e4d892006-01-27 16:43:00 -0500482 ACPI_ERROR((module, line,
483 "Could not allocate size %X", (u32) size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return (NULL);
485 }
486
Len Brown4be44fc2005-08-05 00:44:28 -0400487 status = acpi_ut_track_allocation(allocation, size,
488 ACPI_MEM_CALLOC, component, module,
489 line);
490 if (ACPI_FAILURE(status)) {
491 acpi_os_free(allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return (NULL);
493 }
494
Robert Moore73459f72005-06-24 00:00:00 -0400495 acpi_gbl_global_list->total_allocated++;
496 acpi_gbl_global_list->current_total_size += (u32) size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Len Brown4be44fc2005-08-05 00:44:28 -0400498 return ((void *)&allocation->user_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*******************************************************************************
502 *
503 * FUNCTION: acpi_ut_free_and_track
504 *
505 * PARAMETERS: Allocation - Address of the memory to deallocate
506 * Component - Component type of caller
507 * Module - Source file name of caller
508 * Line - Line number of caller
509 *
510 * RETURN: None
511 *
512 * DESCRIPTION: Frees the memory at Allocation
513 *
514 ******************************************************************************/
515
516void
Len Brown4be44fc2005-08-05 00:44:28 -0400517acpi_ut_free_and_track(void *allocation, u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Len Brown4be44fc2005-08-05 00:44:28 -0400519 struct acpi_debug_mem_block *debug_block;
520 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Len Brown4be44fc2005-08-05 00:44:28 -0400522 ACPI_FUNCTION_TRACE_PTR("ut_free", allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (NULL == allocation) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500525 ACPI_ERROR((module, line, "Attempt to delete a NULL address"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 return_VOID;
528 }
529
Len Brown4be44fc2005-08-05 00:44:28 -0400530 debug_block = ACPI_CAST_PTR(struct acpi_debug_mem_block,
531 (((char *)allocation) -
532 sizeof(struct acpi_debug_mem_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Robert Moore73459f72005-06-24 00:00:00 -0400534 acpi_gbl_global_list->total_freed++;
535 acpi_gbl_global_list->current_total_size -= debug_block->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Len Brown4be44fc2005-08-05 00:44:28 -0400537 status = acpi_ut_remove_allocation(debug_block,
538 component, module, line);
539 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500540 ACPI_EXCEPTION((AE_INFO, status, "Could not free memory"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542
Len Brown4be44fc2005-08-05 00:44:28 -0400543 acpi_os_free(debug_block);
Len Brown4be44fc2005-08-05 00:44:28 -0400544 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "%p freed\n", allocation));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return_VOID;
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/*******************************************************************************
549 *
550 * FUNCTION: acpi_ut_find_allocation
551 *
Robert Moore73459f72005-06-24 00:00:00 -0400552 * PARAMETERS: Allocation - Address of allocated memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 *
554 * RETURN: A list element if found; NULL otherwise.
555 *
556 * DESCRIPTION: Searches for an element in the global allocation tracking list.
557 *
558 ******************************************************************************/
559
Len Brown4be44fc2005-08-05 00:44:28 -0400560static struct acpi_debug_mem_block *acpi_ut_find_allocation(void *allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Len Brown4be44fc2005-08-05 00:44:28 -0400562 struct acpi_debug_mem_block *element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Len Brown4be44fc2005-08-05 00:44:28 -0400564 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Robert Moore73459f72005-06-24 00:00:00 -0400566 element = acpi_gbl_global_list->list_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /* Search for the address. */
569
570 while (element) {
571 if (element == allocation) {
572 return (element);
573 }
574
575 element = element->next;
576 }
577
578 return (NULL);
579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/*******************************************************************************
582 *
583 * FUNCTION: acpi_ut_track_allocation
584 *
Robert Moore73459f72005-06-24 00:00:00 -0400585 * PARAMETERS: Allocation - Address of allocated memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 * Size - Size of the allocation
587 * alloc_type - MEM_MALLOC or MEM_CALLOC
588 * Component - Component type of caller
589 * Module - Source file name of caller
590 * Line - Line number of caller
591 *
592 * RETURN: None.
593 *
594 * DESCRIPTION: Inserts an element into the global allocation tracking list.
595 *
596 ******************************************************************************/
597
Robert Moore44f6c012005-04-18 22:49:35 -0400598static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400599acpi_ut_track_allocation(struct acpi_debug_mem_block *allocation,
600 acpi_size size,
601 u8 alloc_type, u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Len Brown4be44fc2005-08-05 00:44:28 -0400603 struct acpi_memory_list *mem_list;
604 struct acpi_debug_mem_block *element;
605 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Len Brown4be44fc2005-08-05 00:44:28 -0400607 ACPI_FUNCTION_TRACE_PTR("ut_track_allocation", allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Robert Moore73459f72005-06-24 00:00:00 -0400609 mem_list = acpi_gbl_global_list;
Len Brown4be44fc2005-08-05 00:44:28 -0400610 status = acpi_ut_acquire_mutex(ACPI_MTX_MEMORY);
611 if (ACPI_FAILURE(status)) {
612 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
615 /*
616 * Search list for this address to make sure it is not already on the list.
617 * This will catch several kinds of problems.
618 */
Len Brown4be44fc2005-08-05 00:44:28 -0400619 element = acpi_ut_find_allocation(allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (element) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500621 ACPI_ERROR((AE_INFO,
622 "ut_track_allocation: Allocation already present in list! (%p)",
623 allocation));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Bob Mooreb8e4d892006-01-27 16:43:00 -0500625 ACPI_ERROR((AE_INFO, "Element %p Address %p",
626 element, allocation));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 goto unlock_and_exit;
629 }
630
631 /* Fill in the instance data. */
632
Len Brown4be44fc2005-08-05 00:44:28 -0400633 allocation->size = (u32) size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 allocation->alloc_type = alloc_type;
635 allocation->component = component;
Len Brown4be44fc2005-08-05 00:44:28 -0400636 allocation->line = line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Len Brown4be44fc2005-08-05 00:44:28 -0400638 ACPI_STRNCPY(allocation->module, module, ACPI_MAX_MODULE_NAME);
639 allocation->module[ACPI_MAX_MODULE_NAME - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 /* Insert at list head */
642
643 if (mem_list->list_head) {
Len Brown4be44fc2005-08-05 00:44:28 -0400644 ((struct acpi_debug_mem_block *)(mem_list->list_head))->
645 previous = allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
648 allocation->next = mem_list->list_head;
649 allocation->previous = NULL;
650
651 mem_list->list_head = allocation;
652
Len Brown4be44fc2005-08-05 00:44:28 -0400653 unlock_and_exit:
654 status = acpi_ut_release_mutex(ACPI_MTX_MEMORY);
655 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/*******************************************************************************
659 *
660 * FUNCTION: acpi_ut_remove_allocation
661 *
Robert Moore73459f72005-06-24 00:00:00 -0400662 * PARAMETERS: Allocation - Address of allocated memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * Component - Component type of caller
664 * Module - Source file name of caller
665 * Line - Line number of caller
666 *
667 * RETURN:
668 *
669 * DESCRIPTION: Deletes an element from the global allocation tracking list.
670 *
671 ******************************************************************************/
672
Robert Moore44f6c012005-04-18 22:49:35 -0400673static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400674acpi_ut_remove_allocation(struct acpi_debug_mem_block *allocation,
675 u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Len Brown4be44fc2005-08-05 00:44:28 -0400677 struct acpi_memory_list *mem_list;
678 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Len Brown4be44fc2005-08-05 00:44:28 -0400680 ACPI_FUNCTION_TRACE("ut_remove_allocation");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Robert Moore73459f72005-06-24 00:00:00 -0400682 mem_list = acpi_gbl_global_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (NULL == mem_list->list_head) {
684 /* No allocations! */
685
Bob Mooreb8e4d892006-01-27 16:43:00 -0500686 ACPI_ERROR((module, line,
687 "Empty allocation list, nothing to free!"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Len Brown4be44fc2005-08-05 00:44:28 -0400689 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
Len Brown4be44fc2005-08-05 00:44:28 -0400692 status = acpi_ut_acquire_mutex(ACPI_MTX_MEMORY);
693 if (ACPI_FAILURE(status)) {
694 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 /* Unlink */
698
699 if (allocation->previous) {
700 (allocation->previous)->next = allocation->next;
Len Brown4be44fc2005-08-05 00:44:28 -0400701 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 mem_list->list_head = allocation->next;
703 }
704
705 if (allocation->next) {
706 (allocation->next)->previous = allocation->previous;
707 }
708
709 /* Mark the segment as deleted */
710
Len Brown4be44fc2005-08-05 00:44:28 -0400711 ACPI_MEMSET(&allocation->user_space, 0xEA, allocation->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Len Brown4be44fc2005-08-05 00:44:28 -0400713 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "Freeing size 0%X\n",
714 allocation->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Len Brown4be44fc2005-08-05 00:44:28 -0400716 status = acpi_ut_release_mutex(ACPI_MTX_MEMORY);
717 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/*******************************************************************************
721 *
722 * FUNCTION: acpi_ut_dump_allocation_info
723 *
724 * PARAMETERS:
725 *
726 * RETURN: None
727 *
728 * DESCRIPTION: Print some info about the outstanding allocations.
729 *
730 ******************************************************************************/
Robert Moore44f6c012005-04-18 22:49:35 -0400731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -0400733void acpi_ut_dump_allocation_info(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735/*
736 struct acpi_memory_list *mem_list;
737*/
738
Len Brown4be44fc2005-08-05 00:44:28 -0400739 ACPI_FUNCTION_TRACE("ut_dump_allocation_info");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741/*
742 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
743 ("%30s: %4d (%3d Kb)\n", "Current allocations",
744 mem_list->current_count,
745 ROUND_UP_TO_1K (mem_list->current_size)));
746
747 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
748 ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
749 mem_list->max_concurrent_count,
750 ROUND_UP_TO_1K (mem_list->max_concurrent_size)));
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
753 ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
754 running_object_count,
755 ROUND_UP_TO_1K (running_object_size)));
756
757 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
758 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
759 running_alloc_count,
760 ROUND_UP_TO_1K (running_alloc_size)));
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
763 ("%30s: %4d (%3d Kb)\n", "Current Nodes",
764 acpi_gbl_current_node_count,
765 ROUND_UP_TO_1K (acpi_gbl_current_node_size)));
766
767 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
768 ("%30s: %4d (%3d Kb)\n", "Max Nodes",
769 acpi_gbl_max_concurrent_node_count,
Robert Moore44f6c012005-04-18 22:49:35 -0400770 ROUND_UP_TO_1K ((acpi_gbl_max_concurrent_node_count *
771 sizeof (struct acpi_namespace_node)))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772*/
773 return_VOID;
774}
Len Brown4be44fc2005-08-05 00:44:28 -0400775#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777/*******************************************************************************
778 *
779 * FUNCTION: acpi_ut_dump_allocations
780 *
781 * PARAMETERS: Component - Component(s) to dump info for.
782 * Module - Module to dump info for. NULL means all.
783 *
784 * RETURN: None
785 *
786 * DESCRIPTION: Print a list of all outstanding allocations.
787 *
788 ******************************************************************************/
789
Len Brown4be44fc2005-08-05 00:44:28 -0400790void acpi_ut_dump_allocations(u32 component, char *module)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Len Brown4be44fc2005-08-05 00:44:28 -0400792 struct acpi_debug_mem_block *element;
793 union acpi_descriptor *descriptor;
794 u32 num_outstanding = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Len Brown4be44fc2005-08-05 00:44:28 -0400796 ACPI_FUNCTION_TRACE("ut_dump_allocations");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 /*
799 * Walk the allocation list.
800 */
Len Brown4be44fc2005-08-05 00:44:28 -0400801 if (ACPI_FAILURE(acpi_ut_acquire_mutex(ACPI_MTX_MEMORY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return;
803 }
804
Robert Moore73459f72005-06-24 00:00:00 -0400805 element = acpi_gbl_global_list->list_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 while (element) {
807 if ((element->component & component) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400808 ((module == NULL)
809 || (0 == ACPI_STRCMP(module, element->module)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 /* Ignore allocated objects that are in a cache */
811
Len Brown4be44fc2005-08-05 00:44:28 -0400812 descriptor =
813 ACPI_CAST_PTR(union acpi_descriptor,
814 &element->user_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (descriptor->descriptor_id != ACPI_DESC_TYPE_CACHED) {
Len Brown4be44fc2005-08-05 00:44:28 -0400816 acpi_os_printf("%p Len %04X %9.9s-%d [%s] ",
817 descriptor, element->size,
818 element->module, element->line,
819 acpi_ut_get_descriptor_name
820 (descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /* Most of the elements will be Operand objects. */
823
Len Brown4be44fc2005-08-05 00:44:28 -0400824 switch (ACPI_GET_DESCRIPTOR_TYPE(descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 case ACPI_DESC_TYPE_OPERAND:
Len Brown4be44fc2005-08-05 00:44:28 -0400826 acpi_os_printf("%12.12s R%hd",
827 acpi_ut_get_type_name
828 (descriptor->object.
829 common.type),
830 descriptor->object.
831 common.reference_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 break;
833
834 case ACPI_DESC_TYPE_PARSER:
Len Brown4be44fc2005-08-05 00:44:28 -0400835 acpi_os_printf("aml_opcode %04hX",
836 descriptor->op.asl.
837 aml_opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 break;
839
840 case ACPI_DESC_TYPE_NAMED:
Len Brown4be44fc2005-08-05 00:44:28 -0400841 acpi_os_printf("%4.4s",
842 acpi_ut_get_node_name
843 (&descriptor->node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 break;
845
846 default:
847 break;
848 }
849
Len Brown4be44fc2005-08-05 00:44:28 -0400850 acpi_os_printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 num_outstanding++;
852 }
853 }
854 element = element->next;
855 }
856
Len Brown4be44fc2005-08-05 00:44:28 -0400857 (void)acpi_ut_release_mutex(ACPI_MTX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 /* Print summary */
860
861 if (!num_outstanding) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500862 ACPI_INFO((AE_INFO, "No outstanding allocations"));
Len Brown4be44fc2005-08-05 00:44:28 -0400863 } else {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500864 ACPI_ERROR((AE_INFO,
865 "%d(%X) Outstanding allocations",
866 num_outstanding, num_outstanding));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
869 return_VOID;
870}
871
Len Brown4be44fc2005-08-05 00:44:28 -0400872#endif /* #ifdef ACPI_DBG_TRACK_ALLOCATIONS */