1 /******************************************************************************
3 * Module Name: cmobject - ACPI object create/delete/size/cache routines
6 *****************************************************************************/
9 * Copyright (C) 2000, 2001 R. Byron Moore
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #define _COMPONENT ACPI_UTILITIES
31 MODULE_NAME ("cmobject")
34 /*******************************************************************************
36 * FUNCTION: _Cm_create_internal_object
38 * PARAMETERS: Address - Address of the memory to deallocate
39 * Component - Component type of caller
40 * Module - Source file name of caller
41 * Line - Line number of caller
42 * Type - ACPI Type of the new object
44 * RETURN: Object - The new object. Null on failure
46 * DESCRIPTION: Create and initialize a new internal object.
48 * NOTE: We always allocate the worst-case object descriptor because
49 * these objects are cached, and we want them to be
50 * one-size-satisifies-any-request. This in itself may not be
51 * the most memory efficient, but the efficiency of the object
52 * cache should more than make up for this!
54 ******************************************************************************/
57 _cm_create_internal_object (
58 NATIVE_CHAR
*module_name
,
61 OBJECT_TYPE_INTERNAL type
)
63 ACPI_OPERAND_OBJECT
*object
;
66 /* Allocate the raw object descriptor */
68 object
= _cm_allocate_object_desc (module_name
, line_number
, component_id
);
70 /* Allocation failure */
75 /* Save the object type in the object descriptor */
77 object
->common
.type
= type
;
79 /* Init the reference count */
81 object
->common
.reference_count
= 1;
83 /* Any per-type initialization should go here */
90 /*******************************************************************************
92 * FUNCTION: Acpi_cm_valid_internal_object
94 * PARAMETERS: Operand - Object to be validated
96 * RETURN: Validate a pointer to be an ACPI_OPERAND_OBJECT
98 ******************************************************************************/
101 acpi_cm_valid_internal_object (
105 /* Check for a null pointer */
111 /* Check for a pointer within one of the ACPI tables */
113 if (acpi_tb_system_table_pointer (object
)) {
117 /* Check the descriptor type field */
119 if (!VALID_DESCRIPTOR_TYPE (object
, ACPI_DESC_TYPE_INTERNAL
)) {
120 /* Not an ACPI internal object, do some further checking */
129 /* The object appears to be a valid ACPI_OPERAND_OBJECT */
135 /*******************************************************************************
137 * FUNCTION: _Cm_allocate_object_desc
139 * PARAMETERS: Module_name - Caller's module name (for error output)
140 * Line_number - Caller's line number (for error output)
141 * Component_id - Caller's component ID (for error output)
142 * Message - Error message to use on failure
144 * RETURN: Pointer to newly allocated object descriptor. Null on error
146 * DESCRIPTION: Allocate a new object descriptor. Gracefully handle
149 ******************************************************************************/
152 _cm_allocate_object_desc (
153 NATIVE_CHAR
*module_name
,
157 ACPI_OPERAND_OBJECT
*object
;
160 acpi_cm_acquire_mutex (ACPI_MTX_CACHES
);
162 acpi_gbl_object_cache_requests
++;
164 /* Check the cache first */
166 if (acpi_gbl_object_cache
) {
167 /* There is an object available, use it */
169 object
= acpi_gbl_object_cache
;
170 acpi_gbl_object_cache
= object
->cache
.next
;
171 object
->cache
.next
= NULL
;
173 acpi_gbl_object_cache_hits
++;
174 acpi_gbl_object_cache_depth
--;
176 acpi_cm_release_mutex (ACPI_MTX_CACHES
);
180 /* The cache is empty, create a new object */
182 acpi_cm_release_mutex (ACPI_MTX_CACHES
);
184 /* Attempt to allocate new descriptor */
186 object
= _cm_callocate (sizeof (ACPI_OPERAND_OBJECT
), component_id
,
187 module_name
, line_number
);
189 /* Allocation failed */
191 _REPORT_ERROR (module_name
, line_number
, component_id
,
192 ("Could not allocate an object descriptor\n"));
197 /* Memory allocation metrics - compiled out in non debug mode. */
199 INCREMENT_OBJECT_METRICS (sizeof (ACPI_OPERAND_OBJECT
));
202 /* Mark the descriptor type */
204 object
->common
.data_type
= ACPI_DESC_TYPE_INTERNAL
;
210 /*******************************************************************************
212 * FUNCTION: Acpi_cm_delete_object_desc
214 * PARAMETERS: Object - Acpi internal object to be deleted
218 * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
220 ******************************************************************************/
223 acpi_cm_delete_object_desc (
224 ACPI_OPERAND_OBJECT
*object
)
228 /* Make sure that the object isn't already in the cache */
230 if (object
->common
.data_type
== (ACPI_DESC_TYPE_INTERNAL
| ACPI_CACHED_OBJECT
)) {
234 /* Object must be an ACPI_OPERAND_OBJECT */
236 if (object
->common
.data_type
!= ACPI_DESC_TYPE_INTERNAL
) {
241 /* If cache is full, just free this object */
243 if (acpi_gbl_object_cache_depth
>= MAX_OBJECT_CACHE_DEPTH
) {
245 * Memory allocation metrics. Call the macro here since we only
246 * care about dynamically allocated objects.
248 DECREMENT_OBJECT_METRICS (sizeof (ACPI_OPERAND_OBJECT
));
250 acpi_cm_free (object
);
254 acpi_cm_acquire_mutex (ACPI_MTX_CACHES
);
256 /* Clear the entire object. This is important! */
258 MEMSET (object
, 0, sizeof (ACPI_OPERAND_OBJECT
));
259 object
->common
.data_type
= ACPI_DESC_TYPE_INTERNAL
| ACPI_CACHED_OBJECT
;
261 /* Put the object at the head of the global cache list */
263 object
->cache
.next
= acpi_gbl_object_cache
;
264 acpi_gbl_object_cache
= object
;
265 acpi_gbl_object_cache_depth
++;
268 acpi_cm_release_mutex (ACPI_MTX_CACHES
);
273 /*******************************************************************************
275 * FUNCTION: Acpi_cm_delete_object_cache
281 * DESCRIPTION: Purge the global state object cache. Used during subsystem
284 ******************************************************************************/
287 acpi_cm_delete_object_cache (
290 ACPI_OPERAND_OBJECT
*next
;
293 /* Traverse the global cache list */
295 while (acpi_gbl_object_cache
) {
296 /* Delete one cached state object */
298 next
= acpi_gbl_object_cache
->cache
.next
;
299 acpi_gbl_object_cache
->cache
.next
= NULL
;
302 * Memory allocation metrics. Call the macro here since we only
303 * care about dynamically allocated objects.
305 DECREMENT_OBJECT_METRICS (sizeof (ACPI_OPERAND_OBJECT
));
307 acpi_cm_free (acpi_gbl_object_cache
);
308 acpi_gbl_object_cache
= next
;
309 acpi_gbl_object_cache_depth
--;
316 /*******************************************************************************
318 * FUNCTION: Acpi_cm_init_static_object
320 * PARAMETERS: Obj_desc - Pointer to a "static" object - on stack
321 * or in the data segment.
325 * DESCRIPTION: Initialize a static object. Sets flags to disallow dynamic
326 * deletion of the object.
328 ******************************************************************************/
331 acpi_cm_init_static_object (
332 ACPI_OPERAND_OBJECT
*obj_desc
)
342 * Clear the entire descriptor
344 MEMSET ((void *) obj_desc
, 0, sizeof (ACPI_OPERAND_OBJECT
));
348 * Initialize the header fields
349 * 1) This is an ACPI_OPERAND_OBJECT descriptor
350 * 2) The size is the full object (worst case)
351 * 3) The flags field indicates static allocation
352 * 4) Reference count starts at one (not really necessary since the
353 * object can't be deleted, but keeps everything sane)
356 obj_desc
->common
.data_type
= ACPI_DESC_TYPE_INTERNAL
;
357 obj_desc
->common
.flags
= AOPOBJ_STATIC_ALLOCATION
;
358 obj_desc
->common
.reference_count
= 1;
364 /*******************************************************************************
366 * FUNCTION: Acpi_cm_get_simple_object_size
368 * PARAMETERS: *Internal_object - Pointer to the object we are examining
369 * *Ret_length - Where the length is returned
373 * DESCRIPTION: This function is called to determine the space required to
374 * contain a simple object for return to an API user.
376 * The length includes the object structure plus any additional
379 ******************************************************************************/
382 acpi_cm_get_simple_object_size (
383 ACPI_OPERAND_OBJECT
*internal_object
,
387 ACPI_STATUS status
= AE_OK
;
390 /* Handle a null object (Could be a uninitialized package element -- which is legal) */
392 if (!internal_object
) {
398 /* Start with the length of the Acpi object */
400 length
= sizeof (ACPI_OBJECT
);
402 if (VALID_DESCRIPTOR_TYPE (internal_object
, ACPI_DESC_TYPE_NAMED
)) {
403 /* Object is a named object (reference), just return the length */
405 *obj_length
= (u32
) ROUND_UP_TO_NATIVE_WORD (length
);
411 * The final length depends on the object type
412 * Strings and Buffers are packed right up against the parent object and
413 * must be accessed bytewise or there may be alignment problems on
417 switch (internal_object
->common
.type
) {
419 case ACPI_TYPE_STRING
:
421 length
+= internal_object
->string
.length
+ 1;
425 case ACPI_TYPE_BUFFER
:
427 length
+= internal_object
->buffer
.length
;
431 case ACPI_TYPE_INTEGER
:
432 case ACPI_TYPE_PROCESSOR
:
433 case ACPI_TYPE_POWER
:
436 * No extra data for these types
441 case INTERNAL_TYPE_REFERENCE
:
444 * The only type that should be here is opcode AML_NAMEPATH_OP -- since
445 * this means an object reference
447 if (internal_object
->reference
.opcode
!= AML_NAMEPATH_OP
) {
453 * Get the actual length of the full pathname to this object.
454 * The reference will be converted to the pathname to the object
456 length
+= ROUND_UP_TO_NATIVE_WORD (acpi_ns_get_pathname_length (internal_object
->reference
.node
));
469 * Account for the space required by the object rounded up to the next
470 * multiple of the machine word size. This keeps each object aligned
471 * on a machine word boundary. (preventing alignment faults on some
474 *obj_length
= (u32
) ROUND_UP_TO_NATIVE_WORD (length
);
480 /*******************************************************************************
482 * FUNCTION: Acpi_cm_get_element_length
484 * PARAMETERS: ACPI_PKG_CALLBACK
486 * RETURN: Status - the status of the call
488 * DESCRIPTION: Get the length of one package element.
490 ******************************************************************************/
493 acpi_cm_get_element_length (
495 ACPI_OPERAND_OBJECT
*source_object
,
496 ACPI_GENERIC_STATE
*state
,
499 ACPI_STATUS status
= AE_OK
;
500 ACPI_PKG_INFO
*info
= (ACPI_PKG_INFO
*) context
;
504 switch (object_type
) {
508 * Simple object - just get the size (Null object/entry is handled
509 * here also) and sum it into the running package length
511 status
= acpi_cm_get_simple_object_size (source_object
, &object_space
);
512 if (ACPI_FAILURE (status
)) {
516 info
->length
+= object_space
;
521 /* Package - nothing much to do here, let the walk handle it */
523 info
->num_packages
++;
524 state
->pkg
.this_target_obj
= NULL
;
528 return (AE_BAD_PARAMETER
);
536 /*******************************************************************************
538 * FUNCTION: Acpi_cm_get_package_object_size
540 * PARAMETERS: *Internal_object - Pointer to the object we are examining
541 * *Ret_length - Where the length is returned
545 * DESCRIPTION: This function is called to determine the space required to
546 * contain a package object for return to an API user.
548 * This is moderately complex since a package contains other
549 * objects including packages.
551 ******************************************************************************/
554 acpi_cm_get_package_object_size (
555 ACPI_OPERAND_OBJECT
*internal_object
,
563 info
.object_space
= 0;
564 info
.num_packages
= 1;
566 status
= acpi_cm_walk_package_tree (internal_object
, NULL
,
567 acpi_cm_get_element_length
, &info
);
570 * We have handled all of the objects in all levels of the package.
571 * just add the length of the package objects themselves.
572 * Round up to the next machine word.
574 info
.length
+= ROUND_UP_TO_NATIVE_WORD (sizeof (ACPI_OBJECT
)) *
577 /* Return the total package length */
579 *obj_length
= info
.length
;
584 /*******************************************************************************
586 * FUNCTION: Acpi_cm_get_object_size
588 * PARAMETERS: *Internal_object - Pointer to the object we are examining
589 * *Ret_length - Where the length will be returned
593 * DESCRIPTION: This function is called to determine the space required to
594 * contain an object for return to an API user.
596 ******************************************************************************/
599 acpi_cm_get_object_size(
600 ACPI_OPERAND_OBJECT
*internal_object
,
606 if ((VALID_DESCRIPTOR_TYPE (internal_object
, ACPI_DESC_TYPE_INTERNAL
)) &&
607 (IS_THIS_OBJECT_TYPE (internal_object
, ACPI_TYPE_PACKAGE
))) {
608 status
= acpi_cm_get_package_object_size (internal_object
, obj_length
);
612 status
= acpi_cm_get_simple_object_size (internal_object
, obj_length
);