Files
mango/tools/amldecode/aml/object.c

139 lines
2.9 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "object.h"
struct acpi_namespace *acpi_namespace_create(void)
{
struct acpi_namespace *ns = malloc(sizeof *ns);
if (!ns) {
return NULL;
}
memset(ns, 0x00, sizeof *ns);
ns->root = acpi_object_create("\\", ACPI_OBJECT_NAMESPACE);
return ns;
}
struct acpi_object *acpi_namespace_get_object(struct acpi_namespace *ns, const char *path)
{
return NULL;
}
struct acpi_object *acpi_object_create(const char name[ACPI_OBJECT_NAME_MAX], enum acpi_object_type type)
{
struct acpi_object *object = malloc(sizeof *object);
if (!object) {
return NULL;
}
memset(object, 0x00, sizeof *object);
object->ref = 1;
object->type = type;
if (name) {
strncpy(object->name, name, sizeof object->name - 1);
object->name[ACPI_OBJECT_NAME_MAX - 1] = '\0';
}
return object;
}
struct acpi_object *acpi_object_ref(struct acpi_object *object)
{
object->ref++;
return object;
}
void acpi_object_deref(struct acpi_object *object)
{
if (object->ref > 1) {
object->ref--;
return;
}
object->ref = 0;
free(object);
}
void acpi_object_add_child(struct acpi_object *parent, struct acpi_object *child)
{
if (child->parent) {
return;
}
child->parent = parent;
if (!parent->first_child) {
parent->first_child = acpi_object_ref(child);
return;
}
struct acpi_object *cur = parent->first_child;
while (cur->next_sibling) {
cur = cur->next_sibling;
}
cur->next_sibling = acpi_object_ref(child);
}
struct acpi_object *acpi_object_get_child(struct acpi_object *object, const char *name)
{
struct acpi_object *cur = object->first_child;
while (cur) {
if (!strcmp(cur->name, name)) {
return acpi_object_ref(cur);
}
cur = cur->next_sibling;
}
return NULL;
}
void acpi_object_print(struct acpi_object *object, int depth)
{
for (int i = 0; i < depth; i++) {
fputs(" ", stdout);
}
if (object->name[0] == 0) {
printf("<unnamed> [%s]\n", acpi_object_type_string(object->type));
} else {
printf("%s [%s]\n", object->name, acpi_object_type_string(object->type));
}
struct acpi_object *cur = object->first_child;
while (cur) {
acpi_object_print(cur, depth + 1);
cur = cur->next_sibling;
}
}
#define OBJECT_TYPE_STRING(type) \
case type: \
return #type;
const char *acpi_object_type_string(enum acpi_object_type type)
{
switch (type) {
OBJECT_TYPE_STRING(ACPI_OBJECT_NONE)
OBJECT_TYPE_STRING(ACPI_OBJECT_VALUE)
OBJECT_TYPE_STRING(ACPI_OBJECT_NAMESPACE)
OBJECT_TYPE_STRING(ACPI_OBJECT_PROCESSOR)
OBJECT_TYPE_STRING(ACPI_OBJECT_DEVICE)
OBJECT_TYPE_STRING(ACPI_OBJECT_METHOD)
OBJECT_TYPE_STRING(ACPI_OBJECT_POWER_RESOURCE)
OBJECT_TYPE_STRING(ACPI_OBJECT_OPERATION_REGION)
OBJECT_TYPE_STRING(ACPI_OBJECT_THERMAL_ZONE)
OBJECT_TYPE_STRING(ACPI_OBJECT_FIELD)
OBJECT_TYPE_STRING(ACPI_OBJECT_PACKAGE)
OBJECT_TYPE_STRING(ACPI_OBJECT_BUFFER)
OBJECT_TYPE_STRING(ACPI_OBJECT_ALIAS)
OBJECT_TYPE_STRING(ACPI_OBJECT_MUTEX)
default:
return "<unknown>";
}
}