Changeset 8b655705 in mainline for uspace/app/sbi/src/rdata.c


Ignore:
Timestamp:
2011-04-15T19:38:07Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
9dd730d1
Parents:
6b9e85b (diff), b2fb47f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/sbi/src/rdata.c

    r6b9e85b r8b655705  
    11/*
    2  * Copyright (c) 2010 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4949#include <assert.h>
    5050#include "bigint.h"
     51#include "list.h"
    5152#include "mytypes.h"
    5253#include "stree.h"
     
    6970static void rdata_symbol_copy(rdata_symbol_t *src, rdata_symbol_t **dest);
    7071
     72static void rdata_var_destroy_inner(rdata_var_t *var);
     73
     74static void rdata_bool_destroy(rdata_bool_t *bool_v);
     75static void rdata_char_destroy(rdata_char_t *char_v);
     76static void rdata_int_destroy(rdata_int_t *int_v);
     77static void rdata_string_destroy(rdata_string_t *string_v);
     78static void rdata_ref_destroy(rdata_ref_t *ref_v);
     79static void rdata_deleg_destroy(rdata_deleg_t *deleg_v);
     80static void rdata_enum_destroy(rdata_enum_t *enum_v);
     81static void rdata_array_destroy(rdata_array_t *array_v);
     82static void rdata_object_destroy(rdata_object_t *object_v);
     83static void rdata_resource_destroy(rdata_resource_t *resource_v);
     84static void rdata_symbol_destroy(rdata_symbol_t *symbol_v);
     85
    7186static int rdata_array_get_dim(rdata_array_t *array);
     87static void rdata_var_copy_to(rdata_var_t *src, rdata_var_t *dest);
    7288
    7389static void rdata_address_print(rdata_address_t *address);
     
    414430/** Allocate array elements.
    415431 *
    416  * Allocates var nodes for elements of @a array.
     432 * Allocates element array of @a array.
    417433 *
    418434 * @param array         Array.
     
    420436void rdata_array_alloc_element(rdata_array_t *array)
    421437{
    422         int dim, idx;
     438        int dim;
    423439
    424440        dim = rdata_array_get_dim(array);
     
    428444                printf("Memory allocation failed.\n");
    429445                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                 }
    438446        }
    439447}
     
    457465}
    458466
     467/** Deallocate item.
     468 *
     469 * @param item  Item node
     470 */
     471void 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 node
     480 */
     481void 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 node
     490 */
     491void 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 node
     500 */
     501void 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 node
     510 */
     511void 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 node
     520 */
     521void 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 node
     530 */
     531void 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 node
     540 */
     541void 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                Boolean
     550 */
     551void 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        Character
     560 */
     561void 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         Integer
     570 */
     571void 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      String
     580 */
     581void 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         Reference
     590 */
     591void 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               Reference
     600 */
     601void 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                Reference
     610 */
     611void 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               Array
     620 */
     621void 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              Object
     630 */
     631void 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            Resource
     640 */
     641void 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              Symbol
     650 */
     651void 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 value
     660 * @param dest          Place to store pointer to new value
     661 */
     662void 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
    459670/** Make copy of a variable.
    460671 *
     
    470681
    471682        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 of
     692 * @a dest.u must be invalid.
     693 *
     694 * @param src           Source var node.
     695 * @param dest          Destination var node.
     696 */
     697static void rdata_var_copy_to(rdata_var_t *src, rdata_var_t *dest)
     698{
     699        dest->vc = src->vc;
    472700
    473701        switch (src->vc) {
    474702        case vc_bool:
    475                 rdata_bool_copy(src->u.bool_v, &nvar->u.bool_v);
     703                rdata_bool_copy(src->u.bool_v, &dest->u.bool_v);
    476704                break;
    477705        case vc_char:
    478                 rdata_char_copy(src->u.char_v, &nvar->u.char_v);
     706                rdata_char_copy(src->u.char_v, &dest->u.char_v);
    479707                break;
    480708        case vc_int:
    481                 rdata_int_copy(src->u.int_v, &nvar->u.int_v);
     709                rdata_int_copy(src->u.int_v, &dest->u.int_v);
    482710                break;
    483711        case vc_string:
    484                 rdata_string_copy(src->u.string_v, &nvar->u.string_v);
     712                rdata_string_copy(src->u.string_v, &dest->u.string_v);
    485713                break;
    486714        case vc_ref:
    487                 rdata_ref_copy(src->u.ref_v, &nvar->u.ref_v);
     715                rdata_ref_copy(src->u.ref_v, &dest->u.ref_v);
    488716                break;
    489717        case vc_deleg:
    490                 rdata_deleg_copy(src->u.deleg_v, &nvar->u.deleg_v);
     718                rdata_deleg_copy(src->u.deleg_v, &dest->u.deleg_v);
    491719                break;
    492720        case vc_enum:
    493                 rdata_enum_copy(src->u.enum_v, &nvar->u.enum_v);
     721                rdata_enum_copy(src->u.enum_v, &dest->u.enum_v);
    494722                break;
    495723        case vc_array:
    496                 rdata_array_copy(src->u.array_v, &nvar->u.array_v);
     724                rdata_array_copy(src->u.array_v, &dest->u.array_v);
    497725                break;
    498726        case vc_object:
    499                 rdata_object_copy(src->u.object_v, &nvar->u.object_v);
     727                rdata_object_copy(src->u.object_v, &dest->u.object_v);
    500728                break;
    501729        case vc_resource:
    502                 rdata_resource_copy(src->u.resource_v, &nvar->u.resource_v);
     730                rdata_resource_copy(src->u.resource_v, &dest->u.resource_v);
    503731                break;
    504732        case vc_symbol:
    505                 rdata_symbol_copy(src->u.symbol_v, &nvar->u.symbol_v);
    506                 break;
    507         }
    508 
    509         *dest = nvar;
    510 }
     733                rdata_symbol_copy(src->u.symbol_v, &dest->u.symbol_v);
     734                break;
     735        }
     736}
     737
    511738
    512739/** Copy boolean.
    513740 *
    514  * @param src           Source boolean.
    515  * @param dest          Place to store pointer to new boolean.
     741 * @param src           Source boolean
     742 * @param dest          Place to store pointer to new boolean
    516743 */
    517744static void rdata_bool_copy(rdata_bool_t *src, rdata_bool_t **dest)
     
    523750/** Copy character.
    524751 *
    525  * @param src           Source character.
    526  * @param dest          Place to store pointer to new character.
     752 * @param src           Source character
     753 * @param dest          Place to store pointer to new character
    527754 */
    528755static void rdata_char_copy(rdata_char_t *src, rdata_char_t **dest)
     
    534761/** Copy integer.
    535762 *
    536  * @param src           Source integer.
    537  * @param dest          Place to store pointer to new integer.
     763 * @param src           Source integer
     764 * @param dest          Place to store pointer to new integer
    538765 */
    539766static void rdata_int_copy(rdata_int_t *src, rdata_int_t **dest)
     
    632859        *dest = rdata_symbol_new();
    633860        (*dest)->sym = src->sym;
     861}
     862
     863/** Destroy var node.
     864 *
     865 * @param var   Var node
     866 */
     867void 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 node
     879 * itself.
     880 *
     881 * @param var   Var node
     882 */
     883static 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  Item
     929 */
     930void 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       Address
     952 */
     953void 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 address
     973 */
     974void 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 address
     987 */
     988void 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 address
     1015 */
     1016void 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 address
     1029 */
     1030void 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         Value
     1062 */
     1063void 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                Boolean
     1075 */
     1076static 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        Character
     1084 */
     1085static 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         Integer
     1094 */
     1095static 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      String
     1104 */
     1105static void rdata_string_destroy(rdata_string_t *string_v)
     1106{
     1107        /*
     1108         * String values are shared so we cannot free them. Just deallocate
     1109         * the node.
     1110         */
     1111        rdata_string_delete(string_v);
     1112}
     1113
     1114/** Destroy reference.
     1115 *
     1116 * @param ref_v         Reference
     1117 */
     1118static 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               Reference
     1127 */
     1128static 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                Reference
     1138 */
     1139static 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               Array
     1148 */
     1149static 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              Object
     1183 */
     1184static 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            Resource
     1193 */
     1194static void rdata_resource_destroy(rdata_resource_t *resource_v)
     1195{
     1196        /*
     1197         * XXX Presumably this should be handled by the appropriate
     1198         * 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              Symbol
     1207 */
     1208static void rdata_symbol_destroy(rdata_symbol_t *symbol_v)
     1209{
     1210        symbol_v->sym = NULL;
     1211        rdata_symbol_delete(symbol_v);
    6341212}
    6351213
     
    6711249void rdata_var_write(rdata_var_t *var, rdata_value_t *value)
    6721250{
    673         rdata_var_t *nvar;
     1251        /* Free old content of var->u */
     1252        rdata_var_destroy_inner(var);
    6741253
    6751254        /* Perform a shallow copy of @c value->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. */
     1255        rdata_var_copy_to(value->var, var);
    6961256}
    6971257
Note: See TracChangeset for help on using the changeset viewer.