1 /* $NetBSD: aml_obj.c,v 1.1 2007/01/14 04:36:13 christos Exp $ */
4 * Copyright (c) 1999 Takanori Watanabe
5 * Copyright (c) 1999, 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Id: aml_obj.c,v 1.17 2000/08/12 15:20:45 iwasaki Exp
30 * $FreeBSD: src/usr.sbin/acpi/amldb/aml/aml_obj.c,v 1.3 2000/11/09 06:24:45 iwasaki Exp $
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: aml_obj.c,v 1.1 2007/01/14 04:36:13 christos Exp $");
35 #include <sys/param.h>
37 #include <acpi_common.h>
38 #include <aml/aml_amlmem.h>
39 #include <aml/aml_env.h>
40 #include <aml/aml_name.h>
41 #include <aml/aml_obj.h>
42 #include <aml/aml_status.h>
43 #include <aml/aml_store.h>
56 #include <sys/systm.h>
60 aml_copy_object(struct aml_environ
*env
, union aml_object
*orig
)
63 union aml_object
*ret
;
69 ret
= aml_alloc_object(aml_t_buffer
, 0);
70 ret
->buffer
.size
= (orig
->regfield
.bitlen
/ 8) +
71 ((orig
->regfield
.bitlen
% 8) ? 1 : 0);
72 if (ret
->buffer
.size
== 0) {
75 ret
->buffer
.data
= memman_alloc_flexsize(aml_memman
, ret
->buffer
.size
);
76 aml_store_to_object(env
, orig
, ret
);
80 ret
= aml_alloc_object(0, orig
);
84 if (1 || orig
!= &env
->tempobject
) { /* XXX */
85 if (orig
->type
== aml_t_buffer
) {
86 if (orig
->buffer
.size
== 0) {
89 ret
->buffer
.data
= memman_alloc_flexsize(aml_memman
,
91 bcopy(orig
->buffer
.data
, ret
->buffer
.data
, orig
->buffer
.size
);
92 } else if (orig
->type
== aml_t_package
) {
93 if (ret
->package
.elements
== 0) {
96 ret
->package
.objects
= memman_alloc_flexsize(aml_memman
,
97 ret
->package
.elements
* sizeof(union aml_object
*));
98 for (i
= 0; i
< ret
->package
.elements
; i
++) {
99 ret
->package
.objects
[i
] = aml_copy_object(env
, orig
->package
.objects
[i
]);
101 } else if (orig
->type
== aml_t_string
&& orig
->str
.needfree
!= 0) {
102 ret
->str
.string
= memman_alloc_flexsize(aml_memman
,
103 strlen((const char *)orig
->str
.string
) + 1);
104 strcpy((char *)orig
->str
.string
,
105 (const char *)ret
->str
.string
);
106 } else if (orig
->type
== aml_t_num
) {
107 ret
->num
.constant
= 0;
110 printf("%s:%d\n", __FILE__
, __LINE__
);
111 env
->tempobject
.type
= aml_t_null
;
118 * This function have two function: copy or allocate. if orig != NULL,
119 * orig is duplicated.
123 aml_alloc_object(enum aml_objtype type
, union aml_object
*orig
)
126 union aml_object
*ret
;
133 memid
= memid_aml_namestr
;
136 memid
= memid_aml_buffer
;
139 memid
= memid_aml_string
;
141 case aml_t_bufferfield
:
142 memid
= memid_aml_bufferfield
;
145 memid
= memid_aml_package
;
148 memid
= memid_aml_num
;
151 memid
= memid_aml_powerres
;
154 memid
= memid_aml_opregion
;
157 memid
= memid_aml_method
;
159 case aml_t_processor
:
160 memid
= memid_aml_processor
;
163 memid
= memid_aml_field
;
166 memid
= memid_aml_mutex
;
169 memid
= memid_aml_objtype
;
172 memid
= memid_aml_objref
;
175 memid
= memid_aml_objtype
;
178 ret
= memman_alloc(aml_memman
, memid
);
182 bcopy(orig
, ret
, memman_memid2size(aml_memman
, memid
));
188 aml_free_objectcontent(union aml_object
*obj
)
192 if (obj
->type
== aml_t_buffer
&& obj
->buffer
.data
!= NULL
) {
193 memman_free_flexsize(aml_memman
, obj
->buffer
.data
);
194 obj
->buffer
.data
= NULL
;
196 if (obj
->type
== aml_t_string
&& obj
->str
.string
!= NULL
) {
197 if (obj
->str
.needfree
!= 0) {
198 memman_free_flexsize(aml_memman
, obj
->str
.string
);
199 obj
->str
.string
= NULL
;
202 if (obj
->type
== aml_t_package
&& obj
->package
.objects
!= NULL
) {
203 for (i
= 0; i
< obj
->package
.elements
; i
++) {
204 aml_free_object(&obj
->package
.objects
[i
]);
206 memman_free_flexsize(aml_memman
, obj
->package
.objects
);
207 obj
->package
.objects
= NULL
;
212 aml_free_object(union aml_object
**obj
)
214 union aml_object
*body
;
220 aml_free_objectcontent(*obj
);
221 memman_free(aml_memman
, memid_unkown
, *obj
);
226 aml_realloc_object(union aml_object
*obj
, int size
)
229 enum aml_objtype type
;
230 union aml_object tmp
;
235 if (obj
->buffer
.size
>= size
) {
238 tmp
.buffer
.size
= size
;
239 tmp
.buffer
.data
= memman_alloc_flexsize(aml_memman
, size
);
240 bzero(tmp
.buffer
.data
, size
);
241 bcopy(obj
->buffer
.data
, tmp
.buffer
.data
, obj
->buffer
.size
);
242 aml_free_objectcontent(obj
);
246 if ((int)strlen((const char *)obj
->str
.string
) >= size
) {
249 tmp
.str
.string
= memman_alloc_flexsize(aml_memman
, size
+ 1);
250 strcpy((char *)tmp
.str
.string
, (const char *)obj
->str
.string
);
251 aml_free_objectcontent(obj
);
255 if (obj
->package
.elements
>= size
) {
258 tmp
.package
.objects
= memman_alloc_flexsize(aml_memman
,
259 size
* sizeof(union aml_object
*));
260 bzero(tmp
.package
.objects
, size
* sizeof(union aml_object
*));
261 for (i
= 0; i
< obj
->package
.elements
; i
++) {
262 tmp
.package
.objects
[i
] = obj
->package
.objects
[i
];
264 memman_free_flexsize(aml_memman
, obj
->package
.objects
);
265 obj
->package
.objects
= tmp
.package
.objects
;