Changes in uspace/app/sbi/src/rdata.c [051b3db8:c5cb943d] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/sbi/src/rdata.c
r051b3db8 rc5cb943d 1 1 /* 2 * Copyright (c) 201 1Jiri Svoboda2 * Copyright (c) 2010 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 49 49 #include <assert.h> 50 50 #include "bigint.h" 51 #include "list.h"52 51 #include "mytypes.h" 53 52 #include "stree.h" … … 70 69 static void rdata_symbol_copy(rdata_symbol_t *src, rdata_symbol_t **dest); 71 70 72 static void rdata_var_destroy_inner(rdata_var_t *var);73 74 static void rdata_bool_destroy(rdata_bool_t *bool_v);75 static void rdata_char_destroy(rdata_char_t *char_v);76 static void rdata_int_destroy(rdata_int_t *int_v);77 static void rdata_string_destroy(rdata_string_t *string_v);78 static void rdata_ref_destroy(rdata_ref_t *ref_v);79 static void rdata_deleg_destroy(rdata_deleg_t *deleg_v);80 static void rdata_enum_destroy(rdata_enum_t *enum_v);81 static void rdata_array_destroy(rdata_array_t *array_v);82 static void rdata_object_destroy(rdata_object_t *object_v);83 static void rdata_resource_destroy(rdata_resource_t *resource_v);84 static void rdata_symbol_destroy(rdata_symbol_t *symbol_v);85 86 71 static int rdata_array_get_dim(rdata_array_t *array); 87 static void rdata_var_copy_to(rdata_var_t *src, rdata_var_t *dest);88 72 89 73 static void rdata_address_print(rdata_address_t *address); … … 430 414 /** Allocate array elements. 431 415 * 432 * Allocates element arrayof @a array.416 * Allocates var nodes for elements of @a array. 433 417 * 434 418 * @param array Array. … … 436 420 void rdata_array_alloc_element(rdata_array_t *array) 437 421 { 438 int dim ;422 int dim, idx; 439 423 440 424 dim = rdata_array_get_dim(array); … … 444 428 printf("Memory allocation failed.\n"); 445 429 exit(1); 430 } 431 432 for (idx = 0; idx < dim; ++idx) { 433 array->element[idx] = calloc(1, sizeof(rdata_var_t)); 434 if (array->element[idx] == NULL) { 435 printf("Memory allocation failed.\n"); 436 exit(1); 437 } 446 438 } 447 439 } … … 465 457 } 466 458 467 /** Deallocate item.468 *469 * @param item Item node470 */471 void rdata_item_delete(rdata_item_t *item)472 {473 assert(item != NULL);474 free(item);475 }476 477 /** Deallocate variable address.478 *479 * @param addr_var Variable address node480 */481 void rdata_addr_var_delete(rdata_addr_var_t *addr_var)482 {483 assert(addr_var != NULL);484 free(addr_var);485 }486 487 /** Deallocate property address.488 *489 * @param addr_prop Variable address node490 */491 void rdata_addr_prop_delete(rdata_addr_prop_t *addr_prop)492 {493 assert(addr_prop != NULL);494 free(addr_prop);495 }496 497 /** Deallocate named property address.498 *499 * @param aprop_named Variable address node500 */501 void rdata_aprop_named_delete(rdata_aprop_named_t *aprop_named)502 {503 assert(aprop_named != NULL);504 free(aprop_named);505 }506 507 /** Deallocate indexed property address.508 *509 * @param aprop_indexed Variable address node510 */511 void rdata_aprop_indexed_delete(rdata_aprop_indexed_t *aprop_indexed)512 {513 assert(aprop_indexed != NULL);514 free(aprop_indexed);515 }516 517 /** Deallocate address.518 *519 * @param address Address node520 */521 void rdata_address_delete(rdata_address_t *address)522 {523 assert(address != NULL);524 free(address);525 }526 527 /** Deallocate value.528 *529 * @param value Value node530 */531 void rdata_value_delete(rdata_value_t *value)532 {533 assert(value != NULL);534 free(value);535 }536 537 /** Deallocate var node.538 *539 * @param var Var node540 */541 void rdata_var_delete(rdata_var_t *var)542 {543 assert(var != NULL);544 free(var);545 }546 547 /** Deallocate boolean.548 *549 * @param bool_v Boolean550 */551 void rdata_bool_delete(rdata_bool_t *bool_v)552 {553 assert(bool_v != NULL);554 free(bool_v);555 }556 557 /** Deallocate character.558 *559 * @param char_v Character560 */561 void rdata_char_delete(rdata_char_t *char_v)562 {563 assert(char_v != NULL);564 free(char_v);565 }566 567 /** Deallocate integer.568 *569 * @param int_v Integer570 */571 void rdata_int_delete(rdata_int_t *int_v)572 {573 assert(int_v != NULL);574 free(int_v);575 }576 577 /** Deallocate string.578 *579 * @param string_v String580 */581 void rdata_string_delete(rdata_string_t *string_v)582 {583 assert(string_v != NULL);584 free(string_v);585 }586 587 /** Deallocate reference.588 *589 * @param ref_v Reference590 */591 void rdata_ref_delete(rdata_ref_t *ref_v)592 {593 assert(ref_v != NULL);594 free(ref_v);595 }596 597 /** Deallocate delegate.598 *599 * @param deleg_v Reference600 */601 void rdata_deleg_delete(rdata_deleg_t *deleg_v)602 {603 assert(deleg_v != NULL);604 free(deleg_v);605 }606 607 /** Deallocate enum.608 *609 * @param enum_v Reference610 */611 void rdata_enum_delete(rdata_enum_t *enum_v)612 {613 assert(enum_v != NULL);614 free(enum_v);615 }616 617 /** Deallocate array.618 *619 * @param array_v Array620 */621 void rdata_array_delete(rdata_array_t *array_v)622 {623 assert(array_v != NULL);624 free(array_v);625 }626 627 /** Deallocate object.628 *629 * @param object_v Object630 */631 void rdata_object_delete(rdata_object_t *object_v)632 {633 assert(object_v != NULL);634 free(object_v);635 }636 637 /** Deallocate resource.638 *639 * @param resource_v Resource640 */641 void rdata_resource_delete(rdata_resource_t *resource_v)642 {643 assert(resource_v != NULL);644 free(resource_v);645 }646 647 /** Deallocate symbol.648 *649 * @param symbol_v Symbol650 */651 void rdata_symbol_delete(rdata_symbol_t *symbol_v)652 {653 assert(symbol_v != NULL);654 free(symbol_v);655 }656 657 /** Copy value.658 *659 * @param src Input value660 * @param dest Place to store pointer to new value661 */662 void rdata_value_copy(rdata_value_t *src, rdata_value_t **dest)663 {664 assert(src != NULL);665 666 *dest = rdata_value_new();667 rdata_var_copy(src->var, &(*dest)->var);668 }669 670 459 /** Make copy of a variable. 671 460 * … … 681 470 682 471 nvar = rdata_var_new(src->vc); 683 rdata_var_copy_to(src, nvar);684 685 *dest = nvar;686 }687 688 /** Copy variable content to another variable.689 *690 * Writes an exact copy of an existing var node to another var node.691 * The varclass of @a src and @a dest must match. The content of692 * @a dest.u must be invalid.693 *694 * @param src Source var node.695 * @param dest Destination var node.696 */697 static void rdata_var_copy_to(rdata_var_t *src, rdata_var_t *dest)698 {699 dest->vc = src->vc;700 472 701 473 switch (src->vc) { 702 474 case vc_bool: 703 rdata_bool_copy(src->u.bool_v, & dest->u.bool_v);475 rdata_bool_copy(src->u.bool_v, &nvar->u.bool_v); 704 476 break; 705 477 case vc_char: 706 rdata_char_copy(src->u.char_v, & dest->u.char_v);478 rdata_char_copy(src->u.char_v, &nvar->u.char_v); 707 479 break; 708 480 case vc_int: 709 rdata_int_copy(src->u.int_v, & dest->u.int_v);481 rdata_int_copy(src->u.int_v, &nvar->u.int_v); 710 482 break; 711 483 case vc_string: 712 rdata_string_copy(src->u.string_v, & dest->u.string_v);484 rdata_string_copy(src->u.string_v, &nvar->u.string_v); 713 485 break; 714 486 case vc_ref: 715 rdata_ref_copy(src->u.ref_v, & dest->u.ref_v);487 rdata_ref_copy(src->u.ref_v, &nvar->u.ref_v); 716 488 break; 717 489 case vc_deleg: 718 rdata_deleg_copy(src->u.deleg_v, & dest->u.deleg_v);490 rdata_deleg_copy(src->u.deleg_v, &nvar->u.deleg_v); 719 491 break; 720 492 case vc_enum: 721 rdata_enum_copy(src->u.enum_v, & dest->u.enum_v);493 rdata_enum_copy(src->u.enum_v, &nvar->u.enum_v); 722 494 break; 723 495 case vc_array: 724 rdata_array_copy(src->u.array_v, & dest->u.array_v);496 rdata_array_copy(src->u.array_v, &nvar->u.array_v); 725 497 break; 726 498 case vc_object: 727 rdata_object_copy(src->u.object_v, & dest->u.object_v);499 rdata_object_copy(src->u.object_v, &nvar->u.object_v); 728 500 break; 729 501 case vc_resource: 730 rdata_resource_copy(src->u.resource_v, & dest->u.resource_v);502 rdata_resource_copy(src->u.resource_v, &nvar->u.resource_v); 731 503 break; 732 504 case vc_symbol: 733 rdata_symbol_copy(src->u.symbol_v, &dest->u.symbol_v); 734 break; 735 } 736 } 737 505 rdata_symbol_copy(src->u.symbol_v, &nvar->u.symbol_v); 506 break; 507 } 508 509 *dest = nvar; 510 } 738 511 739 512 /** Copy boolean. 740 513 * 741 * @param src Source boolean 742 * @param dest Place to store pointer to new boolean 514 * @param src Source boolean. 515 * @param dest Place to store pointer to new boolean. 743 516 */ 744 517 static void rdata_bool_copy(rdata_bool_t *src, rdata_bool_t **dest) … … 750 523 /** Copy character. 751 524 * 752 * @param src Source character 753 * @param dest Place to store pointer to new character 525 * @param src Source character. 526 * @param dest Place to store pointer to new character. 754 527 */ 755 528 static void rdata_char_copy(rdata_char_t *src, rdata_char_t **dest) … … 761 534 /** Copy integer. 762 535 * 763 * @param src Source integer 764 * @param dest Place to store pointer to new integer 536 * @param src Source integer. 537 * @param dest Place to store pointer to new integer. 765 538 */ 766 539 static void rdata_int_copy(rdata_int_t *src, rdata_int_t **dest) … … 859 632 *dest = rdata_symbol_new(); 860 633 (*dest)->sym = src->sym; 861 }862 863 /** Destroy var node.864 *865 * @param var Var node866 */867 void rdata_var_destroy(rdata_var_t *var)868 {869 /* First destroy class-specific part */870 rdata_var_destroy_inner(var);871 872 /* Deallocate var node */873 rdata_var_delete(var);874 }875 876 /** Destroy inside of var node.877 *878 * Destroy content of var node, but do not deallocate the var node879 * itself.880 *881 * @param var Var node882 */883 static void rdata_var_destroy_inner(rdata_var_t *var)884 {885 /* First destroy class-specific part */886 887 switch (var->vc) {888 case vc_bool:889 rdata_bool_destroy(var->u.bool_v);890 break;891 case vc_char:892 rdata_char_destroy(var->u.char_v);893 break;894 case vc_int:895 rdata_int_destroy(var->u.int_v);896 break;897 case vc_string:898 rdata_string_destroy(var->u.string_v);899 break;900 case vc_ref:901 rdata_ref_destroy(var->u.ref_v);902 break;903 case vc_deleg:904 rdata_deleg_destroy(var->u.deleg_v);905 break;906 case vc_enum:907 rdata_enum_destroy(var->u.enum_v);908 break;909 case vc_array:910 rdata_array_destroy(var->u.array_v);911 break;912 case vc_object:913 rdata_object_destroy(var->u.object_v);914 break;915 case vc_resource:916 rdata_resource_destroy(var->u.resource_v);917 break;918 case vc_symbol:919 rdata_symbol_destroy(var->u.symbol_v);920 break;921 }922 }923 924 /** Destroy item.925 *926 * Destroy an item including the value or address within.927 *928 * @param item Item929 */930 void rdata_item_destroy(rdata_item_t *item)931 {932 /* First destroy class-specific part */933 934 switch (item->ic) {935 case ic_address:936 rdata_address_destroy(item->u.address);937 break;938 case ic_value:939 rdata_value_destroy(item->u.value);940 break;941 }942 943 /* Deallocate item node */944 rdata_item_delete(item);945 }946 947 /** Destroy address.948 *949 * Destroy an address node.950 *951 * @param address Address952 */953 void rdata_address_destroy(rdata_address_t *address)954 {955 switch (address->ac) {956 case ac_var:957 rdata_addr_var_destroy(address->u.var_a);958 break;959 case ac_prop:960 rdata_addr_prop_destroy(address->u.prop_a);961 break;962 }963 964 /* Deallocate address node */965 rdata_address_delete(address);966 }967 968 /** Destroy variable address.969 *970 * Destroy a variable address node.971 *972 * @param addr_var Variable address973 */974 void rdata_addr_var_destroy(rdata_addr_var_t *addr_var)975 {976 addr_var->vref = NULL;977 978 /* Deallocate variable address node */979 rdata_addr_var_delete(addr_var);980 }981 982 /** Destroy property address.983 *984 * Destroy a property address node.985 *986 * @param addr_prop Property address987 */988 void rdata_addr_prop_destroy(rdata_addr_prop_t *addr_prop)989 {990 switch (addr_prop->apc) {991 case apc_named:992 rdata_aprop_named_destroy(addr_prop->u.named);993 break;994 case apc_indexed:995 rdata_aprop_indexed_destroy(addr_prop->u.indexed);996 break;997 }998 999 if (addr_prop->tvalue != NULL) {1000 rdata_value_destroy(addr_prop->tvalue);1001 addr_prop->tvalue = NULL;1002 }1003 1004 addr_prop->tpos = NULL;1005 1006 /* Deallocate property address node */1007 rdata_addr_prop_delete(addr_prop);1008 }1009 1010 /** Destroy named property address.1011 *1012 * Destroy a named property address node.1013 *1014 * @param aprop_named Named property address1015 */1016 void rdata_aprop_named_destroy(rdata_aprop_named_t *aprop_named)1017 {1018 rdata_deleg_destroy(aprop_named->prop_d);1019 1020 /* Deallocate named property address node */1021 rdata_aprop_named_delete(aprop_named);1022 }1023 1024 /** Destroy indexed property address.1025 *1026 * Destroy a indexed property address node.1027 *1028 * @param aprop_indexed Indexed property address1029 */1030 void rdata_aprop_indexed_destroy(rdata_aprop_indexed_t *aprop_indexed)1031 {1032 list_node_t *arg_node;1033 rdata_item_t *arg_i;1034 1035 /* Destroy the object delegate. */1036 rdata_deleg_destroy(aprop_indexed->object_d);1037 1038 /*1039 * Walk through all argument items (indices) and destroy them,1040 * removing them from the list as well.1041 */1042 while (!list_is_empty(&aprop_indexed->args)) {1043 arg_node = list_first(&aprop_indexed->args);1044 arg_i = list_node_data(arg_node, rdata_item_t *);1045 1046 rdata_item_destroy(arg_i);1047 list_remove(&aprop_indexed->args, arg_node);1048 }1049 1050 /* Destroy the now empty list */1051 list_fini(&aprop_indexed->args);1052 1053 /* Deallocate indexed property address node */1054 rdata_aprop_indexed_delete(aprop_indexed);1055 }1056 1057 /** Destroy value.1058 *1059 * Destroy a value node.1060 *1061 * @param value Value1062 */1063 void rdata_value_destroy(rdata_value_t *value)1064 {1065 /* Assumption: Var nodes in values are not shared. */1066 rdata_var_destroy(value->var);1067 1068 /* Deallocate value node */1069 rdata_value_delete(value);1070 }1071 1072 /** Destroy boolean.1073 *1074 * @param bool_v Boolean1075 */1076 static void rdata_bool_destroy(rdata_bool_t *bool_v)1077 {1078 rdata_bool_delete(bool_v);1079 }1080 1081 /** Destroy character.1082 *1083 * @param char_v Character1084 */1085 static void rdata_char_destroy(rdata_char_t *char_v)1086 {1087 bigint_destroy(&char_v->value);1088 rdata_char_delete(char_v);1089 }1090 1091 /** Destroy integer.1092 *1093 * @param int_v Integer1094 */1095 static void rdata_int_destroy(rdata_int_t *int_v)1096 {1097 bigint_destroy(&int_v->value);1098 rdata_int_delete(int_v);1099 }1100 1101 /** Destroy string.1102 *1103 * @param string_v String1104 */1105 static void rdata_string_destroy(rdata_string_t *string_v)1106 {1107 /*1108 * String values are shared so we cannot free them. Just deallocate1109 * the node.1110 */1111 rdata_string_delete(string_v);1112 }1113 1114 /** Destroy reference.1115 *1116 * @param ref_v Reference1117 */1118 static void rdata_ref_destroy(rdata_ref_t *ref_v)1119 {1120 ref_v->vref = NULL;1121 rdata_ref_delete(ref_v);1122 }1123 1124 /** Destroy delegate.1125 *1126 * @param deleg_v Reference1127 */1128 static void rdata_deleg_destroy(rdata_deleg_t *deleg_v)1129 {1130 deleg_v->obj = NULL;1131 deleg_v->sym = NULL;1132 rdata_deleg_delete(deleg_v);1133 }1134 1135 /** Destroy enum.1136 *1137 * @param enum_v Reference1138 */1139 static void rdata_enum_destroy(rdata_enum_t *enum_v)1140 {1141 enum_v->value = NULL;1142 rdata_enum_delete(enum_v);1143 }1144 1145 /** Destroy array.1146 *1147 * @param array_v Array1148 */1149 static void rdata_array_destroy(rdata_array_t *array_v)1150 {1151 int d;1152 size_t n_elems, p;1153 1154 /*1155 * Compute total number of elements in array.1156 * At the same time zero out the extent array.1157 */1158 n_elems = 1;1159 for (d = 0; d < array_v->rank; d++) {1160 n_elems = n_elems * array_v->extent[d];1161 array_v->extent[d] = 0;1162 }1163 1164 /* Destroy all elements and zero out the array */1165 for (p = 0; p < n_elems; p++) {1166 rdata_var_delete(array_v->element[p]);1167 array_v->element[p] = NULL;1168 }1169 1170 /* Free the arrays */1171 free(array_v->element);1172 free(array_v->extent);1173 1174 array_v->rank = 0;1175 1176 /* Deallocate the node */1177 rdata_array_delete(array_v);1178 }1179 1180 /** Destroy object.1181 *1182 * @param object_v Object1183 */1184 static void rdata_object_destroy(rdata_object_t *object_v)1185 {1186 /* XXX TODO */1187 rdata_object_delete(object_v);1188 }1189 1190 /** Destroy resource.1191 *1192 * @param resource_v Resource1193 */1194 static void rdata_resource_destroy(rdata_resource_t *resource_v)1195 {1196 /*1197 * XXX Presumably this should be handled by the appropriate1198 * built-in module, so, some call-back function would be required.1199 */1200 resource_v->data = NULL;1201 rdata_resource_delete(resource_v);1202 }1203 1204 /** Destroy symbol.1205 *1206 * @param symbol_v Symbol1207 */1208 static void rdata_symbol_destroy(rdata_symbol_t *symbol_v)1209 {1210 symbol_v->sym = NULL;1211 rdata_symbol_delete(symbol_v);1212 634 } 1213 635 … … 1249 671 void rdata_var_write(rdata_var_t *var, rdata_value_t *value) 1250 672 { 1251 /* Free old content of var->u */ 1252 rdata_var_destroy_inner(var); 673 rdata_var_t *nvar; 1253 674 1254 675 /* Perform a shallow copy of @c value->var. */ 1255 rdata_var_copy_to(value->var, var); 676 rdata_var_copy(value->var, &nvar); 677 678 /* XXX do this in a prettier way. */ 679 680 var->vc = nvar->vc; 681 switch (nvar->vc) { 682 case vc_bool: var->u.bool_v = nvar->u.bool_v; break; 683 case vc_char: var->u.char_v = nvar->u.char_v; break; 684 case vc_int: var->u.int_v = nvar->u.int_v; break; 685 case vc_string: var->u.string_v = nvar->u.string_v; break; 686 case vc_ref: var->u.ref_v = nvar->u.ref_v; break; 687 case vc_deleg: var->u.deleg_v = nvar->u.deleg_v; break; 688 case vc_enum: var->u.enum_v = nvar->u.enum_v; break; 689 case vc_array: var->u.array_v = nvar->u.array_v; break; 690 case vc_object: var->u.object_v = nvar->u.object_v; break; 691 case vc_resource: var->u.resource_v = nvar->u.resource_v; break; 692 case vc_symbol: var->u.symbol_v = nvar->u.symbol_v; break; 693 } 694 695 /* XXX We should free some stuff around here. */ 1256 696 } 1257 697
Note:
See TracChangeset
for help on using the changeset viewer.