source: mainline/uspace/app/sbi/src/rdata.c@ a95310e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a95310e was 1ebc1a62, checked in by Jiri Svoboda <jiri@…>, 16 years ago

Update SBI to rev. 157.

  • Property mode set to 100644
File size: 10.2 KB
Line 
1/*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file Run-time data representation. */
30
31#include <stdlib.h>
32#include <assert.h>
33#include "mytypes.h"
34#include "stree.h"
35
36#include "rdata.h"
37
38static void rdata_int_copy(rdata_int_t *src, rdata_int_t **dest);
39static void rdata_string_copy(rdata_string_t *src, rdata_string_t **dest);
40static void rdata_ref_copy(rdata_ref_t *src, rdata_ref_t **dest);
41static void rdata_deleg_copy(rdata_deleg_t *src, rdata_deleg_t **dest);
42static void rdata_array_copy(rdata_array_t *src, rdata_array_t **dest);
43static void rdata_object_copy(rdata_object_t *src, rdata_object_t **dest);
44static void rdata_resource_copy(rdata_resource_t *src,
45 rdata_resource_t **dest);
46
47static int rdata_array_get_dim(rdata_array_t *array);
48
49static void rdata_address_print(rdata_address_t *address);
50static void rdata_var_print(rdata_var_t *var);
51
52
53rdata_item_t *rdata_item_new(item_class_t ic)
54{
55 rdata_item_t *item;
56
57 item = calloc(1, sizeof(rdata_item_t));
58 if (item == NULL) {
59 printf("Memory allocation failed.\n");
60 exit(1);
61 }
62
63 item->ic = ic;
64 return item;
65}
66
67rdata_addr_var_t *rdata_addr_var_new(void)
68{
69 rdata_addr_var_t *addr_var;
70
71 addr_var = calloc(1, sizeof(rdata_addr_var_t));
72 if (addr_var == NULL) {
73 printf("Memory allocation failed.\n");
74 exit(1);
75 }
76
77 return addr_var;
78}
79
80rdata_aprop_named_t *rdata_aprop_named_new(void)
81{
82 rdata_aprop_named_t *aprop_named;
83
84 aprop_named = calloc(1, sizeof(rdata_aprop_named_t));
85 if (aprop_named == NULL) {
86 printf("Memory allocation failed.\n");
87 exit(1);
88 }
89
90 return aprop_named;
91}
92
93rdata_aprop_indexed_t *rdata_aprop_indexed_new(void)
94{
95 rdata_aprop_indexed_t *aprop_indexed;
96
97 aprop_indexed = calloc(1, sizeof(rdata_aprop_indexed_t));
98 if (aprop_indexed == NULL) {
99 printf("Memory allocation failed.\n");
100 exit(1);
101 }
102
103 return aprop_indexed;
104}
105
106rdata_addr_prop_t *rdata_addr_prop_new(aprop_class_t apc)
107{
108 rdata_addr_prop_t *addr_prop;
109
110 addr_prop = calloc(1, sizeof(rdata_addr_prop_t));
111 if (addr_prop == NULL) {
112 printf("Memory allocation failed.\n");
113 exit(1);
114 }
115
116 addr_prop->apc = apc;
117 return addr_prop;
118}
119
120rdata_address_t *rdata_address_new(address_class_t ac)
121{
122 rdata_address_t *address;
123
124 address = calloc(1, sizeof(rdata_address_t));
125 if (address == NULL) {
126 printf("Memory allocation failed.\n");
127 exit(1);
128 }
129
130 address->ac = ac;
131 return address;
132}
133
134rdata_value_t *rdata_value_new(void)
135{
136 rdata_value_t *value;
137
138 value = calloc(1, sizeof(rdata_value_t));
139 if (value == NULL) {
140 printf("Memory allocation failed.\n");
141 exit(1);
142 }
143
144 return value;
145}
146
147rdata_var_t *rdata_var_new(var_class_t vc)
148{
149 rdata_var_t *var;
150
151 var = calloc(1, sizeof(rdata_var_t));
152 if (var == NULL) {
153 printf("Memory allocation failed.\n");
154 exit(1);
155 }
156
157 var->vc = vc;
158 return var;
159}
160
161rdata_ref_t *rdata_ref_new(void)
162{
163 rdata_ref_t *ref;
164
165 ref = calloc(1, sizeof(rdata_ref_t));
166 if (ref == NULL) {
167 printf("Memory allocation failed.\n");
168 exit(1);
169 }
170
171 return ref;
172}
173
174rdata_deleg_t *rdata_deleg_new(void)
175{
176 rdata_deleg_t *deleg;
177
178 deleg = calloc(1, sizeof(rdata_deleg_t));
179 if (deleg == NULL) {
180 printf("Memory allocation failed.\n");
181 exit(1);
182 }
183
184 return deleg;
185}
186
187rdata_array_t *rdata_array_new(int rank)
188{
189 rdata_array_t *array;
190
191 array = calloc(1, sizeof(rdata_array_t));
192 if (array == NULL) {
193 printf("Memory allocation failed.\n");
194 exit(1);
195 }
196
197 array->rank = rank;
198 array->extent = calloc(rank, sizeof(int));
199 if (array == NULL) {
200 printf("Memory allocation failed.\n");
201 exit(1);
202 }
203
204 return array;
205}
206
207rdata_object_t *rdata_object_new(void)
208{
209 rdata_object_t *object;
210
211 object = calloc(1, sizeof(rdata_object_t));
212 if (object == NULL) {
213 printf("Memory allocation failed.\n");
214 exit(1);
215 }
216
217 return object;
218}
219
220rdata_int_t *rdata_int_new(void)
221{
222 rdata_int_t *int_v;
223
224 int_v = calloc(1, sizeof(rdata_int_t));
225 if (int_v == NULL) {
226 printf("Memory allocation failed.\n");
227 exit(1);
228 }
229
230 return int_v;
231}
232
233rdata_string_t *rdata_string_new(void)
234{
235 rdata_string_t *string_v;
236
237 string_v = calloc(1, sizeof(rdata_string_t));
238 if (string_v == NULL) {
239 printf("Memory allocation failed.\n");
240 exit(1);
241 }
242
243 return string_v;
244}
245
246rdata_resource_t *rdata_resource_new(void)
247{
248 rdata_resource_t *resource_v;
249
250 resource_v = calloc(1, sizeof(rdata_resource_t));
251 if (resource_v == NULL) {
252 printf("Memory allocation failed.\n");
253 exit(1);
254 }
255
256 return resource_v;
257}
258
259void rdata_array_alloc_element(rdata_array_t *array)
260{
261 int dim, idx;
262
263 dim = rdata_array_get_dim(array);
264
265 array->element = calloc(dim, sizeof(rdata_var_t *));
266 if (array->element == NULL) {
267 printf("Memory allocation failed.\n");
268 exit(1);
269 }
270
271 for (idx = 0; idx < dim; ++idx) {
272 array->element[idx] = calloc(1, sizeof(rdata_var_t));
273 if (array->element[idx] == NULL) {
274 printf("Memory allocation failed.\n");
275 exit(1);
276 }
277 }
278}
279
280/** Get array dimension.
281 *
282 * Dimension is the total number of elements in an array, in other words,
283 * the product of all extents.
284 */
285static int rdata_array_get_dim(rdata_array_t *array)
286{
287 int didx, dim;
288
289 dim = 1;
290 for (didx = 0; didx < array->rank; ++didx)
291 dim = dim * array->extent[didx];
292
293 return dim;
294}
295
296/** Make copy of a variable. */
297void rdata_var_copy(rdata_var_t *src, rdata_var_t **dest)
298{
299 rdata_var_t *nvar;
300
301 nvar = rdata_var_new(src->vc);
302
303 switch (src->vc) {
304 case vc_int:
305 rdata_int_copy(src->u.int_v, &nvar->u.int_v);
306 break;
307 case vc_string:
308 rdata_string_copy(src->u.string_v, &nvar->u.string_v);
309 break;
310 case vc_ref:
311 rdata_ref_copy(src->u.ref_v, &nvar->u.ref_v);
312 break;
313 case vc_deleg:
314 rdata_deleg_copy(src->u.deleg_v, &nvar->u.deleg_v);
315 break;
316 case vc_array:
317 rdata_array_copy(src->u.array_v, &nvar->u.array_v);
318 break;
319 case vc_object:
320 rdata_object_copy(src->u.object_v, &nvar->u.object_v);
321 break;
322 case vc_resource:
323 rdata_resource_copy(src->u.resource_v, &nvar->u.resource_v);
324 break;
325 }
326
327 *dest = nvar;
328}
329
330static void rdata_int_copy(rdata_int_t *src, rdata_int_t **dest)
331{
332 *dest = rdata_int_new();
333 (*dest)->value = src->value;
334}
335
336static void rdata_string_copy(rdata_string_t *src, rdata_string_t **dest)
337{
338 *dest = rdata_string_new();
339 (*dest)->value = src->value;
340}
341
342static void rdata_ref_copy(rdata_ref_t *src, rdata_ref_t **dest)
343{
344 *dest = rdata_ref_new();
345 (*dest)->vref = src->vref;
346}
347
348static void rdata_deleg_copy(rdata_deleg_t *src, rdata_deleg_t **dest)
349{
350 (void) src; (void) dest;
351 printf("Unimplemented: Copy delegate.\n");
352 exit(1);
353}
354
355static void rdata_array_copy(rdata_array_t *src, rdata_array_t **dest)
356{
357 (void) src; (void) dest;
358 printf("Unimplemented: Copy array.\n");
359 exit(1);
360}
361
362static void rdata_object_copy(rdata_object_t *src, rdata_object_t **dest)
363{
364 (void) src; (void) dest;
365 printf("Unimplemented: Copy object.\n");
366 exit(1);
367}
368
369static void rdata_resource_copy(rdata_resource_t *src, rdata_resource_t **dest)
370{
371 *dest = rdata_resource_new();
372 (*dest)->data = src->data;
373}
374
375/** Read data from a variable.
376 *
377 * Return value stored in variable @a var.
378 */
379void rdata_var_read(rdata_var_t *var, rdata_item_t **ritem)
380{
381 rdata_value_t *value;
382 rdata_var_t *rvar;
383
384 /* Perform a shallow copy of @a var. */
385 rdata_var_copy(var, &rvar);
386
387 value = rdata_value_new();
388 value->var = rvar;
389 *ritem = rdata_item_new(ic_value);
390 (*ritem)->u.value = value;
391}
392
393/** Write data to a variable.
394 *
395 * Store @a value to variable @a var.
396 */
397void rdata_var_write(rdata_var_t *var, rdata_value_t *value)
398{
399 rdata_var_t *nvar;
400
401 /* Perform a shallow copy of @c value->var. */
402 rdata_var_copy(value->var, &nvar);
403
404 /* XXX do this in a prettier way. */
405
406 var->vc = nvar->vc;
407 switch (nvar->vc) {
408 case vc_int: var->u.int_v = nvar->u.int_v; break;
409 case vc_string: var->u.string_v = nvar->u.string_v; break;
410 case vc_ref: var->u.ref_v = nvar->u.ref_v; break;
411 case vc_deleg: var->u.deleg_v = nvar->u.deleg_v; break;
412 case vc_array: var->u.array_v = nvar->u.array_v; break;
413 case vc_object: var->u.object_v = nvar->u.object_v; break;
414 case vc_resource: var->u.resource_v = nvar->u.resource_v; break;
415 }
416
417 /* XXX We should free some stuff around here. */
418}
419
420void rdata_item_print(rdata_item_t *item)
421{
422 if (item == NULL) {
423 printf("none");
424 return;
425 }
426
427 switch (item->ic) {
428 case ic_address:
429 printf("address:");
430 rdata_address_print(item->u.address);
431 break;
432 case ic_value:
433 printf("value:");
434 rdata_value_print(item->u.value);
435 break;
436 }
437}
438
439static void rdata_address_print(rdata_address_t *address)
440{
441 switch (address->ac) {
442 case ac_var:
443 rdata_var_print(address->u.var_a->vref);
444 break;
445 case ac_prop:
446 printf("Warning: Unimplemented: Print property address.\n");
447 break;
448 }
449}
450
451void rdata_value_print(rdata_value_t *value)
452{
453 rdata_var_print(value->var);
454}
455
456static void rdata_var_print(rdata_var_t *var)
457{
458 switch (var->vc) {
459 case vc_int:
460 printf("int(%d)", var->u.int_v->value);
461 break;
462 case vc_string:
463 printf("string(\"%s\")", var->u.string_v->value);
464 break;
465 case vc_ref:
466 printf("ref");
467 break;
468 case vc_deleg:
469 printf("deleg");
470 break;
471 case vc_object:
472 printf("object");
473 break;
474 default:
475 printf("print(%d)\n", var->vc);
476 assert(b_false);
477 }
478}
Note: See TracBrowser for help on using the repository browser.