/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <inttypes.h>
#include <pthread.h>
#include "adr.h"
#include "adr_object.h"
void
adr_free(adr_object_t *o)
{
free((char *)o->ao_aname);
free((char *)o->ao_ifname);
for (int i = 0; i < o->ao_nparents; i++) {
adr_object_t *p = o->ao_parents[i];
if (p != NULL)
adr_free(p);
}
free(o->ao_parents);
for (int i = 0; i < o->ao_nattributes; i++) {
adr_attribute_t *a = o->ao_attributes[i];
if (a != NULL) {
free((char *)a->aa_name);
adr_type_free(a->aa_type);
adr_type_free(a->aa_read_error);
adr_type_free(a->aa_write_error);
free(a);
}
}
free(o->ao_attributes);
for (int i = 0; i < o->ao_nmethods; i++) {
adr_method_t *m = o->ao_methods[i];
if (m != NULL) {
free((char *)m->am_name);
adr_type_free(m->am_result);
adr_type_free(m->am_error);
for (int j = 0; j < m->am_nargs; j++) {
adr_parameter_t *p = &m->am_args[j];
free((char *)p->ap_name);
adr_type_free(p->ap_type);
}
free(m);
}
}
free(o->ao_methods);
for (int i = 0; i < o->ao_nevents; i++) {
adr_event_t *e = &o->ao_events[i];
free((char *)e->ae_name);
adr_type_free(e->ae_type);
}
free(o->ao_events);
free(o);
}
int
adr_lookup_attr_index(adr_object_t *o, const char *name)
{
adr_attribute_t **attrs = o->ao_attributes;
for (int i = 0; i < o->ao_nattributes; i++)
if (strcmp(attrs[i]->aa_name, name) == 0)
return (i);
return (-1);
}
adr_attribute_t *
adr_lookup_attr(adr_object_t *o, const char *name)
{
int index = adr_lookup_attr_index(o, name);
return (index == -1 ? NULL : o->ao_attributes[index]);
}
adr_attribute_t *
adr_get_attr(adr_object_t *o, int index)
{
return (index >= 0 && index < o->ao_nattributes ?
o->ao_attributes[index] : NULL);
}
int
adr_lookup_method_index(adr_object_t *o, const char *name)
{
adr_method_t **methods = o->ao_methods;
for (int i = 0; i < o->ao_nmethods; i++)
if (strcmp(methods[i]->am_name, name) == 0)
return (i);
return (-1);
}
adr_method_t *
adr_lookup_method(adr_object_t *o, const char *name)
{
int index = adr_lookup_method_index(o, name);
return (index == -1 ? NULL : o->ao_methods[index]);
}
adr_method_t *
adr_get_method(adr_object_t *o, int index)
{
return (index >= 0 && index < o->ao_nmethods ?
o->ao_methods[index] : NULL);
}
int
adr_lookup_event_index(adr_object_t *o, const char *name)
{
adr_event_t *events = o->ao_events;
for (int i = 0; i < o->ao_nevents; i++)
if (strcmp(events[i].ae_name, name) == 0)
return (i);
return (-1);
}
adr_event_t *
adr_lookup_event(adr_object_t *o, const char *name)
{
int index = adr_lookup_event_index(o, name);
return (index == -1 ? NULL : &o->ao_events[index]);
}
adr_event_t *
adr_get_event(adr_object_t *o, int index)
{
return (index >= 0 && index < o->ao_nmethods ?
&o->ao_events[index] : NULL);
}