[ACPI] ACPICA 20050930
[deliverable/linux.git] / drivers / acpi / utilities / utalloc.c
CommitLineData
1da177e4
LT
1/******************************************************************************
2 *
73459f73 3 * Module Name: utalloc - local memory allocation routines
1da177e4
LT
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2005, R. Byron Moore
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
1da177e4
LT
44#include <acpi/acpi.h>
45
46#define _COMPONENT ACPI_UTILITIES
4be44fcd 47ACPI_MODULE_NAME("utalloc")
1da177e4 48
44f6c012 49/* Local prototypes */
44f6c012 50#ifdef ACPI_DBG_TRACK_ALLOCATIONS
4be44fcd 51static struct acpi_debug_mem_block *acpi_ut_find_allocation(void *allocation);
44f6c012
RM
52
53static acpi_status
4be44fcd
LB
54acpi_ut_track_allocation(struct acpi_debug_mem_block *address,
55 acpi_size size,
56 u8 alloc_type, u32 component, char *module, u32 line);
44f6c012
RM
57
58static acpi_status
4be44fcd
LB
59acpi_ut_remove_allocation(struct acpi_debug_mem_block *address,
60 u32 component, char *module, u32 line);
61#endif /* ACPI_DBG_TRACK_ALLOCATIONS */
44f6c012 62
73459f73
RM
63#ifdef ACPI_DBG_TRACK_ALLOCATIONS
64static acpi_status
4be44fcd
LB
65acpi_ut_create_list(char *list_name,
66 u16 object_size, struct acpi_memory_list **return_cache);
1da177e4 67#endif
1da177e4 68
44f6c012 69/*******************************************************************************
1da177e4 70 *
73459f73 71 * FUNCTION: acpi_ut_create_caches
1da177e4 72 *
73459f73 73 * PARAMETERS: None
1da177e4 74 *
73459f73 75 * RETURN: Status
1da177e4 76 *
73459f73 77 * DESCRIPTION: Create all local caches
1da177e4
LT
78 *
79 ******************************************************************************/
80
4be44fcd 81acpi_status acpi_ut_create_caches(void)
1da177e4 82{
4be44fcd 83 acpi_status status;
1da177e4 84
73459f73 85#ifdef ACPI_DBG_TRACK_ALLOCATIONS
1da177e4 86
73459f73 87 /* Memory allocation lists */
1da177e4 88
4be44fcd
LB
89 status = acpi_ut_create_list("Acpi-Global", 0, &acpi_gbl_global_list);
90 if (ACPI_FAILURE(status)) {
73459f73 91 return (status);
1da177e4
LT
92 }
93
4be44fcd
LB
94 status =
95 acpi_ut_create_list("Acpi-Namespace",
96 sizeof(struct acpi_namespace_node),
97 &acpi_gbl_ns_node_list);
98 if (ACPI_FAILURE(status)) {
73459f73
RM
99 return (status);
100 }
1da177e4
LT
101#endif
102
73459f73 103 /* Object Caches, for frequently used objects */
1da177e4 104
4be44fcd
LB
105 status =
106 acpi_os_create_cache("acpi_state", sizeof(union acpi_generic_state),
107 ACPI_MAX_STATE_CACHE_DEPTH,
108 &acpi_gbl_state_cache);
109 if (ACPI_FAILURE(status)) {
73459f73 110 return (status);
1da177e4
LT
111 }
112
4be44fcd
LB
113 status =
114 acpi_os_create_cache("acpi_parse",
115 sizeof(struct acpi_parse_obj_common),
116 ACPI_MAX_PARSE_CACHE_DEPTH,
117 &acpi_gbl_ps_node_cache);
118 if (ACPI_FAILURE(status)) {
73459f73 119 return (status);
1da177e4
LT
120 }
121
4be44fcd
LB
122 status =
123 acpi_os_create_cache("acpi_parse_ext",
124 sizeof(struct acpi_parse_obj_named),
125 ACPI_MAX_EXTPARSE_CACHE_DEPTH,
126 &acpi_gbl_ps_node_ext_cache);
127 if (ACPI_FAILURE(status)) {
73459f73
RM
128 return (status);
129 }
1da177e4 130
4be44fcd
LB
131 status =
132 acpi_os_create_cache("acpi_operand",
133 sizeof(union acpi_operand_object),
134 ACPI_MAX_OBJECT_CACHE_DEPTH,
135 &acpi_gbl_operand_cache);
136 if (ACPI_FAILURE(status)) {
73459f73
RM
137 return (status);
138 }
1da177e4 139
73459f73 140 return (AE_OK);
1da177e4
LT
141}
142
44f6c012 143/*******************************************************************************
1da177e4 144 *
73459f73 145 * FUNCTION: acpi_ut_delete_caches
1da177e4 146 *
73459f73 147 * PARAMETERS: None
1da177e4 148 *
73459f73 149 * RETURN: Status
1da177e4 150 *
73459f73 151 * DESCRIPTION: Purge and delete all local caches
1da177e4
LT
152 *
153 ******************************************************************************/
154
4be44fcd 155acpi_status acpi_ut_delete_caches(void)
1da177e4 156{
1da177e4 157
4be44fcd 158 (void)acpi_os_delete_cache(acpi_gbl_state_cache);
73459f73 159 acpi_gbl_state_cache = NULL;
1da177e4 160
4be44fcd 161 (void)acpi_os_delete_cache(acpi_gbl_operand_cache);
73459f73 162 acpi_gbl_operand_cache = NULL;
1da177e4 163
4be44fcd 164 (void)acpi_os_delete_cache(acpi_gbl_ps_node_cache);
73459f73 165 acpi_gbl_ps_node_cache = NULL;
1da177e4 166
4be44fcd 167 (void)acpi_os_delete_cache(acpi_gbl_ps_node_ext_cache);
73459f73 168 acpi_gbl_ps_node_ext_cache = NULL;
1da177e4 169
73459f73 170 return (AE_OK);
1da177e4 171}
1da177e4
LT
172
173/*******************************************************************************
174 *
175 * FUNCTION: acpi_ut_validate_buffer
176 *
177 * PARAMETERS: Buffer - Buffer descriptor to be validated
178 *
179 * RETURN: Status
180 *
181 * DESCRIPTION: Perform parameter validation checks on an struct acpi_buffer
182 *
183 ******************************************************************************/
184
4be44fcd 185acpi_status acpi_ut_validate_buffer(struct acpi_buffer * buffer)
1da177e4
LT
186{
187
188 /* Obviously, the structure pointer must be valid */
189
190 if (!buffer) {
191 return (AE_BAD_PARAMETER);
192 }
193
194 /* Special semantics for the length */
195
4be44fcd
LB
196 if ((buffer->length == ACPI_NO_BUFFER) ||
197 (buffer->length == ACPI_ALLOCATE_BUFFER) ||
198 (buffer->length == ACPI_ALLOCATE_LOCAL_BUFFER)) {
1da177e4
LT
199 return (AE_OK);
200 }
201
202 /* Length is valid, the buffer pointer must be also */
203
204 if (!buffer->pointer) {
205 return (AE_BAD_PARAMETER);
206 }
207
208 return (AE_OK);
209}
210
1da177e4
LT
211/*******************************************************************************
212 *
213 * FUNCTION: acpi_ut_initialize_buffer
214 *
215 * PARAMETERS: Buffer - Buffer to be validated
216 * required_length - Length needed
217 *
218 * RETURN: Status
219 *
220 * DESCRIPTION: Validate that the buffer is of the required length or
221 * allocate a new buffer. Returned buffer is always zeroed.
222 *
223 ******************************************************************************/
224
225acpi_status
4be44fcd
LB
226acpi_ut_initialize_buffer(struct acpi_buffer * buffer,
227 acpi_size required_length)
1da177e4 228{
4be44fcd 229 acpi_status status = AE_OK;
1da177e4
LT
230
231 switch (buffer->length) {
232 case ACPI_NO_BUFFER:
233
234 /* Set the exception and returned the required length */
235
236 status = AE_BUFFER_OVERFLOW;
237 break;
238
1da177e4
LT
239 case ACPI_ALLOCATE_BUFFER:
240
241 /* Allocate a new buffer */
242
4be44fcd 243 buffer->pointer = acpi_os_allocate(required_length);
1da177e4
LT
244 if (!buffer->pointer) {
245 return (AE_NO_MEMORY);
246 }
247
248 /* Clear the buffer */
249
4be44fcd 250 ACPI_MEMSET(buffer->pointer, 0, required_length);
1da177e4
LT
251 break;
252
1da177e4
LT
253 case ACPI_ALLOCATE_LOCAL_BUFFER:
254
255 /* Allocate a new buffer with local interface to allow tracking */
256
4be44fcd 257 buffer->pointer = ACPI_MEM_CALLOCATE(required_length);
1da177e4
LT
258 if (!buffer->pointer) {
259 return (AE_NO_MEMORY);
260 }
261 break;
262
1da177e4
LT
263 default:
264
265 /* Existing buffer: Validate the size of the buffer */
266
267 if (buffer->length < required_length) {
268 status = AE_BUFFER_OVERFLOW;
269 break;
270 }
271
272 /* Clear the buffer */
273
4be44fcd 274 ACPI_MEMSET(buffer->pointer, 0, required_length);
1da177e4
LT
275 break;
276 }
277
278 buffer->length = required_length;
279 return (status);
280}
281
1da177e4
LT
282/*******************************************************************************
283 *
284 * FUNCTION: acpi_ut_allocate
285 *
286 * PARAMETERS: Size - Size of the allocation
287 * Component - Component type of caller
288 * Module - Source file name of caller
289 * Line - Line number of caller
290 *
291 * RETURN: Address of the allocated memory on success, NULL on failure.
292 *
293 * DESCRIPTION: The subsystem's equivalent of malloc.
294 *
295 ******************************************************************************/
296
4be44fcd 297void *acpi_ut_allocate(acpi_size size, u32 component, char *module, u32 line)
1da177e4 298{
4be44fcd 299 void *allocation;
1da177e4 300
4be44fcd 301 ACPI_FUNCTION_TRACE_U32("ut_allocate", size);
1da177e4
LT
302
303 /* Check for an inadvertent size of zero bytes */
304
305 if (!size) {
4be44fcd 306 _ACPI_REPORT_ERROR(module, line, component,
50eca3eb 307 ("ut_allocate: Attempt to allocate zero bytes, allocating 1 byte\n"));
1da177e4
LT
308 size = 1;
309 }
310
4be44fcd 311 allocation = acpi_os_allocate(size);
1da177e4
LT
312 if (!allocation) {
313 /* Report allocation error */
314
4be44fcd
LB
315 _ACPI_REPORT_ERROR(module, line, component,
316 ("ut_allocate: Could not allocate size %X\n",
317 (u32) size));
1da177e4 318
4be44fcd 319 return_PTR(NULL);
1da177e4
LT
320 }
321
4be44fcd 322 return_PTR(allocation);
1da177e4
LT
323}
324
1da177e4
LT
325/*******************************************************************************
326 *
327 * FUNCTION: acpi_ut_callocate
328 *
329 * PARAMETERS: Size - Size of the allocation
330 * Component - Component type of caller
331 * Module - Source file name of caller
332 * Line - Line number of caller
333 *
334 * RETURN: Address of the allocated memory on success, NULL on failure.
335 *
336 * DESCRIPTION: Subsystem equivalent of calloc.
337 *
338 ******************************************************************************/
339
4be44fcd 340void *acpi_ut_callocate(acpi_size size, u32 component, char *module, u32 line)
1da177e4 341{
4be44fcd 342 void *allocation;
1da177e4 343
4be44fcd 344 ACPI_FUNCTION_TRACE_U32("ut_callocate", size);
1da177e4
LT
345
346 /* Check for an inadvertent size of zero bytes */
347
348 if (!size) {
4be44fcd 349 _ACPI_REPORT_ERROR(module, line, component,
50eca3eb
BM
350 ("ut_callocate: Attempt to allocate zero bytes, allocating 1 byte\n"));
351 size = 1;
1da177e4
LT
352 }
353
4be44fcd 354 allocation = acpi_os_allocate(size);
1da177e4
LT
355 if (!allocation) {
356 /* Report allocation error */
357
4be44fcd
LB
358 _ACPI_REPORT_ERROR(module, line, component,
359 ("ut_callocate: Could not allocate size %X\n",
360 (u32) size));
361 return_PTR(NULL);
1da177e4
LT
362 }
363
364 /* Clear the memory block */
365
4be44fcd
LB
366 ACPI_MEMSET(allocation, 0, size);
367 return_PTR(allocation);
1da177e4
LT
368}
369
1da177e4
LT
370#ifdef ACPI_DBG_TRACK_ALLOCATIONS
371/*
372 * These procedures are used for tracking memory leaks in the subsystem, and
373 * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
374 *
375 * Each memory allocation is tracked via a doubly linked list. Each
376 * element contains the caller's component, module name, function name, and
377 * line number. acpi_ut_allocate and acpi_ut_callocate call
378 * acpi_ut_track_allocation to add an element to the list; deletion
379 * occurs in the body of acpi_ut_free.
380 */
381
73459f73
RM
382/*******************************************************************************
383 *
384 * FUNCTION: acpi_ut_create_list
385 *
386 * PARAMETERS: cache_name - Ascii name for the cache
387 * object_size - Size of each cached object
388 * return_cache - Where the new cache object is returned
389 *
390 * RETURN: Status
391 *
392 * DESCRIPTION: Create a local memory list for tracking purposed
393 *
394 ******************************************************************************/
395
396static acpi_status
4be44fcd
LB
397acpi_ut_create_list(char *list_name,
398 u16 object_size, struct acpi_memory_list **return_cache)
73459f73 399{
4be44fcd 400 struct acpi_memory_list *cache;
73459f73 401
4be44fcd 402 cache = acpi_os_allocate(sizeof(struct acpi_memory_list));
73459f73
RM
403 if (!cache) {
404 return (AE_NO_MEMORY);
405 }
406
4be44fcd 407 ACPI_MEMSET(cache, 0, sizeof(struct acpi_memory_list));
73459f73 408
4be44fcd 409 cache->list_name = list_name;
73459f73
RM
410 cache->object_size = object_size;
411
412 *return_cache = cache;
413 return (AE_OK);
414}
415
1da177e4
LT
416/*******************************************************************************
417 *
418 * FUNCTION: acpi_ut_allocate_and_track
419 *
420 * PARAMETERS: Size - Size of the allocation
421 * Component - Component type of caller
422 * Module - Source file name of caller
423 * Line - Line number of caller
424 *
425 * RETURN: Address of the allocated memory on success, NULL on failure.
426 *
427 * DESCRIPTION: The subsystem's equivalent of malloc.
428 *
429 ******************************************************************************/
430
4be44fcd
LB
431void *acpi_ut_allocate_and_track(acpi_size size,
432 u32 component, char *module, u32 line)
1da177e4 433{
4be44fcd
LB
434 struct acpi_debug_mem_block *allocation;
435 acpi_status status;
1da177e4 436
4be44fcd
LB
437 allocation =
438 acpi_ut_allocate(size + sizeof(struct acpi_debug_mem_header),
439 component, module, line);
1da177e4
LT
440 if (!allocation) {
441 return (NULL);
442 }
443
4be44fcd
LB
444 status = acpi_ut_track_allocation(allocation, size,
445 ACPI_MEM_MALLOC, component, module,
446 line);
447 if (ACPI_FAILURE(status)) {
448 acpi_os_free(allocation);
1da177e4
LT
449 return (NULL);
450 }
451
73459f73
RM
452 acpi_gbl_global_list->total_allocated++;
453 acpi_gbl_global_list->current_total_size += (u32) size;
1da177e4 454
4be44fcd 455 return ((void *)&allocation->user_space);
1da177e4
LT
456}
457
1da177e4
LT
458/*******************************************************************************
459 *
460 * FUNCTION: acpi_ut_callocate_and_track
461 *
462 * PARAMETERS: Size - Size of the allocation
463 * Component - Component type of caller
464 * Module - Source file name of caller
465 * Line - Line number of caller
466 *
467 * RETURN: Address of the allocated memory on success, NULL on failure.
468 *
469 * DESCRIPTION: Subsystem equivalent of calloc.
470 *
471 ******************************************************************************/
472
4be44fcd
LB
473void *acpi_ut_callocate_and_track(acpi_size size,
474 u32 component, char *module, u32 line)
1da177e4 475{
4be44fcd
LB
476 struct acpi_debug_mem_block *allocation;
477 acpi_status status;
1da177e4 478
4be44fcd
LB
479 allocation =
480 acpi_ut_callocate(size + sizeof(struct acpi_debug_mem_header),
481 component, module, line);
1da177e4
LT
482 if (!allocation) {
483 /* Report allocation error */
484
4be44fcd
LB
485 _ACPI_REPORT_ERROR(module, line, component,
486 ("ut_callocate: Could not allocate size %X\n",
487 (u32) size));
1da177e4
LT
488 return (NULL);
489 }
490
4be44fcd
LB
491 status = acpi_ut_track_allocation(allocation, size,
492 ACPI_MEM_CALLOC, component, module,
493 line);
494 if (ACPI_FAILURE(status)) {
495 acpi_os_free(allocation);
1da177e4
LT
496 return (NULL);
497 }
498
73459f73
RM
499 acpi_gbl_global_list->total_allocated++;
500 acpi_gbl_global_list->current_total_size += (u32) size;
1da177e4 501
4be44fcd 502 return ((void *)&allocation->user_space);
1da177e4
LT
503}
504
1da177e4
LT
505/*******************************************************************************
506 *
507 * FUNCTION: acpi_ut_free_and_track
508 *
509 * PARAMETERS: Allocation - Address of the memory to deallocate
510 * Component - Component type of caller
511 * Module - Source file name of caller
512 * Line - Line number of caller
513 *
514 * RETURN: None
515 *
516 * DESCRIPTION: Frees the memory at Allocation
517 *
518 ******************************************************************************/
519
520void
4be44fcd 521acpi_ut_free_and_track(void *allocation, u32 component, char *module, u32 line)
1da177e4 522{
4be44fcd
LB
523 struct acpi_debug_mem_block *debug_block;
524 acpi_status status;
1da177e4 525
4be44fcd 526 ACPI_FUNCTION_TRACE_PTR("ut_free", allocation);
1da177e4
LT
527
528 if (NULL == allocation) {
4be44fcd
LB
529 _ACPI_REPORT_ERROR(module, line, component,
530 ("acpi_ut_free: Attempt to delete a NULL address\n"));
1da177e4
LT
531
532 return_VOID;
533 }
534
4be44fcd
LB
535 debug_block = ACPI_CAST_PTR(struct acpi_debug_mem_block,
536 (((char *)allocation) -
537 sizeof(struct acpi_debug_mem_header)));
1da177e4 538
73459f73
RM
539 acpi_gbl_global_list->total_freed++;
540 acpi_gbl_global_list->current_total_size -= debug_block->size;
1da177e4 541
4be44fcd
LB
542 status = acpi_ut_remove_allocation(debug_block,
543 component, module, line);
544 if (ACPI_FAILURE(status)) {
545 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Could not free memory, %s\n",
546 acpi_format_exception(status)));
1da177e4
LT
547 }
548
4be44fcd 549 acpi_os_free(debug_block);
1da177e4 550
4be44fcd 551 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "%p freed\n", allocation));
1da177e4
LT
552
553 return_VOID;
554}
555
1da177e4
LT
556/*******************************************************************************
557 *
558 * FUNCTION: acpi_ut_find_allocation
559 *
73459f73 560 * PARAMETERS: Allocation - Address of allocated memory
1da177e4
LT
561 *
562 * RETURN: A list element if found; NULL otherwise.
563 *
564 * DESCRIPTION: Searches for an element in the global allocation tracking list.
565 *
566 ******************************************************************************/
567
4be44fcd 568static struct acpi_debug_mem_block *acpi_ut_find_allocation(void *allocation)
1da177e4 569{
4be44fcd 570 struct acpi_debug_mem_block *element;
1da177e4 571
4be44fcd 572 ACPI_FUNCTION_ENTRY();
1da177e4 573
73459f73 574 element = acpi_gbl_global_list->list_head;
1da177e4
LT
575
576 /* Search for the address. */
577
578 while (element) {
579 if (element == allocation) {
580 return (element);
581 }
582
583 element = element->next;
584 }
585
586 return (NULL);
587}
588
1da177e4
LT
589/*******************************************************************************
590 *
591 * FUNCTION: acpi_ut_track_allocation
592 *
73459f73 593 * PARAMETERS: Allocation - Address of allocated memory
1da177e4
LT
594 * Size - Size of the allocation
595 * alloc_type - MEM_MALLOC or MEM_CALLOC
596 * Component - Component type of caller
597 * Module - Source file name of caller
598 * Line - Line number of caller
599 *
600 * RETURN: None.
601 *
602 * DESCRIPTION: Inserts an element into the global allocation tracking list.
603 *
604 ******************************************************************************/
605
44f6c012 606static acpi_status
4be44fcd
LB
607acpi_ut_track_allocation(struct acpi_debug_mem_block *allocation,
608 acpi_size size,
609 u8 alloc_type, u32 component, char *module, u32 line)
1da177e4 610{
4be44fcd
LB
611 struct acpi_memory_list *mem_list;
612 struct acpi_debug_mem_block *element;
613 acpi_status status = AE_OK;
1da177e4 614
4be44fcd 615 ACPI_FUNCTION_TRACE_PTR("ut_track_allocation", allocation);
1da177e4 616
73459f73 617 mem_list = acpi_gbl_global_list;
4be44fcd
LB
618 status = acpi_ut_acquire_mutex(ACPI_MTX_MEMORY);
619 if (ACPI_FAILURE(status)) {
620 return_ACPI_STATUS(status);
1da177e4
LT
621 }
622
623 /*
624 * Search list for this address to make sure it is not already on the list.
625 * This will catch several kinds of problems.
626 */
4be44fcd 627 element = acpi_ut_find_allocation(allocation);
1da177e4 628 if (element) {
4be44fcd 629 ACPI_REPORT_ERROR(("ut_track_allocation: Allocation already present in list! (%p)\n", allocation));
1da177e4 630
4be44fcd
LB
631 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Element %p Address %p\n",
632 element, allocation));
1da177e4
LT
633
634 goto unlock_and_exit;
635 }
636
637 /* Fill in the instance data. */
638
4be44fcd 639 allocation->size = (u32) size;
1da177e4
LT
640 allocation->alloc_type = alloc_type;
641 allocation->component = component;
4be44fcd 642 allocation->line = line;
1da177e4 643
4be44fcd
LB
644 ACPI_STRNCPY(allocation->module, module, ACPI_MAX_MODULE_NAME);
645 allocation->module[ACPI_MAX_MODULE_NAME - 1] = 0;
1da177e4
LT
646
647 /* Insert at list head */
648
649 if (mem_list->list_head) {
4be44fcd
LB
650 ((struct acpi_debug_mem_block *)(mem_list->list_head))->
651 previous = allocation;
1da177e4
LT
652 }
653
654 allocation->next = mem_list->list_head;
655 allocation->previous = NULL;
656
657 mem_list->list_head = allocation;
658
4be44fcd
LB
659 unlock_and_exit:
660 status = acpi_ut_release_mutex(ACPI_MTX_MEMORY);
661 return_ACPI_STATUS(status);
1da177e4
LT
662}
663
1da177e4
LT
664/*******************************************************************************
665 *
666 * FUNCTION: acpi_ut_remove_allocation
667 *
73459f73 668 * PARAMETERS: Allocation - Address of allocated memory
1da177e4
LT
669 * Component - Component type of caller
670 * Module - Source file name of caller
671 * Line - Line number of caller
672 *
673 * RETURN:
674 *
675 * DESCRIPTION: Deletes an element from the global allocation tracking list.
676 *
677 ******************************************************************************/
678
44f6c012 679static acpi_status
4be44fcd
LB
680acpi_ut_remove_allocation(struct acpi_debug_mem_block *allocation,
681 u32 component, char *module, u32 line)
1da177e4 682{
4be44fcd
LB
683 struct acpi_memory_list *mem_list;
684 acpi_status status;
1da177e4 685
4be44fcd 686 ACPI_FUNCTION_TRACE("ut_remove_allocation");
1da177e4 687
73459f73 688 mem_list = acpi_gbl_global_list;
1da177e4
LT
689 if (NULL == mem_list->list_head) {
690 /* No allocations! */
691
4be44fcd
LB
692 _ACPI_REPORT_ERROR(module, line, component,
693 ("ut_remove_allocation: Empty allocation list, nothing to free!\n"));
1da177e4 694
4be44fcd 695 return_ACPI_STATUS(AE_OK);
1da177e4
LT
696 }
697
4be44fcd
LB
698 status = acpi_ut_acquire_mutex(ACPI_MTX_MEMORY);
699 if (ACPI_FAILURE(status)) {
700 return_ACPI_STATUS(status);
1da177e4
LT
701 }
702
703 /* Unlink */
704
705 if (allocation->previous) {
706 (allocation->previous)->next = allocation->next;
4be44fcd 707 } else {
1da177e4
LT
708 mem_list->list_head = allocation->next;
709 }
710
711 if (allocation->next) {
712 (allocation->next)->previous = allocation->previous;
713 }
714
715 /* Mark the segment as deleted */
716
4be44fcd 717 ACPI_MEMSET(&allocation->user_space, 0xEA, allocation->size);
1da177e4 718
4be44fcd
LB
719 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "Freeing size 0%X\n",
720 allocation->size));
1da177e4 721
4be44fcd
LB
722 status = acpi_ut_release_mutex(ACPI_MTX_MEMORY);
723 return_ACPI_STATUS(status);
1da177e4
LT
724}
725
1da177e4
LT
726/*******************************************************************************
727 *
728 * FUNCTION: acpi_ut_dump_allocation_info
729 *
730 * PARAMETERS:
731 *
732 * RETURN: None
733 *
734 * DESCRIPTION: Print some info about the outstanding allocations.
735 *
736 ******************************************************************************/
44f6c012 737
1da177e4 738#ifdef ACPI_FUTURE_USAGE
4be44fcd 739void acpi_ut_dump_allocation_info(void)
1da177e4
LT
740{
741/*
742 struct acpi_memory_list *mem_list;
743*/
744
4be44fcd 745 ACPI_FUNCTION_TRACE("ut_dump_allocation_info");
1da177e4
LT
746
747/*
748 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
749 ("%30s: %4d (%3d Kb)\n", "Current allocations",
750 mem_list->current_count,
751 ROUND_UP_TO_1K (mem_list->current_size)));
752
753 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
754 ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
755 mem_list->max_concurrent_count,
756 ROUND_UP_TO_1K (mem_list->max_concurrent_size)));
757
1da177e4
LT
758 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
759 ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
760 running_object_count,
761 ROUND_UP_TO_1K (running_object_size)));
762
763 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
764 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
765 running_alloc_count,
766 ROUND_UP_TO_1K (running_alloc_size)));
767
1da177e4
LT
768 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
769 ("%30s: %4d (%3d Kb)\n", "Current Nodes",
770 acpi_gbl_current_node_count,
771 ROUND_UP_TO_1K (acpi_gbl_current_node_size)));
772
773 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
774 ("%30s: %4d (%3d Kb)\n", "Max Nodes",
775 acpi_gbl_max_concurrent_node_count,
44f6c012
RM
776 ROUND_UP_TO_1K ((acpi_gbl_max_concurrent_node_count *
777 sizeof (struct acpi_namespace_node)))));
1da177e4
LT
778*/
779 return_VOID;
780}
4be44fcd 781#endif /* ACPI_FUTURE_USAGE */
1da177e4
LT
782
783/*******************************************************************************
784 *
785 * FUNCTION: acpi_ut_dump_allocations
786 *
787 * PARAMETERS: Component - Component(s) to dump info for.
788 * Module - Module to dump info for. NULL means all.
789 *
790 * RETURN: None
791 *
792 * DESCRIPTION: Print a list of all outstanding allocations.
793 *
794 ******************************************************************************/
795
4be44fcd 796void acpi_ut_dump_allocations(u32 component, char *module)
1da177e4 797{
4be44fcd
LB
798 struct acpi_debug_mem_block *element;
799 union acpi_descriptor *descriptor;
800 u32 num_outstanding = 0;
1da177e4 801
4be44fcd 802 ACPI_FUNCTION_TRACE("ut_dump_allocations");
1da177e4
LT
803
804 /*
805 * Walk the allocation list.
806 */
4be44fcd 807 if (ACPI_FAILURE(acpi_ut_acquire_mutex(ACPI_MTX_MEMORY))) {
1da177e4
LT
808 return;
809 }
810
73459f73 811 element = acpi_gbl_global_list->list_head;
1da177e4
LT
812 while (element) {
813 if ((element->component & component) &&
4be44fcd
LB
814 ((module == NULL)
815 || (0 == ACPI_STRCMP(module, element->module)))) {
1da177e4
LT
816 /* Ignore allocated objects that are in a cache */
817
4be44fcd
LB
818 descriptor =
819 ACPI_CAST_PTR(union acpi_descriptor,
820 &element->user_space);
1da177e4 821 if (descriptor->descriptor_id != ACPI_DESC_TYPE_CACHED) {
4be44fcd
LB
822 acpi_os_printf("%p Len %04X %9.9s-%d [%s] ",
823 descriptor, element->size,
824 element->module, element->line,
825 acpi_ut_get_descriptor_name
826 (descriptor));
1da177e4
LT
827
828 /* Most of the elements will be Operand objects. */
829
4be44fcd 830 switch (ACPI_GET_DESCRIPTOR_TYPE(descriptor)) {
1da177e4 831 case ACPI_DESC_TYPE_OPERAND:
4be44fcd
LB
832 acpi_os_printf("%12.12s R%hd",
833 acpi_ut_get_type_name
834 (descriptor->object.
835 common.type),
836 descriptor->object.
837 common.reference_count);
1da177e4
LT
838 break;
839
840 case ACPI_DESC_TYPE_PARSER:
4be44fcd
LB
841 acpi_os_printf("aml_opcode %04hX",
842 descriptor->op.asl.
843 aml_opcode);
1da177e4
LT
844 break;
845
846 case ACPI_DESC_TYPE_NAMED:
4be44fcd
LB
847 acpi_os_printf("%4.4s",
848 acpi_ut_get_node_name
849 (&descriptor->node));
1da177e4
LT
850 break;
851
852 default:
853 break;
854 }
855
4be44fcd 856 acpi_os_printf("\n");
1da177e4
LT
857 num_outstanding++;
858 }
859 }
860 element = element->next;
861 }
862
4be44fcd 863 (void)acpi_ut_release_mutex(ACPI_MTX_MEMORY);
1da177e4
LT
864
865 /* Print summary */
866
867 if (!num_outstanding) {
4be44fcd
LB
868 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
869 "No outstanding allocations.\n"));
870 } else {
871 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
872 "%d(%X) Outstanding allocations\n",
873 num_outstanding, num_outstanding));
1da177e4
LT
874 }
875
876 return_VOID;
877}
878
4be44fcd 879#endif /* #ifdef ACPI_DBG_TRACK_ALLOCATIONS */
This page took 0.092149 seconds and 5 git commands to generate.