Home | History | Annotate | Line # | Download | only in aml
      1  1.2     lukem /*	$NetBSD: aml_obj.c,v 1.2 2009/01/18 09:46:59 lukem Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 1999 Takanori Watanabe
      5  1.1  christos  * Copyright (c) 1999, 2000 Mitsuru IWASAKI <iwasaki (at) FreeBSD.org>
      6  1.1  christos  * All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * Redistribution and use in source and binary forms, with or without
      9  1.1  christos  * modification, are permitted provided that the following conditions
     10  1.1  christos  * are met:
     11  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  christos  *    documentation and/or other materials provided with the distribution.
     16  1.1  christos  *
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.1  christos  * SUCH DAMAGE.
     28  1.1  christos  *
     29  1.1  christos  *	Id: aml_obj.c,v 1.17 2000/08/12 15:20:45 iwasaki Exp
     30  1.1  christos  *	$FreeBSD: src/usr.sbin/acpi/amldb/aml/aml_obj.c,v 1.3 2000/11/09 06:24:45 iwasaki Exp $
     31  1.1  christos  */
     32  1.1  christos #include <sys/cdefs.h>
     33  1.2     lukem __RCSID("$NetBSD: aml_obj.c,v 1.2 2009/01/18 09:46:59 lukem Exp $");
     34  1.1  christos 
     35  1.1  christos #include <sys/param.h>
     36  1.1  christos 
     37  1.1  christos #include <acpi_common.h>
     38  1.1  christos #include <aml/aml_amlmem.h>
     39  1.1  christos #include <aml/aml_env.h>
     40  1.1  christos #include <aml/aml_name.h>
     41  1.1  christos #include <aml/aml_obj.h>
     42  1.1  christos #include <aml/aml_status.h>
     43  1.1  christos #include <aml/aml_store.h>
     44  1.1  christos 
     45  1.1  christos #ifndef _KERNEL
     46  1.1  christos #include <sys/stat.h>
     47  1.1  christos #include <sys/mman.h>
     48  1.1  christos 
     49  1.1  christos #include <assert.h>
     50  1.1  christos #include <err.h>
     51  1.1  christos #include <fcntl.h>
     52  1.1  christos #include <stdio.h>
     53  1.1  christos #include <stdlib.h>
     54  1.1  christos #include <string.h>
     55  1.1  christos #else /* _KERNEL */
     56  1.1  christos #include <sys/systm.h>
     57  1.1  christos #endif /* !_KERNEL */
     58  1.1  christos 
     59  1.1  christos union aml_object *
     60  1.1  christos aml_copy_object(struct aml_environ *env, union aml_object *orig)
     61  1.1  christos {
     62  1.1  christos 	int	i;
     63  1.1  christos 	union	aml_object *ret;
     64  1.1  christos 
     65  1.1  christos 	if (orig == NULL)
     66  1.1  christos 		return (NULL);
     67  1.1  christos 	switch (orig->type) {
     68  1.1  christos 	case aml_t_regfield:
     69  1.1  christos 		ret = aml_alloc_object(aml_t_buffer, 0);
     70  1.1  christos 		ret->buffer.size = (orig->regfield.bitlen / 8) +
     71  1.1  christos 		    ((orig->regfield.bitlen % 8) ? 1 : 0);
     72  1.1  christos 		if (ret->buffer.size == 0) {
     73  1.1  christos 			goto out;
     74  1.1  christos 		}
     75  1.1  christos 		ret->buffer.data = memman_alloc_flexsize(aml_memman, ret->buffer.size);
     76  1.1  christos 		aml_store_to_object(env, orig, ret);
     77  1.1  christos 		break;
     78  1.1  christos 
     79  1.1  christos 	default:
     80  1.1  christos 		ret = aml_alloc_object(0, orig);
     81  1.1  christos 		break;
     82  1.1  christos 	}
     83  1.1  christos 
     84  1.1  christos 	if (1 || orig != &env->tempobject) {	/* XXX */
     85  1.1  christos 		if (orig->type == aml_t_buffer) {
     86  1.1  christos 			if (orig->buffer.size == 0) {
     87  1.1  christos 				goto out;
     88  1.1  christos 			}
     89  1.1  christos 			ret->buffer.data = memman_alloc_flexsize(aml_memman,
     90  1.1  christos 			    orig->buffer.size);
     91  1.1  christos 			bcopy(orig->buffer.data, ret->buffer.data, orig->buffer.size);
     92  1.1  christos 		} else if (orig->type == aml_t_package) {
     93  1.1  christos 			if (ret->package.elements == 0) {
     94  1.1  christos 				goto out;
     95  1.1  christos 			}
     96  1.1  christos 			ret->package.objects = memman_alloc_flexsize(aml_memman,
     97  1.1  christos 			    ret->package.elements * sizeof(union aml_object *));
     98  1.1  christos 			for (i = 0; i < ret->package.elements; i++) {
     99  1.1  christos 				ret->package.objects[i] = aml_copy_object(env, orig->package.objects[i]);
    100  1.1  christos 			}
    101  1.1  christos 		} else if (orig->type == aml_t_string && orig->str.needfree != 0) {
    102  1.1  christos 			ret->str.string = memman_alloc_flexsize(aml_memman,
    103  1.1  christos 			    strlen((const char *)orig->str.string) + 1);
    104  1.1  christos 			strcpy((char *)orig->str.string,
    105  1.1  christos 			    (const char *)ret->str.string);
    106  1.1  christos 		} else if (orig->type == aml_t_num) {
    107  1.1  christos                         ret->num.constant = 0;
    108  1.1  christos                 }
    109  1.1  christos 	} else {
    110  1.1  christos 		printf("%s:%d\n", __FILE__, __LINE__);
    111  1.1  christos 		env->tempobject.type = aml_t_null;
    112  1.1  christos 	}
    113  1.1  christos out:
    114  1.1  christos 	return ret;
    115  1.1  christos }
    116  1.1  christos 
    117  1.1  christos /*
    118  1.1  christos  * This function have two function: copy or allocate.  if orig != NULL,
    119  1.1  christos  *  orig is duplicated.
    120  1.1  christos  */
    121  1.1  christos 
    122  1.1  christos union aml_object *
    123  1.1  christos aml_alloc_object(enum aml_objtype type, union aml_object *orig)
    124  1.1  christos {
    125  1.1  christos 	unsigned	int memid;
    126  1.1  christos 	union	aml_object *ret;
    127  1.1  christos 
    128  1.1  christos 	if (orig != NULL) {
    129  1.1  christos 		type = orig->type;
    130  1.1  christos 	}
    131  1.1  christos 	switch (type) {
    132  1.1  christos 	case aml_t_namestr:
    133  1.1  christos 		memid = memid_aml_namestr;
    134  1.1  christos 		break;
    135  1.1  christos 	case aml_t_buffer:
    136  1.1  christos 		memid = memid_aml_buffer;
    137  1.1  christos 		break;
    138  1.1  christos 	case aml_t_string:
    139  1.1  christos 		memid = memid_aml_string;
    140  1.1  christos 		break;
    141  1.1  christos 	case aml_t_bufferfield:
    142  1.1  christos 		memid = memid_aml_bufferfield;
    143  1.1  christos 		break;
    144  1.1  christos 	case aml_t_package:
    145  1.1  christos 		memid = memid_aml_package;
    146  1.1  christos 		break;
    147  1.1  christos 	case aml_t_num:
    148  1.1  christos 		memid = memid_aml_num;
    149  1.1  christos 		break;
    150  1.1  christos 	case aml_t_powerres:
    151  1.1  christos 		memid = memid_aml_powerres;
    152  1.1  christos 		break;
    153  1.1  christos 	case aml_t_opregion:
    154  1.1  christos 		memid = memid_aml_opregion;
    155  1.1  christos 		break;
    156  1.1  christos 	case aml_t_method:
    157  1.1  christos 		memid = memid_aml_method;
    158  1.1  christos 		break;
    159  1.1  christos 	case aml_t_processor:
    160  1.1  christos 		memid = memid_aml_processor;
    161  1.1  christos 		break;
    162  1.1  christos 	case aml_t_field:
    163  1.1  christos 		memid = memid_aml_field;
    164  1.1  christos 		break;
    165  1.1  christos 	case aml_t_mutex:
    166  1.1  christos 		memid = memid_aml_mutex;
    167  1.1  christos 		break;
    168  1.1  christos 	case aml_t_device:
    169  1.1  christos 		memid = memid_aml_objtype;
    170  1.1  christos 		break;
    171  1.1  christos 	case aml_t_objref:
    172  1.1  christos 		memid = memid_aml_objref;
    173  1.1  christos 		break;
    174  1.1  christos 	default:
    175  1.1  christos 		memid = memid_aml_objtype;
    176  1.1  christos 		break;
    177  1.1  christos 	}
    178  1.1  christos 	ret = memman_alloc(aml_memman, memid);
    179  1.1  christos 	ret->type = type;
    180  1.1  christos 
    181  1.1  christos 	if (orig != NULL) {
    182  1.1  christos 		bcopy(orig, ret, memman_memid2size(aml_memman, memid));
    183  1.1  christos 	}
    184  1.1  christos 	return (ret);
    185  1.1  christos }
    186  1.1  christos 
    187  1.1  christos void
    188  1.1  christos aml_free_objectcontent(union aml_object *obj)
    189  1.1  christos {
    190  1.1  christos 	int	i;
    191  1.1  christos 
    192  1.1  christos 	if (obj->type == aml_t_buffer && obj->buffer.data != NULL) {
    193  1.1  christos 		memman_free_flexsize(aml_memman, obj->buffer.data);
    194  1.1  christos 		obj->buffer.data = NULL;
    195  1.1  christos 	}
    196  1.1  christos 	if (obj->type == aml_t_string && obj->str.string != NULL) {
    197  1.1  christos 		if (obj->str.needfree != 0) {
    198  1.1  christos 			memman_free_flexsize(aml_memman, obj->str.string);
    199  1.1  christos 			obj->str.string = NULL;
    200  1.1  christos 		}
    201  1.1  christos 	}
    202  1.1  christos 	if (obj->type == aml_t_package && obj->package.objects != NULL) {
    203  1.1  christos 		for (i = 0; i < obj->package.elements; i++) {
    204  1.1  christos 			aml_free_object(&obj->package.objects[i]);
    205  1.1  christos 		}
    206  1.1  christos 		memman_free_flexsize(aml_memman, obj->package.objects);
    207  1.1  christos 		obj->package.objects = NULL;
    208  1.1  christos 	}
    209  1.1  christos }
    210  1.1  christos 
    211  1.1  christos void
    212  1.1  christos aml_free_object(union aml_object **obj)
    213  1.1  christos {
    214  1.1  christos 	union	aml_object *body;
    215  1.1  christos 
    216  1.1  christos 	body = *obj;
    217  1.1  christos 	if (body == NULL) {
    218  1.1  christos 		return;
    219  1.1  christos 	}
    220  1.1  christos 	aml_free_objectcontent(*obj);
    221  1.1  christos 	memman_free(aml_memman, memid_unkown, *obj);
    222  1.1  christos 	*obj = NULL;
    223  1.1  christos }
    224  1.1  christos 
    225  1.1  christos void
    226  1.1  christos aml_realloc_object(union aml_object *obj, int size)
    227  1.1  christos {
    228  1.1  christos 	int	i;
    229  1.1  christos 	enum	aml_objtype type;
    230  1.1  christos 	union	aml_object tmp;
    231  1.1  christos 
    232  1.1  christos 	type = obj->type;
    233  1.1  christos 	switch (type) {
    234  1.1  christos 	case aml_t_buffer:
    235  1.1  christos 		if (obj->buffer.size >= size) {
    236  1.1  christos 			return;
    237  1.1  christos 		}
    238  1.1  christos 		tmp.buffer.size = size;
    239  1.1  christos 		tmp.buffer.data = memman_alloc_flexsize(aml_memman, size);
    240  1.1  christos 		bzero(tmp.buffer.data, size);
    241  1.1  christos 		bcopy(obj->buffer.data, tmp.buffer.data, obj->buffer.size);
    242  1.1  christos 		aml_free_objectcontent(obj);
    243  1.1  christos 		*obj = tmp;
    244  1.1  christos 		break;
    245  1.1  christos 	case aml_t_string:
    246  1.2     lukem 		if ((int)strlen((const char *)obj->str.string) >= size) {
    247  1.1  christos 			return;
    248  1.1  christos 		}
    249  1.1  christos 		tmp.str.string = memman_alloc_flexsize(aml_memman, size + 1);
    250  1.1  christos 		strcpy((char *)tmp.str.string, (const char *)obj->str.string);
    251  1.1  christos 		aml_free_objectcontent(obj);
    252  1.1  christos 		*obj = tmp;
    253  1.1  christos 		break;
    254  1.1  christos 	case aml_t_package:
    255  1.1  christos 		if (obj->package.elements >= size) {
    256  1.1  christos 			return;
    257  1.1  christos 		}
    258  1.1  christos 		tmp.package.objects = memman_alloc_flexsize(aml_memman,
    259  1.1  christos 		    size * sizeof(union aml_object *));
    260  1.1  christos 		bzero(tmp.package.objects, size * sizeof(union aml_object *));
    261  1.1  christos 		for (i = 0; i < obj->package.elements; i++) {
    262  1.1  christos 			tmp.package.objects[i] = obj->package.objects[i];
    263  1.1  christos 		}
    264  1.1  christos 		memman_free_flexsize(aml_memman, obj->package.objects);
    265  1.1  christos 		obj->package.objects = tmp.package.objects;
    266  1.1  christos 		break;
    267  1.1  christos 	default:
    268  1.1  christos 		break;
    269  1.1  christos 	}
    270  1.1  christos }
    271