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