blob: 068450b36475a8b9b3552594bfcfbbf1409f87b0 [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/*
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
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 */
Robert Moore44f6c012005-04-18 22:49:35 -040050#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);
61#endif /* ACPI_DBG_TRACK_ALLOCATIONS */
Robert Moore44f6c012005-04-18 22:49:35 -040062
Robert Moore73459f72005-06-24 00:00:00 -040063#ifdef ACPI_DBG_TRACK_ALLOCATIONS
64static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040065acpi_ut_create_list(char *list_name,
66 u16 object_size, struct acpi_memory_list **return_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Robert Moore44f6c012005-04-18 22:49:35 -040069/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 *
Robert Moore73459f72005-06-24 00:00:00 -040071 * FUNCTION: acpi_ut_create_caches
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
Robert Moore73459f72005-06-24 00:00:00 -040073 * PARAMETERS: None
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
Robert Moore73459f72005-06-24 00:00:00 -040075 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
Robert Moore73459f72005-06-24 00:00:00 -040077 * DESCRIPTION: Create all local caches
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
79 ******************************************************************************/
80
Len Brown4be44fc2005-08-05 00:44:28 -040081acpi_status acpi_ut_create_caches(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Len Brown4be44fc2005-08-05 00:44:28 -040083 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#ifdef ACPI_DBG_TRACK_ALLOCATIONS
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Robert Moore73459f72005-06-24 00:00:00 -040087 /* Memory allocation lists */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Len Brown4be44fc2005-08-05 00:44:28 -040089 status = acpi_ut_create_list("Acpi-Global", 0, &acpi_gbl_global_list);
90 if (ACPI_FAILURE(status)) {
Robert Moore73459f72005-06-24 00:00:00 -040091 return (status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
Len Brown4be44fc2005-08-05 00:44:28 -040094 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)) {
Robert Moore73459f72005-06-24 00:00:00 -040099 return (status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#endif
102
Robert Moore73459f72005-06-24 00:00:00 -0400103 /* Object Caches, for frequently used objects */
104
Len Brown4be44fc2005-08-05 00:44:28 -0400105 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)) {
Robert Moore73459f72005-06-24 00:00:00 -0400110 return (status);
111 }
112
Len Brown4be44fc2005-08-05 00:44:28 -0400113 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)) {
Robert Moore73459f72005-06-24 00:00:00 -0400119 return (status);
120 }
121
Len Brown4be44fc2005-08-05 00:44:28 -0400122 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)) {
Robert Moore73459f72005-06-24 00:00:00 -0400128 return (status);
129 }
130
Len Brown4be44fc2005-08-05 00:44:28 -0400131 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)) {
Robert Moore73459f72005-06-24 00:00:00 -0400137 return (status);
138 }
139
140 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Robert Moore44f6c012005-04-18 22:49:35 -0400143/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 *
Robert Moore73459f72005-06-24 00:00:00 -0400145 * FUNCTION: acpi_ut_delete_caches
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 *
Robert Moore73459f72005-06-24 00:00:00 -0400147 * PARAMETERS: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *
Robert Moore73459f72005-06-24 00:00:00 -0400149 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
Robert Moore73459f72005-06-24 00:00:00 -0400151 * DESCRIPTION: Purge and delete all local caches
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 *
153 ******************************************************************************/
154
Len Brown4be44fc2005-08-05 00:44:28 -0400155acpi_status acpi_ut_delete_caches(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Len Brown4be44fc2005-08-05 00:44:28 -0400158 (void)acpi_os_delete_cache(acpi_gbl_state_cache);
Robert Moore73459f72005-06-24 00:00:00 -0400159 acpi_gbl_state_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Len Brown4be44fc2005-08-05 00:44:28 -0400161 (void)acpi_os_delete_cache(acpi_gbl_operand_cache);
Robert Moore73459f72005-06-24 00:00:00 -0400162 acpi_gbl_operand_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Len Brown4be44fc2005-08-05 00:44:28 -0400164 (void)acpi_os_delete_cache(acpi_gbl_ps_node_cache);
Robert Moore73459f72005-06-24 00:00:00 -0400165 acpi_gbl_ps_node_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Len Brown4be44fc2005-08-05 00:44:28 -0400167 (void)acpi_os_delete_cache(acpi_gbl_ps_node_ext_cache);
Robert Moore73459f72005-06-24 00:00:00 -0400168 acpi_gbl_ps_node_ext_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Robert Moore73459f72005-06-24 00:00:00 -0400170 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
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
Len Brown4be44fc2005-08-05 00:44:28 -0400185acpi_status acpi_ut_validate_buffer(struct acpi_buffer * buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
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
Len Brown4be44fc2005-08-05 00:44:28 -0400196 if ((buffer->length == ACPI_NO_BUFFER) ||
197 (buffer->length == ACPI_ALLOCATE_BUFFER) ||
198 (buffer->length == ACPI_ALLOCATE_LOCAL_BUFFER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*******************************************************************************
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
Len Brown4be44fc2005-08-05 00:44:28 -0400226acpi_ut_initialize_buffer(struct acpi_buffer * buffer,
227 acpi_size required_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Len Brown4be44fc2005-08-05 00:44:28 -0400229 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 case ACPI_ALLOCATE_BUFFER:
240
241 /* Allocate a new buffer */
242
Len Brown4be44fc2005-08-05 00:44:28 -0400243 buffer->pointer = acpi_os_allocate(required_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (!buffer->pointer) {
245 return (AE_NO_MEMORY);
246 }
247
248 /* Clear the buffer */
249
Len Brown4be44fc2005-08-05 00:44:28 -0400250 ACPI_MEMSET(buffer->pointer, 0, required_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 break;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 case ACPI_ALLOCATE_LOCAL_BUFFER:
254
255 /* Allocate a new buffer with local interface to allow tracking */
256
Len Brown4be44fc2005-08-05 00:44:28 -0400257 buffer->pointer = ACPI_MEM_CALLOCATE(required_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (!buffer->pointer) {
259 return (AE_NO_MEMORY);
260 }
261 break;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 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
Len Brown4be44fc2005-08-05 00:44:28 -0400274 ACPI_MEMSET(buffer->pointer, 0, required_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 break;
276 }
277
278 buffer->length = required_length;
279 return (status);
280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282/*******************************************************************************
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
Len Brown4be44fc2005-08-05 00:44:28 -0400297void *acpi_ut_allocate(acpi_size size, u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Len Brown4be44fc2005-08-05 00:44:28 -0400299 void *allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Len Brown4be44fc2005-08-05 00:44:28 -0400301 ACPI_FUNCTION_TRACE_U32("ut_allocate", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /* Check for an inadvertent size of zero bytes */
304
305 if (!size) {
Len Brown4be44fc2005-08-05 00:44:28 -0400306 _ACPI_REPORT_ERROR(module, line, component,
307 ("ut_allocate: Attempt to allocate zero bytes\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 size = 1;
309 }
310
Len Brown4be44fc2005-08-05 00:44:28 -0400311 allocation = acpi_os_allocate(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (!allocation) {
313 /* Report allocation error */
314
Len Brown4be44fc2005-08-05 00:44:28 -0400315 _ACPI_REPORT_ERROR(module, line, component,
316 ("ut_allocate: Could not allocate size %X\n",
317 (u32) size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Len Brown4be44fc2005-08-05 00:44:28 -0400319 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
Len Brown4be44fc2005-08-05 00:44:28 -0400322 return_PTR(allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/*******************************************************************************
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
Len Brown4be44fc2005-08-05 00:44:28 -0400340void *acpi_ut_callocate(acpi_size size, u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Len Brown4be44fc2005-08-05 00:44:28 -0400342 void *allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Len Brown4be44fc2005-08-05 00:44:28 -0400344 ACPI_FUNCTION_TRACE_U32("ut_callocate", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* Check for an inadvertent size of zero bytes */
347
348 if (!size) {
Len Brown4be44fc2005-08-05 00:44:28 -0400349 _ACPI_REPORT_ERROR(module, line, component,
350 ("ut_callocate: Attempt to allocate zero bytes\n"));
351 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
Len Brown4be44fc2005-08-05 00:44:28 -0400354 allocation = acpi_os_allocate(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (!allocation) {
356 /* Report allocation error */
357
Len Brown4be44fc2005-08-05 00:44:28 -0400358 _ACPI_REPORT_ERROR(module, line, component,
359 ("ut_callocate: Could not allocate size %X\n",
360 (u32) size));
361 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
364 /* Clear the memory block */
365
Len Brown4be44fc2005-08-05 00:44:28 -0400366 ACPI_MEMSET(allocation, 0, size);
367 return_PTR(allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#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
Robert Moore73459f72005-06-24 00:00:00 -0400382/*******************************************************************************
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
Len Brown4be44fc2005-08-05 00:44:28 -0400397acpi_ut_create_list(char *list_name,
398 u16 object_size, struct acpi_memory_list **return_cache)
Robert Moore73459f72005-06-24 00:00:00 -0400399{
Len Brown4be44fc2005-08-05 00:44:28 -0400400 struct acpi_memory_list *cache;
Robert Moore73459f72005-06-24 00:00:00 -0400401
Len Brown4be44fc2005-08-05 00:44:28 -0400402 cache = acpi_os_allocate(sizeof(struct acpi_memory_list));
Robert Moore73459f72005-06-24 00:00:00 -0400403 if (!cache) {
404 return (AE_NO_MEMORY);
405 }
406
Len Brown4be44fc2005-08-05 00:44:28 -0400407 ACPI_MEMSET(cache, 0, sizeof(struct acpi_memory_list));
Robert Moore73459f72005-06-24 00:00:00 -0400408
Len Brown4be44fc2005-08-05 00:44:28 -0400409 cache->list_name = list_name;
Robert Moore73459f72005-06-24 00:00:00 -0400410 cache->object_size = object_size;
411
412 *return_cache = cache;
413 return (AE_OK);
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/*******************************************************************************
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
Len Brown4be44fc2005-08-05 00:44:28 -0400431void *acpi_ut_allocate_and_track(acpi_size size,
432 u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Len Brown4be44fc2005-08-05 00:44:28 -0400434 struct acpi_debug_mem_block *allocation;
435 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Len Brown4be44fc2005-08-05 00:44:28 -0400437 allocation =
438 acpi_ut_allocate(size + sizeof(struct acpi_debug_mem_header),
439 component, module, line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (!allocation) {
441 return (NULL);
442 }
443
Len Brown4be44fc2005-08-05 00:44:28 -0400444 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return (NULL);
450 }
451
Robert Moore73459f72005-06-24 00:00:00 -0400452 acpi_gbl_global_list->total_allocated++;
453 acpi_gbl_global_list->current_total_size += (u32) size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Len Brown4be44fc2005-08-05 00:44:28 -0400455 return ((void *)&allocation->user_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*******************************************************************************
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
Len Brown4be44fc2005-08-05 00:44:28 -0400473void *acpi_ut_callocate_and_track(acpi_size size,
474 u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Len Brown4be44fc2005-08-05 00:44:28 -0400476 struct acpi_debug_mem_block *allocation;
477 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Len Brown4be44fc2005-08-05 00:44:28 -0400479 allocation =
480 acpi_ut_callocate(size + sizeof(struct acpi_debug_mem_header),
481 component, module, line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!allocation) {
483 /* Report allocation error */
484
Len Brown4be44fc2005-08-05 00:44:28 -0400485 _ACPI_REPORT_ERROR(module, line, component,
486 ("ut_callocate: Could not allocate size %X\n",
487 (u32) size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return (NULL);
489 }
490
Len Brown4be44fc2005-08-05 00:44:28 -0400491 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return (NULL);
497 }
498
Robert Moore73459f72005-06-24 00:00:00 -0400499 acpi_gbl_global_list->total_allocated++;
500 acpi_gbl_global_list->current_total_size += (u32) size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Len Brown4be44fc2005-08-05 00:44:28 -0400502 return ((void *)&allocation->user_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/*******************************************************************************
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
Len Brown4be44fc2005-08-05 00:44:28 -0400521acpi_ut_free_and_track(void *allocation, u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Len Brown4be44fc2005-08-05 00:44:28 -0400523 struct acpi_debug_mem_block *debug_block;
524 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Len Brown4be44fc2005-08-05 00:44:28 -0400526 ACPI_FUNCTION_TRACE_PTR("ut_free", allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 if (NULL == allocation) {
Len Brown4be44fc2005-08-05 00:44:28 -0400529 _ACPI_REPORT_ERROR(module, line, component,
530 ("acpi_ut_free: Attempt to delete a NULL address\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 return_VOID;
533 }
534
Len Brown4be44fc2005-08-05 00:44:28 -0400535 debug_block = ACPI_CAST_PTR(struct acpi_debug_mem_block,
536 (((char *)allocation) -
537 sizeof(struct acpi_debug_mem_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Robert Moore73459f72005-06-24 00:00:00 -0400539 acpi_gbl_global_list->total_freed++;
540 acpi_gbl_global_list->current_total_size -= debug_block->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Len Brown4be44fc2005-08-05 00:44:28 -0400542 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)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
Len Brown4be44fc2005-08-05 00:44:28 -0400549 acpi_os_free(debug_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Len Brown4be44fc2005-08-05 00:44:28 -0400551 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "%p freed\n", allocation));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 return_VOID;
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/*******************************************************************************
557 *
558 * FUNCTION: acpi_ut_find_allocation
559 *
Robert Moore73459f72005-06-24 00:00:00 -0400560 * PARAMETERS: Allocation - Address of allocated memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 *
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
Len Brown4be44fc2005-08-05 00:44:28 -0400568static struct acpi_debug_mem_block *acpi_ut_find_allocation(void *allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Len Brown4be44fc2005-08-05 00:44:28 -0400570 struct acpi_debug_mem_block *element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Len Brown4be44fc2005-08-05 00:44:28 -0400572 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Robert Moore73459f72005-06-24 00:00:00 -0400574 element = acpi_gbl_global_list->list_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/*******************************************************************************
590 *
591 * FUNCTION: acpi_ut_track_allocation
592 *
Robert Moore73459f72005-06-24 00:00:00 -0400593 * PARAMETERS: Allocation - Address of allocated memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * 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
Robert Moore44f6c012005-04-18 22:49:35 -0400606static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400607acpi_ut_track_allocation(struct acpi_debug_mem_block *allocation,
608 acpi_size size,
609 u8 alloc_type, u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Len Brown4be44fc2005-08-05 00:44:28 -0400611 struct acpi_memory_list *mem_list;
612 struct acpi_debug_mem_block *element;
613 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Len Brown4be44fc2005-08-05 00:44:28 -0400615 ACPI_FUNCTION_TRACE_PTR("ut_track_allocation", allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Robert Moore73459f72005-06-24 00:00:00 -0400617 mem_list = acpi_gbl_global_list;
Len Brown4be44fc2005-08-05 00:44:28 -0400618 status = acpi_ut_acquire_mutex(ACPI_MTX_MEMORY);
619 if (ACPI_FAILURE(status)) {
620 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
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 */
Len Brown4be44fc2005-08-05 00:44:28 -0400627 element = acpi_ut_find_allocation(allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (element) {
Len Brown4be44fc2005-08-05 00:44:28 -0400629 ACPI_REPORT_ERROR(("ut_track_allocation: Allocation already present in list! (%p)\n", allocation));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Len Brown4be44fc2005-08-05 00:44:28 -0400631 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Element %p Address %p\n",
632 element, allocation));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 goto unlock_and_exit;
635 }
636
637 /* Fill in the instance data. */
638
Len Brown4be44fc2005-08-05 00:44:28 -0400639 allocation->size = (u32) size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 allocation->alloc_type = alloc_type;
641 allocation->component = component;
Len Brown4be44fc2005-08-05 00:44:28 -0400642 allocation->line = line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Len Brown4be44fc2005-08-05 00:44:28 -0400644 ACPI_STRNCPY(allocation->module, module, ACPI_MAX_MODULE_NAME);
645 allocation->module[ACPI_MAX_MODULE_NAME - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /* Insert at list head */
648
649 if (mem_list->list_head) {
Len Brown4be44fc2005-08-05 00:44:28 -0400650 ((struct acpi_debug_mem_block *)(mem_list->list_head))->
651 previous = allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
654 allocation->next = mem_list->list_head;
655 allocation->previous = NULL;
656
657 mem_list->list_head = allocation;
658
Len Brown4be44fc2005-08-05 00:44:28 -0400659 unlock_and_exit:
660 status = acpi_ut_release_mutex(ACPI_MTX_MEMORY);
661 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664/*******************************************************************************
665 *
666 * FUNCTION: acpi_ut_remove_allocation
667 *
Robert Moore73459f72005-06-24 00:00:00 -0400668 * PARAMETERS: Allocation - Address of allocated memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 * 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
Robert Moore44f6c012005-04-18 22:49:35 -0400679static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400680acpi_ut_remove_allocation(struct acpi_debug_mem_block *allocation,
681 u32 component, char *module, u32 line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Len Brown4be44fc2005-08-05 00:44:28 -0400683 struct acpi_memory_list *mem_list;
684 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Len Brown4be44fc2005-08-05 00:44:28 -0400686 ACPI_FUNCTION_TRACE("ut_remove_allocation");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Robert Moore73459f72005-06-24 00:00:00 -0400688 mem_list = acpi_gbl_global_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (NULL == mem_list->list_head) {
690 /* No allocations! */
691
Len Brown4be44fc2005-08-05 00:44:28 -0400692 _ACPI_REPORT_ERROR(module, line, component,
693 ("ut_remove_allocation: Empty allocation list, nothing to free!\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Len Brown4be44fc2005-08-05 00:44:28 -0400695 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
Len Brown4be44fc2005-08-05 00:44:28 -0400698 status = acpi_ut_acquire_mutex(ACPI_MTX_MEMORY);
699 if (ACPI_FAILURE(status)) {
700 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
703 /* Unlink */
704
705 if (allocation->previous) {
706 (allocation->previous)->next = allocation->next;
Len Brown4be44fc2005-08-05 00:44:28 -0400707 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 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
Len Brown4be44fc2005-08-05 00:44:28 -0400717 ACPI_MEMSET(&allocation->user_space, 0xEA, allocation->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Len Brown4be44fc2005-08-05 00:44:28 -0400719 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "Freeing size 0%X\n",
720 allocation->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Len Brown4be44fc2005-08-05 00:44:28 -0400722 status = acpi_ut_release_mutex(ACPI_MTX_MEMORY);
723 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726/*******************************************************************************
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 ******************************************************************************/
Robert Moore44f6c012005-04-18 22:49:35 -0400737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738#ifdef ACPI_FUTURE_USAGE
Len Brown4be44fc2005-08-05 00:44:28 -0400739void acpi_ut_dump_allocation_info(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741/*
742 struct acpi_memory_list *mem_list;
743*/
744
Len Brown4be44fc2005-08-05 00:44:28 -0400745 ACPI_FUNCTION_TRACE("ut_dump_allocation_info");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 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,
Robert Moore44f6c012005-04-18 22:49:35 -0400776 ROUND_UP_TO_1K ((acpi_gbl_max_concurrent_node_count *
777 sizeof (struct acpi_namespace_node)))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778*/
779 return_VOID;
780}
Len Brown4be44fc2005-08-05 00:44:28 -0400781#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
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
Len Brown4be44fc2005-08-05 00:44:28 -0400796void acpi_ut_dump_allocations(u32 component, char *module)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Len Brown4be44fc2005-08-05 00:44:28 -0400798 struct acpi_debug_mem_block *element;
799 union acpi_descriptor *descriptor;
800 u32 num_outstanding = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Len Brown4be44fc2005-08-05 00:44:28 -0400802 ACPI_FUNCTION_TRACE("ut_dump_allocations");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /*
805 * Walk the allocation list.
806 */
Len Brown4be44fc2005-08-05 00:44:28 -0400807 if (ACPI_FAILURE(acpi_ut_acquire_mutex(ACPI_MTX_MEMORY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return;
809 }
810
Robert Moore73459f72005-06-24 00:00:00 -0400811 element = acpi_gbl_global_list->list_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 while (element) {
813 if ((element->component & component) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400814 ((module == NULL)
815 || (0 == ACPI_STRCMP(module, element->module)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* Ignore allocated objects that are in a cache */
817
Len Brown4be44fc2005-08-05 00:44:28 -0400818 descriptor =
819 ACPI_CAST_PTR(union acpi_descriptor,
820 &element->user_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (descriptor->descriptor_id != ACPI_DESC_TYPE_CACHED) {
Len Brown4be44fc2005-08-05 00:44:28 -0400822 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 /* Most of the elements will be Operand objects. */
829
Len Brown4be44fc2005-08-05 00:44:28 -0400830 switch (ACPI_GET_DESCRIPTOR_TYPE(descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 case ACPI_DESC_TYPE_OPERAND:
Len Brown4be44fc2005-08-05 00:44:28 -0400832 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 break;
839
840 case ACPI_DESC_TYPE_PARSER:
Len Brown4be44fc2005-08-05 00:44:28 -0400841 acpi_os_printf("aml_opcode %04hX",
842 descriptor->op.asl.
843 aml_opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 break;
845
846 case ACPI_DESC_TYPE_NAMED:
Len Brown4be44fc2005-08-05 00:44:28 -0400847 acpi_os_printf("%4.4s",
848 acpi_ut_get_node_name
849 (&descriptor->node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 break;
851
852 default:
853 break;
854 }
855
Len Brown4be44fc2005-08-05 00:44:28 -0400856 acpi_os_printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 num_outstanding++;
858 }
859 }
860 element = element->next;
861 }
862
Len Brown4be44fc2005-08-05 00:44:28 -0400863 (void)acpi_ut_release_mutex(ACPI_MTX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /* Print summary */
866
867 if (!num_outstanding) {
Len Brown4be44fc2005-08-05 00:44:28 -0400868 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
876 return_VOID;
877}
878
Len Brown4be44fc2005-08-05 00:44:28 -0400879#endif /* #ifdef ACPI_DBG_TRACK_ALLOCATIONS */