summaryrefslogtreecommitdiff
path: root/usr.sbin/acpidump/aml/aml_obj.c
blob: 5589bb379b33af30620adf83b42eeec20e28e8dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*	$OpenBSD: aml_obj.c,v 1.3 2005/06/07 16:27:03 deraadt Exp $	*/
/*-
 * Copyright (c) 1999 Takanori Watanabe
 * Copyright (c) 1999, 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$Id: aml_obj.c,v 1.3 2005/06/07 16:27:03 deraadt Exp $
 *	$FreeBSD: src/usr.sbin/acpi/amldb/aml/aml_obj.c,v 1.3 2000/11/09 06:24:45 iwasaki Exp $
 */

#include <sys/types.h>

#include <aml/aml_amlmem.h>
#include <aml/aml_env.h>
#include <aml/aml_name.h>
#include <aml/aml_obj.h>
#include <aml/aml_status.h>
#include <aml/aml_store.h>

#ifndef _KERNEL
#include <sys/stat.h>
#include <sys/mman.h>

#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#else /* _KERNEL */
#include <sys/systm.h>
#endif /* !_KERNEL */

union aml_object *
aml_copy_object(struct aml_environ *env, union aml_object *orig)
{
	int	i;
	union	aml_object *ret;

	if (orig == NULL)
		return (NULL);
	switch (orig->type) {
	case aml_t_regfield:
		ret = aml_alloc_object(aml_t_buffer, 0);
		ret->buffer.size = (orig->regfield.bitlen / 8) +
		    ((orig->regfield.bitlen % 8) ? 1 : 0);
		if (ret->buffer.size == 0) {
			goto out;
		}
		ret->buffer.data = memman_alloc_flexsize(aml_memman, ret->buffer.size);
		aml_store_to_object(env, orig, ret);
		break;

	default:
		ret = aml_alloc_object(0, orig);
		break;
	}

	if (1 || orig != &env->tempobject) {	/* XXX */
		if (orig->type == aml_t_buffer) {
			if (orig->buffer.size == 0) {
				goto out;
			}
			ret->buffer.data = memman_alloc_flexsize(aml_memman,
			    orig->buffer.size);
			bcopy(orig->buffer.data, ret->buffer.data, orig->buffer.size);
		} else if (orig->type == aml_t_package) {
			if (ret->package.elements == 0) {
				goto out;
			}
			ret->package.objects = memman_alloc_flexsize(aml_memman,
			    ret->package.elements * sizeof(union aml_object *));
			for (i = 0; i < ret->package.elements; i++) {
				ret->package.objects[i] = aml_copy_object(env, orig->package.objects[i]);
			}
		} else if (orig->type == aml_t_string && orig->str.needfree != 0) {
			int l = strlen(orig->str.string) + 1;
			ret->str.string = memman_alloc_flexsize(aml_memman, l);
			strlcpy(orig->str.string, ret->str.string, l);
		} else if (orig->type == aml_t_num) {
                        ret->num.constant = 0;
                }
	} else {
#if 0
		printf("%s:%d\n", __FILE__, __LINE__);
#endif
		env->tempobject.type = aml_t_null;
	}
out:
	return ret;
}

/*
 * This function have two function: copy or allocate.  if orig != NULL,
 *  orig is duplicated.
 */

union aml_object *
aml_alloc_object(enum aml_objtype type, union aml_object *orig)
{
	unsigned	int memid;
	union	aml_object *ret;

	if (orig != NULL) {
		type = orig->type;
	}
	switch (type) {
	case aml_t_namestr:
		memid = memid_aml_namestr;
		break;
	case aml_t_buffer:
		memid = memid_aml_buffer;
		break;
	case aml_t_string:
		memid = memid_aml_string;
		break;
	case aml_t_bufferfield:
		memid = memid_aml_bufferfield;
		break;
	case aml_t_package:
		memid = memid_aml_package;
		break;
	case aml_t_num:
		memid = memid_aml_num;
		break;
	case aml_t_powerres:
		memid = memid_aml_powerres;
		break;
	case aml_t_opregion:
		memid = memid_aml_opregion;
		break;
	case aml_t_method:
		memid = memid_aml_method;
		break;
	case aml_t_processor:
		memid = memid_aml_processor;
		break;
	case aml_t_field:
		memid = memid_aml_field;
		break;
	case aml_t_mutex:
		memid = memid_aml_mutex;
		break;
	case aml_t_device:
		memid = memid_aml_objtype;
		break;
	case aml_t_objref:
		memid = memid_aml_objref;
		break;
	default:
		memid = memid_aml_objtype;
		break;
	}
	ret = memman_alloc(aml_memman, memid);
	ret->type = type;

	if (orig != NULL) {
		bcopy(orig, ret, memman_memid2size(aml_memman, memid));
	}
	return (ret);
}

void
aml_free_objectcontent(union aml_object *obj)
{
	int	i;

	if (obj->type == aml_t_buffer && obj->buffer.data != NULL) {
		memman_free_flexsize(aml_memman, obj->buffer.data);
		obj->buffer.data = NULL;
	}
	if (obj->type == aml_t_string && obj->str.string != NULL) {
		if (obj->str.needfree != 0) {
			memman_free_flexsize(aml_memman, obj->str.string);
			obj->str.string = NULL;
		}
	}
	if (obj->type == aml_t_package && obj->package.objects != NULL) {
		for (i = 0; i < obj->package.elements; i++) {
			aml_free_object(&obj->package.objects[i]);
		}
		memman_free_flexsize(aml_memman, obj->package.objects);
		obj->package.objects = NULL;
	}
}

void
aml_free_object(union aml_object **obj)
{
	union	aml_object *body;

	body = *obj;
	if (body == NULL) {
		return;
	}
	aml_free_objectcontent(*obj);
	memman_free(aml_memman, memid_unkown, *obj);
	*obj = NULL;
}

void
aml_realloc_object(union aml_object *obj, int size)
{
	int	i;
	enum	aml_objtype type;
	union	aml_object tmp;

	type = obj->type;
	switch (type) {
	case aml_t_buffer:
		if (obj->buffer.size >= size) {
			return;
		}
		tmp.buffer.size = size;
		tmp.buffer.data = memman_alloc_flexsize(aml_memman, size);
		bzero(tmp.buffer.data, size);
		bcopy(obj->buffer.data, tmp.buffer.data, obj->buffer.size);
		aml_free_objectcontent(obj);
		*obj = tmp;
		break;
	case aml_t_string:
		if (strlen(obj->str.string) >= size) {
			return;
		}
		tmp.str.string = memman_alloc_flexsize(aml_memman, size + 1);
		strlcpy(tmp.str.string, obj->str.string, size + 1);
		aml_free_objectcontent(obj);
		*obj = tmp;
		break;
	case aml_t_package:
		if (obj->package.elements >= size) {
			return;
		}
		tmp.package.objects = memman_alloc_flexsize(aml_memman,
		    size * sizeof(union aml_object *));
		bzero(tmp.package.objects, size * sizeof(union aml_object *));
		for (i = 0; i < obj->package.elements; i++) {
			tmp.package.objects[i] = obj->package.objects[i];
		}
		memman_free_flexsize(aml_memman, obj->package.objects);
		obj->package.objects = tmp.package.objects;
		break;
	default:
		break;
	}
}