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
34 #define _COMPONENT ACPI_UTILITIES
35 MODULE_NAME ("cmobject")
38 /*******************************************************************************
40 * FUNCTION: _Cm_create_internal_object
42 * PARAMETERS: Address - Address of the memory to deallocate
43 * Component - Component type of caller
44 * Module - Source file name of caller
45 * Line - Line number of caller
46 * Type - ACPI Type of the new object
48 * RETURN: Object - The new object. Null on failure
50 * DESCRIPTION: Create and initialize a new internal object.
52 * NOTE: We always allocate the worst-case object descriptor because
53 * these objects are cached, and we want them to be
54 * one-size-satisifies-any-request. This in itself may not be
55 * the most memory efficient, but the efficiency of the object
56 * cache should more than make up for this!
58 ******************************************************************************/
61 _cm_create_internal_object (
62 NATIVE_CHAR *module_name,
65 OBJECT_TYPE_INTERNAL type)
67 ACPI_OPERAND_OBJECT *object;
70 /* Allocate the raw object descriptor */
72 object = _cm_allocate_object_desc (module_name, line_number, component_id);
74 /* Allocation failure */
79 /* Save the object type in the object descriptor */
81 object->common.type = type;
83 /* Init the reference count */
85 object->common.reference_count = 1;
87 /* Any per-type initialization should go here */
94 /*******************************************************************************
96 * FUNCTION: Acpi_cm_valid_internal_object
98 * PARAMETERS: Operand - Object to be validated
100 * RETURN: Validate a pointer to be an ACPI_OPERAND_OBJECT
102 ******************************************************************************/
105 acpi_cm_valid_internal_object (
109 /* Check for a null pointer */
115 /* Check for a pointer within one of the ACPI tables */
117 if (acpi_tb_system_table_pointer (object)) {
121 /* Check the descriptor type field */
123 if (!VALID_DESCRIPTOR_TYPE (object, ACPI_DESC_TYPE_INTERNAL)) {
124 /* Not an ACPI internal object, do some further checking */
133 /* The object appears to be a valid ACPI_OPERAND_OBJECT */
139 /*******************************************************************************
141 * FUNCTION: _Cm_allocate_object_desc
143 * PARAMETERS: Module_name - Caller's module name (for error output)
144 * Line_number - Caller's line number (for error output)
145 * Component_id - Caller's component ID (for error output)
146 * Message - Error message to use on failure
148 * RETURN: Pointer to newly allocated object descriptor. Null on error
150 * DESCRIPTION: Allocate a new object descriptor. Gracefully handle
153 ******************************************************************************/
156 _cm_allocate_object_desc (
157 NATIVE_CHAR *module_name,
161 ACPI_OPERAND_OBJECT *object;
164 acpi_cm_acquire_mutex (ACPI_MTX_CACHES);
166 acpi_gbl_object_cache_requests++;
168 /* Check the cache first */
170 if (acpi_gbl_object_cache) {
171 /* There is an object available, use it */
173 object = acpi_gbl_object_cache;
174 acpi_gbl_object_cache = object->cache.next;
175 object->cache.next = NULL;
177 acpi_gbl_object_cache_hits++;
178 acpi_gbl_object_cache_depth--;
180 acpi_cm_release_mutex (ACPI_MTX_CACHES);
184 /* The cache is empty, create a new object */
186 acpi_cm_release_mutex (ACPI_MTX_CACHES);
188 /* Attempt to allocate new descriptor */
190 object = _cm_callocate (sizeof (ACPI_OPERAND_OBJECT), component_id,
191 module_name, line_number);
193 /* Allocation failed */
195 _REPORT_ERROR (module_name, line_number, component_id,
196 ("Could not allocate an object descriptor\n"));
201 /* Memory allocation metrics - compiled out in non debug mode. */
203 INCREMENT_OBJECT_METRICS (sizeof (ACPI_OPERAND_OBJECT));
206 /* Mark the descriptor type */
208 object->common.data_type = ACPI_DESC_TYPE_INTERNAL;
214 /*******************************************************************************
216 * FUNCTION: Acpi_cm_delete_object_desc
218 * PARAMETERS: Object - Acpi internal object to be deleted
222 * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
224 ******************************************************************************/
227 acpi_cm_delete_object_desc (
228 ACPI_OPERAND_OBJECT *object)
232 /* Make sure that the object isn't already in the cache */
234 if (object->common.data_type == (ACPI_DESC_TYPE_INTERNAL | ACPI_CACHED_OBJECT)) {
238 /* Object must be an ACPI_OPERAND_OBJECT */
240 if (object->common.data_type != ACPI_DESC_TYPE_INTERNAL) {
245 /* If cache is full, just free this object */
247 if (acpi_gbl_object_cache_depth >= MAX_OBJECT_CACHE_DEPTH) {
249 * Memory allocation metrics. Call the macro here since we only
250 * care about dynamically allocated objects.
252 DECREMENT_OBJECT_METRICS (sizeof (ACPI_OPERAND_OBJECT));
254 acpi_cm_free (object);
258 acpi_cm_acquire_mutex (ACPI_MTX_CACHES);
260 /* Clear the entire object. This is important! */
262 MEMSET (object, 0, sizeof (ACPI_OPERAND_OBJECT));
263 object->common.data_type = ACPI_DESC_TYPE_INTERNAL | ACPI_CACHED_OBJECT;
265 /* Put the object at the head of the global cache list */
267 object->cache.next = acpi_gbl_object_cache;
268 acpi_gbl_object_cache = object;
269 acpi_gbl_object_cache_depth++;
272 acpi_cm_release_mutex (ACPI_MTX_CACHES);
277 /*******************************************************************************
279 * FUNCTION: Acpi_cm_delete_object_cache
285 * DESCRIPTION: Purge the global state object cache. Used during subsystem
288 ******************************************************************************/
291 acpi_cm_delete_object_cache (
294 ACPI_OPERAND_OBJECT *next;
297 /* Traverse the global cache list */
299 while (acpi_gbl_object_cache) {
300 /* Delete one cached state object */
302 next = acpi_gbl_object_cache->cache.next;
303 acpi_gbl_object_cache->cache.next = NULL;
306 * Memory allocation metrics. Call the macro here since we only
307 * care about dynamically allocated objects.
309 DECREMENT_OBJECT_METRICS (sizeof (ACPI_OPERAND_OBJECT));
311 acpi_cm_free (acpi_gbl_object_cache);
312 acpi_gbl_object_cache = next;
313 acpi_gbl_object_cache_depth--;
320 /*******************************************************************************
322 * FUNCTION: Acpi_cm_init_static_object
324 * PARAMETERS: Obj_desc - Pointer to a "static" object - on stack
325 * or in the data segment.
329 * DESCRIPTION: Initialize a static object. Sets flags to disallow dynamic
330 * deletion of the object.
332 ******************************************************************************/
335 acpi_cm_init_static_object (
336 ACPI_OPERAND_OBJECT *obj_desc)
346 * Clear the entire descriptor
348 MEMSET ((void *) obj_desc, 0, sizeof (ACPI_OPERAND_OBJECT));
352 * Initialize the header fields
353 * 1) This is an ACPI_OPERAND_OBJECT descriptor
354 * 2) The size is the full object (worst case)
355 * 3) The flags field indicates static allocation
356 * 4) Reference count starts at one (not really necessary since the
357 * object can't be deleted, but keeps everything sane)
360 obj_desc->common.data_type = ACPI_DESC_TYPE_INTERNAL;
361 obj_desc->common.flags = AOPOBJ_STATIC_ALLOCATION;
362 obj_desc->common.reference_count = 1;
368 /*******************************************************************************
370 * FUNCTION: Acpi_cm_get_simple_object_size
372 * PARAMETERS: *Internal_object - Pointer to the object we are examining
373 * *Ret_length - Where the length is returned
377 * DESCRIPTION: This function is called to determine the space required to
378 * contain a simple object for return to an API user.
380 * The length includes the object structure plus any additional
383 ******************************************************************************/
386 acpi_cm_get_simple_object_size (
387 ACPI_OPERAND_OBJECT *internal_object,
391 ACPI_STATUS status = AE_OK;
394 /* Handle a null object (Could be a uninitialized package element -- which is legal) */
396 if (!internal_object) {
402 /* Start with the length of the Acpi object */
404 length = sizeof (ACPI_OBJECT);
406 if (VALID_DESCRIPTOR_TYPE (internal_object, ACPI_DESC_TYPE_NAMED)) {
407 /* Object is a named object (reference), just return the length */
409 *obj_length = (u32) ROUND_UP_TO_NATIVE_WORD (length);
415 * The final length depends on the object type
416 * Strings and Buffers are packed right up against the parent object and
417 * must be accessed bytewise or there may be alignment problems on
421 switch (internal_object->common.type) {
423 case ACPI_TYPE_STRING:
425 length += internal_object->string.length + 1;
429 case ACPI_TYPE_BUFFER:
431 length += internal_object->buffer.length;
435 case ACPI_TYPE_INTEGER:
436 case ACPI_TYPE_PROCESSOR:
437 case ACPI_TYPE_POWER:
440 * No extra data for these types
445 case INTERNAL_TYPE_REFERENCE:
448 * The only type that should be here is opcode AML_NAMEPATH_OP -- since
449 * this means an object reference
451 if (internal_object->reference.opcode != AML_NAMEPATH_OP) {
457 * Get the actual length of the full pathname to this object.
458 * The reference will be converted to the pathname to the object
460 length += ROUND_UP_TO_NATIVE_WORD (acpi_ns_get_pathname_length (internal_object->reference.node));
473 * Account for the space required by the object rounded up to the next
474 * multiple of the machine word size. This keeps each object aligned
475 * on a machine word boundary. (preventing alignment faults on some
478 *obj_length = (u32) ROUND_UP_TO_NATIVE_WORD (length);
484 /*******************************************************************************
486 * FUNCTION: Acpi_cm_get_element_length
488 * PARAMETERS: ACPI_PKG_CALLBACK
490 * RETURN: Status - the status of the call
492 * DESCRIPTION: Get the length of one package element.
494 ******************************************************************************/
497 acpi_cm_get_element_length (
499 ACPI_OPERAND_OBJECT *source_object,
500 ACPI_GENERIC_STATE *state,
503 ACPI_STATUS status = AE_OK;
504 ACPI_PKG_INFO *info = (ACPI_PKG_INFO *) context;
508 switch (object_type) {
512 * Simple object - just get the size (Null object/entry is handled
513 * here also) and sum it into the running package length
515 status = acpi_cm_get_simple_object_size (source_object, &object_space);
516 if (ACPI_FAILURE (status)) {
520 info->length += object_space;
525 /* Package - nothing much to do here, let the walk handle it */
527 info->num_packages++;
528 state->pkg.this_target_obj = NULL;
532 return (AE_BAD_PARAMETER);
540 /*******************************************************************************
542 * FUNCTION: Acpi_cm_get_package_object_size
544 * PARAMETERS: *Internal_object - Pointer to the object we are examining
545 * *Ret_length - Where the length is returned
549 * DESCRIPTION: This function is called to determine the space required to
550 * contain a package object for return to an API user.
552 * This is moderately complex since a package contains other
553 * objects including packages.
555 ******************************************************************************/
558 acpi_cm_get_package_object_size (
559 ACPI_OPERAND_OBJECT *internal_object,
567 info.object_space = 0;
568 info.num_packages = 1;
570 status = acpi_cm_walk_package_tree (internal_object, NULL,
571 acpi_cm_get_element_length, &info);
574 * We have handled all of the objects in all levels of the package.
575 * just add the length of the package objects themselves.
576 * Round up to the next machine word.
578 info.length += ROUND_UP_TO_NATIVE_WORD (sizeof (ACPI_OBJECT)) *
581 /* Return the total package length */
583 *obj_length = info.length;
588 /*******************************************************************************
590 * FUNCTION: Acpi_cm_get_object_size
592 * PARAMETERS: *Internal_object - Pointer to the object we are examining
593 * *Ret_length - Where the length will be returned
597 * DESCRIPTION: This function is called to determine the space required to
598 * contain an object for return to an API user.
600 ******************************************************************************/
603 acpi_cm_get_object_size(
604 ACPI_OPERAND_OBJECT *internal_object,
610 if ((VALID_DESCRIPTOR_TYPE (internal_object, ACPI_DESC_TYPE_INTERNAL)) &&
611 (IS_THIS_OBJECT_TYPE (internal_object, ACPI_TYPE_PACKAGE))) {
612 status = acpi_cm_get_package_object_size (internal_object, obj_length);
616 status = acpi_cm_get_simple_object_size (internal_object, obj_length);